package com.brilliance.isc.common.liaget.service.impl;

import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.isc.bo.Cbb;
import com.brilliance.isc.bo.Pte;
import com.brilliance.isc.bo.Pts;
import com.brilliance.isc.common.cbsmod.service.CbsmodService;
import com.brilliance.isc.common.liaall.vo.LiagetMixInfo;
import com.brilliance.isc.common.liaget.service.LiagetService;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.SimpleModuleVo;
import com.brilliance.isc.mda.dao.PteMapper;
import com.brilliance.isc.mda.dao.PtsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import static com.brilliance.mda.runtime.mda.Constants.CR;

/**
 * @author wangguangchao
 * @create 2023-01-11 14:11
 */
@Service("liagetService")
public class LiagetServiceImpl implements LiagetService {

    @Autowired
    private PtsMapper ptsMapper;
    @Autowired
    private PteMapper pteMapper;
    @Autowired
    private CbsmodService cbsmodService;

    public BigDecimal trnmodGetTenorAmount(SimpleModuleVo linkModule, String cbtpfxlst) {
        BigDecimal res = BigDecimal.ZERO;
        if (StringUtils.isEmpty(linkModule.getObjinr())) {
            return res;
        }
        List<LiagetMixInfo> list = getTenorStreams(linkModule, cbtpfxlst);
        for (LiagetMixInfo liagetMixInfo : list) {
            res = res.add(liagetMixInfo.getTotalOldAmt());
        }
        return res;
    }

    public boolean trnmodGetTenors(SimpleModuleVo linkModule, String cbtPFXlst) {
        boolean flag = false;
        List<LiagetMixInfo> list = getTenorStreams(linkModule, cbtPFXlst);
        if (list != null && list.size() > 0) {
            flag = true;
        }
        return flag;
    }

    public List<LiagetMixInfo> getTenorStreams(SimpleModuleVo linkModule, String cbtPFXlst) {
        // internal fuction to retrieve tenor info.
        // retrieve PTS-records of contract
        Map<String, LiagetMixInfo> maps = new HashMap<>();
        List<Pts> ptsList = ptsMapper.getPtsByObjInrTyp(linkModule.getObjinr(), linkModule.getObjtyp());
        if (ptsList != null && ptsList.size() > 0) {
            ptsList.forEach(pts -> {
                String role = pts.getRol();
                List<Pte> pteList = pteMapper.getPteByObjtypAndObjinr("PTS", pts.getInr());
                if (pteList != null && pteList.size() > 0) {
                    pteList.forEach(pte -> {
                        if ((!cbtPFXlst.startsWith("-") && cbtPFXlst.contains(pte.getCbtpfx()))
                                || StringUtils.isEmpty(cbtPFXlst)
                                || (cbtPFXlst.length() >= 5 && "*ALL*".equals(cbtPFXlst.substring(0, 5)))
                                || (cbtPFXlst.startsWith("-") && !cbtPFXlst.contains(pte.getCbtpfx()))) {
                            //查找cbb数据
                            Date date = Dates.add(Utils.getMaxDate(), -1);
                            Cbb cbb = new Cbb();
                            cbsmodService.cbsGetBalance(cbb, "PTE", pte.getInr(), "LIASUM", "AMT1", date);
                            if (cbb != null && cbb.getAmt().compareTo(BigDecimal.ZERO) > 0) {
                                LiagetMixInfo liagetMixInfo = new LiagetMixInfo();
                                liagetMixInfo.setRol(role);
                                liagetMixInfo.setCur(cbb.getCur());
                                liagetMixInfo.setMatDat(pte.getMatdat());
                                liagetMixInfo.setCdtPtsInr(pte.getCdtptsinr());
                                liagetMixInfo.setCbtPfx(pte.getCbtpfx());
                                liagetMixInfo.setPteInr(pte.getInr());
                                String key = role + "-" + cbb.getCur() + "-" + pte.getMatdat() == null ? "NO" : Utils.fmtDate(pte.getMatdat(), "DD/MM/YYYY");
                                if ("*ALL*+CDTROL+".equals(cbtPFXlst)) {
                                    key += "-" + pte.getCdtptsinr();
                                } else if ("*ALL*MATCBT*".equals(cbtPFXlst)) {
                                    key += "-" + pte.getCbtpfx();
                                }
                                if (maps.containsKey(key)) {
                                    maps.get(key).setTotalOldAmt(maps.get(key).getTotalOldAmt().add(cbb.getAmt()));
                                } else {
                                    liagetMixInfo.setTotalOldAmt(cbb.getAmt());
                                    maps.put(key, liagetMixInfo);
                                }
                            }
                        }
                    });
                }
            });
        }
        List<LiagetMixInfo> list = new ArrayList<>();
        if (maps.size() > 0) {
            list.addAll(maps.values());
        }
        return list;
    }

    @Override
    public String trnmodGetUsanceCbtpfxLst() {
        return "DPU" + CR + "DPZ" + CR + "AKZ";
    }
}
