package com.hfkj.channel.saas;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.channel.saas.config.SaasConfig;
import com.hfkj.channel.saas.utils.RSAUtil;
import com.hfkj.channel.saas.utils.SaasCommon;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.HttpsUtils;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Component;

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

/**
 *
 * 活动服务
 * @className: SaasActivityService
 * @author: HuRui
 * @date: 2023/5/12
 **/
@Component
public class SaasActivityService {
    @Resource
    private SaasCommon saasCommon;

    /**
     * 查询服务商活动
     * @return
     */
    public JSONArray queryAgentActivityList() {
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("agentNo", SaasConfig.userNo);

            Map<String,Object> header = new HashMap<>();
            header.put("Authorization", "Bearer " + saasCommon.getToken());

            JSONObject obj = new JSONObject();
            obj.put("data", Base64.encodeBase64String(RSAUtil.encryptByPrivateKey(JSONObject.toJSONString(param).getBytes(), Base64.decodeBase64(RSAUtil.PRIVATE_KEY))));

            JSONObject responseObj = HttpsUtils.doPost(SaasConfig.requestUrl + "htkmarketing/open/welfare/activity", obj, header);
            System.out.println("参数：" + JSONObject.toJSONString(param));
            System.out.println("加密：" + obj.getString("data"));
            System.out.println("request：" + obj.toJSONString());
            System.out.println("response：" + responseObj.toJSONString());
            if (responseObj == null || !responseObj.getString("code").equals("000000")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, responseObj.getString("message"));
            }
            return JSONArray.parseArray(new String(RSAUtil.decryptByPublicKey(Base64.decodeBase64(responseObj.getString("data")), Base64.decodeBase64(RSAUtil.PUBLIC_KEY))));

        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    /**
     * 查询服务商参与状态查询
     * @param activityId
     * @return
     */
    public JSONObject queryAgentActivityStatus(String activityId) throws Exception {
        Map<String,Object> param = new HashMap<>();
        param.put("activityId", activityId);
        param.put("agentNo", SaasConfig.userNo);

        Map<String,Object> header = new HashMap<>();
        header.put("Authorization", "Bearer " + saasCommon.getToken());

        JSONObject obj = new JSONObject();
        obj.put("data", Base64.encodeBase64String(RSAUtil.encryptByPrivateKey(JSONObject.toJSONString(param).getBytes(), Base64.decodeBase64(RSAUtil.PRIVATE_KEY))));

        JSONObject responseObj = HttpsUtils.doPost(SaasConfig.requestUrl + "htkmarketing/open/welfare/agent/status", obj, header);
        System.out.println("参数：" + JSONObject.toJSONString(param));
        System.out.println("加密：" + obj.getString("data"));
        System.out.println("request：" + obj.toJSONString());
        System.out.println("response：" + responseObj.toJSONString());
        if (responseObj == null || !responseObj.getString("code").equals("000000")) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, responseObj.getString("message"));
        }
        return JSONObject.parseObject(new String(RSAUtil.decryptByPublicKey(Base64.decodeBase64(responseObj.getString("data")), Base64.decodeBase64(RSAUtil.PUBLIC_KEY))));
    }

    /**
     * 查询商户参与状态查询
     * @param activityId
     * @param externalCustomerNo 外部商户号
     * @return
     */
    public JSONObject queryCustomerActivityStatus(String activityId,String externalCustomerNo) throws Exception {
        Map<String,Object> param = new HashMap<>();
        param.put("activityId", activityId);
        param.put("externalCustomerNo", externalCustomerNo);

        Map<String,Object> header = new HashMap<>();
        header.put("Authorization", "Bearer " + saasCommon.getToken());

        JSONObject obj = new JSONObject();
        obj.put("data", Base64.encodeBase64String(RSAUtil.encryptByPrivateKey(JSONObject.toJSONString(param).getBytes(), Base64.decodeBase64(RSAUtil.PRIVATE_KEY))));

        JSONObject responseObj = HttpsUtils.doPost(SaasConfig.requestUrl + "htkmarketing/open/welfare/customer/status", obj, header);
        System.out.println("参数：" + JSONObject.toJSONString(param));
        System.out.println("加密：" + obj.getString("data"));
        System.out.println("request：" + obj.toJSONString());
        System.out.println("response：" + responseObj.toJSONString());
        if (responseObj == null || !responseObj.getString("code").equals("000000")) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, responseObj.getString("message"));
        }
        return JSONObject.parseObject(new String(RSAUtil.decryptByPublicKey(Base64.decodeBase64(responseObj.getString("data")), Base64.decodeBase64(RSAUtil.PUBLIC_KEY))));
}

    /**
     * 服务商报名活动
     * @return
     */
    public JSONObject agentRegisterActivity(String activityId) throws Exception {
        Map<String,Object> param = new HashMap<>();
        param.put("agentNo", SaasConfig.userNo);
        param.put("activityId", activityId);

        Map<String,Object> header = new HashMap<>();
        header.put("Authorization", "Bearer " + saasCommon.getToken());

        JSONObject obj = new JSONObject();
        obj.put("data", Base64.encodeBase64String(RSAUtil.encryptByPrivateKey(JSONObject.toJSONString(param).getBytes(), Base64.decodeBase64(RSAUtil.PRIVATE_KEY))));

        JSONObject responseObj = HttpsUtils.doPost(SaasConfig.requestUrl + "htkmarketing/open/welfare/agent/register", obj, header);
        System.out.println("参数：" + JSONObject.toJSONString(param));
        System.out.println("加密：" + obj.getString("data"));
        System.out.println("request：" + obj.toJSONString());
        System.out.println("response：" + responseObj.toJSONString());
        if (responseObj == null || !responseObj.getString("code").equals("000000")) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, responseObj.getString("message"));
        }
        return responseObj;
    }

    /**
     * 商户报名活动
     * @return
     */
    public JSONObject customerRegisterActivity(String activityId, String branchBankNo, String refereeId, List<String> externalCustomerNos) throws Exception {
        Map<String,Object> param = new HashMap<>();
        param.put("activityId", activityId);
        param.put("refereeId", refereeId);
        param.put("branchBankNo", branchBankNo);
        param.put("externalCustomerNos", externalCustomerNos);

        Map<String,Object> header = new HashMap<>();
        header.put("Authorization", "Bearer " + saasCommon.getToken());

        JSONObject obj = new JSONObject();
        obj.put("data", Base64.encodeBase64String(RSAUtil.encryptByPrivateKey(JSONObject.toJSONString(param).getBytes(), Base64.decodeBase64(RSAUtil.PRIVATE_KEY))));

        JSONObject responseObj = HttpsUtils.doPost(SaasConfig.requestUrl + "htkmarketing/open/welfare/customer/register", obj, header);
        System.out.println("参数：" + JSONObject.toJSONString(param));
        System.out.println("加密：" + obj.getString("data"));
        System.out.println("request：" + obj.toJSONString());
        System.out.println("response：" + responseObj.toJSONString());
        if (responseObj == null || !responseObj.getString("code").equals("000000")) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, responseObj.getString("message"));
        }
        return responseObj;
    }


    /**
     * 商户报名活动
     * @return
     */
    public JSONArray referee(String externalCustomerNo) throws Exception {
        Map<String,Object> param = new HashMap<>();
        param.put("externalCustomerNo", externalCustomerNo);
        param.put("page", 1);

        Map<String,Object> header = new HashMap<>();
        header.put("Authorization", "Bearer " + saasCommon.getToken());

        JSONObject obj = new JSONObject();
        obj.put("data", Base64.encodeBase64String(RSAUtil.encryptByPrivateKey(JSONObject.toJSONString(param).getBytes(), Base64.decodeBase64(RSAUtil.PRIVATE_KEY))));

        JSONObject responseObj = HttpsUtils.doPost(SaasConfig.requestUrl + "htkmarketing/open/welfare/referee", obj, header);
        System.out.println("参数：" + JSONObject.toJSONString(param));
        System.out.println("加密：" + obj.getString("data"));
        System.out.println("request：" + obj.toJSONString());
        System.out.println("response：" + responseObj.toJSONString());
        if (responseObj == null || !responseObj.getString("code").equals("000000")) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, responseObj.getString("message"));
        }
        JSONObject responseData = JSONObject.parseObject(new String(RSAUtil.decryptByPublicKey(Base64.decodeBase64(responseObj.getString("data")), Base64.decodeBase64(RSAUtil.PUBLIC_KEY))));
        System.out.println("responseData：" + responseData);
        return responseData.getJSONArray("referees");
    }


    /**
     * 商户费率修改
     * @param activityId
     * @param externalCustomerNo
     * @param rates
     * @return
     * @throws Exception
     */
    public JSONObject customerRatesUpdate(String activityId, String externalCustomerNo, List<Map<String,Object>> rates) throws Exception {
        Map<String,Object> param = new HashMap<>();
        param.put("activityId", activityId);
        param.put("externalCustomerNo", externalCustomerNo);
        param.put("rates", rates);

        Map<String,Object> header = new HashMap<>();
        header.put("Authorization", "Bearer " + saasCommon.getToken());

        JSONObject obj = new JSONObject();
        obj.put("data", Base64.encodeBase64String(RSAUtil.encryptByPrivateKey(JSONObject.toJSONString(param).getBytes(), Base64.decodeBase64(RSAUtil.PRIVATE_KEY))));

        JSONObject responseObj = HttpsUtils.doPost(SaasConfig.requestUrl + "htkmarketing/open/welfare/customer/rates/update", obj, header);
        System.out.println("参数：" + JSONObject.toJSONString(param));
        System.out.println("加密：" + obj.getString("data"));
        System.out.println("request：" + obj.toJSONString());
        System.out.println("response：" + responseObj.toJSONString());
        if (responseObj == null || !responseObj.getString("code").equals("000000")) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, responseObj.getString("message"));
        }
        return responseObj;
    }

    /**
     * 查询商户费率
     * @param activityId
     * @param externalCustomerNo
     * @return
     * @throws Exception
     */
    public JSONArray queryCustomerRates(String activityId, String externalCustomerNo) throws Exception {
        Map<String,Object> param = new HashMap<>();
        param.put("activityId", activityId);
        param.put("externalCustomerNo", externalCustomerNo);

        Map<String,Object> header = new HashMap<>();
        header.put("Authorization", "Bearer " + saasCommon.getToken());

        JSONObject obj = new JSONObject();
        obj.put("data", Base64.encodeBase64String(RSAUtil.encryptByPrivateKey(JSONObject.toJSONString(param).getBytes(), Base64.decodeBase64(RSAUtil.PRIVATE_KEY))));

        JSONObject responseObj = HttpsUtils.doPost(SaasConfig.requestUrl + "htkmarketing/open/welfare/customer/rates/query", obj, header);
        System.out.println("参数：" + JSONObject.toJSONString(param));
        System.out.println("加密：" + obj.getString("data"));
        System.out.println("request：" + obj.toJSONString());
        System.out.println("response：" + responseObj.toJSONString());
        if (responseObj == null || !responseObj.getString("code").equals("000000")) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, responseObj.getString("message"));
        }

        JSONArray responseData = JSONArray.parseArray(new String(RSAUtil.decryptByPublicKey(Base64.decodeBase64(responseObj.getString("data")), Base64.decodeBase64(RSAUtil.PUBLIC_KEY))));
        System.out.println("responseData：" + responseData);
        return responseData;
    }

}
