package com.brilliance.isc.common.register.trtcre.impl;

import com.brilliance.isc.bo.Cbb;
import com.brilliance.isc.bo.Trn;
import com.brilliance.isc.bo.model.Trtcre;
import com.brilliance.isc.common.component.DyncDaoService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.register.trtcre.ITrtcreRegister;
import com.brilliance.isc.vo.model.TrtcreBizInfoVo;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("bcd.trtcreRegister")
public class BcdTrtcreRegister implements ITrtcreRegister {

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    @Autowired
    private DyncDaoService dyncDaoService;

    @Override
    public void setTRDfieldsfromXXX(Trtcre trtcreVo) {
        TrtcreBizInfoVo bizInfoVo = trtcreVo.getRec();
        trtcreVo.getTrdgrp().getRec().setPnttyp(bizInfoVo.getObjtyp());
        trtcreVo.getTrdgrp().getRec().setPntinr(bizInfoVo.getObjinr());
        trtcreVo.getTrdgrp().getRec().setPntref(bizInfoVo.getOwnref());
        trtcreVo.getTrdgrp().getRec().setPntnam(bizInfoVo.getNam());
        trtcreVo.getTrdgrp().getRec().setAvbby(bizInfoVo.getDoctypcod());
        trtcreVo.getTrdgrp().getRec().setTenorday(bizInfoVo.getMatpercnt());
        trtcreVo.getTrdgrp().getRec().setMatpretyp(bizInfoVo.getMatpertyp());
        if(trtcreVo.getTrdgrp().getRec().getStttendat() == null) {
            if(bizInfoVo.getAccdat() == null) {
                trtcreVo.getTrdgrp().getRec().setStttendat(bizInfoVo.getCredat());
            } else {
                trtcreVo.getTrdgrp().getRec().setStttendat(bizInfoVo.getAccdat());
            }
        }
        //中文货物描述
        String goddspcn = dyncDaoService.dyncReadFieldByInr(bizInfoVo.getObjtyp(), bizInfoVo.getObjinr(), "goddspcn");
        bizInfoVo.setGoddspcn(goddspcn);
        //货物品类
        String stagod = dyncDaoService.dyncReadFieldByInr(bizInfoVo.getObjtyp(), bizInfoVo.getObjinr(), "stagod");
        bizInfoVo.setStagod(stagod);
        //商业发票
        String invtyp = dyncDaoService.dyncReadFieldByInr(bizInfoVo.getObjtyp(), bizInfoVo.getObjinr(), "invtyp");
        bizInfoVo.setInvtyp(invtyp);
        Cbb maxcbb = trtcreVo.getCbsMap().getOrDefault("MAX", new Cbb());
        if (StringUtils.isNotEmpty(maxcbb.getCur()) && !trtcreVo.getTrdgrp().getCbs().getMax().isModified("cur")) {
            trtcreVo.getTrdgrp().getCbs().getMax().setCur(maxcbb.getCur());
        }
        String ref = trtcreVo.getTrdgrp().getFip().getRef();
        if (ptsptaToolComponent.ptsIsRolSet(trtcreVo.getPtsptaList(), "CO2")) {
            trtcreVo.getTrdgrp().setFip(MdaUtils.deepClone(ptsptaToolComponent.getPtsFromListByRol(trtcreVo.getPtsptaList(), "CO2")));
        } else if (ptsptaToolComponent.ptsIsRolSet(trtcreVo.getPtsptaList(), "DRE")) {
            trtcreVo.getTrdgrp().setFip(MdaUtils.deepClone(ptsptaToolComponent.getPtsFromListByRol(trtcreVo.getPtsptaList(), "DRE")));
        }
        trtcreVo.getTrdgrp().getFip().setRol("FIP");
        trtcreVo.getTrdgrp().getFip().getPts().setRol("FIP");
        if (StringUtils.isNotEmpty(ref)) {
            //trtcreVo.getTrdgrp().getFip().setRef(ref);
            trtcreVo.getTrdgrp().getFip().getPts().setRef(ref);
        }
        //fiprol赋值
        String transName = SettleContext.getTransName();
        if ("BCTPAY".equals(transName)) {
            if (ptsptaToolComponent.ptsIsRolSet(trtcreVo.getPtsptaList(), "CO2")) {
                trtcreVo.setFiprol("CO2");
            } else {
                trtcreVo.setFiprol("DRE");
            }
        } else {
            trtcreVo.setFiprol("DRE");
        }
    }

    @Override
    public boolean isNeedRepair(Trn trn) {
        if ("BCTSET".equals(trn.getInifrm()) || "BCTSET1".equals(trn.getInifrm())) {
            return true;
        }
        return false;
    }
}
