package com.brilliance.isc.common.component;

import com.brilliance.isc.bo.Bch;
import com.brilliance.isc.bo.Cnd;
import com.brilliance.isc.bo.Cnn;
import com.brilliance.isc.bo.Pta;
import com.brilliance.isc.common.api.bch.service.BchService;
import com.brilliance.isc.common.cache.pta.PtaCacheService;
import com.brilliance.isc.common.counter.service.CounterService;
import com.brilliance.isc.common.setgll.vo.SetgllVo;
import com.brilliance.isc.common.sysmod.SysmodService;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.BizInfoVo;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.CndMapper;
import com.brilliance.isc.mda.dao.CnnMapper;
import com.brilliance.isc.vo.HvpmodVo;
import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

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

@Component
public class HvpHelperComponent {
    @Autowired
    private CndMapper cndMapper;
    @Autowired
    private CnnMapper cnnMapper;
    @Autowired
    private CounterService counterService;
    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;
    @Autowired
    private PtaCacheService ptaCacheService;
    @Autowired
    private SysmodService sysmodService;
    @Autowired
    private BchService bchService;

    public void cndSav(HvpmodVo hvpmodVo, BizInfoVo bizInfoVo, List<PtsptaVo> ptsList, List<SetgllVo> setglls){
        //主键
//        this.getTrnmod().getCnd().setInr( MdaUtils.toString(ctx.getDaoSession().dbCounter("CND"),-8));
//        if( ctx.getErrorCode() > 0 && ctx.getErrorCode() != tdStrBase36Encoding ){
//            ctx.reraise();
//        }
        String cndinr = counterService.dbCounterFormat("CND");
        Cnd cnd = new Cnd();
        cnd.setInr(cndinr);

//        this.getTrnmod().getCnd().setObjtyp( Platform.recGetObj(this.getCpdgrp()));
        cnd.setObjtyp(bizInfoVo.getObjtyp());

//        this.getTrnmod().getCnd().setObjinr( this.getCpdgrp().getRec().getInr());
        cnd.setObjinr(bizInfoVo.getObjinr());

//        this.getTrnmod().getCnd().setOrcnam( this.getCptp().getOrcnam());
//        this.getTrnmod().getCnd().setOrcadr( this.getCptp().getOrcadr());
        PtsptaVo apl = ptsptaToolComponent.getPtsFromListByRol(ptsList, "APL");
        if (apl != null) {
            Pta pta = ptaCacheService.getPtaByInr(apl.getPtainr());
            if (pta != null) {
                cnd.setOrcnam(pta.getNam());
            }
            if (apl.getPts() != null) {
                String address = StringUtils.isEmpty(apl.getPts().getCnadr()) ? apl.getPts().getEnadr() : apl.getPts().getCnadr();
                cnd.setOrcadr(address);
            }
        }

//        this.getTrnmod().getCnd().setPyenam( this.getCptp().getPyenam());
//        this.getTrnmod().getCnd().setPyeadr( this.getCptp().getPyeadr());
        PtsptaVo ben = ptsptaToolComponent.getPtsFromListByRol(ptsList, "BEN");
        if (ben != null) {
            Pta pta = ptaCacheService.getPtaByInr(ben.getPtainr());
            if (pta != null) {
                cnd.setPyenam(pta.getNam());
            }
            if (ben.getPts() != null) {
                String address = StringUtils.isEmpty(ben.getPts().getCnadr()) ? ben.getPts().getEnadr() : ben.getPts().getCnadr();
                cnd.setPyeadr(address);
            }
        }

//        this.getTrnmod().getCnd().setPyeact( this.getCpdgrp().getRec().getPyeact());
        String pyeact = Utils.getFieldValue(bizInfoVo, "pyeact", NULLSTR);
        cnd.setPyeact(pyeact);

//        this.getTrnmod().getCnd().setCommet( this.getCptp().getCommet());
        String commet = Utils.getFieldValue(bizInfoVo, "commet", NULLSTR);
        cnd.setCommet(commet);

//        this.getTrnmod().getCnd().setFbmosi( this.getCptp().getSecmod());
        String secmod = Utils.getFieldValue(bizInfoVo, "secmod", NULLSTR);
        cnd.setFbmosi(secmod);

//        this.getTrnmod().getCnd().setOwnref( this.getCpdgrp().getRec().getOwnref());
        cnd.setOwnref(bizInfoVo.getOwnref());
//        this.getTrnmod().getCnd().setDlgdat( Dates.today());
        cnd.setDlgdat(Dates.today());

//        this.getTrnmod().getCnd().setOrcact( Strings.trim(this.getTrnmod().getDocbch().getBchref()) + "0121200000001");  if分支
//        this.getTrnmod().getCnd().setOrcact( Strings.trim(this.getTrnmod().getDocbch().getBchref()) + "0121200000001");  else分支
        String bchkey = sysmodService.getProcessBranch();
        Bch docbch = bchService.getByBranch(bchkey);
        if (docbch != null) {
            String bchNo = StringUtils.isNotEmpty(docbch.getBchref())?docbch.getBchref():docbch.getBranch();
            cnd.setOrcact(Strings.trim(bchNo) + "0121200000001");
        }

//        this.getTrnmod().getCnd().setPyebchnam( this.getCptp().getResbchnam());  if分支
//        this.getTrnmod().getCnd().setRecbchnam( this.getCptp().getResbchnam());  else分支
        String resbchnam = Utils.getFieldValue(bizInfoVo, "resbchnam", NULLSTR);

//        this.getTrnmod().getCnd().setPyebchkey( this.getCptp().getResbch()); if分支
//        this.getTrnmod().getCnd().setRecbchkey( this.getCptp().getResbch()); else分支
        String resbch = Utils.getFieldValue(bizInfoVo, "resbch", NULLSTR);

//        this.getTrnmod().getCnd().setBustyp( this.getCptp().getBustyp());  if分支
//        this.getTrnmod().getCnd().setBustyp( this.getCptp().getBustyp());  else分支
        String bustyp = Utils.getFieldValue(bizInfoVo, "bustyp", NULLSTR);
        cnd.setBustyp(bustyp);

//        String sty = this.getCpdgrp().getRec().getSwftyp();
        String sty = bizInfoVo.getSwftyp();
        if(MdaUtils.compareTo(sty,"CMT") == 0){
//            this.getTrnmod().getCnd().setExctyp( "0");
            cnd.setExctyp("0");

            // 取值已提到上面
            cnd.setPyebchnam(resbchnam);

//            this.getTrnmod().getCnd().setRecbchnam( this.getTrnmod().getCnd().getPyebchnam());
            cnd.setRecbchnam(resbchnam);

            // 取值已提到上面
            cnd.setPyebchkey(resbch);

//            this.getTrnmod().getCnd().setRecbchkey( this.getTrnmod().getCnd().getPyebchkey());
            cnd.setRecbchkey(resbch);

//            this.getTrnmod().getCnd().setUrgtyp( "2");
            cnd.setUrgtyp("2");

//            this.getTrnmod().getCnd().setBusmod( "CMT00");
            cnd.setBusmod("CMT00");
        } else {
//            this.getTrnmod().getCnd().setExctyp(NULLSTR);
            cnd.setExctyp(NULLSTR);

//            this.getTrnmod().getCnd().setOrcbchkey( this.getTrnmod().getDocbch().getBchkey());
            if (docbch != null) {
                Bch accBch = bchService.getDirectAccBch(docbch.getBranch());
                if (accBch != null) {
                    cnd.setOrcbchkey(accBch.getBranch());
                }
            }

//            this.getTrnmod().getCnd().setPyebchkey( this.getCptp().getPyebchkey());
            String pyebchkey = Utils.getFieldValue(bizInfoVo, "pyebchkey", NULLSTR);
            cnd.setPyebchkey(pyebchkey);

            // 取值已提到上面
            cnd.setRecbchnam(resbchnam);

            // 取值已提到上面
            cnd.setRecbchkey(resbch);

//            this.getTrnmod().getCnd().setAgency( this.getTrnmod().getHvpmod().getAgencydl());
            cnd.setAgency(hvpmodVo.getAgencydl());

//            this.getTrnmod().getCnd().setAgname( this.getTrnmod().getHvpmod().getAgnamedl());
            cnd.setAgname(hvpmodVo.getAgnamedl());

//            this.getTrnmod().getCnd().setUrgtyp( "URGT");
            cnd.setUrgtyp("URGT");

//            this.getTrnmod().getCnd().setBusmod( "A113");
            cnd.setBusmod("A113");
        }

//        int gridcnt = Grids.gridCount(ctx.absGet(Setmod.class).getSetglg().getSetgll());
//        for(int  n = 1;n <= gridcnt;n++){
//            if( MdaUtils.compareTo(ctx.absGet(Setmod.class).getSetglg().getSetgll().getElement(n).getDsp(),"PAC") == 0 ) {
//                this.getTrnmod().getCnd().setAmt( Numerics.abs(ctx.absGet(Setmod.class).getSetglg().getSetgll().getElement(n).getAcccpramt()));
//                this.getTrnmod().getCnd().setCur( ctx.absGet(Setmod.class).getSetglg().getSetgll().getElement(n).getAcccur());
//            }
//        }
        if (setglls != null && setglls.size() > 0) {
            for (SetgllVo setgllVo : setglls) {
                if (MdaUtils.compareTo(setgllVo.getDsp(),"PAC") == 0) {
                    cnd.setAmt(setgllVo.getAcccpramt().abs());
                    cnd.setCur(setgllVo.getAcccur());
                }
            }
        }

//        if( ! MdaUtils.isEmpty(this.getTrnmod().getHvpmod().getOribusnum())){
        if(!MdaUtils.isEmpty(hvpmodVo.getOribusnum())){
//            this.getTrnmod().getCnd().setOribusnum( this.getTrnmod().getHvpmod().getOribusnum());
            cnd.setOribusnum(hvpmodVo.getOribusnum());
        } else {
//            this.getTrnmod().getHvpmod().setOribusnum( ctx.absGet(Cpdgrp.class).getRec().getOwnref());
            hvpmodVo.setOribusnum(bizInfoVo.getOwnref());
//            this.getTrnmod().getCnd().setOribusnum( this.getCpdgrp().getRec().getOwnref());
            cnd.setOribusnum(bizInfoVo.getOwnref());
        }
//        ctx.getDaoSession().dbInsert(this.getTrnmod().getCnd());
        cndMapper.insert(cnd);

//        if( MdaUtils.compareTo(this.getCptp().getSecmod(),"1") == 0 || MdaUtils.compareTo(this.getCptp().getSecmod(),"2") == 0 ) {
        if(MdaUtils.compareTo(secmod,"1") == 0 || MdaUtils.compareTo(secmod,"2") == 0){
//            this.getTrnmod().getCnn().setInr( MdaUtils.toString(ctx.getDaoSession().dbCounter("CNN"),-8));
//            if( ctx.getErrorCode() > 0 && ctx.getErrorCode() != tdStrBase36Encoding ) {
//                ctx.reraise();
//            }
//            this.getTrnmod().getCnn().setCndinr( this.getTrnmod().getCnd().getInr());
            String cnninr = counterService.dbCounterFormat("CNN");
            Cnn cnn = new Cnn();
            cnn.setInr(cnninr);
            cnn.setCndinr(cndinr);
//            this.getTrnmod().getCnn().setLindat( Formats.format(Dates.today(),MdaUtils.getI18NString("cptopn","CT000377")));
            cnn.setLindat(Utils.fmtDate(Dates.today(), "yyyyMMdd"));
//            this.getTrnmod().getCnn().setFeecod( this.getTrnmod().getHvpmod().getFeecod());
            cnn.setFeecod(hvpmodVo.getFeecod());
//            if( MdaUtils.compareTo(this.getCptp().getSecmod(),"2") == 0){
            if(MdaUtils.compareTo(secmod,"2") == 0){
//                this.getTrnmod().getCnn().setCommet( this.getTrnmod().getHvpmod().getFecomm());
                cnn.setCommet(hvpmodVo.getFecomm());
//                this.getTrnmod().getCnn().setSkrmc2( this.getTrnmod().getHvpmod().getSkrmc2());
                cnn.setSkrmc2(hvpmodVo.getSkrmc2());
//                this.getTrnmod().getCnn().setSkrmc3( this.getTrnmod().getHvpmod().getSkrmc3());
                cnn.setSkrmc3(hvpmodVo.getSkrmc3());
//                this.getTrnmod().getCnn().setSkrdz2( this.getTrnmod().getHvpmod().getSkrdz2());
                cnn.setSkrdz2(hvpmodVo.getSkrdz2());
//                this.getTrnmod().getCnn().setSkrdz3( this.getTrnmod().getHvpmod().getSkrdz3());
                cnn.setSkrdz3(hvpmodVo.getSkrdz3());
//                this.getTrnmod().getCnn().setSkrdz4( this.getTrnmod().getHvpmod().getSkrdz4());
                cnn.setSkrdz4(hvpmodVo.getSkrdz4());
//                this.getTrnmod().getCnn().setSkrdz5( this.getTrnmod().getHvpmod().getSkrdz5());
                cnn.setSkrdz5(hvpmodVo.getSkrdz5());
//                this.getTrnmod().getCnn().setSkrzh2( this.getTrnmod().getHvpmod().getSkrzh2());
                cnn.setSkrzh2(hvpmodVo.getSkrzh2());
//                this.getTrnmod().getCnn().setFkrmc2( this.getTrnmod().getHvpmod().getFkrmc2());
                cnn.setFkrmc2(hvpmodVo.getFkrmc2());
//                this.getTrnmod().getCnn().setFkrmc3( this.getTrnmod().getHvpmod().getFkrmc3());
                cnn.setFkrmc3(hvpmodVo.getFkrmc3());
//                this.getTrnmod().getCnn().setFkrdz2( this.getTrnmod().getHvpmod().getFkrdz2());
                cnn.setFkrdz2(hvpmodVo.getFkrdz2());
//                this.getTrnmod().getCnn().setFkrdz3( this.getTrnmod().getHvpmod().getFkrdz3());
                cnn.setFkrdz3(hvpmodVo.getFkrdz3());
//                this.getTrnmod().getCnn().setFkrdz4( this.getTrnmod().getHvpmod().getFkrdz4());
                cnn.setFkrdz4(hvpmodVo.getFkrdz4());
//                this.getTrnmod().getCnn().setFkrdz5( this.getTrnmod().getHvpmod().getFkrdz5());
                cnn.setFkrdz5(hvpmodVo.getFkrdz5());
//                this.getTrnmod().getCnn().setFkrzh2( this.getTrnmod().getHvpmod().getFkrzh2());
                cnn.setFkrzh2(hvpmodVo.getFkrzh2());
//                this.getTrnmod().getCnn().setFeecom( this.getTrnmod().getHvpmod().getFeecom());
                cnn.setFeecom(hvpmodVo.getFeecom());
//                this.getTrnmod().getCnn().setFeesen( this.getTrnmod().getHvpmod().getFeesen());
                cnn.setFeesen(hvpmodVo.getFeesen());
            }
//            ctx.getDaoSession().dbInsert(this.getTrnmod().getCnn());
            cnnMapper.insert(cnn);
        }
    }

}
