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.PetroEncryptUtil;
import com.hfkj.config.CommonSysConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @serviceName .java
 * @author Sum1Dream
 * @version 1.0.0
 * @Description // 四川中石油
 * @createTime 18:33 2023/11/13
 **/
public class PetroConfig {

    private static String reqUrl =  CommonSysConst.getSysConfig().getGzPetroUrl();
    private static  String petroAppKey= CommonSysConst.getSysConfig().getGzPetroAppKey();
    private static  String petroAppid= CommonSysConst.getSysConfig().getGzPetroAppid();
    private static  String petroAesKey= CommonSysConst.getSysConfig().getGzPetroAesKey();


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



    /**
    * @Author Sum1Dream
    * @Name synCouponRule
    * @Description // 获取券列表
    * @Date 11:17 2023/11/17

     * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject synCouponRule() {
        log.info("========================请求任务Start=========================");
        Map<String, Object> req = new HashMap<>();
        req.put("appId", petroAppid);
        req.put("appKey", petroAppKey);
        req.put("jsonData" , "");
        req.put("timestamp", System.currentTimeMillis() + "");
        req.put("nonce" , generateRandomString(16));

        //生成签名
        String sign = PetroEncryptUtil.generateSign(req);

        Map<String , Object> postData = new HashMap<>();
        postData.put("sign" , sign);
        postData.put("timestamp" , req.get("timestamp"));
        postData.put("jsonData" , "");
        postData.put("nonce" , req.get("nonce"));
        postData.put("appId", petroAppid);
        //签名放到参数中

        log.info("获取券列表-请求参数: " + JSON.toJSONString(req));
        JSONObject object = HttpsUtils.doPost(reqUrl + "/outapi/getCouTypesYt", postData);
        log.info("购买加油券充值订单-回调参数: " + JSON.toJSONString(object));
        log.info("========================请求任务End=========================");
        // 请求接口
        return object;
    }


    /**
    * @Author Sum1Dream
    * @Name getCoupon
    * @Description // 获取券code
    * @Date 11:19 2023/11/17
    * @Param code
     * @Param phone
     * @Param orderNo
     * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject getCoupon(String code , String phone) {
        log.info("========================请求任务Start=========================");
        Map<String, Object> req = new HashMap<>();
        req.put("appId", petroAppid);
        req.put("appKey", petroAppKey);
        req.put("timestamp", System.currentTimeMillis()+ "");
        req.put("nonce" , generateRandomString(16));

        // 业务数据
        JSONObject jsonData = new JSONObject();
        jsonData.put("alias" , code);
        jsonData.put("businessId" , phone);

        //业务内容加密
        String bizContent = JSONObject.toJSONString(jsonData);
        bizContent = encrypt(bizContent);

        //加密的内容放到参数中
        req.put("jsonData", bizContent);
        //生成签名
        String sign = PetroEncryptUtil.generateSign(req);
        log.info("获取电子券-请求参数: " + JSON.toJSONString(req));
        Map<String , Object> postData = new HashMap<>();
        postData.put("sign" , sign);
        postData.put("timestamp" , req.get("timestamp"));
        postData.put("jsonData" , bizContent);
        postData.put("nonce" , req.get("nonce"));
        postData.put("appId", petroAppid);
        JSONObject object = HttpsUtils.doPost(reqUrl + "/outapi/getCouponYt", postData);
        log.info("获取电子券-回调参数: " + JSON.toJSONString(object));
        log.info("========================请求任务End=========================");
        // 请求接口
        return object;
    }

    /**
    * @Author Sum1Dream
    * @Name couponDetail
    * @Description // 查询电子券状态
    * @Date 11:20 2023/11/17
    * @Param ticketNum
     * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject couponDetail(String ticketNum , String phone)  throws Exception{
        log.info("========================请求任务Start=========================");
        Map<String, Object> req = new HashMap<>();
        req.put("appId", petroAppid);
        req.put("appKey", petroAppKey);
        req.put("timestamp", System.currentTimeMillis() + "");
        req.put("nonce" , generateRandomString(16));

        // 业务数据
        JSONObject jsonData = new JSONObject();
        jsonData.put("voucher" , ticketNum);
        jsonData.put("businessId" , phone);


        //业务内容加密
        String bizContent = JSONObject.toJSONString(jsonData);
        bizContent = encrypt(bizContent);

        //加密的内容放到参数中
        req.put("jsonData", bizContent);
        //生成签名
        String sign = PetroEncryptUtil.generateSign(req);
        //签名放到参数中
        req.put("sign", sign);
        Map<String , Object> postData = new HashMap<>();
        postData.put("sign" , sign);
        postData.put("timestamp" , req.get("timestamp"));
        postData.put("jsonData" , bizContent);
        postData.put("nonce" , req.get("nonce"));
        postData.put("appId", petroAppid);
        log.info("查询电子券状态-请求参数: " + JSON.toJSONString(req));
        // 请求接口
        JSONObject object = HttpsUtils.doPost(reqUrl + "/outapi/getStateYt", postData);

        log.info("查询电子券状态-回调参数: " + JSON.toJSONString(object));
        return object;
    }

    /**
    * @Author Sum1Dream
    * @Name unusedCoupons
    * @Description //  获取核销码
    * @Date 14:32 2024/1/31
    * @Param ticketNum
     * @Param phone
     * @return com.alibaba.fastjson.JSONObject
    */
    public  JSONObject getCheckCode(String ticketNum , String phone)  throws Exception{
        log.info("========================请求任务Start=========================");
        Map<String, Object> req = new HashMap<>();
        req.put("appId", petroAppid);
        req.put("appKey", petroAppKey);
        req.put("timestamp", System.currentTimeMillis() + "");
        req.put("nonce" , generateRandomString(16));

        // 业务数据
        JSONObject jsonData = new JSONObject();
        jsonData.put("voucher" , ticketNum);
        jsonData.put("businessId" , phone);


        //业务内容加密
        String bizContent = JSONObject.toJSONString(jsonData);
        bizContent = encrypt(bizContent);

        //加密的内容放到参数中
        req.put("jsonData", bizContent);
        //生成签名
        String sign = PetroEncryptUtil.generateSign(req);
        //签名放到参数中
        req.put("sign", sign);
        Map<String , Object> postData = new HashMap<>();
        postData.put("sign" , sign);
        postData.put("timestamp" , req.get("timestamp"));
        postData.put("jsonData" , bizContent);
        postData.put("nonce" , req.get("nonce"));
        postData.put("appId", petroAppid);
        log.info("查询电子券状态-请求参数: " + JSON.toJSONString(req));
        // 请求接口
        JSONObject object = HttpsUtils.doPost(reqUrl + "/outapi/getCheckCode", postData);

        log.info("查询电子券状态-回调参数: " + JSON.toJSONString(object));
        return object;
    }


