package com.hfkj.service.coupon.channel;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.AESTool;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.common.utils.MD5Util;
import com.hfkj.config.CommonSysConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 重庆中石油卡券
 * @className: CqPetroChina
 * @author: HuRui
 * @date: 2022/9/26
 **/
public class ChongQingCNPCCouponService {

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

    /**
     * 电子券预发放接口
     * @param requestCode
     * @param tradeId
     * @param ticketSum
     * @param phone
     */
    public static JSONObject preSendCoupon(String requestCode, String tradeId, Integer ticketSum, String phone) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("requestCode", requestCode);
        map.put("tradeId", tradeId);
        map.put("ticketSum", ticketSum);
        map.put("phone", phone);
        return request("preSendCoupon", map);
    }

    /**
     * 发放电子券
     * @param requestCode
     * @param tradeId
     * @param ticketSum
     * @param phone
     */
    public static JSONObject sendCNPCTicket(String requestCode, String tradeId, Integer ticketSum, String phone) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("requestCode", requestCode);
        map.put("tradeId", tradeId);
        map.put("ticketSum", ticketSum);
        map.put("phone", phone);
        return request("sendCNPCTicket", map);
    }

    /**
     * 电子券作废
     * @param couponNo 电子券编号
     * @param random 随机数
     */
    public static JSONObject invalidCoupon(String couponNo, String random) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("couponNo", couponNo);
        map.put("random", random);
        return request("invalidCoupon", map);
    }

    /**
     * 获取中石油跨界券核销码
     * @param couponNo
     * @param tradeId
     * @return
     */
    public static JSONObject getCNPCCheckCode(String couponNo, String tradeId) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("couponNo", couponNo);
        map.put("tradeId", tradeId);
        return request("getCNPCCheckCode", map);
    }

    /**
     * 获取电子券详情
     * @param couponNo
     * @return
     */
    public static JSONObject queryCoupon(String couponNo) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("couponNo", couponNo);
        map.put("random", String.valueOf(new Random().nextInt(899999) + 100000));
        return request("queryCoupon", map);
    }


    /**
     * 请求
     * @param actionType  接口类型
     * @param param       参数
     * @return
     * @throws Exception
     */
    public static JSONObject request(String actionType, Map<String,Object> param) throws Exception {
        String verifyCode = AESTool.Encrypt(JSONObject.toJSONString(param), CommonSysConst.getSysConfig().getChongQingCnpcMerKey());

        JSONObject header = new JSONObject();
        header.put("strVendorCode", CommonSysConst.getSysConfig().getChongQingCnpcMerNo());
        header.put("strActionType", actionType);
        header.put("verifyCode", MD5Util.encode((verifyCode + CommonSysConst.getSysConfig().getChongQingCnpcMerKey()).getBytes()).toLowerCase());

        JSONObject msg = new JSONObject();
        msg.put("head", header);
        msg.put("body", verifyCode);

        Map<String, Object> reqParam = new HashMap<>();
        reqParam.put("sendMessage", msg);

        log.info("============start============");
        log.info("请求接口：" + actionType);
        log.info("参数：" + JSONObject.toJSONString(param));
        log.info("请求参数：" + JSONObject.toJSONString(reqParam));
        JSONObject response = HttpsUtils.doPost(CommonSysConst.getSysConfig().getChongQingCnpcUrl(), reqParam, new HashMap<>());
        log.info("响应参数：" + response.toJSONString());
        log.info("============end============");

        JSONObject postMessage = response.getJSONObject("postMessage");
        if (postMessage == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请求失败！");
        }
        // 解密body
        JSONObject body = decryptBody(response.getJSONObject("postMessage").getString("body"));
        if (!body.getInteger("status").equals(1)) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, body.getString("info"));
        }
        return body;
    }

    /**
     * 解密body
     * @param bodyStr body加密字符串
     * @return
     */
    public static JSONObject decryptBody(String bodyStr) {
        // 解密body
        String decryptBody = AESTool.Decrypt(bodyStr, CommonSysConst.getSysConfig().getChongQingCnpcMerKey());
        return JSONObject.parseObject(decryptBody, JSONObject.class);
    }

    /**
     * 解密电子券码
     * @param couponCode 券码加密字符串
     * @return 核销码
     */
    public static String decryptCouponCode(String couponCode) {
        return AESTool.Decrypt(couponCode, CommonSysConst.getSysConfig().getChongQingCnpcCouponSignKey());
    }

    public static void main(String[] args) {
        //System.out.println(AESTool.Decrypt("A9D356D4614F874586EAF8678C8C3E7D8CD0EEF031ADA44DDAA3B342CE4BBE44F115AABE27324A6A1F049619139A1A889FCA0FB48EB6E1EA4B1B50F041961B2D686FE8B9696C02DF95BA99B342747D67ECC7847646C87993CF924F33C1308829","n2j30jxhl3rhuoci"));
        System.out.println(AESTool.Decrypt("0E14C37B5657B48857A7D3AEFB8E24BACCA40AFF00B073FC22B4116FC97BAED3CBF38FDC5196FDF595637701FDEC1CDDA0AE518707D91B16DA65A63848AF6DB117D2F6E31A09074EB0E42422C6B3B15B96D230680D9E97E4324A384AA8E60F6E","5ojd0nkdqb1ff84t"));
        //System.out.println(AESTool.Decrypt("3549AD565800370205265D7518D26E5F40C90A61FDEB5DD73966126A5D25F5E9","5ojldakiz343a6yk"));
    }
}
