package com.hai.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hai.common.pay.util.sdk.WXPayConstants;
import com.hai.common.utils.DateUtil;
import com.hai.common.utils.HttpsUtils;
import com.hai.common.utils.WxUtils;

import com.hai.entity.OutRechargeChildOrder;
import com.hai.entity.OutRechargeOrder;
import com.hai.service.OutRechargeChildOrderService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName TelConfig
 * @Author Sum1Dream
 * @Version 1.0.0
 * @Description //  充值接口
 * @Date  2021/7/18 14:18
 **/
@Component
public class RechargeConfig {

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


    /**
    * @Author Sum1Dream
    * @name getProductsList.java
    * @Description // 获取龙阅产品
    * @Date 10:03 2022/5/24
    * @Param []
    * @return com.alibaba.fastjson.JSONObject
    */
    public  static JSONObject getProductsListByLy() throws Exception {
        Map<String,Object> paramMap = new HashMap<>();

        paramMap.put("userid" , CommonSysConst.getSysConfig().getLyMemberId());
        paramMap.put("sign", WxUtils.generateSignatureLy(paramMap , CommonSysConst.getSysConfig().getLyApiKey() , WXPayConstants.SignType.MD5));

        return HttpsUtils.doPost(CommonSysConst.getSysConfig().getLyPostUrl()+ "index/product", paramMap);
    }

    /**
    * @Author Sum1Dream
    * @name rechargeOrderByLy.java
    * @Description // 龙阅订单充值
    * @Date 09:57 2022/5/25
    * @Param [com.alibaba.fastjson.JSONObject]
    * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject rechargeOrderByLy(JSONObject object) {

        try {

            Map<String,Object> paramMap = new HashMap<>();

            paramMap.put("out_trade_num" , object.getString("out_trade_num"));
            paramMap.put("product_id" , object.getString("product_id"));
            paramMap.put("mobile" , object.getString("mobile"));
            paramMap.put("notify_url" , object.getString("notifyUrl"));
            paramMap.put("userid" , CommonSysConst.getSysConfig().getLyMemberId());
            paramMap.put("sign", WxUtils.generateSignatureLy(paramMap , CommonSysConst.getSysConfig().getLyApiKey() , WXPayConstants.SignType.MD5));

            log.info("发起龙阅订单充值");
            log.info("龙阅订单充值-请求参数: " + JSON.toJSONString(paramMap));

            JSONObject jsonObject =  HttpsUtils.doPost(CommonSysConst.getSysConfig().getLyPostUrl()+ "index/recharge", paramMap);

            log.info("龙阅订单充值-响应参数: " + jsonObject.toJSONString());
            return jsonObject;

        } catch (Exception e) {
            return null;
        }

    }


    /**
    * @Author Sum1Dream
    * @name checkOrderByLy.java
    * @Description // 龙阅查询订单结果
    * @Date 14:05 2022/5/24
    * @Param [com.alibaba.fastjson.JSONObject]
    * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject checkOrderByLy(JSONObject object) throws Exception {

        Map<String,Object> paramMap = new HashMap<>();

        paramMap.put("userid" , CommonSysConst.getSysConfig().getLyMemberId());
        paramMap.put("out_trade_nums" , object.getString("out_trade_nums"));
        paramMap.put("sign", WxUtils.generateSignatureLy(paramMap , CommonSysConst.getSysConfig().getLyApiKey() , WXPayConstants.SignType.MD5));

        log.info("发起龙阅订单查询");
        log.info("龙阅订单查询-请求参数: " + JSON.toJSONString(paramMap));

        JSONObject jsonObject =  HttpsUtils.doPost(CommonSysConst.getSysConfig().getLyPostUrl()+ "index/check", paramMap);

        log.info("龙阅订单查询-响应参数: " + jsonObject.toJSONString());

        return jsonObject;
    }

    /**
     * @Author Sum1Dream
     * @name rechargeOrderByJj.java
     * @Description // 尖椒订单充值
     * @Date 09:57 2022/5/25
     * @Param [com.alibaba.fastjson.JSONObject]
     * @return com.alibaba.fastjson.JSONObject
     */
    public static JSONObject rechargeOrderByJj(JSONObject object) {

        try {
            String timestamp = String.valueOf(System.currentTimeMillis());

            Map<String , Object> map = new HashMap<>();
            map.put("mobile" , object.getString("mobile"));
            map.put("amount" , object.getString("amount"));
            map.put("out_order_id" , object.getString("out_order_id"));
            map.put("app_key" , CommonSysConst.getSysConfig().getJjAppKey());
            map.put("timestamp" , timestamp.substring(0,timestamp.length()-3));
            map.put("notify_url" , object.getString("notifyUrl"));
            String signStr = WxUtils.generateSignatureAppSecret(map, CommonSysConst.getSysConfig().getJjAppSecret() , WXPayConstants.SignType.MD5);
            map.put("sign" , signStr);

            log.info("发起尖椒充值");
            log.info("尖椒订单充值-请求参数: " + JSON.toJSONString(map));

            JSONObject jsonObject =  HttpsUtils.doPost(CommonSysConst.getSysConfig().getJjUrl() + "createOrder" , map);

            log.info("尖椒订单充值-响应参数: " + jsonObject.toJSONString());
            return jsonObject;
        } catch (Exception e) {
            return  null;
        }



    }


