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


import com.brilliance.isc.bo.Cbb;
import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.register.fee.IFeeRegister;
import com.brilliance.isc.common.setglg.service.SetglgService;
import com.brilliance.isc.common.setmod.service.SetmodService;
import com.brilliance.isc.common.setmod.vo.PtsFeeDetailVo;
import com.brilliance.isc.common.setmod.vo.SetmodVo;
import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("setmod.feeRegister")
public class SetmodFeeRegister implements IFeeRegister {

    @Autowired
    private SetmodService setmodService;

    @Autowired
    private SetglgService setglgService;

    @Autowired
    private XrtmodService xrtmodService;

    @Override
    public void registerSettlement(SetmodVo setmodVo) {
        //! installation specific settlement defaulting
        //! Here: Set items that may be changed in transaction
        //! static items, that are constant in transation should be set in inittransaction order 1710 for performance reasons and to avoid changes again and again for items with special settings in SETxxD or transaction.
//			String path = MdaDriver.getModuleInfo(this.getRecgrp(),tdModuleInfoFullName);
        setmodVo.setRef(setmodVo.getRec().getOwnref());
        if (!setmodVo.isModified("refdat") && setmodVo.getRefdat() == null) {
            setmodVo.setRefdat(Dates.today());
        }
        Cbb cbb = setmodVo.getCbsMap().get("MAX");
        if (cbb != null) {
            setmodVo.setDoccur(cbb.getCur());
        }
        if (cbb == null || MdaUtils.isEmpty(setmodVo.getDoccur())) {
            cbb = setmodVo.getCbsMap().get("NOM1");
            if (cbb != null) {
                setmodVo.setDoccur(cbb.getCur());
            }
        }
        setmodVo.setOpndat(setmodVo.getRec().getOpndat());
        setmodVo.setExpdat(setmodVo.getRec().getExpdat());
        // expdat is not in all business sectors available:
        // support of GLGGRPFLG in PTS
        int gridCnt = setmodVo.getPtydet().size();
        for (int i = 0; i < gridCnt; i++) {
            PtsFeeDetailVo ptyDet = setmodVo.getPtydet().get(i);
            if (!MdaUtils.isEmpty(ptyDet.getRol())) {
                if (MdaUtils.compareTo(ptyDet.getRol(), setmodVo.getFeeclirol()) == 0) {
                    setmodService.setDftFeecur(ptyDet.getRol(), xrtmodService.sysiso(), setmodVo);
                }
            }
        }
        // if contract will be closed, fees for all parties will be settled now and dispositioncode is disable
        //setRolDspMdyFlg(setmodVo, "");
        setmodVo.getSetfeg().setRoldspmdyflg("");
        //国内证相关交易国外收费不可见
        String objtyp = setmodVo.getRec().getObjtyp();
        if ("DID|BDD|DED|BFD".indexOf(objtyp) > -1) {
            setmodVo.getSetfog().setInvisibleFlag("X");
        }
    }
}
