package com.brilliance.isc.common.api.fee;


import com.brilliance.isc.bo.Fee;
import com.brilliance.isc.bo.Pty;
import com.brilliance.isc.common.api.fee.service.FeeDetailApiService;
import com.brilliance.isc.common.api.fee.vo.FeeDetailRequestVo;
import com.brilliance.isc.common.api.fee.vo.FeeDetailResponseVo;
import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.exception.CommonServiceException;
import com.brilliance.isc.common.fecism.calcfee.helper.CalcFeeRequestBuilder;
import com.brilliance.isc.common.fecism.service.FecismService;
import com.brilliance.isc.common.fecism.vo.CalcFeeRequest;
import com.brilliance.isc.common.fecism.vo.CalcFeeResponse;
import com.brilliance.isc.common.fecism.vo.CalcFeeVo;
import com.brilliance.isc.common.fecmod.service.FecmodService;
import com.brilliance.isc.common.setfeg.vo.SetfelVo;
import com.brilliance.isc.common.setmod.service.SetmodService;
import com.brilliance.isc.common.setmod.vo.SetmodVo;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.FecVo;
import com.brilliance.isc.mda.dao.FeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class FeeDetailApiServiceImpl implements FeeDetailApiService {

    @Autowired
    private SetmodService setmodService;

    @Autowired
    private FecismService fecismService;

    @Autowired
    private FecmodService fecmodService;

    @Autowired
    private FeeMapper feeMapper;

    @Autowired
    private XrtmodService xrtmodService;

    @Override
    public FeeDetailResponseVo calcFeeDetail(FeeDetailRequestVo feeDetailRequestVo) {
        feeDetailRequestVo.reset();
        SetmodVo setmodVo = feeDetailRequestVo.getSetmodVo();
        //1.计算party信息放到setpty等
        setmodService.ptsChgAssert(setmodVo);
        //2.费用注册，根据交易码+”.feeRegister”)找交易注册类
        setmodService.internalPostFeeRegistersettlement(setmodVo);
        //试算他行收费
        setmodService.assertSetfog(setmodVo, true);
        //3.试算费用条目
        setmodService.assertSetfeg(setmodVo, true);
        //自贸区字段赋值
        setmodService.getZmqacc(setmodVo);
        //4.封装FeeDetailResponseVo
        return FeeDetailResponseVo.builder()
                .setfog(setmodVo.getSetfog())
                .setfeg(setmodVo.getSetfeg())
                .ptydet(setmodVo.getPtydet())
                .ref(setmodVo.getRef())
                .doccur(setmodVo.getDoccur())
                .docamt(setmodVo.getDocamt())
                .redamt(setmodVo.getRedamt())
                .feeclirol(setmodVo.getFeeclirol())
                .feecorrol(setmodVo.getFeecorrol())
                .refdat(setmodVo.getRefdat())
                .zmqflg(setmodVo.getZmqflg())
                .zmqacc(setmodVo.getZmqacc())
                .build();
    }

    @Override
    public CalcFeeResponse calcFeeamt(SetfelVo setfelVo) {
        CalcFeeRequest calcFeeRequest = CalcFeeRequestBuilder.build(setfelVo, setfelVo.getWrkfec());
        if (StringUtils.isNotEmpty(calcFeeRequest.getPertyp()) && !"n".equals(calcFeeRequest.getPertyp())) {
            calcFeeRequest.setDat2(null);
        }
        return fecismService.clcFecAmt(calcFeeRequest);
    }

    @Override
    public CalcFeeVo calcFeeamt(CalcFeeVo calcFeeVo) {
        String ptyinr = "";
        Pty pty = calcFeeVo.getPty();
        if (pty != null) {
            ptyinr = pty.getInr();
        }
        Fee fee = feeMapper.selectByCod(calcFeeVo.getFeecod());
        if (fee == null) {
            throw new CommonServiceException("找不到费用代码"+calcFeeVo.getFeecod()+"数据");
        }
        FecVo fecVo = fecmodService.getFec(null, calcFeeVo.getDat1(), calcFeeVo.getRelamt(), ptyinr, fee);
        if (fecVo == null) {
            throw new CommonServiceException("找不到费用{"+calcFeeVo.getFeecod()+"}计算规则条目fec");
        }
        int unt = 0;
        if ("Y".equals(calcFeeVo.getCalcFlg())) {
            fecVo.setPertyp(calcFeeVo.getPertyp());
            fecVo.setRatcal(calcFeeVo.getRatcal());
            fecVo.setUntamt(calcFeeVo.getUntamt());
            fecVo.setMinamt(calcFeeVo.getMinamt());
            fecVo.setMaxamt(calcFeeVo.getMaxamt());
            unt = calcFeeVo.getUnt();
            Date date2 = Utils.calcDate(calcFeeVo.getDat1(), unt, fecVo.getPertyp(), "");
            if (date2 != null) {
                calcFeeVo.setDat2(date2);
            }
        } else {
            unt = Utils.calcUnt(calcFeeVo.getDat1(), calcFeeVo.getDat2(), fecVo.getPertyp());
        }
        calcFeeVo.setCalrul(fecVo.getCalrul());
        calcFeeVo.setRatcal(fecVo.getRatcal());
        calcFeeVo.setRatcur(fecVo.getRatcur());
        calcFeeVo.setUntamt(fecVo.getUntamt());
        calcFeeVo.setPertyp(fecVo.getPertyp());
        calcFeeVo.setCalcbs(fecVo.getCalcbs());
        calcFeeVo.setMincur(fecVo.getMincur());
        calcFeeVo.setMinamt(fecVo.getMinamt());
        calcFeeVo.setMaxcur(fecVo.getMaxcur());
        calcFeeVo.setMaxamt(fecVo.getMaxamt());
        CalcFeeRequest calcFeeRequest = CalcFeeRequestBuilder.build(xrtmodService.basRatCur(), calcFeeVo.getFeecod(), calcFeeVo.getRelcur(), calcFeeVo.getRelamt(), calcFeeVo.getDat1(), calcFeeVo.getDat2(), unt, fecVo);
        CalcFeeResponse response = fecismService.clcFecAmt(calcFeeRequest);
        if (response != null) {
            calcFeeVo.setCalcFlg("Y");
            calcFeeVo.setUnt(response.getUnt());
            calcFeeVo.setFeecur(response.getCur());
            calcFeeVo.setFeeamt(response.getAmt());
            calcFeeVo.setInftxt(response.getInftxt());
        }
        return calcFeeVo;
    }

    @Override
    public CalcFeeVo calcFeeamtByDctrat(CalcFeeVo calcFeeVo) {
        BigDecimal dctrat = calcFeeVo.getDctrat();
        BigDecimal ogiamt = calcFeeVo.getOgiamt();
        String feecur = calcFeeVo.getFeecur();
        BigDecimal feeamt = BigDecimal.ZERO;
        if (dctrat != null && dctrat.compareTo(new BigDecimal("100")) < 0) {
            BigDecimal dctamt = ogiamt.multiply(dctrat).divide(new BigDecimal("100"));
            dctamt = xrtmodService.amtRnd(dctamt, feecur);
            feeamt = ogiamt.subtract(dctamt);
        } else {
            feeamt = ogiamt;
        }
        calcFeeVo.setFeeamt(feeamt);
        return calcFeeVo;
    }

    @Override
    public CalcFeeVo calcDctratByFeeamt(CalcFeeVo calcFeeVo) {
        BigDecimal dctrat = BigDecimal.ZERO;
        if (calcFeeVo.getOgiamt().compareTo(BigDecimal.ZERO) != 0 && calcFeeVo.getFeeamt().compareTo(calcFeeVo.getOgiamt()) < 0) {
            BigDecimal dctamt = calcFeeVo.getOgiamt().subtract(calcFeeVo.getFeeamt());
            dctrat = dctamt.divide(calcFeeVo.getOgiamt(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"));
        }
        calcFeeVo.setDctrat(dctrat);
        return calcFeeVo;
    }


}
