package com.brilliance.isc.common.component;

import com.brilliance.isc.bo.Bpd;
import com.brilliance.isc.bo.Bpt;
import com.brilliance.isc.bo.model.Bpdgrp;
import com.brilliance.isc.common.cbsmod.service.CbsmodService;
import com.brilliance.isc.common.exception.CommonServiceException;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.BpdMapper;
import com.brilliance.isc.mda.dao.BptMapper;
import com.brilliance.mda.runtime.mda.util.Dates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class BpdToolComponent {

    @Autowired
    private BpdMapper bpdMapper;

    @Autowired
    private BptMapper bptMapper;

    @Autowired
    private CbsmodService cbsmodService;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    public Bpdgrp getBpdgrpByInr(String inr) {
        Bpd bpd = bpdMapper.getBpdByInr(inr);
        if (bpd == null) {
            throw new CommonServiceException("主键"+inr+"找不到BPD数据");
        }
        Bpdgrp bpdgrp = new Bpdgrp();
        bpdgrp.reset();
        bpdgrp.setRec(bpd);
        Bpt bpt = new Bpt();
        bpt.setInr(inr);
        bpt = bptMapper.selectByPrimaryKey(bpt);
        bpdgrp.setBlk(bpt);
        //获取cbs金额信息
        Date date = Dates.add(Utils.getMaxDate(), -1);
        cbsmodService.cbsGetBalance(bpdgrp.getCbs().getMax(), "BPD", inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(bpdgrp.getCbs().getOpn1(),"BPD", inr, "OPN", "AMT1", date);
        cbsmodService.cbsGetBalance(bpdgrp.getCbs().getNom1(),"BPD", inr, "NOMSUM", "AMT1", date);
        //获取ptspta信息
        storePtsToGrp(bpdgrp, inr, "BPD");
        return bpdgrp;
    }

    public void storePtsToGrp(Object grpObject, String objInr, String objType) {
        //找ptspta数据
        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(objInr, objType);
        if (maps.size() > 0) {
            maps.keySet().forEach(key -> {
                PtsptaVo ptsptaVo = maps.get(key);
                if (key.equalsIgnoreCase("INT")) {
                    key = "tmpint";
                }
                Utils.setFieldValue(grpObject, key.toLowerCase(), ptsptaVo);
            });
        }
    }
}