    /**
    * @Author Sum1Dream
    * @name rechargeOrderByCy.java
    * @Description // 城宇话费下单
    * @Date 10:22 2022/7/14
    * @Param [com.alibaba.fastjson.JSONObject]
    * @return com.alibaba.fastjson.JSONObject
    */
    public static JSONObject rechargeOrderByCy(JSONObject object) throws Exception {

        Map<String,Object> paramMap = new HashMap<>();

        paramMap.put("agentOrderId" , object.getString("agentOrderId"));
        paramMap.put("productId" , object.getInteger("productId"));
        paramMap.put("mobile" , object.getString("mobile"));
        paramMap.put("timestamp" , DateUtil.date2String(new Date() , DateUtil.Y_M_D_HMS));
        paramMap.put("appid" , CommonSysConst.getSysConfig().getCyApiKey());
        paramMap.put("sign", WxUtils.generateSignatureCy(paramMap , CommonSysConst.getSysConfig().getCyAppSecret() , WXPayConstants.SignType.MD5));

        log.info("发起城宇话费下单=");
        log.info("城宇话费下单-请求参数: " + JSON.toJSONString(paramMap));

        JSONObject jsonObject =  HttpsUtils.doPostByCy(CommonSysConst.getSysConfig().getCyPostUrl()+ "/api/interface/recharge", paramMap);

        log.info("城宇话费下单-响应参数: " + jsonObject.toJSONString());
        return jsonObject;

    }


    /**
     * @Author Sum1Dream
     * @Name checkOrderByHg
     * @Description // 查询畅停订单下单
     * @Date 14:50 2023/2/8
     * @Param [object]
     * @Return com.alibaba.fastjson.JSONObject
     */
    public static  JSONObject rechargeOrderByHg(JSONObject object) {
        try {
            Map<String,Object> paramMap = new HashMap<>();

            paramMap.put("mchid" , CommonSysConst.getSysConfig().getHgMemberId());
            paramMap.put("phone" , object.getString("phone"));
            paramMap.put("orderid" , object.getString("orderid"));
            paramMap.put("price" , object.getInteger("price").toString());
            paramMap.put("product_id" , object.getString("product_id"));
            paramMap.put("notify" ,  CommonSysConst.getSysConfig().getHgNotifyUrl());
            paramMap.put("sign", WxUtils.generateSignatureHg(paramMap , CommonSysConst.getSysConfig().getHgApiKey() , WXPayConstants.SignType.MD5));

            log.info("发起畅停话费查询订单=");
            log.info("畅停话费查询订单-请求参数: " + JSON.toJSONString(paramMap));

            System.out.println("畅停话费查询订单-请求参数: " + JSON.toJSONString(paramMap));
            JSONObject repsObject = HttpsUtils.doPostForm(CommonSysConst.getSysConfig().getHgPostUrl()+ "external/api/telPay", paramSort(paramMap), new HashMap<>());

            log.info("畅停话费查询订单-响应参数: " + repsObject.toJSONString());
            return repsObject;
        }catch (Exception e) {
            return null;
        }

    }

    /**
     * @Author Sum1Dream
     * @Name searchOrder
     * @Description // 畅停话费查询订单详情
     * @Date 11:39 2023/2/7
     * @Param [object]
     * @Return com.alibaba.fastjson.JSONObject
     */
    public static  JSONObject checkOrderByHg(JSONObject object) throws Exception {
        Map<String,Object> paramMap = new HashMap<>();

        paramMap.put("mchid" , CommonSysConst.getSysConfig().getHgMemberId());
        paramMap.put("orderid" , object.getString("orderid"));
        paramMap.put("sign", WxUtils.generateSignatureHg(paramMap , CommonSysConst.getSysConfig().getHgApiKey() , WXPayConstants.SignType.MD5));

        log.info("发起畅停话费查询订单");
        log.info("畅停话费查询订单-请求参数: " + JSON.toJSONString(paramMap));

        JSONObject jsonObject =  HttpsUtils.doPostForm(CommonSysConst.getSysConfig().getHgPostUrl()+ "external/api/searchOrder", paramSort(paramMap) , new HashMap<>());

        log.info("畅停话费查询订单-响应参数: " + jsonObject.toJSONString());
        return jsonObject;
    }


    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();
    }



}
