package cn.pluss.platform.merchant.impl;

import cn.jiguang.common.utils.StringUtils;
import cn.pluss.platform.agent.AgentService;
import cn.pluss.platform.api.ResultCode;
import cn.pluss.platform.channel.MchChannelRateNewService;
import cn.pluss.platform.constants.Constant;
import cn.pluss.platform.converter.Converter;
import cn.pluss.platform.dto.MerchantRateNewDTO;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.exception.RateNoChangeException;
import cn.pluss.platform.mapper.DictMapper;
import cn.pluss.platform.mapper.MerchantRateMapper;
import cn.pluss.platform.mapper.MerchantRateNewMapper;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchant.MerchantRateNewRecordService;
import cn.pluss.platform.merchant.MerchantRateNewService;
import cn.pluss.platform.merchantChannelStatus.MerchantChannelStatusService;
import cn.pluss.platform.notice.NoticeService;
import cn.pluss.platform.notice.template.NoticeTemplateService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.CheckUtils;
import cn.pluss.platform.user.impl.GeneralPushUtil;
import cn.pluss.platform.vo.MerchantRateNewVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * <p>
 * 商户费率表(新) 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2021-08-09
 */
@Service
public class MerchantRateNewServiceImpl extends ServiceImpl<MerchantRateNewMapper, MerchantRateNew> implements MerchantRateNewService {

    @Setter(onMethod_ = {@Lazy, @Autowired})
    private UserAppService userAppService;

    @Autowired
    private MerchantChannelStatusService mcsService;

    @Autowired
    private MerchantBaseInfoService mbiService;

    @Autowired
    private MerchantRateNewRecordService rateNewRecordService;

    @Resource
    private MerchantRateNewMapper rateNewMapper;

    @Autowired @Lazy
    private MchChannelRateNewService sxfChangeRateNewService;

    @Autowired @Lazy
    private MchChannelRateNewService ryxChangeRateNewService;

    @Autowired @Lazy
    private MchChannelRateNewService ysChangeRateService;

    @Autowired @Lazy
    private MchChannelRateNewService ysChangeRateNewService;

    @Autowired
    private AgentService agentInfoService;

    @Resource
    private DictMapper dictMapper;

    @Resource
    private MerchantRateMapper merchantRateMapper;

    @Autowired
    private NoticeTemplateService nTempService;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private GeneralPushUtil generalPushUtil;

    @Autowired
    @Lazy
    private MerchantRateNewService self;

    public static final String WX_RATE = "WX_RATE";

    public static final String ALI_RATE = "ALI_RATE";

    public static final String BANK_RATE = "BANK_RATE_1000";

