package cn.pluss.platform.subMerchant.impl;

import cn.pluss.platform.channel.MerchantAuditService;
import cn.pluss.platform.entity.MerchantChannelStatus;
import cn.pluss.platform.entity.SubMerchant;
import cn.pluss.platform.mapper.SubMerchantMapper;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.subMerchant.SubMerchantService;
import cn.pluss.platform.yinsheng.v20210929.ReqMethod;
import cn.pluss.platform.ys.impl.v20210929.YsConfigV2;
import cn.pluss.platform.ys.impl.v20210929.YsConstant;
import cn.pluss.platform.ys.impl.v20210929.YsServiceV2;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class SubMerchantServiceImpl extends ServiceImpl<SubMerchantMapper, SubMerchant> implements SubMerchantService {

    @Autowired @Lazy
    private MerchantAuditService ryxMerchantAuditService;

    @Autowired @Lazy
    private MerchantAuditService sxfMerchantAuditService;

    @Autowired @Lazy
    private YsServiceV2 ysServiceV2;

    @Autowired @Lazy
    private MerchantChannelStatusService mcsService;

    @Override
    public void reqChannelSubMerchant(String merchantCode, int channel) {
        MerchantChannelStatus mcs = mcsService.getByMerchantCode(merchantCode, channel);
        if (mcs == null || !Objects.equals(mcs.getChannel(), channel)) {
            return;
        }

        switch (channel) {
            case 1:
                reqSxfSubMerchant(mcs);
                break;
            case 3:
                reqRyxSubMerchant(mcs);
                break;
            case 4:
                reqYsSubMerchant(mcs);
                break;
            default:
                break;
        }
    }

    @Override
    public void reqChannelSubMerchant(MerchantChannelStatus mcs) {
        if (mcs == null || mcs.getChannel() == null) {
            return;
        }

        switch (mcs.getChannel()) {
            case 1:
                reqSxfSubMerchant(mcs);
                break;
            case 3:
                reqRyxSubMerchant(mcs);
                break;
            case 4:
                reqYsSubMerchant(mcs);
                break;
            default:
                break;
        }
    }

    private void reqYsSubMerchant(MerchantChannelStatus mcs) {
        if (mcs.getApplicationId() == null) {
            return;
        }

        JSONObject params = new JSONObject();
        params.put("sysFlowId", mcs.getApplicationId());
        JSONObject data = ysServiceV2.req(YsConfigV2.MER_RESULT, ReqMethod.MERCHANT_QUERY, params);

        String status = data.getString("status");

        if (YsConstant.AuditStatus.AUDIT_SUCCESS.getCode().equals(status)) {
            String mercId = data.getString("mercId");

            // 第三方商户号信息等
            JSONArray thirdMercIdList = data.getJSONArray("thridMercList");
            try {
                if (thirdMercIdList != null) {

                    int size = thirdMercIdList.size();
                    for (int i = 0; i < size; i++) {
                        JSONObject thirdMerc = thirdMercIdList.getJSONObject(i);
                        String flag = thirdMerc.getString("flag");
                        String thirdMercId = thirdMerc.getString("thirdMercId");
                        String type = thirdMerc.getString("type");

                        SubMerchant subMerchant = new SubMerchant();
                        subMerchant.setMerchantId(mercId);

                        if ("wx".equals(flag)) {
                            flag = "WX";
                        }

                        if ("alipay".equals(flag)) {
                            flag = "ZFB";
                        }

                        if (StringUtils.isEmpty(thirdMercId)) {
                            continue;
                        }

                        subMerchant.setSubMchId(thirdMercId + "_" + type);
                        subMerchant.setSubMchType(flag);
                        subMerchant.setChannelTypeId("4");
                        save(subMerchant);
                    }
                }
            } catch (Exception ignored) {
            }
        }
    }

    private void reqRyxSubMerchant(MerchantChannelStatus mcs) {
        String applicationId = mcs.getApplicationId();
        // 拼接成真正的orderId
        String orderId = applicationId.substring(0, 8) + "-" +
                applicationId.substring(8, 12) + "-" +
                applicationId.substring(12, 16) + "-" +
                applicationId.substring(16, 20) + "-" +
                applicationId.substring(20);
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("orderId", orderId);
        try {
            JSONObject auditResult = ryxMerchantAuditService.merchantAuditResult(reqMap);

            // 进件成功
            if (auditResult.getString("wxMerId") != null) {
                try {
                    // 保存微信子商户信息
                    SubMerchant subMerchantWx = new SubMerchant();
                    subMerchantWx.setMerchantId(auditResult.getString("onlMerId"));
                    subMerchantWx.setSubMchType("WX");
                    subMerchantWx.setStatus(auditResult.getString("wxStatus"));
                    subMerchantWx.setChannelTypeId("3");
                    subMerchantWx.setRemark(auditResult.getString("wxResult"));
                    subMerchantWx.setSubMchId(auditResult.getString("wxMerId"));
                    save(subMerchantWx);
                } catch (DuplicateKeyException e) {
                    log.warn("相同的微信子商户号, 不需要重复保存");
                } catch (Exception e) {
                    e.printStackTrace();
                    log.warn("保存微信子商户号错误");
                }
            }

            if (auditResult.getString("zfbMerId") != null) {
                try {
                    // 保存支付宝子商户信息
                    SubMerchant subMerchantZfb = new SubMerchant();
                    subMerchantZfb.setMerchantId(auditResult.getString("onlMerId"));
                    subMerchantZfb.setSubMchType("ZFB");
                    subMerchantZfb.setStatus(auditResult.getString("zfbStatus"));
                    subMerchantZfb.setChannelTypeId("3");
                    subMerchantZfb.setRemark(auditResult.getString("zfbResult"));
                    subMerchantZfb.setSubMchId(auditResult.getString("zfbMerId"));
                    save(subMerchantZfb);
                } catch (DuplicateKeyException e) {
                    log.warn("相同的支付宝子商户号, 不需要重复保存");
                } catch (Exception e) {
                    e.printStackTrace();
                    log.warn("保存支付宝子商户号错误");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void reqSxfSubMerchant(MerchantChannelStatus mcs) {
        Map<String, Object> param = new HashMap<>();
        param.put("merchantId", mcs.getMerchantId());
        try {
            sxfMerchantAuditService.merchantAuditResult(param);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