    /**
    * @Author Sum1Dream
    * @Name cancelCouponsYt
    * @Description // 注销电子券
    * @Date 15:19 2024/1/31
    * @Param ticketNum
     * @Param phone
     * @return com.alibaba.fastjson.JSONObject
    */
    public JSONObject cancelCouponsYt(String ticketNum , String phone)  throws Exception{
        log.info("========================请求任务Start=========================");
        Map<String, Object> req = new HashMap<>();
        req.put("appId", petroAppid);
        req.put("appKey", petroAppKey);
        req.put("timestamp", System.currentTimeMillis() + "");
        req.put("nonce" , generateRandomString(16));

        String [] vouchers = {ticketNum};

        // 业务数据
        JSONObject jsonData = new JSONObject();
        jsonData.put("vouchers" , vouchers);
        jsonData.put("businessId" , phone);

        //业务内容加密
        String bizContent = JSONObject.toJSONString(jsonData);
        bizContent = encrypt(bizContent);

        //加密的内容放到参数中
        req.put("jsonData", bizContent);
        //生成签名
        String sign = PetroEncryptUtil.generateSign(req);
        //签名放到参数中
        req.put("sign", sign);
        Map<String , Object> postData = new HashMap<>();
        postData.put("sign" , sign);
        postData.put("timestamp" , req.get("timestamp"));
        postData.put("jsonData" , bizContent);
        postData.put("nonce" , req.get("nonce"));
        postData.put("appId", petroAppid);
        log.info("查询电子券状态-请求参数: " + JSON.toJSONString(req));
        // 请求接口
        JSONObject object = HttpsUtils.doPost(reqUrl + "/outapi/cancelCouponsYt", postData);

        log.info("查询电子券状态-回调参数: " + JSON.toJSONString(object));
        return object;
    }
    
