package nc.bs.sscivm.ivsale.rule;

import nc.bs.framework.common.NCLocator;
import nc.bs.sscivm.ivsale.ivaconst.IVAWebConst;
import nc.bs.sscivm.ivsale.util.IVApplicationTspzUtil;
import nc.bs.sscivm.logger.SSCIVMLogger;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.itf.sscivm.service.IVMInvoiceQueryService;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.sscivm.invoice.IVMInvoiceAggVO;
import nc.vo.sscivm.ivmpub.IVAplocationFPXZ;
import nc.vo.sscivm.ivmpub.IVAplocationZSFS;
import nc.vo.sscivm.ivmpub.InvoiceTypeEnum;
import nc.vo.sscivm.ivsale.IVApplicationAggVO;
import nc.vo.sscivm.ivsale.IVApplicationBodyVO;
import nc.vo.sscivm.ivsale.IVApplicationHeadVO;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class IVApplicationCheckRule implements IRule<IVApplicationAggVO>{
    @Override
    public void process(IVApplicationAggVO[] vos) {
        // 校验单据的购买方信息
        if(vos != null && vos.length > 0){
            for (IVApplicationAggVO aggVO : vos) {
                // 增值税专用发票校验客户名称、客户纳税人识别号、地址电话、开户行及账号
                Integer fplx =  aggVO.getParentVO().getFplx();
                UFBoolean sgbz = aggVO.getParentVO().getSgbz();
                if (InvoiceTypeEnum.getSpecialInvoice().contains(fplx) && (sgbz == null || !sgbz.booleanValue())) {
                    checkHeadNull(aggVO);
                } else if (sgbz != null && sgbz.booleanValue()){//收购类申请表头判空
                    checkSGHeadNull(aggVO);
                } else {
                    // 其它类型发票客户名称不能为空
                    if (isEmpty(aggVO.getParentVO().getGmf_mc())) {
                        ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                                "01058sal-0044")/*@res "该类型的发票购买方名称不能为空"*/);
                    }
                }

                //红冲原因校验
                checkCommonInvoice(aggVO);

                // 校验表体的必输项：项目名称、数量
                checkBodyNull(aggVO);
                // 校验折扣行行号和被折扣行行号合法性
                checkBodyValid(aggVO);
                //校验表头表体金额是否一致
                checkAmount(aggVO);

                if (fplx == InvoiceTypeEnum.INVOICETYPE_ZYFP_JDC.toIntValue()){
                    //机动车专用发票单位校验
                    checkDw(aggVO);
                }

                //矿产类发票校验
                if (InvoiceTypeEnum.getKCLInvoice().contains(fplx)){
                    checkKCL(aggVO);
                }

                //特殊票种校验
                checkTspz(aggVO);

            }
        }
    }

    private void checkBodyNull(IVApplicationAggVO aggVO) throws RuntimeException {
        IVApplicationBodyVO[] applicationBodyVOs = (IVApplicationBodyVO[])aggVO.getChildrenVO();
        List<IVApplicationBodyVO> applicationBodyVOList = new ArrayList<>();

        if(applicationBodyVOs != null && applicationBodyVOs.length > 0){
            for(IVApplicationBodyVO bodyvo : applicationBodyVOs){
                if(bodyvo.getStatus() != VOStatus.DELETED){
                    applicationBodyVOList.add(bodyvo);
                }
            }
        }
        applicationBodyVOs = applicationBodyVOList.toArray(new IVApplicationBodyVO[0]);
        IVApplicationHeadVO hvo = aggVO.getParentVO();
        Integer fplx = hvo.getFplx();

        if (applicationBodyVOs != null && applicationBodyVOs.length > 0) {
            for (IVApplicationBodyVO applicationBodyVO : applicationBodyVOs) {
                if (applicationBodyVO.getXmmc() == null) {
                    ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                            "01058sal-0047")/*@res "开票申请单表体行的物料名称不能为空！"*/);
                }

                if (hvo.getSgbz() == null || !hvo.getSgbz().booleanValue() && (hvo.getHzfp() == null || !hvo.getHzfp().booleanValue()) && (hvo.getZffp() == null || !hvo.getZffp().booleanValue()) && (hvo.getCkfp() == null || !hvo.getCkfp().booleanValue())){
                    if (applicationBodyVO.getPk_materiel() != null && applicationBodyVO.getTaxcode() == null) {
                        ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                                "01058sal-0159")/*@res "开票申请单表体行的税码不能为空！"*/);
                    }
                }
                // 成品油普通发票、成品油专用发票、成品油普通发票(卷式)、增值税电子普通发票（成品油）表体数量是必输
                if (fplx == InvoiceTypeEnum.INVOICETYPE_CPYPTFP.toIntValue()
                        || fplx == InvoiceTypeEnum.INVOICETYPE_CPYZXFP.toIntValue()
                        || fplx == InvoiceTypeEnum.INVOICETYPE_CPYPTFP_JS.toIntValue()
                        || fplx == InvoiceTypeEnum.INVOICETYPE_DZPTFP_CPY.toIntValue()) {
                    if (applicationBodyVO.getXmsl() == null || applicationBodyVO.getXmsl().doubleValue() == 0.0) {
                        ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                                "01058sal-0059")/*@res "该类型的发票表体行的数量不能为空！"*/);
                    }
                }
            }
        } else {
            ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                    "01058sal-0099")/*@res "表体数据不能为空"*/);
        }
    }

    private void checkBodyValid(IVApplicationAggVO aggVO) {
        IVApplicationBodyVO[] applicationBodyVOs = (IVApplicationBodyVO[])aggVO.getChildrenVO();
        List<IVApplicationBodyVO> applicationBodyVOList = new ArrayList<>();

        if(applicationBodyVOs != null && applicationBodyVOs.length > 0){
            for(IVApplicationBodyVO bodyvo : applicationBodyVOs){
                if(bodyvo.getStatus() != VOStatus.DELETED){
                    applicationBodyVOList.add(bodyvo);
                }
            }
        }
        applicationBodyVOs = applicationBodyVOList.toArray(new IVApplicationBodyVO[0]);
        if((null == aggVO.getParentVO().getHzfp() || UFBoolean.FALSE.equals(aggVO.getParentVO().getHzfp()))
                && (null == aggVO.getParentVO().getZffp() || UFBoolean.FALSE.equals(aggVO.getParentVO().getZffp()))){
            Map<String, IVApplicationBodyVO> rowNumAndBodyMap = new HashMap<>();
            Map<String, String> rowNumAndXmmc = new HashMap<>();
            Map<String,String> hhMap = new HashMap<String,String>();
            for (IVApplicationBodyVO applicationBodyVO : applicationBodyVOs) {
                if (applicationBodyVO.getHh() != null){
                    if (hhMap.get("hh"+applicationBodyVO.getHh()) != null){
                        ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                                "01058sal-0109")/*"行号不允许重复"*/);
                    } else {
                        hhMap.put("hh"+applicationBodyVO.getHh(), "hh"+applicationBodyVO.getHh());
                    }
                }
                if(!StringUtils.isEmpty(applicationBodyVO.getZkhhh())){
                    if (applicationBodyVO.getFphxz() == null || IVAplocationFPXZ.PFPXZ_BZKH.toIntValue() != applicationBodyVO.getFphxz()){
                        ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                                "01058sal-0156")/*"当前行不是被折扣行，折扣行行号不能有值！"*/);
                    }
                    if (hhMap.get("zkhhh"+applicationBodyVO.getZkhhh()) != null){
                        ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                                "01058sal-0110")/*"折扣行行号不允许重复"*/);
                    } else {
                        hhMap.put("zkhhh"+applicationBodyVO.getZkhhh(), "zkhhh"+applicationBodyVO.getZkhhh());
                    }
                }
                // 校验折扣行和被折扣行上的行号必输
                if (applicationBodyVO.getFphxz() !=null && IVAplocationFPXZ.PFPXZ_BZKH.toIntValue() == applicationBodyVO.getFphxz() && StringUtils.isEmpty(applicationBodyVO.getZkhhh())) {
                    ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                            "01058sal-0097")/*@res "被折扣行的折扣行行号不能为空"*/);
                } else if (applicationBodyVO.getFphxz() !=null && IVAplocationFPXZ.FPXZ_ZKH.toIntValue() == applicationBodyVO.getFphxz() && StringUtils.isEmpty(applicationBodyVO.getHh())) {
                    ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                            "01058sal-0098")/*@res "折扣行的行号不能为空"*/);
                }
                // 折扣行与被折扣行金额正负校验
                if ( (applicationBodyVO.getFphxz() != null && IVAplocationFPXZ.PFPXZ_BZKH.toIntValue() == applicationBodyVO.getFphxz())
                        && applicationBodyVO.getXmjshj().compareTo(UFDouble.ZERO_DBL) < 0){
                    ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                            "01058sal-0108")/*@res "蓝字开票申请被折扣行金额不能为负数"*/);
                }
                if ( (applicationBodyVO.getFphxz() == null || IVAplocationFPXZ.FPXZ_ZKH.toIntValue() == applicationBodyVO.getFphxz())
                        && applicationBodyVO.getXmjshj().compareTo(UFDouble.ZERO_DBL) > 0){
                    ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                            "01058sal-0137")/*@res "蓝字开票申请单折扣行金额不能大于零，请修改数量为负数！"*/);
                }
                if (applicationBodyVO.getFphxz() !=null && IVAplocationFPXZ.FPXZ_ZKH.toIntValue() == applicationBodyVO.getFphxz()){
                    rowNumAndBodyMap.put(applicationBodyVO.getHh(), applicationBodyVO);
                    rowNumAndXmmc.put(applicationBodyVO.getHh(), applicationBodyVO.getXmmc());
                }
            }
            for (IVApplicationBodyVO applicationBodyVO : applicationBodyVOs) {
                if (applicationBodyVO.getFphxz() !=null && IVAplocationFPXZ.PFPXZ_BZKH.toIntValue() == applicationBodyVO.getFphxz() && !rowNumAndBodyMap.containsKey(applicationBodyVO.getZkhhh())){
                    ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                            "01058sal-0099")/*@res "折扣行与被折扣行数据校验不通过"*/);
                }
            }
            // 校验折扣行数与被折扣行数是否一致 add by ligru 20201203
            int zkhNum = 0, bzkhNum = 0;
            Map<String,IVApplicationBodyVO> zkhMap = new HashMap<String,IVApplicationBodyVO>();
            for (IVApplicationBodyVO applicationBodyVO : applicationBodyVOs) {
                if (applicationBodyVO.getFphxz() !=null && IVAplocationFPXZ.PFPXZ_BZKH.toIntValue() == applicationBodyVO.getFphxz()){
                    bzkhNum++;
                } else if(applicationBodyVO.getFphxz() !=null && IVAplocationFPXZ.FPXZ_ZKH.toIntValue() == applicationBodyVO.getFphxz()){
                    zkhMap.put(applicationBodyVO.getHh(), applicationBodyVO);
                    zkhNum++;
                }
            }
            if(zkhNum != bzkhNum){
                ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                        "01058sal-0099")/*@res "折扣行与被折扣行数据校验不通过"*/);
            }
            // 校验折扣行与被折扣行项目名称是否相同
            for (IVApplicationBodyVO applicationBodyVO : applicationBodyVOs) {
                if (applicationBodyVO.getFphxz() != null
                        && IVAplocationFPXZ.PFPXZ_BZKH.toIntValue() == applicationBodyVO.getFphxz()
                        && !java.util.Objects.equals(rowNumAndXmmc.get(applicationBodyVO.getZkhhh()), applicationBodyVO.getXmmc()))
                {
                    ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID(
                            "1058sal_0",
                            "01058sal-0116",
                            null,
                            new String[] { rowNumAndXmmc.get(applicationBodyVO.getZkhhh()), applicationBodyVO.getXmmc() }/*@res 折扣行项目名称[{0}]与被折扣行项目名称[{1}]不一致*/));
                }

                if(applicationBodyVO.getFphxz() != null
                        && IVAplocationFPXZ.PFPXZ_BZKH.toIntValue() == applicationBodyVO.getFphxz()
                        && applicationBodyVO.getXmjshj().add(zkhMap.get(applicationBodyVO.getZkhhh()).getXmjshj()).doubleValue() <0){
                    ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0", "01058sal-0120")/*"被折扣行的金额应大于折扣行金额！"*/);
                }
            }
        }
    }

    private void checkSGHeadNull(IVApplicationAggVO aggVO) throws RuntimeException{
        if(isEmpty(aggVO.getParentVO().getXsf_mc())){
            ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                    "01058sal-0193")/*@res "收购发票销售方名称不能为空"*/);
        }
        if(isEmpty(aggVO.getParentVO().getXsf_nsrsbh())){
            ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                    "01058sal-0194")/*@res "收购发票销售方纳税人识别号不能为空"*/);
        }
    }

    private void checkHeadNull(IVApplicationAggVO aggVO) throws RuntimeException {
        if(isEmpty(aggVO.getParentVO().getGmf_mc())){
            ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                    "01058sal-0040")/*@res "增值税专用发票购买方名称不能为空"*/);
        }

        if(isEmpty(aggVO.getParentVO().getGmf_nsrsbh())){
            ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                    "01058sal-0041")/*@res "增值税专用发票购买方纳税人识别号不能为空"*/);
        }
        //全电专票无需校验地址电话银行账号
        if(aggVO.getHeadVO().getFplx() == InvoiceTypeEnum.DZFP_ZZSZYFP.toIntValue() || aggVO.getHeadVO().getFplx() == InvoiceTypeEnum.ZZFP_ZZSZYFP.toIntValue()
                || aggVO.getHeadVO().getFplx() == InvoiceTypeEnum.ZZFP_SDFP.toIntValue()){
            return;
        }
        if(isEmpty(aggVO.getParentVO().getGmf_dzdh())){
            ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                    "01058sal-0042")/*@res "增值税专用发票购买方地址电话不能为空"*/);
        }

        if(isEmpty(aggVO.getParentVO().getGmf_yhzh())){
            ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                    "01058sal-0043")/*@res "增值税专用发票购买方银行账号不能为空"*/);
        }

    }

    private void checkCommonInvoice(IVApplicationAggVO aggVO) throws RuntimeException {

        //普票红字申请红冲原因不能为空,专票红字信息申请表不能为空@税率说明红冲原因@
        if (aggVO.getHeadVO().getHzfp() != null && aggVO.getHeadVO().getHzfp().booleanValue()){
            if ((InvoiceTypeEnum.getCommonInvoice().contains(aggVO.getHeadVO().getFplx()) || InvoiceTypeEnum.DZFP_ZZSZYFP.toIntValue()==aggVO.getHeadVO().getFplx())){
                if (isEmpty(aggVO.getParentVO().getHcyy())){
                    ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                            "01058sal-0185")/*"红冲原因不能为空"*/);
                }

                IVApplicationBodyVO[] applicationBodyVOs = (IVApplicationBodyVO[])aggVO.getChildrenVO();
                for (IVApplicationBodyVO applicationBodyVO : applicationBodyVOs){
                    //不校验删除行
                    if (applicationBodyVO.getStatus() == VOStatus.DELETED){
                        continue;
                    }
                    if (applicationBodyVO.getSpbm() != null){
                        //税收编码为以1、2开头的冲红原因不允许选择“服务中止”；
                        if ((applicationBodyVO.getSpbm().startsWith("1") ||applicationBodyVO.getSpbm().startsWith("2")) && aggVO.getParentVO().getHcyy().equals("3")){
                            ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                                    "01058sal-0186")/*"当前税收编码的红冲原因不允许选择:服务中止"*/);
                        }
                        //税收编码为以3开头的冲红原因不允许选择“销售退回”
                        if (applicationBodyVO.getSpbm().startsWith("3") && aggVO.getParentVO().getHcyy().equals("1")){
                            ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                                    "01058sal-0187")/*"当前税收编码的红冲原因不允许选择:销货退回"*/);
                        }
                    }
                }

                //红冲金额与蓝字发票金额一致时，红冲原因不允许选择“销售折让”
                String condition = "fpdm='"+aggVO.getHeadVO().getFpdm()+"' and fphm='"+aggVO.getHeadVO().getFphm()+"' and dr = 0 and (billtypecode = 'SSIV-Cxx-sale' or (billtypecode = 'SSIV-Cxx-rec' and invoice_type in (109,309) )) ";
                if (aggVO.getHeadVO().getFpdm() == null){
                    condition = " fphm='"+aggVO.getHeadVO().getFphm()+"' and dr = 0 and (billtypecode = 'SSIV-Cxx-sale' or (billtypecode = 'SSIV-Cxx-rec' and invoice_type in (109,309) )) ";
                }
                try {
                    IVMInvoiceAggVO[] vos = NCLocator.getInstance().lookup(IVMInvoiceQueryService.class).queryAggVOsByCondition(condition);
                    if (vos != null){
                        if ((vos[0].getHeadVO().getJshj().add(aggVO.getHeadVO().getJshj())).compareTo(UFDouble.ZERO_DBL) == 0 && aggVO.getParentVO().getHcyy().equals("4")){
                            ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                                    "01058sal-0188")/*"全部红冲的原因不允许选择:销售折让"*/);
                        }
                    }
                } catch (BusinessException e) {
                    SSCIVMLogger.error(e.getMessage(), e);
                    throw new RuntimeException(e);
                }

            }

            if (InvoiceTypeEnum.getSpecialInvoice().contains(aggVO.getHeadVO().getFplx()) && aggVO.getHeadVO().getFplx() != InvoiceTypeEnum.DZFP_ZZSZYFP.toIntValue() && isEmpty(aggVO.getParentVO().getHzxxsqb())){
                ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                        "01058sal-0189")/*"红字信息申请表不能为空"*/);
            }
        }
    }

    /**
     * 校验表头表体金额是否一致
     * @param aggVO
     */
    private void checkAmount(IVApplicationAggVO aggVO) {
        UFDouble jshj = UFDouble.ZERO_DBL;
        UFDouble hjje = UFDouble.ZERO_DBL;
        UFDouble hjse = UFDouble.ZERO_DBL;

        //表体金额合计
        for (IVApplicationBodyVO bodyvo : aggVO.getBodyVOs()){
            if (bodyvo.getStatus() == VOStatus.DELETED){
                continue;
            }
            jshj = jshj.add(bodyvo.getXmjshj() == null ? UFDouble.ZERO_DBL : bodyvo.getXmjshj());
            hjje = hjje.add(bodyvo.getXmje() == null ? UFDouble.ZERO_DBL : bodyvo.getXmje());
            hjse = hjse.add(bodyvo.getSe() == null ? UFDouble.ZERO_DBL : bodyvo.getSe());
        }

        IVApplicationHeadVO headvo = aggVO.getHeadVO();
        //价税合计判断
        if (headvo.getJshj().compareTo(jshj) != 0){
            ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                    "01058sal-0160")/*"表头表体价税合计不一致!"*/);
        }
        //无税金额判断
        if (headvo.getHjje().compareTo(hjje) != 0){
            ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                    "01058sal-0161")/*"表头表体无税金额不一致!"*/);
        }
        //无税金额判断
        if (headvo.getHjse().compareTo(hjse) != 0){
            ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                    "01058sal-0162")/*"表头表体税额不一致!"*/);
        }
    }

    /**
     * 校验机动车专用发票单位
     * @param aggVO
     */
    private void checkDw(IVApplicationAggVO aggVO){
        for (IVApplicationBodyVO body : aggVO.getBodyVOs()){
            if (body == null || !nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0","01058sal-0225")/*@res "辆"*/.equals(body.getDw())){
                ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                        "01058sal-0155", null, new String[] {nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0","01058sal-0225")/*@res "辆"*/})/*"增值税专用发票(机动车)单位必须为辆，请修改！"*/);
            }
        }
    }

    /**
     * 矿产类发票校验
     * @param aggVO 开票申请VO
     */
    private void checkKCL(IVApplicationAggVO aggVO){
        for (IVApplicationBodyVO body : aggVO.getBodyVOs()){
            //不校验删除行
            if (body.getStatus() == VOStatus.DELETED){
                continue;
            }
            //计量单位校验
            if(body.getDw() == null || (!nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0","01058sal-0226")/*@res "立方米"*/.equals(body.getDw()) && !nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0","01058sal-0227")/*@res "吨"*/.equals(body.getDw()) && !nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0","01058sal-0228")/*@res "千克"*/.equals(body.getDw()) && !nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0","01058sal-0229")/*@res "克"*/.equals(body.getDw())&& !nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0","01058sal-0230")/*@res "克拉"*/.equals(body.getDw()))){
                ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                        "01058sal-0190", null, new String[]{nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0","01058sal-0231")/*@res "立方米、吨、千克、克、克拉"*/})/*"矿产类发票计量单位必须为:立方米、吨、千克、克、克拉!"*/);
            }
            //数量单价校验
            if (body.getXmsl() == null || body.getXmsl().compareTo(UFDouble.ZERO_DBL) == 0 || body.getXmdj() == null || body.getXmdj().compareTo(UFDouble.ZERO_DBL) == 0){
                ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                        "01058sal-0191")/*"矿产类发票单价数量不能为空!"*/);
            }
            //税收分类编码不为空时，需校验：税编为102矿产品开头且非102040303稀土矿产品开头的商品。
            //税收分类编码为空且物料为空，“商品编码”字段含义为税收分类编码，此时需校验：税编为102矿产品开头且非102040303稀土矿产品开头的商品
            if ((!StringUtils.isEmpty(body.getSsflbm()) && (!body.getSsflbm().startsWith("102") || body.getSsflbm().startsWith("102040303")))
                    ||(StringUtils.isEmpty(body.getSsflbm()) && StringUtils.isEmpty(body.getPk_materiel()) &&(StringUtils.isEmpty(body.getSpbm()) || !body.getSpbm().startsWith("102") || body.getSpbm().startsWith("102040303")))){
                ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                        "01058sal-0192", null, new String[]{"102", "102040303"})/*"矿产类发票税收分类编码必须为102矿产品开头且非102040303稀土矿产品开头的商品!"*/);
            }
        }
    }


    public void checkTspz(IVApplicationAggVO aggVO) {

        String tspz =  aggVO.getParentVO().getTspz() == null ? null : IVApplicationTspzUtil.getTspzCode( aggVO.getParentVO().getTspz());
        //数电特殊票种为E06不动产租赁、E05不动产销售、E03建筑服务时，或为差额开票时，发票明细表体行只允许一正常明细行，或一组折扣与被折扣行
        String zsfs =  aggVO.getParentVO().getZsfs();
        if(IVAWebConst.TSPZ_JZFW.equals(tspz)||
                IVAWebConst.TSPZ_BDCXS.equals(tspz) ||
                IVAWebConst.TSPZ_BDCZN.equals(tspz) ||
                String.valueOf(IVAplocationZSFS.HCYY_CEZS.toIntValue()).equals(zsfs)
        ) {
            //判断表体是否满足条件
            boolean bodyflag = false;
            IVApplicationBodyVO[] bodys = aggVO.getBodyVOs();
            //过滤已删除的表体
            List<IVApplicationBodyVO> unDelBodys = new ArrayList<>();
            for (IVApplicationBodyVO body : bodys){
                if (body.getStatus() != VOStatus.DELETED){
                    unDelBodys.add(body);
                }
            }
            bodys = unDelBodys.toArray(new IVApplicationBodyVO[0]);
            if(bodys!=null && bodys.length>2) {
                bodyflag =true;
            }else if(bodys!=null && bodys.length==2){
                for(IVApplicationBodyVO body:bodys) {
                    int fphxz = body.getFphxz();
                    //判断是否折扣及被折扣
                    if(IVAplocationFPXZ.FPXZ_ZKH.toIntValue()!= fphxz && IVAplocationFPXZ.PFPXZ_BZKH.toIntValue()!=fphxz) {
                        bodyflag = true;
                    }
                }
            }
            if(bodyflag) {
                ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                        "01058sal-0263")/*@res "该票种属于特定业务或差额开票，开具时不可多行开具!"*/);
            }
        }

        if(	IVAWebConst.TSPZ_BDCXS.equals(tspz) || IVAWebConst.TSPZ_BDCZN.equals(tspz)) {
            String bdcdz = aggVO.getParentVO().getBdcdz();
            if (bdcdz == null ||(!bdcdz.contains("街") && !bdcdz.contains("路") && !bdcdz.contains("村") && !bdcdz.contains("乡") && !bdcdz.contains("镇")&& !bdcdz.contains("道") && !bdcdz.contains("巷") && !bdcdz.contains("号"))) {
                ExceptionUtils.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("1058sal_0",
                        "01058sal-0265")/*@res "不动产地址必须包含街、路、村、乡、 镇、道、巷、号等任意一个关键词"*/);
            }
        }

    }
    public boolean isEmpty(String str) {
        return StringUtils.isBlank(str) || "null".equals(str);
    }

}