    public static final String BANK_RATE_LARGE = "BANK_RATE_1000+";

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void passD0(String merchantId) {
        LambdaQueryWrapper<MerchantRateNewRecord> queryWrapper = new LambdaQueryWrapper<MerchantRateNewRecord>()
                .eq(MerchantRateNewRecord::getMerchantId, merchantId)
                .eq(MerchantRateNewRecord::getStatus, "0");

        LambdaQueryWrapper<MerchantRateNew> queryWrapper2 = new LambdaQueryWrapper<MerchantRateNew>()
                .eq(MerchantRateNew::getMerchantId, merchantId)
                .eq(MerchantRateNew::getChannel, 4);

        MerchantRateNewRecord one = rateNewRecordService.getOne(queryWrapper);
        if (one == null) {
            throw new MsgException("没有找到费率修改记录");
        }

        MerchantRateNew current = getOne(queryWrapper2);
        if (current == null) {
            current = new MerchantRateNew();
            current.setChannel(4);
            current.setMerchantId(merchantId);
        }
        one.setStatus("1");
        rateNewRecordService.updateById(one);

        current.setAliRate(one.getAliRate());
        current.setWxRate(one.getWxRate());
        current.setBankRate(one.getBankRate());
        current.setBankRateLarge(one.getBankRateLarge());
        saveOrUpdate(current);

        // 获取用户id
        LambdaQueryWrapper<MerchantChannelStatus> qWrapper2 = Wrappers.lambdaQuery();
        qWrapper2.eq(MerchantChannelStatus::getMerchantId, merchantId)
                .eq(MerchantChannelStatus::getChannel, 4);
        MerchantChannelStatus mcs = mcsService.getOne(qWrapper2);

        List<String> aliasList = new ArrayList<>();

        LambdaQueryWrapper<UserApp> qWrapper3 = Wrappers.lambdaQuery();
        qWrapper3.eq(UserApp::getMerchantCode, mcs.getMerchantCode());
        qWrapper3.eq(UserApp::getUserType, "promoter");
        UserApp ua = userAppService.getOne(qWrapper3);
        aliasList.add(ua.getUserId() + "");

        LambdaQueryWrapper<MerchantBaseInfo> qWrapper6 = Wrappers.lambdaQuery();
        qWrapper6.eq(MerchantBaseInfo::getMerchantCode, mcs.getMerchantCode());
        MerchantBaseInfo mbi = mbiService.getOne(qWrapper6);

        // 获取上级用户id
        UserApp uaParent = null;
        if (ua.getParentId() != null) {
            LambdaQueryWrapper<UserApp> qWrapper4 = Wrappers.lambdaQuery();
            qWrapper4.eq(UserApp::getUserId, ua.getParentId());
            uaParent = userAppService.getOne(qWrapper4);
            aliasList.add(uaParent.getUserId() + "");
        }

        // 获取通知模板
        NoticeTemplate template = nTempService.getNoticeTemplateByType("14");
        String templateContent;
        if (template == null) {
            templateContent = "尊敬的商户“$1”，您的交易费率调整审核已通过，费率修改记录在个人页面【商户认证】-【商户费率】-右上角【变更记录】中查看，如有疑问请咨询您的业务员。";
        } else {
            templateContent = template.getContent();
        }
        templateContent = templateContent.replace("$1", mbi.getAlias());

        List<Notice> noticeList = new ArrayList<>();

        Notice notice = new Notice(1, -1, ua.getUserId() + "", ua.getUserName());
        notice.setConrtent(templateContent);
        notice.setDesc(templateContent);
        notice.setTitle("费率修改通知");
        noticeList.add(notice);

        if (uaParent != null) {
            Notice notice2 = new Notice(1, -1, uaParent.getUserId() + "", uaParent.getUserName());
            notice2.setConrtent(templateContent);
            notice2.setDesc(templateContent);
            notice2.setTitle("费率修改通知");
            noticeList.add(notice2);
        }

        noticeService.saveBatch(noticeList);

        generalPushUtil.sendAllPlatByAlias(aliasList, "费率修改通知", templateContent, "1");
    }

    @Override
    public void passD0(List<String> merchantIdList) {
        for (String merchantId : merchantIdList) {
            self.passD0(merchantId);
        }
    }

    @Override
    public BigDecimal getMchRate(String merchantCode,String payWay,Double orderAmt) {
        MerchantChannelStatus validData = mcsService.getValidData(merchantCode);
        MerchantRateNew result = getByMerchantCode(merchantCode, validData.getChannel() + "");
        if (result == null) {
            switch (validData.getChannel()) {
                case 1:
                    result = sxfChangeRateNewService.getDefaultRate();
                    break;
                case 3:
                    result = ryxChangeRateNewService.getDefaultRate();
                    break;
                case 4:
                    result = ysChangeRateService.getDefaultRate();
                    break;
                default:
                    MsgException.throwException("通道异常！");
            }
        }
        MerchantRateNew.setRateValue(result);
        if(Constant.PAY_TYPE_ALIPAY.equals(payWay) || "sxfPay0".equals(payWay)){
            return result.getAliRateValue();
        }else if(Constant.PAY_TYPE_WECHAT.equals(payWay) || "sxfPay1".equals(payWay)){
            return result.getWxRateValue();
        }else if(Constant.PAY_TYPE_YSFPAY.equals(payWay) || "sxfPay2".equals(payWay)){
            if(orderAmt > BANK_RATE_CRITICAL_VALUE){
                return result.getBankRateLargeValue().compareTo(new BigDecimal(-1)) == 0 ? new BigDecimal(60) : result.getBankRateLargeValue();
            }else{
                return result.getBankRateValue();
            }
        }else{
            MsgException.throwException("费率获取失败，未知交易类型");
        }
        return null;
    }

    public static final double BANK_RATE_CRITICAL_VALUE = 1000.0d;


