package cn.pluss.platform.channel;

import cn.hutool.core.date.DateUtil;
import cn.pluss.platform.converter.Converter;
import cn.pluss.platform.entity.MerchantRateNew;
import cn.pluss.platform.entity.MerchantRateNewRecord;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.entity.UserLevel;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.exception.RateNoChangeException;
import cn.pluss.platform.mapper.MerchantRateMapper;
import cn.pluss.platform.merchant.MerchantRateNewRecordService;
import cn.pluss.platform.merchant.MerchantRateNewService;
import cn.pluss.platform.user.UserLevelService;
import cn.pluss.platform.userApp.UserAppService;
import lombok.Data;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author djh
 */
@Data
public abstract class BaseMchChannelRateNewService implements MchChannelRateNewService {

    @Autowired
    protected MerchantRateNewService rateNewService;

    @Resource
    protected MerchantRateMapper merchantRateMapper;

    @Autowired
    protected UserLevelService ulService;

    @Autowired
    protected MerchantRateNewRecordService rateNewRecordService;

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

    @Override
    public void before(String userId, MerchantRateNew rate) {

        if (userId == null) {
            throw new MsgException("用户id不能为空");
        }

        UserApp userApp = userAppService.queryUserAppByToken();
        if (userApp == null) {
            throw new MsgException("拒绝访问");
        }

        if (!"SV2".equals(userApp.getRoleCode())) {
            throw new MsgException("暂无权限");
        }

        UserLevel userLevel = ulService.getLevelInfoByRoleLevel(userApp.getRoleCode(), userApp.getLevelCode());

        MsgException.checkNull(userLevel, "数据异常");

        //费率校验
        rateValueCheck(rate.getWxRate(), userLevel.getRate().intValue());
        rateValueCheck(rate.getAliRate(), userLevel.getRate().intValue());
        rateValueCheckBank(rate.getBankRate(), userLevel.getRate().intValue());
        rateValueCheckBankLarge(rate.getBankRateLarge(), userLevel.getRate().intValue());

        UserApp optedUserApp = userAppService.getUserAppByUserId(userId);

        MerchantRateNewRecord validRecord = rateNewRecordService.getValidRecordByMerchantCode(optedUserApp.getMerchantCode(), getChannelId());
        if (validRecord != null) {
            if (validRecord.getStatus().equals(MerchantRateNewRecord.STATUS_IN_REVIEW) ||
                    validRecord.getStatus().equals(MerchantRateNewRecord.STATUS_SUCCESS_NOT_EFFECT) ) {
                MsgException.throwException("当前费率修改正在审核中");
            }

            if (DateUtil.isSameDay(validRecord.getCreateTime(), new Date())) {
                throw new MsgException("请于一天之后再修改费率");
            }
        }

        MerchantRateNew merchantRate = rateNewService.getByUserId(userId, getChannelId());

        if (merchantRate == null) {
            merchantRate = getDefaultRate();
        } else {
            rate.setId(merchantRate.getId());
        }

        boolean flagWx = rate.getWxRate().equals(merchantRate.getWxRate());
        boolean flagAli = rate.getAliRate().equals(merchantRate.getAliRate());
        boolean flagBank = rate.getBankRate().equals(merchantRate.getBankRate());
        // boolean flagBankLarge = ("1".equals(getChannelId()) || "3".equals(getChannelId()) || rate.getBankRateLarge().equals(merchantRate.getBankRateLarge()));
        boolean flagBankLarge = true;
        if (flagWx && flagAli && flagBank && flagBankLarge) {
            throw new RateNoChangeException();
        }
    }

    @Override
    public void after(String userId, MerchantRateNew merchantRate) {
        // 更新本地数据库保存的费率
//        rateNewService.saveOrUpdate(merchantRate);

        // 保存费率修改记录
        MerchantRateNewRecord merchantRateRecord = Converter.INSTANCE.trans(merchantRate);
        merchantRateRecord.setCreateTime(new Date());
        merchantRateRecord.setUpdateTime(new Date());
        rateNewRecordService.save(merchantRateRecord);
    }

    private void rateValueCheck(int rateValue, int minRate) {
        if (rateValue < minRate || rateValue > 38) {
            throw new MsgException("商户支付宝或微信费率应在0." + minRate + "%到0.38%之间");
        }
    }

    private void rateValueCheckBank(int rateValue, int minRate) {
        if (rateValue < minRate || rateValue > 38) {
            throw new MsgException("商户费率应在0." + (minRate + 2) + "%到0.40%之间");
        }
    }

    private void rateValueCheckBankLarge(int rateValue, int minRate) {
        if ("1".equals(getChannelId())) {
            return;
        }

        if (rateValue < minRate || rateValue > 38) {
            throw new MsgException("商户费率应在0." + ((minRate - 53) * 2 + 21) + "%到0.60%之间");
        }
    }
}
