package cn.pluss.platform.ys.edit;

import cn.pluss.platform.channel.BaseMchChannelRateNewService;
import cn.pluss.platform.channel.MerchantAuditService;
import cn.pluss.platform.converter.Converter;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.yinsheng.v20210929.ReqMethod;
import cn.pluss.platform.ys.impl.v20210929.YsAuditServiceImpl;
import cn.pluss.platform.ys.impl.v20210929.YsConfigV2;
import cn.pluss.platform.ys.impl.v20210929.YsServiceV2;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 银盛修改费率的接口
 * @author djh
 */
@Slf4j
@Service("ysChangeRateService")
public class YsChangeRateServiceImpl extends BaseMchChannelRateNewService {

    @Autowired
    private YsServiceV2 ysServiceV2;

    @Autowired
    private MerchantAuditService ysAuditServiceV2;

    @Autowired @Lazy
    private MerchantBaseInfoService mbiService;

    @Autowired @Lazy
    private MerchantChannelStatusService mcsService;

    @Override
    public void process(String userId, MerchantRateNew merchantRate) {

        MerchantBaseInfo mbi = mbiService.getMerchantBaseInfoByUserId(userId);
        MerchantChannelStatus mcs = mcsService.getByMerchantCodeAndChannelType(mbi.getMerchantCode(), Account.CHANNEL_TYPE_D0);
        if (!MerchantChannelStatus.isAudited(mcs)) {
            throw new MsgException("暂未开通实时通道");
        }

        // 先赋值
        MerchantRateNew.setRateValue(merchantRate);

        JSONObject reqParam = new JSONObject();
        JSONObject bankFee1 = new JSONObject();
        bankFee1.put("creditRate", merchantRate.getBankRateLargeValue().divide(new BigDecimal(100)).doubleValue() + "");
        bankFee1.put("creditMin", "1");
        bankFee1.put("debitRate", merchantRate.getBankRateLargeValue().divide(new BigDecimal(100)).doubleValue() + "");
        bankFee1.put("debitMin", "1");
        bankFee1.put("debitMax", "999900");

        JSONObject bankFee2 = new JSONObject();
        bankFee2.put("creditRate", merchantRate.getBankRateValue().divide(new BigDecimal(100)).doubleValue() + "");
        bankFee2.put("creditMin", "1");
        bankFee2.put("debitRate", merchantRate.getBankRateValue().divide(new BigDecimal(100)).doubleValue() + "");
        bankFee2.put("debitMin", "1");
        bankFee2.put("debitMax", "999900");

        JSONObject wxAliFee = new JSONObject();
        wxAliFee.put("wechatSignRate", merchantRate.getWxRateValue().divide(new BigDecimal(100)).doubleValue() + "");
        wxAliFee.put("wechatSignMin", "1");
        wxAliFee.put("alipaySignRate", merchantRate.getAliRateValue().divide(new BigDecimal(100)).doubleValue() + "");
        wxAliFee.put("alipaySignMin", "1");

        reqParam.put("mercId", mcs.getMerchantId());
        reqParam.put("bankFee1", bankFee1);
        reqParam.put("bankFee2", bankFee2);
        reqParam.put("wxAliFee", wxAliFee);

        // TODO 发起修改费率请求
        JSONObject respData = ysServiceV2.req(YsConfigV2.MER_RATE_CHANGE, ReqMethod.MERCHANT_CHANGE_ONLINE_FEE, reqParam);
        log.info("请求结果：{}", respData);
        String changeFlowId = respData.getString("changeFlowId");
        mcs.getExtra().put("changeFlowId", changeFlowId);

        MerchantRateNewRecord mrnr = Converter.INSTANCE.trans(merchantRate);
        mrnr.setCreateTime(new Date());
        mrnr.setUpdateTime(new Date());
        mrnr.setRemark(changeFlowId);
        rateNewRecordService.save(mrnr);

        // 上传一张空白图作为结算信息修改申请表
        ((YsAuditServiceImpl) ysAuditServiceV2).imageUploadEdit("https://www.shouyinbei.net/resources/images/upload/blank.png", "B008", changeFlowId);
    }

    @Override
    public void after(String userId, MerchantRateNew merchantRate) {
        // 不作任何保存
    }

    @Override
    public String getChannelId() {
        return "4";
    }

    @Override
    public MerchantRateNew getDefaultRate() {
        MerchantRateNew rate = new MerchantRateNew();
        rate.setAliRate(38);
        rate.setWxRate(38);
        rate.setBankRate(36);
        rate.setBankRateLarge(37);

        MerchantRateNew.setRateValue(rate);
        return rate;
    }
}