    @Override
    public Map<String, Object> apiGetRate(String userId) {
        LambdaQueryWrapper<UserApp> qWrapperMerch = new LambdaQueryWrapper<UserApp>().eq(UserApp::getUserId, userId);
        UserApp merchUserApp = userAppService.getOne(qWrapperMerch);

        HashMap<String, Object> result = new HashMap<>();

        UserApp userApp = userAppService.queryUserAppByToken();

        boolean isParentOrSelf = Objects.equals(userId, userApp.getUserId() + "");
        boolean isSelf = isParentOrSelf;
        if (!isParentOrSelf) {
            isParentOrSelf = Objects.equals(merchUserApp.getParentId(), userApp.getUserId());
        }
        if (!isParentOrSelf) {
            // 查间推
            LambdaQueryWrapper<UserApp> qWrapperMerch2 = new LambdaQueryWrapper<UserApp>().eq(UserApp::getUserId, merchUserApp.getParentId());
            UserApp merchParentUserApp = userAppService.getOne(qWrapperMerch2);
            if (merchParentUserApp != null) {
                isParentOrSelf = Objects.equals(merchParentUserApp.getParentId(), userApp.getUserId());

                if (!isParentOrSelf) {
                    // 查二间
                    LambdaQueryWrapper<UserApp> qWrapperMerch3 = new LambdaQueryWrapper<UserApp>().eq(UserApp::getUserId, merchParentUserApp.getParentId());
                    UserApp merchGrantParentUserApp = userAppService.getOne(qWrapperMerch3);

                    if (merchGrantParentUserApp != null) {
                        isParentOrSelf = Objects.equals(merchGrantParentUserApp.getParentId(), userApp.getUserId());
                    }
                }
            }
        }

        // 只有自己或这上级才能查看商户的费率
        if (!isParentOrSelf) {
            throw new MsgException("拒绝访问");
        }

        if (StringUtils.isEmpty(merchUserApp.getMerchantCode())) {
            throw new MsgException("非商户没有商户费率信息");
        }

        MerchantBaseInfo mbiMerchant = mbiService.getMerchantBaseInfoByMerchantCode(merchUserApp.getMerchantCode());

        LambdaQueryWrapper<MerchantChannelStatus> qWrapper = new LambdaQueryWrapper<>();
        qWrapper.in(MerchantChannelStatus::getStatus, MerchantChannelStatus.AUDIT_STATUS_SUCCESS, MerchantChannelStatus.AUDIT_STATUS_DATA_EDIT);
        qWrapper.eq(MerchantChannelStatus::getMerchantCode, merchUserApp.getMerchantCode());
        List<MerchantChannelStatus> mcsList = mcsService.list(qWrapper);

        for (MerchantChannelStatus mcs : mcsList) {
            if (!MerchantChannelStatus.isAudited(mcs)) {
                continue;
            }

            MerchantRateNewVO mrnVO = getChannelRate(mcs.getChannel() + "", merchUserApp.getMerchantCode());
            if (isSelf && Objects.equals(mbiMerchant.getAllowBankLarge(), "0")) {
                mrnVO.setBankRateLarge(-1);
            }
            switch (mcs.getChannel()) {
                case 1:
                case 3:
                    result.put("d1", mrnVO);
                    break;
                case 4:
                    result.put("d0", mrnVO);
                    break;
                default:
                    throw new MsgException("未知的进件通道");
            }
        }

        MerchantRateNewVO emptyMrnVO = null;
        if (result.get("d1") == null) {
            emptyMrnVO = new MerchantRateNewVO();
            emptyMrnVO.setWxRate(-1);
            emptyMrnVO.setAliRate(-1);
            emptyMrnVO.setBankRate(-1);
            emptyMrnVO.setBankRateLarge(-1);
            emptyMrnVO.setStatus("-1");
            result.put("d1", emptyMrnVO);
        }

        if (result.get("d0") == null) {
            if (emptyMrnVO == null) {
                emptyMrnVO = new MerchantRateNewVO();
                emptyMrnVO.setWxRate(-1);
                emptyMrnVO.setAliRate(-1);
                emptyMrnVO.setBankRate(-1);
                emptyMrnVO.setBankRateLarge(-1);
                emptyMrnVO.setStatus("-1");
            }
            result.put("d0", emptyMrnVO);
        }

        result.put("allowBankLarge", mbiMerchant.getAllowBankLarge());

        return result;
    }

    @Override
    public Page<Map<String, Object>> pageData(Page<Object> page, Map<String, Object> condition) {
        return baseMapper.pageData(page, condition);
    }