    /**
    * @Author Sum1Dream
    * @Name memberCards
    * @Description // ETC卡券下发接口
    * @Date 14:17 2024/2/28
    * @Param object  
     * @return com.alibaba.fastjson.JSONObject
    */
    public JSONObject etcOrder(Map<String , Object> object) {
        log.info("========================请求任务Start=========================");
        log.info("卡券下发接口-请求参数: " + JSON.toJSONString(object));
        // 请求接口
        JSONObject jsonObject = HttpsUtils.doPost(CommonSysConst.getSysConfig().getEtcPostUrl() + "channel/order"  , object);
        log.info("卡券下发接口-回调参数: " + JSON.toJSONString(jsonObject));
        return jsonObject;
    }

    /**
    * @Author Sum1Dream
    * @Name etcCardStatus
    * @Description // etc卡券状态查询接口
    * @Date 14:22 2024/2/28
    * @Param object
     * @return com.alibaba.fastjson.JSONObject
    */
    public JSONObject etcCardStatus(Map<String , Object> object) {
        log.info("========================请求任务Start=========================");
        log.info("卡券状态查询接口-请求参数: " + JSON.toJSONString(object));
        // 请求接口
        JSONObject jsonObject = HttpsUtils.doPost(CommonSysConst.getSysConfig().getEtcPostUrl() + "/channel/cardStatus"  , object);
        log.info("卡券状态查询接口-回调参数: " + JSON.toJSONString(jsonObject));
        return jsonObject;
    }

    /**
    * @Author Sum1Dream
    * @Name etcDestroy
    * @Description //  卡券退款接口
    * @Date 14:23 2024/2/28 
    * @Param object  
     * @return com.alibaba.fastjson.JSONObject
    */
    public JSONObject etcDestroy(Map<String , Object> object) {
        log.info("========================请求任务Start=========================");
        log.info(" 卡券退款接口-请求参数: " + JSON.toJSONString(object));
        // 请求接口
        JSONObject jsonObject = HttpsUtils.doPost(CommonSysConst.getSysConfig().getEtcPostUrl() + "/channel/order/destroy"  , object);
        log.info(" 卡券退款接口-回调参数: " + JSON.toJSONString(jsonObject));
        return jsonObject;
    }

    /**
    * @Author Sum1Dream
    * @Name etcQueryStock
    * @Description // 查询卡券库存接口
    * @Date 14:23 2024/2/28
    * @Param object
     * @return com.alibaba.fastjson.JSONObject
    */
    public JSONObject etcQueryStock(Map<String , Object> object) {
        log.info("========================请求任务Start=========================");
        log.info(" 查询卡券库存接口-请求参数: " + JSON.toJSONString(object));
        // 请求接口
        JSONObject jsonObject = HttpsUtils.doPost(CommonSysConst.getSysConfig().getEtcPostUrl() + "/channel/queryStock"  , object);
        log.info(" 查询卡券库存接口-回调参数: " + JSON.toJSONString(jsonObject));
        return jsonObject;
    }

    /**
    * @Author Sum1Dream
    * @Name generateRandomString
    * @Description // 生成16位随机字符串
    * @Date 16:24 2024/1/15
    * @Param length 长度
     * @return java.lang.String
    */
    private static String generateRandomString(Integer length) {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replaceAll("-", "").substring(0, length);
    }


    /**
     * @param content AES加密前的明文
     * @return AES加密后的内容
     */
    public static  String encrypt(final String content) {
        return PetroEncryptUtil.encrypt(content , petroAesKey);
    }

    /**
     * @Author Sum1Dream
     * @Name decrypt
     * @Description // 解密
     * @Date 16:19 2023/11/15
     * @Param content
     * @Param key
     * @return java.lang.String
     */
    public static String decrypt(final String content) {
        return PetroEncryptUtil.decrypt(content , petroAesKey);
    }

}
