package com.hai.openApi.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hai.common.utils.HttpsUtils;
import com.hai.common.utils.RedisUtil;
import com.hai.config.CommonSysConst;
import com.hai.config.HuiLianTongUnionCardConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class BlxConfig {

    @Resource
    private RedisUtil redisUtil;

    private static Logger log = LoggerFactory.getLogger(BlxConfig.class);

    /**
     * @Author Sum1Dream
     * @Name generateJw
     * @Description // 获取token
     * @Date 10:42 2023/7/11
     * @Param []
     * @Return java.lang.String
     */
    public String generateJw() {

        Object token = redisUtil.get("BlxToken");

        if (token == null) {
            Map<String , Object > map = new HashMap<>();
            map.put("organizeCode" , "dhy");
            log.info("============ 比邻星停车券业务-获取TOKEN-START =============");
            JSONObject object = HttpsUtils.doGet("https://vertical-formats-gateway.blx.bodata.cn:18443/admin/api/generateJwt" , map);
            log.info("请求参数: " + map);
            log.info("响应参数: " + object);
            log.info("============ 比邻星停车券业务-获取TOKEN-END ==============");
            return object.getString("data");
        }

        return token.toString();
    }

    /**
     * @Author Sum1Dream
     * @Name queryCouponPage
     * @Description // 获取比邻星停车券列表
     * @Date 10:57 2023/7/11
     * @Param []
     * @Return com.alibaba.fastjson.JSONObject
     */
    public JSONObject queryCouponPage() {
        log.info("============ 比邻星停车券业务-获取比邻星停车券列表-START =============");
        Map<String , Object > map = new HashMap<>();
        map.put("rows" , "20");
        map.put("currPage" , "1");

        Map<String , Object> header = new HashMap<>();
        header.put("token" , generateJw());

        JSONObject object = HttpsUtils.doPost(CommonSysConst.getSysConfig().getBlxPostUrl() + "/admin/api/qureyCouponPage" , map , header);
        log.info("头部参数: " + header);
        log.info("请求参数: " + map);
        log.info("响应参数: " + object);
        log.info("============ 比邻星停车券业务-获取比邻星停车券列表-END ==============");
        return object;

    }
    /**
     * @Author Sum1Dream
     * @Name sendCoupon
     * @Description // 发放券
     * @Date 11:01 2023/7/11
     * @Param [object]
     * @Return com.alibaba.fastjson.JSONObject
     */
    public JSONObject sendCoupon(JSONObject object) {

        Map<String , Object > map = new HashMap<>();
        map.put("couponCode" , object.getString("couponCode"));
        map.put("receiverPhone" , object.getString("receiverPhone"));
        map.put("receiverCount" , object.getInteger("receiverCount"));

        Map<String , Object> header = new HashMap<>();
        header.put("token" , generateJw());
        log.info("============ 比邻星停车券业务-发放停车券-START =============");
        JSONObject data = HttpsUtils.doPost(CommonSysConst.getSysConfig().getBlxPostUrl() + "/admin/api/sendCupon" , map , header);
        log.info("头部参数: " + header);
        log.info("请求参数: " + map);
        log.info("响应参数: " + data);
        log.info("============ 比邻星停车券业务-发放停车券-END =============");
        return data;

    }

    /**
     * @Author Sum1Dream
     * @Name queryCouponPage
     * @Description // 获取比邻星停车券列表
     * @Date 10:57 2023/7/11
     * @Param []
     * @Return com.alibaba.fastjson.JSONObject
     */
    public JSONObject getUserCouponPage(JSONObject object) {
        log.info("============ 比邻星停车券业务-获取用户未使用卡券列表-START =============");
        Map<String , Object > map = new HashMap<>();
        map.put("rows" , "2000");
        map.put("currPage" , "1");

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("userMobile" , object.getString("userMobile"));
        jsonObject.put("couponStatus" , 1);
        map.put("data" , jsonObject);

        Map<String , Object> header = new HashMap<>();
        header.put("token" , generateJw());

        JSONObject data = HttpsUtils.doPost(CommonSysConst.getSysConfig().getBlxPostUrl() + "/admin/api/getUserCouponPage" , map , header);
        log.info("头部参数: " + header);
        log.info("请求参数: " + map);
        log.info("响应参数: " + data);
        log.info("============ 比邻星停车券业务-获取比邻星停车券列表-END ==============");
        return data;

    }

}