    @Override
    public void apiChangeMerchantRateNew(MerchantRateNewDTO param) {
        // =========修改费率校验
        MsgException.checkNull(param.getUserId(), "缺少用户id");
        // 校验超级服务商
        UserApp userAppToken = userAppService.queryUserAppByToken();

        MsgException.checkEquals("staff", userAppToken.getUserType(), "拒绝访问");
        MsgException.checkEquals("agent_staff", userAppToken.getUserType(), "拒绝访问");
        MsgException.checkEquals("ZY_BST", userAppToken.getRoleCode(), "活动商户不允许修改费率");

        String levelCodeNum = userAppToken.getLevelCode().replace("LV", "");
        if (Integer.parseInt(levelCodeNum) < 4) {
            throw new MsgException("暂无权限");
        }

        // 一天只能修改一次
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        
        LambdaQueryWrapper<UserApp> qWrapper = new LambdaQueryWrapper<UserApp>()
                .eq(UserApp::getUserId, param.getUserId());
        UserApp operateUserApp = userAppService.getOne(qWrapper);

        // 校验当前服务商是否为操作的商户的直接上级
        MsgException.checkUnequals(operateUserApp.getParentId(), userAppToken.getUserId(), "拒绝访问");

        MerchantRateNew minSettingRateNew = agentInfoService.getMinSettingRateNew();

        LambdaQueryWrapper<MerchantChannelStatus> mcsWrapper = new LambdaQueryWrapper<MerchantChannelStatus>()
                .eq(MerchantChannelStatus::getMerchantCode, operateUserApp.getMerchantCode())
                .in(MerchantChannelStatus::getStatus, "3", MerchantChannelStatus.AUDIT_STATUS_DATA_EDIT);
        List<MerchantChannelStatus> mcsList = mcsService.list(mcsWrapper);

        Map<String, MerchantRateNew> rateNewMap = MerchantRateNewDTO.toMerchantRateNew(param);

        MerchantChannelStatus d1 = null;
        MerchantChannelStatus d0 = null;
        for (MerchantChannelStatus mcs : mcsList) {
            switch (mcs.getChannel()) {
                case 1:
                case 3:
                    MerchantRateNew rateNewD1 = rateNewMap.get("d1");
                    if (rateNewD1 == null) {
                        break;
                    }
                    MerchantRateNew.setRateValue(rateNewD1);
                    CheckUtils.limit(rateNewD1.getWxRate(), minSettingRateNew.getWxRate(), MerchantRateNew.MAX_LEVEL,
                            "微信费率必须设置在" + minSettingRateNew.getWxRateValue().divide(BigDecimal.valueOf(100)) + "到0.38之间");
                    CheckUtils.limit(rateNewD1.getAliRate(), minSettingRateNew.getAliRate(), MerchantRateNew.MAX_LEVEL,
                            "支付宝费率必须设置在" + minSettingRateNew.getAliRateValue().divide(BigDecimal.valueOf(100)) + "到0.38之间");
                    CheckUtils.limit(rateNewD1.getBankRate(), minSettingRateNew.getBankRate(), MerchantRateNew.MAX_LEVEL,
                            "银联≤1000费率必须设置在" + minSettingRateNew.getBankRateValue().divide(BigDecimal.valueOf(100)) + "到0.40之间");
                    d1 = mcs;
                    break;
                case 4:
                    MerchantRateNew rateNewD0 = rateNewMap.get("d0");
                    if (rateNewD0 == null) {
                        break;
                    }
                    MerchantRateNew.setRateValue(rateNewD0);
                    CheckUtils.limit(rateNewD0.getWxRate(), minSettingRateNew.getWxRate(), MerchantRateNew.MAX_LEVEL,
                            "微信费率必须设置在" + minSettingRateNew.getWxRateValue().divide(BigDecimal.valueOf(100)) + "到0.38之间");
                    CheckUtils.limit(rateNewD0.getAliRate(), minSettingRateNew.getAliRate(), MerchantRateNew.MAX_LEVEL,
                            "支付宝费率必须设置在" + minSettingRateNew.getAliRateValue().divide(BigDecimal.valueOf(100)) + "到0.38之间");
                    CheckUtils.limit(rateNewD0.getBankRate(), minSettingRateNew.getBankRate(), MerchantRateNew.MAX_LEVEL,
                            "银联≤1000费率必须设置在" + minSettingRateNew.getBankRateValue().divide(BigDecimal.valueOf(100)) + "到0.40之间");
                    CheckUtils.limit(rateNewD0.getBankRateLarge(), minSettingRateNew.getBankRateLarge(), MerchantRateNew.MAX_LEVEL,
                            "微信>1000费率必须设置在" + minSettingRateNew.getBankRateLargeValue().divide(BigDecimal.valueOf(100)) + "到0.615之间");
                    d0 = mcs;
                    break;
                default:
                    break;
            }
        }

        // =========修改费率校验
        changeMerchantRateNew(param, d1, d0);
    }

