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

import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.Formats;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import com.brilliance.isc.bo.Dnf;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.counter.service.CounterService;
import com.brilliance.isc.common.dnfmod.service.DnfmodService;
import com.brilliance.isc.common.sysmod.SysmodService;
import com.brilliance.isc.common.transaction.help.TransactionHelpService;
import com.brilliance.isc.common.trnmod.vo.TrnmodVo;
import com.brilliance.isc.common.vo.DnfmodVo;
import com.brilliance.isc.common.wfmmod.service.WfmmodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

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

/**
 * 保证金收取后续处理，会去与核心对接，例如定转活，销户什么的
 */
@Service
public class DnfmodServiceImpl implements DnfmodService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TransactionHelpService transactionHelpService;

    @Autowired
    private WfmmodService wfmmodService;

    @Autowired
    private SysmodService sysmodService;

    @Autowired
    private CounterService counterService;

    @Override
    public void dnfSav(DnfmodVo dnfmodVo) {
        logger.info("dnfSav获取参数dnfmodVo:{}",dnfmodVo.toString());
        Dnf dnf = new Dnf();
        //!保证金接口数据保存以及各种类型注册服务
        TrnmodVo trnmodVo = transactionHelpService.getTransactionStoreSet().getTrnmodVo();
        logger.info("dnfSav获取参数trnmodVo.getTrn:{}",trnmodVo.getTrn().toString());
        dnf.setTrninr( trnmodVo.getTrn().getInr());
        dnf.setZhangh( dnfmodVo.getKehuzh());
        dnf.setBjzyzh( dnfmodVo.getZhruzh());
        dnf.setLxzyzh( dnfmodVo.getLxzyzh());
        dnf.setTizhje( dnfmodVo.getTizamt());
        dnf.setZhhuye( dnfmodVo.getZhhuye());
        dnf.setYewudh( dnfmodVo.getYewudh());
        dnf.setQicuje( dnfmodVo.getQicuje());
        dnf.setZhqtyp( dnfmodVo.getZhqtyp());
        dnf.setZhqubz( dnfmodVo.getCur());
        String dngjbh = NULLSTR;
        if( MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(),4,3),"ENG") == 0 || MdaUtils.compareTo(Strings.mid(SettleContext.getTransName(),4,3),"ENC") == 0 ) {
            /**
             * 原意是DNF  break 再TAKE之后， 会从dnflod模型中加载，但因为break新国结下发生修改了，DNF会自动原样还原，应该不需要
             */
            //dngjbh = Datas.getContent(this,"\\DNFLOD\\DNF\\DNGJBH");
        }
        dnf.setDngjbh( dngjbh);

        String rflg = "";
        if(Strings.len(dnfmodVo.getKehuzh()) == 17) {
            rflg = Strings.mid(dnfmodVo.getKehuzh(),7,2);
        }
        if(Strings.len(dnfmodVo.getKehuzh()) == 21) {
            rflg = Strings.mid(dnfmodVo.getKehuzh(),11,2);
        }
        if(MdaUtils.compareTo(Strings.mid(dnfmodVo.getKehuzh(),1,3),"ZMQ") == 0 || MdaUtils.compareTo(Strings.mid(dnfmodVo.getKehuzh(),1,3),"NRA") == 0) {
            rflg = Strings.mid(dnfmodVo.getKehuzh(),10,2);
        }
        if(MdaUtils.compareTo(rflg,"81") == 0) {
            //1为定期 0为活期
            dnf.setRmzhlx( "1");
        } else {
            dnf.setRmzhlx( "0");
        }
        //DNF\\JDNGRQ = FmtDate( DateAdd( Today, 1 ), "YYYYMMDD" )
        dnf.setCredat(Formats.format(Dates.today(),"yyyyMMdd"));
        logger.info("dnfSav,dnf:{}",dnf.toString());
        dnf.setInr(counterService.dbCounterFormat("DNF"));
        sysmodService.sdbInsert(dnf);
        logger.info("dnfSav,dnfmodVo.getCcvtyp():{},dnfmodVo.getZhqtyp:{}",dnfmodVo.getCcvtyp(),dnfmodVo.getZhqtyp());
        //保证金帐户冻结
        if(MdaUtils.compareTo(dnfmodVo.getCcvtyp(),"A") == 0) {
            wfmmodService.wfeAdd ( "TON", "TON", Dates.add(Dates.today(),1), new BigDecimal(24) );
        }
        //保证金帐户解冻
        if(MdaUtils.compareTo(dnfmodVo.getCcvtyp(),"B") == 0) {
            wfmmodService.wfeAdd ( "BIE", "BIE", Dates.add(Dates.today(),1), new BigDecimal(24) );
        }
        //定期转活期
        if(MdaUtils.compareTo(dnfmodVo.getCcvtyp(),"C") == 0) {
            switch( dnfmodVo.getZhqtyp() ) {
                //部分提支
                case "B":
                    wfmmodService.wfeAdd ( "BIE", "BIE", Dates.add(Dates.today(),1), new BigDecimal(24) );
                    wfmmodService.wfeAdd ( "DRW", "DRW", Dates.add(Dates.today(),1), new BigDecimal(24) );
                    wfmmodService.wfeAdd ( "MSF", "MSF", Dates.add(Dates.today(),1), new BigDecimal(24) );
                    wfmmodService.wfeAdd ( "TON", "TON", Dates.add(Dates.today(),1), new BigDecimal(24) );
                    //存款销户
                    break;
                case "C":
                    wfmmodService.wfeAdd ( "BIE", "BIE", Dates.add(Dates.today(),1), new BigDecimal(24) );
                    wfmmodService.wfeAdd ( "DEP", "DEP", Dates.add(Dates.today(),1), new BigDecimal(24) );
                    wfmmodService.wfeAdd ( "MSF", "MSF", Dates.add(Dates.today(),1), new BigDecimal(24) );
                    break;
            }
        }
    }
}
