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

import com.brilliance.mda.runtime.mda.util.Decimals;
import com.brilliance.mda.runtime.mda.util.Formats;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.isc.common.api.bptbck.service.BptbckApiService;
import com.brilliance.isc.common.api.bptbck.vo.BptbckRequest;
import com.brilliance.isc.common.api.bptbck.vo.BptbckResponse;
import com.brilliance.isc.common.bptbck.service.BptbckService;
import com.brilliance.isc.common.esb.finance.IFinanceFeign;
import com.brilliance.isc.common.finmod.service.FinmodService;
import com.brilliance.isc.esb.model.inner.*;
import com.brilliance.isc.utils.DateUtils;
import com.brilliance.isc.vo.component.bptbck.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class BptbckApiServiceImpl implements BptbckApiService {

    @Autowired
    private BptbckService bptbckService;

    @Autowired
    private FinmodService finmodService;

    @Autowired(required = false)
    private IFinanceFeign financeFeign;

    @Override
    public BptbckResponse assertBptbck(BptbckRequest request) {
        request.reset();
        BptbckVo bptbckVo = request.getBptbck();
        bptbckVo.setRec(request.getRec());
        bptbckService.assertBptbck(bptbckVo);
        return BptbckResponse.builder()
                .bptbck(bptbckVo)
                .build();
    }

    @Override
    public BptbckResponse call6157(BptbckRequest request) {
        BptbckVo bptbckVo = request.getBptbck();
        String errorMsg = "";
        if(MdaUtils.compareTo(bptbckVo.getSetamt(), 0) > 0 || MdaUtils.compareTo(bptbckVo.getWrkfinmod().getReptyp(), "1") == 0) {
            errorMsg = bptbckService.call6157Data(bptbckVo);
        }
        finmodService.defaultTotintN1000(bptbckVo.getWrkfinmod());
        bptbckService.defaultSetamtN1000(bptbckVo);
        if (bptbckVo.getBptbckg() != null && bptbckVo.getBptbckg().size() > 0) {
            if(!MdaUtils.isEmpty(bptbckVo.getWrkfinmod().getCbs().getCalflg())) {
                // 解决那些Repair了点计算却不点Apply的Bug
                bptbckVo.getBptbckg().get(0).setBckamt(Decimals.sub(Decimals.add(bptbckVo.getBptbckg().get(0).getOpnamt(), bptbckVo.getWrkfinmod().getCbs().getCbe().getAmt()), bptbckVo.getWrkfinmod().getCbs().getCbb().getAmt()));
                bptbckVo.getBptbckg().get(0).setModified("bckamt");
            } else {
                bptbckVo.getBptbckg().get(0).setBckamt(new BigDecimal("0"));
            }
        }
        return BptbckResponse.builder()
                .bptbck(bptbckVo)
                .errorMsg(errorMsg)
                .build();
    }

    @Override
    public List<Bms9011ResVo> call9011(Bms9011ReqVo request) {
        InnerReq9011 innerReq9011 = new InnerReq9011();
        innerReq9011.setZHANGH(request.getBaomzh());
        innerReq9011.setQISHRQ(request.getStadat() == null ? "" : Formats.fmtDateShort(request.getStadat()));
        innerReq9011.setZZHIRQ(request.getEnddat() == null ? "" : Formats.fmtDateShort(request.getEnddat()));
        innerReq9011.setZUIDJE(request.getLowamt() == null ? "" : request.getLowamt().toPlainString());
        innerReq9011.setZUIGJE(request.getHigamt() == null ? "" : request.getHigamt().toPlainString());
        innerReq9011.setQISHBS("0");
        innerReq9011.setCXUNBS("100");
        List<Bms9011ResVo> result = new ArrayList<>();
        InnerReq<InnerReq9011> innerReq = new InnerReq<>(innerReq9011);
        try {
            InnerRes<InnerRes9011> innerRes = financeFeign.call9011(innerReq);
            if (innerRes != null && "AAAAAAA".equals(innerRes.header.code) && innerRes.DATA != null) {
                InnerRes9011 innerRes9011 = innerRes.DATA;
                List<InnerResItem9011> item9011 = innerRes9011.item9011;
                for (InnerResItem9011 innerResItem9011 : item9011) {
                    //只过滤贷记
                    if (!"1".equals(innerResItem9011.JIEDBZ)) {
                        continue;
                    }
                    String duifzh = innerResItem9011.getDUIFZH();
                    String accbch = duifzh.substring(0, 4);
                    String trmcod = duifzh.substring(6, 10);
                    if (accbch.equals(request.getAccbch()) && ("2120".equals(trmcod) || "2149".equals(trmcod))) {
                        continue;
                    }
                    Bms9011ResVo bms9011ResVo = new Bms9011ResVo();
                    bms9011ResVo.setZhangh(innerRes9011.ZHANGH);
                    bms9011ResVo.setZhuzwm(innerRes9011.ZHUZWM);
                    bms9011ResVo.setJiaoym(innerResItem9011.getJIAOYM());
                    if (!MdaUtils.isEmpty(innerResItem9011.getJIOYRQ())) {
                        bms9011ResVo.setJioyrq(DateUtils.parseDate(innerResItem9011.getJIOYRQ(), "yyyyMMdd"));
                    }
                    bms9011ResVo.setJiedbz(innerResItem9011.getJIEDBZ());
                    if (!MdaUtils.isEmpty(innerResItem9011.getJIO1JE())) {
                        bms9011ResVo.setJio1je(new BigDecimal(innerResItem9011.getJIO1JE()));
                    }
                    if (!MdaUtils.isEmpty(innerResItem9011.getZHHUYE())) {
                        bms9011ResVo.setJio1je(new BigDecimal(innerResItem9011.getZHHUYE()));
                    }
                    bms9011ResVo.setDuifzh(innerResItem9011.getDUIFZH());
                    bms9011ResVo.setZhyodm(innerResItem9011.getZHYODM());
                    bms9011ResVo.setGuiyls(innerResItem9011.getGUIYLS());
                    result.add(bms9011ResVo);
                }
            }
        } catch (Exception e) {

        }
        return result;
    }

    @Override
    public List<Gle4338Vo> calcGle4338(BptbckQueryVo bptbckQueryVo) {
        return bptbckService.calcGle4338(bptbckQueryVo);
    }
}