    @Override
    public void changeMerchantRateNew(MerchantRateNewDTO param, MerchantChannelStatus d1, MerchantChannelStatus d0) {
        StringBuilder result = new StringBuilder();

        LambdaQueryWrapper<UserApp> qWrapper = new LambdaQueryWrapper<UserApp>()
                .eq(UserApp::getUserId, param.getUserId());
        UserApp operateUserApp = userAppService.getOne(qWrapper);

        // 参数转换，将费率值转换为费率等级
        Map<String, MerchantRateNew> rateNewMap = MerchantRateNewDTO.toMerchantRateNew(param);

        MerchantRateNew rateNewD1 = rateNewMap.get("d1");
        MerchantRateNew rateNewD0 = rateNewMap.get("d0");

        MchChannelRateNewService d1Service;

        if (d1 != null && rateNewD1 != null) {
            rateNewD1.setMerchantId(d1.getMerchantId());
            rateNewD1.setChannel(d1.getChannel());
            if (d1.getChannel() == 1) {
                d1Service = sxfChangeRateNewService;
            } else {
                d1Service = ryxChangeRateNewService;
            }

            try {
                d1Service.changeRate(operateUserApp.getUserId() + "", rateNewD1, true);
            } catch (RateNoChangeException e) {
                log.error(e.getMessage());
            } catch (MsgException e) {
                log.error(e.getMessage());
                if (e.getCode().equals(ResultCode.SERVICE_UNAVAILABLE)) {
                    throw new MsgException(e.getMessage());
                } else {
                    result.append("D1修改费率失败,").append(e.getMessage());
                }
            } catch (Exception e) {
                log.error(e.getMessage());
                result.append("D1修改费率失败,").append(e.getMessage());
            }
        }

        if (d0 != null && rateNewD0 != null) {
            rateNewD0.setMerchantId(d0.getMerchantId());
            rateNewD0.setChannel(d0.getChannel());
            try {
                if (StringUtils.isEmpty(d0.getApplicationId())) {
                    ysChangeRateNewService.changeRate(operateUserApp.getUserId() + "", rateNewD0, true);
                } else {
                    ysChangeRateService.changeRate(operateUserApp.getUserId() + "", rateNewD0, true);
                }
            } catch (RateNoChangeException e) {
                log.error(e.getMessage());
            } catch (MsgException e) {
                log.error(e.getMessage());
                if (e.getCode().equals(ResultCode.SERVICE_UNAVAILABLE)) {
                    throw new MsgException(e.getMessage());
                } else {
                    result.append("D0修改费率失败,").append(e.getMessage());
                }
            } catch (Exception e) {
                log.error(e.getMessage());
                result.append("D0修改费率失败, ").append(e.getMessage());
            }
        }

        if (result.length() > 0) {
            throw new MsgException(ResultCode.SUCCESS, result.toString(), null);
        }
    }

    @Override
    public MerchantRateNewVO getChannelRate(String channel, String merchantCode) {

        MerchantRateNew currentRate = getByMerchantCode(merchantCode, channel);
        if (currentRate == null) {
            MerchantRateNewVO defResult = new MerchantRateNewVO();
            defResult.setStatus("1");
            defResult.setWxRateValue(BigDecimal.valueOf(38).setScale(2, RoundingMode.HALF_UP));
            defResult.setAliRateValue(BigDecimal.valueOf(38).setScale(2, RoundingMode.HALF_UP));
            switch (channel) {
                case "1":
                case "3":
                case "4":
                    defResult.setBankRateValue(BigDecimal.valueOf(40).setScale(2, RoundingMode.HALF_UP));
                    defResult.setBankRateLargeValue(BigDecimal.valueOf(61.5).setScale(1, RoundingMode.HALF_UP));
                    break;
                default:
                    throw new MsgException("未知的进件通道");
            }

            currentRate = defResult;
        } else {
            MerchantRateNewVO.setRateValue(currentRate);
        }
        MerchantRateNewVO result = Converter.INSTANCE.toVO(currentRate);
        MerchantRateNewRecord rateNewRecord = rateNewRecordService.getExaminedDataByMerchantCode(merchantCode, channel);

        if (rateNewRecord != null) {
            MerchantRateNew record = Converter.INSTANCE.trans(rateNewRecord);
            MerchantRateNewVO.setRateValue(record);

            result.setAliRateNewValue(record.getAliRateValue());
            result.setWxRateNewValue(record.getWxRateValue());
            result.setBankRateNewValue(record.getBankRateValue());
            result.setBankRateLargeNewValue(record.getBankRateLargeValue());
            result.setStatus("0");
        } else {
            result.setStatus("1");
        }

        return result;
    }

