package cn.pluss.platform.dto;


import cn.pluss.platform.entity.MerchantRateNew;
import cn.pluss.platform.exception.MsgException;
import lombok.Data;
import lombok.val;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Data
public class MerchantRateNewDTO {

    /**
     * 用户id
     */
    private String userId;

    /**
     * 微信D0费率值
     */
    private BigDecimal wxRateD0;


    /**
     * 支付宝D0费率
     */
    private BigDecimal aliRateD0;


    /**
     * 银联D0费率
     */
    private BigDecimal bankRateD0;


    /**
     * 银联大额D0费率，>1000
     */
    private BigDecimal bankRateLargeD0;


    /**
     * 微信D1费率
     */
    private BigDecimal wxRateD1;


    /**
     * 支付宝D1费率
     */
    private BigDecimal aliRateD1;


    /**
     * 银联D1费率
     */
    private BigDecimal bankRateD1;


    /**
     * 银联大额D1费率
     */
    private BigDecimal bankRateLargeD1;



    public static Map<String, MerchantRateNew> toMerchantRateNew(MerchantRateNewDTO rateNewDTO) {
        Map<String, MerchantRateNew> result = new HashMap<>();

        boolean wxRateD0Exist = rateNewDTO.getWxRateD0() == null;
        boolean aliRateD0Exist = rateNewDTO.getAliRateD0() == null;
        boolean bankRateD0Exist = rateNewDTO.getBankRateD0() == null;
        boolean bankRateLargeD0Exist = rateNewDTO.getBankRateLargeD0() == null;

        if (wxRateD0Exist ^ aliRateD0Exist ^ bankRateD0Exist ^ bankRateLargeD0Exist) {
            throw new MsgException("D0费率参数不全");
        }

        if (!wxRateD0Exist) {
            MerchantRateNew d0 = new MerchantRateNew();
            d0.setWxRate(rateNewDTO.getWxRateD0().multiply(BigDecimal.valueOf(100)).intValue());
            d0.setAliRate(rateNewDTO.getAliRateD0().multiply(BigDecimal.valueOf(100)).intValue());
            d0.setBankRate(rateNewDTO.getBankRateD0().multiply(BigDecimal.valueOf(100)).intValue() - 2);
            d0.setBankRateLarge(rateNewDTO.getBankRateLargeD0().subtract(BigDecimal.valueOf(0.53d)).divide(BigDecimal.valueOf(0.005d), RoundingMode.HALF_UP).intValue() + 21);

            result.put("d0", d0);
        }

        boolean wxRateD1Exist = rateNewDTO.getWxRateD1() == null;
        boolean aliRateD1Exist = rateNewDTO.getAliRateD1() == null;
        boolean bankRateD1Exist = rateNewDTO.getBankRateD1() == null;
        boolean bankRateLargeD1Exist = rateNewDTO.getBankRateLargeD1() == null;

        if (wxRateD1Exist ^ aliRateD1Exist ^ bankRateD1Exist ^ bankRateLargeD1Exist) {
            throw new MsgException("D1费率参数不全");
        }

        if (!wxRateD1Exist) {
            MerchantRateNew d1 = new MerchantRateNew();
            d1.setWxRate(rateNewDTO.getWxRateD1().multiply(BigDecimal.valueOf(100)).intValue());
            d1.setAliRate(rateNewDTO.getAliRateD1().multiply(BigDecimal.valueOf(100)).intValue());
            d1.setBankRate(rateNewDTO.getBankRateD1().multiply(BigDecimal.valueOf(100)).intValue() - 2);
            if (rateNewDTO.getBankRateLargeD1() == null || Objects.equals(rateNewDTO.getBankRateLargeD1(), BigDecimal.ZERO)) {
                d1.setBankRateLarge(-1);
            } else {
                d1.setBankRateLarge(rateNewDTO.getBankRateLargeD1().subtract(BigDecimal.valueOf(0.53d)).divide(BigDecimal.valueOf(0.005d), RoundingMode.HALF_UP).intValue() + 21);
            }

            result.put("d1", d1);
        }

        return result;
    }
}
