package com.hfkj.service.coupon.channel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.common.utils.MD5Util;
import com.hfkj.config.CommonSysConst;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @serviceName .java
 * @author Sum1Dream
 * @version 1.0.0
 * @Description // 中油优途中石油卡券
 * @createTime 18:33 2023/11/13
 **/
@Configuration
public class PcytCNPCCouponConfig {

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

    /**
    * @Author Sum1Dream
    * @Name getCouponList
    * @Description // 获取卡券列表
    * @Date 11:50 2024/4/7
     * @return com.alibaba.fastjson.JSONObject
    */
    public  static JSONObject getCouponList() throws Exception {
        log.info("========================请求任务Start=========================");
        JSONObject paramMap = new JSONObject();
        paramMap.put("method" , "api.coupon.reserve");
        paramMap.put("merchant" ,CommonSysConst.getSysConfig().getPcytCnpcMerchant());
        paramMap.put("sign", generateSign(paramMap , CommonSysConst.getSysConfig().getPcytCnpcSecret()));

        log.info("获取卡券列表-请求参数: " + JSON.toJSONString(paramMap));
        JSONObject object = HttpsUtils.doPostForm(CommonSysConst.getSysConfig().getPcytCnpcPostUrl() , paramSort(paramMap), new HashMap<>());
        log.info("获取卡券列表-回调参数: " + JSON.toJSONString(object));
        log.info("========================请求任务End=========================");
        // 请求接口
        return object;
    }

    /**
    * @Author Sum1Dream
    * @Name getCoupon
    * @Description // 获取电子券
    * @Date 17:19 2024/4/7

     * @return com.alibaba.fastjson.JSONObject
    */
    public  static JSONObject getCoupon(String orderNo , String mobile, String reqCode) throws Exception {
        log.info("========================请求任务Start=========================");
        JSONObject paramMap = new JSONObject();

        paramMap.put("method" , "api.coupon.once");
        paramMap.put("batchId" , orderNo);
        paramMap.put("mobile" , mobile);
        paramMap.put("reqCode" , reqCode);
        paramMap.put("merchant" ,CommonSysConst.getSysConfig().getPcytCnpcMerchant());
        paramMap.put("sign", generateSign(paramMap , CommonSysConst.getSysConfig().getPcytCnpcSecret()));


        log.info("获取电子券-请求参数: " + JSON.toJSONString(paramMap));
        JSONObject object = HttpsUtils.doPostForm(CommonSysConst.getSysConfig().getPcytCnpcPostUrl() , paramSort(paramMap), new HashMap<>());
        log.info("获取电子券-回调参数: " + JSON.toJSONString(object));
        log.info("========================请求任务End=========================");
        // 请求接口
        return object;
    }

    /**
    * @Author Sum1Dream
    * @Name getCouponQuery
    * @Description // 查询电子券信息
    * @Date 17:25 2024/4/7 
    * @Param couponCode  
     * @return com.alibaba.fastjson.JSONObject
    */
    public  static JSONObject getCouponQuery(String couponCode ) throws Exception {
        log.info("========================请求任务Start=========================");
        JSONObject paramMap = new JSONObject();

        paramMap.put("method" , "api.coupon.query");
        paramMap.put("couponCode" , couponCode);
        paramMap.put("merchant" ,CommonSysConst.getSysConfig().getPcytCnpcMerchant());
        paramMap.put("sign", generateSign(paramMap , CommonSysConst.getSysConfig().getPcytCnpcSecret()));

        log.info("查询电子券信息-请求参数: " + JSON.toJSONString(paramMap));
        JSONObject object = HttpsUtils.doPostForm(CommonSysConst.getSysConfig().getPcytCnpcPostUrl() , paramSort(paramMap), new HashMap<>());
        log.info("查询电子券信息-回调参数: " + JSON.toJSONString(object));
        log.info("========================请求任务End=========================");
        // 请求接口
        return object;
    }

    /**
    * @Author Sum1Dream
    * @Name getCouponDestroy
    * @Description // 作废电子券
    * @Date 17:26 2024/4/7 
    * @Param couponCode  
     * @return com.alibaba.fastjson.JSONObject
    */
    public  static JSONObject getCouponDestroy(String couponCode ) throws Exception {

        log.info("========================请求任务Start=========================");
        JSONObject paramMap = new JSONObject();

        paramMap.put("method" , "api.coupon.destroy");
        paramMap.put("couponCode" , couponCode);
        paramMap.put("merchant" ,CommonSysConst.getSysConfig().getPcytCnpcMerchant());
        paramMap.put("sign", generateSign(paramMap , CommonSysConst.getSysConfig().getPcytCnpcSecret()));
        log.info("作废电子券-请求参数: " + JSON.toJSONString(paramMap));
        JSONObject object = HttpsUtils.doPostForm(CommonSysConst.getSysConfig().getPcytCnpcPostUrl() , paramSort(paramMap), new HashMap<>());
        log.info("作废电子券-回调参数: " + JSON.toJSONString(object));
        log.info("========================请求任务End=========================");
        // 请求接口
        return object;
    }


    /**
     * 排序
     * @param param
     * @return
     */
    private static String paramSort(final Map<String, Object> param) {
        Set<String> keySet = param.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        StringBuilder sb = new StringBuilder();
        for (String k : keyArray) {
            if (StringUtils.isBlank(sb.toString())) {
                sb.append(k).append("=").append(param.get(k));
            } else {
                sb.append("&").append(k).append("=").append(param.get(k));
            }
        }
        return sb.toString();
    }

    /**
     * 签名
     * @param map
     * @param key
     * @return
     * @throws Exception
     */
    public static String generateSign(Map<String , Object> map , String key) throws Exception {
        Set<String> keySet = map.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        for (String k : keyArray) {
            if (k.equals("sign") || k.equals("secret")) {
                continue;
            }
            if (map.get(k) != null) // 参数值为空，则不参与签名
            {
                sb.append(k).append("=").append(map.get(k)).append("&");
            }
        }
        sb.append("secret=").append(key);
        return MD5Util.encode(sb.toString().getBytes()).toUpperCase();
    }


}