    @Override
    public MerchantRateNew getByUserId(String userId, String channel) {
        return rateNewMapper.selectByUserId(userId, channel);
    }

    @Override
    public MerchantRateNew getByMerchantCode(String merchantCode, String channel) {
        return rateNewMapper.selectByMerchantCode(merchantCode, channel);
    }

    @Override
    public double getRate(String userId, String payWay,Double orderAmt) {
        List<MerchantRate> myRateList = merchantRateMapper.selectList(new QueryWrapper<MerchantRate>().eq("userId", userId));
        if (myRateList.isEmpty()) {
           return 0;
        }
        String rateLevel = myRateList.get(0).getCurrentRate().toString();
        if(Constant.PAY_TYPE_WECHAT.equals(payWay) || "sxfPay1".equals(payWay)){
            payWay = WX_RATE;
        }else if(Constant.PAY_TYPE_ALIPAY.equals(payWay) || "sxfPay0".equals(payWay)){
            payWay = ALI_RATE;
        }else if(Constant.PAY_TYPE_YSFPAY.equals(payWay) || "sxfPay2".equals(payWay)){
            if(orderAmt > BANK_RATE_CRITICAL_VALUE){
                payWay = BANK_RATE_LARGE;
            }else{
                payWay = BANK_RATE;
            }
        }
        Dict dict = dictMapper.selectByNameAndPCode(payWay, rateLevel);
        if (dict == null) {
            throw new MsgException("未查询到费率信息");
        }

        return Double.parseDouble(dict.getValue());
    }

    @Override
    public double getWxRate(String rateLevel,Double orderAmt) {
        return getRate(rateLevel, WX_RATE, orderAmt);
    }

    @Override
    public double getAliRate(String rateLevel,Double orderAmt) {
        return getRate(rateLevel, ALI_RATE,orderAmt);
    }

    @Override
    public double getBankRate(String rateLevel,Double orderAmt) {
        return getRate(rateLevel, BANK_RATE,orderAmt);
    }

    @Override
    public double getBankRateLarge(String rateLevel,Double orderAmt) {
        return getRate(rateLevel, BANK_RATE_LARGE,orderAmt);
    }

    /**
     * @description:根据订单以及费率等级获取商户费率
     * @date: 2021/8/19 15:32
     * @param payWay: 支付方式
     * @param orderAmt:订单金额
     * @param userId:用户id
     * @return java.math.BigDecimal
     */
//    @Override
//    public BigDecimal getMercOrderRate(String payWay, Double orderAmt,String userId) {
//        BigDecimal rateLevel = DEFAULT_RATE_LEVEL;
//        Double rate = null;
//        List<MerchantRate> myRateList = merchantRateMapper.selectList(new QueryWrapper<MerchantRate>().eq("userId", userId));
//        if (!myRateList.isEmpty()) {
//            rateLevel = new BigDecimal(myRateList.get(0).getCurrentRate());
//        }
//        if(Constant.PAY_TYPE_WECHAT.equals(payWay) || "sxfPay1".equals(payWay)){
//            rate = getWxRate(rateLevel.toString());
//        }
//        if(Constant.PAY_TYPE_ALIPAY.equals(payWay) || "sxfPay0".equals(payWay)){
//            rate = getAliRate(rateLevel.toString());
//        }
//        if(Constant.PAY_TYPE_YSFPAY.equals(payWay) || "sxfPay2".equals(payWay)){
//            if(orderAmt > BANK_RATE_CRITICAL_VALUE){
//                rate = getBankRateLarge(rateLevel.toString());
//            }else{
//                rate = getBankRate(rateLevel.toString());
//            }
//        }
//        if(rate == null){
//            MsgException.throwException("费率获取失败！");
//        }
//        return new BigDecimal(String.valueOf(rate));
//    }
}
