package cn.com.ctop.track.service.impl;

import cn.com.ctop.track.entity.ClickInfo;
import cn.com.ctop.track.mapper.ClickInfoMapper;
import cn.com.ctop.track.service.ClickInfoService;
import cn.com.ctop.track.utils.Check;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayUserInviteRtaConsultModel;
import com.alipay.api.request.AlipayUserInviteRtaConsultRequest;
import com.alipay.api.response.AlipayUserInviteRtaConsultResponse;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.TreeMap;

@Service
public class ClickInfoServiceImpl extends ServiceImpl<ClickInfoMapper, ClickInfo> implements ClickInfoService {
    private static String url = "https://openapi.alipay.com/gateway.do";

    @Override
    public JSONObject getRtaInfo(String oaid, String imei, String targetCrowdPackageKey) {
        AlipayClient alipayClient = null;
        JSONObject returnJson = new JSONObject();
        try {
            alipayClient = new DefaultAlipayClient(getAlipayConfig());
            // 构造请求参数以调用接口
            AlipayUserInviteRtaConsultRequest request = new AlipayUserInviteRtaConsultRequest();
            AlipayUserInviteRtaConsultModel model = new AlipayUserInviteRtaConsultModel();
            // 设置设备标识信息

            if (!Check.isNull(oaid)) {
                model.setPrincipalType("OAID");
                model.setPrincipal(oaid);
            } else {
                model.setPrincipalType("IMEI");
                model.setPrincipal(imei);
            }
            // 设置OpenId映射
            model.setOpenId("");
            model.setEncryptType("MD5");
            // 设置加密类型

            // 设置咨询策略组
            if (!Check.isNull(targetCrowdPackageKey)) {
                model.setTargetCrowdPackageKey(targetCrowdPackageKey);
            }
            request.setBizModel(model);
            // 第三方代调用模式下请设置app_auth_token
            // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");
            AlipayUserInviteRtaConsultResponse response = alipayClient.execute(request);
            System.err.println("调用rta返回：" + JSONObject.parseObject(response.getBody()));
            if (response.isSuccess()) {
                JSONObject jsonObject = JSONObject.parseObject(response.getBody());
                JSONObject alipayUserInviteRtaConsultResponse = jsonObject.getJSONObject("alipay_user_invite_rta_consult_response");
                if (Check.isNull(alipayUserInviteRtaConsultResponse)) {
                    returnJson.put("code", "500");
                    returnJson.put("message", "返回信息为空");
                } else {
                    returnJson.put("code", alipayUserInviteRtaConsultResponse.getIntValue("code"));
                    returnJson.put("message", alipayUserInviteRtaConsultResponse.getString("msg"));
                    returnJson.put("requiredFlow", alipayUserInviteRtaConsultResponse.getBoolean("required_flow"));
                    JSONArray rtaInfoList = alipayUserInviteRtaConsultResponse.getJSONArray("rta_info_list");
                    returnJson.put("rtaInfoList", rtaInfoList);
                }
            } else {
                returnJson.put("code", "500");
                returnJson.put("message", "调用失败");
            }
        } catch (AlipayApiException e) {
            returnJson.put("code", "500");
            returnJson.put("message", e.getMessage());
            throw new RuntimeException(e);


        }

        return returnJson;
    }


    private static AlipayConfig getAlipayConfig() {
        String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC/l2Xqk2jJ5gWqR4hHYy8cpWUzmjandQ5+ndc5UZOxTwu+c7T86a5j0S/AGt9LboEax7G4lNqKLPyLUbvp1KZXe82JhyAX7SzjcYghDNaqtXbqvlaflxJWNklCdbmqqHMevIvRniYbShTW//+o9deJx8lvxUNpYw2/sEgmKobkCDTS5BA/aHl1fGrwAiS3rnR+CR2tPsf43KYCnHRq8JGxCVWNo8YJaPDjuT+HSnKv8MXCj3Z+uvdXZVbmPyyKObjutErgfVkYNkt1O4RO+OkIl3lZdNAQ/rY89MwiYHcacbtVoj1Lw4EWU/Uhl4fMseIkDTUcJHCz8APh/jYdNKiHAgMBAAECggEALpchiI66P8a4//SPNlTvQvcRih/pRIWZtb6qdNzanpaLU5ANpicpakqXYw7l2BESQjHnjOxdzJYt/68rlCxLGyEXFWePrWHWtaSJJwhZN4ATX/hTVlHFesOOwyOft9lozUOqeJoNy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I5YqV20kW9RnvqLmW/Mw==";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlYtIyaiB6OUh8hHUSM2KlVspA4kvNbz9HJxfjyCK0X4jqSd47e8Y/aB0WlLM/L/FnC0WOvFo6JpuWoT0lvgQDLqvr5K7Ieb9vkwR8kNousrSzeHnh8j82SdVLliBfrDZbdIkQJg69x3/O1L1+BsJuDbMYpc7Nr0QVrslde/lQkTAUHajvZx1c6Tg8iy5CNqFA6ro/85doANqmqdDMkDkVobnO1gtIdfTIXhAS90tK/YBIAdhWqTQg9sT9qoY5PLLRH7YeORO32zwv7e97ZFPCPHYR2SZrLUlWiNVsLaHJlAirdEbSWeHBqGrkGezVAdDZ28gl2aTeeXHve1WKn4GLwIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("2021001185680036");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }

}
