package com.brilliance.isc.common.nosmod.impl;

import com.brilliance.isc.bo.Act;
import com.brilliance.isc.common.cbsmod.service.CbsmodService;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.nosmod.NosmodService;
import com.brilliance.isc.common.nosmod.vo.Nosmod;
import com.brilliance.isc.common.setgll.vo.SetgllVo;
import com.brilliance.isc.common.setmod.vo.SetmodVo;
import com.brilliance.isc.common.trnmod.vo.TrnmodVo;
import com.brilliance.isc.mda.dao.ActMapper;
import com.brilliance.isc.utils.StringUtils;
import com.brilliance.mda.runtime.mda.util.Dates;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

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

/**
 * @program: isc-core
 * @ClassName: NosmodServiceImpl
 * @description:
 * @author: huangshunlin
 * @create: 2024-10-18 20:43
 */
@Service
public class NosmodServiceImpl implements NosmodService {

    @Resource
    private CbsmodService cbsmodService;

    @Resource
    private ActMapper actMapper;


    /**
     * source:nosmod.0000.script
     */
    public void cbsNostroStore(Nosmod nosmod, String cbt, String extid, String trninr, Date dat, String actcur, BigDecimal amt, String nam, String acc, Object obj) {
        //!保存存放同业台账
        nosmod.getWrkcbe().setCbt(cbt);
        nosmod.getWrkcbe().setExtid(extid);
        nosmod.getWrkcbe().setTrntyp("TRN");
        nosmod.getWrkcbe().setTrninr(trninr);
        nosmod.getWrkcbe().setDat(dat);
        nosmod.getWrkcbe().setCur(actcur);
        nosmod.getWrkcbe().setAmt(amt);
        nosmod.getWrkcbe().setNam(nam);
        nosmod.getWrkcbe().setAcc(acc);
        cbsmodService.cbsStoreEntry(nosmod.getWrkcbe(), obj);

    }

    /**
     * source:nosmod.0001.script
     */
    public boolean isSavNostroCbs(SetmodVo setmodVo) {
        //!判断是否需要保存存放同业的台账
        return getNosRowInSetgll(setmodVo) >= 0;
    }

    /**
     * source:nosmod.0002.script
     */
    public int getNosRowInSetgll(SetmodVo setmodVo) {
        //!取存放同业分录在settlement的位置
        int row = -1;
//        int size = Grids.gridCount(MdaDriver.getModuleList("\\SETMOD\\SETGLG\\SETGLL"));
        List<SetgllVo> setgll = setmodVo.getSetglg().getSetgll();
        if (CollectionUtils.isEmpty(setgll)) {
            return row;
        }
        String dsp = NULLSTR;
        for (int i = 0, size = setgll.size(); i < size; i++) {
//            dsp = Datas.getContent(this, "\\SETMOD\\SETGLG\\SETGLL(" + MdaUtils.toString(i) + ")\\DSP");
            dsp = setgll.get(i).getDsp();
            switch (StringUtils.trimToEmpty(dsp)) {
                case "NOD":
                case "PAC":
                case "DBD":
                case "DBC":
                    row = i;
                    break;
            }
        }
        return row;

    }

    /**
     * source:nosmod.0003.script
     */
    public void savNostroCbs(Nosmod nosmod, String trninr, String actcur, BigDecimal amt, String acc, Date dat, String nam, Object obj) {
        //!保存存放同业台账
        cbsmodService.cbsTrnSetNew("TRN", trninr);
        cbsNostroStore(nosmod, "NOSAMT", "AMT1", trninr, dat, actcur, amt, nam, acc, obj);

    }

    /**
     * source:nosmod.0004.script
     */
    public void savNostro(Nosmod nosmod, SetmodVo setmodVo, TrnmodVo trnmodVo) {
        //! 保存存放同业台账
        int row = getNosRowInSetgll(setmodVo);
        String trninr = NULLSTR;
        int err = 0;
        String actcur = NULLSTR;
        BigDecimal amt = null;
        String act = NULLSTR;
        List<SetgllVo> setgll = setmodVo.getSetglg().getSetgll();
        if (row >= 0) {
//            trninr = Datas.getContent(this, "\\TRNMOD\\TRN\\INR");
            trninr = trnmodVo.getTrn().getInr();
//            actcur = Datas.getContent(this, "\\SETMOD\\SETGLG\\SETGLL(" + MdaUtils.toString(row) + ")\\CUR");
//            amt = Datas.getContentNumeric(this, "\\SETMOD\\SETGLG\\SETGLL(" + MdaUtils.toString(row) + ")\\AMT");
//            act = Datas.getContent(this, "\\SETMOD\\SETGLG\\SETGLL(" + MdaUtils.toString(row) + ")\\ACT");
//            actcur = Datas.getContent(this, "\\SETMOD\\SETGLG\\SETGLL(" + MdaUtils.toString(row) + ")\\CUR");
            amt = setgll.get(row).getAmt();
            act = setgll.get(row).getAct();
            actcur = setgll.get(row).getCur();
            List<Act> actList = actMapper.getActListByExtkey(act);
            // modify for hzbank,only save when act exist
            if (CollectionUtils.isNotEmpty(actList)) {
                nosmod.setRec(actList.get(0));
                savNostroCbs(nosmod, trninr, actcur, amt, act, Dates.today(), SettleContext.getTransName(), nosmod.getRec());
            }
        }

    }
}
