package cn.pluss.platform.sxf.audit;

import cn.pluss.platform.channel.BaseMchChannelRateService;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.MerchantChannelStatus;
import cn.pluss.platform.entity.MerchantRate;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.MerchantBaseInfoMapper;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.util.SxfConfg;
import cn.pluss.platform.sxf.SxfService;
import cn.pluss.platform.util.RSAUtils;
import cn.pluss.platform.util.SignUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service("sxfChangeRateService")
@EqualsAndHashCode(callSuper = true)
public class SxfChangeRateServiceImpl extends BaseMchChannelRateService {

    private final SxfService sxfService;
    private final MerchantBaseInfoMapper merchantBaseInfoMapper;
    private final MerchantChannelStatusService merchantChannelStatusService;

    @Override
    public void process(String userId, Integer rate, MerchantRate merchantRate) {
        List<MerchantBaseInfo> merchantBaseInfoList = merchantBaseInfoMapper.selectListByUserId(userId);
        if (merchantBaseInfoList == null || merchantBaseInfoList.size() == 0) {
            throw new MsgException("无法获取商户信息");
        }

        MerchantBaseInfo merchantBaseInfo = merchantBaseInfoList.get(0);
        MerchantChannelStatus merchantChannelStatus = new MerchantChannelStatus().setChannel(1).setMerchantCode(merchantBaseInfo.getMerchantCode());
        merchantChannelStatus = merchantChannelStatusService.getOne(new QueryWrapper<>(merchantChannelStatus));
        if (merchantChannelStatus == null) {
            throw new MsgException("商户还未进件");
        }

        JSONObject reqParam = new JSONObject();
        reqParam.put("mno", merchantChannelStatus.getMerchantId());
        JSONArray qrCodeList = new JSONArray();
        // 微信
        JSONObject rate1 = new JSONObject();
        rate1.put("rate", String.valueOf((rate / 100d)));
        rate1.put("rateType", "01");
        qrCodeList.add(rate1);
        // 支付宝
        rate1 = new JSONObject();
        rate1.put("rate", String.valueOf((rate / 100d)));
        rate1.put("rateType", "02");
        qrCodeList.add(rate1);
        // 银联，小于1000, 银联费率最低是23
        rate1 = new JSONObject();
        rate1.put("rate", String.valueOf((Math.max(rate, 23) / 100d)));
        rate1.put("rateType", "06");
        qrCodeList.add(rate1);
        // 银联，大于1000
        rate1 = new JSONObject();
        rate1.put("rate", "0.6");
        rate1.put("rateType", "07");
        qrCodeList.add(rate1);
        reqParam.put("qrcodeList", qrCodeList);
        //reqParam.put("mecAuthority", "01");
        reqParam.put("startSettleAmt", "0.00");
        reqParam.put("reserveSettleAmt", "0.00");
        reqParam.put("settleType", "04");
        String[] channelArr = new String[]{"01", "02", "03"};
        reqParam.put("supportPayChannels", channelArr);

        String[] tradeType = new String[] {"01", "02", "03", "04"};
        reqParam.put("supportTradeTypes", tradeType);

        JSONObject result = sxfService.requestApi(SxfConfg.SXF_UPDATE_MERCHANT_RATE, reqParam);

        String signContent = SignUtils.getSignContent(result);
        boolean sign;
        try {
            sign = RSAUtils.checkSign(SxfConfg.PUBLIC_KEY, signContent, result.getString("sign"));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("修改费率结果验签失败");
            throw new MsgException("修改费率失败");
        }
        if (!sign) {
            throw new MsgException("修改费率失败");
        }

        if (!SxfConfg.SXF_SUCCESS_CODE.equals(result.getString("code"))) {
            throw new MsgException(result.getString("msg"));
        }

        JSONObject respData = result.getJSONObject("respData");
        if (!SxfConfg.SXF_SUCCESS_CODE.equals(respData.get("bizCode"))) {
            throw new MsgException(respData.getString("bizMsg"));
        }
    }
}
