package nc.bs.bd.bom.bom0202.bp;

import nc.bs.bd.bom.bom0202.plugin.bpplugin.BomPluginPoint;
import nc.bs.bd.bom.bom0202.rule.*;
import nc.bs.mmbd.pub.rule.MMAutoMaterialAssignRule;
import nc.bs.pubapp.pub.rule.FieldLengthCheckRule;
import nc.bs.pubapp.pub.rule.FillInsertDataRule;
import nc.bs.pubapp.pub.rule.OrgDisabledCheckRule;
import nc.impl.pubapp.bd.material.assistant.MarAssistantSaveRule;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.impl.pubapp.pattern.rule.processer.AroundProcesser;
import nc.itf.org.IOrgConstBasic;
import nc.util.mmf.busi.rule.MMRowNoCheckRule;
import nc.util.mmf.framework.gc.GCInsertBPTemplate;
import nc.vo.bd.bom.bom0202.entity.AggBomVO;
import nc.vo.bd.bom.bom0202.entity.BomActivityVO;
import nc.vo.bd.bom.bom0202.entity.BomItemVO;
import nc.vo.bd.bom.bom0202.entity.BomOutputsVO;
import nc.vo.bd.bom.bom0202.entity.BomPosVO;
import nc.vo.bd.bom.bom0202.entity.BomReplVO;
import nc.vo.bd.bom.bom0202.entity.BomVO;
import nc.vo.bd.bom.bom0202.entity.BomWipVO;

public class BomInsertBP {
    /**
     * 是否校验工程变更单是否启用
     */
    private boolean checkEcn = true;

    /**
     * 是否支持审批流
     */
    private boolean isOnApprove;

    public boolean isOnApprove() {
        return this.isOnApprove;
    }

    public void setOnApprove(boolean isOnApprove) {
        this.isOnApprove = isOnApprove;
    }

    public AggBomVO[] insert(AggBomVO[] aggVO) {
        return this.insert(aggVO, true);
    }

    /**
     * 跨组织复制时，原bom主键
     */
    private String[] origins = null;

    /**
     * 带参数的BOM新增插入方法
     *
     * @param aggVO
     * @param newCheckEcn
     * @return
     */
    public AggBomVO[] insert(AggBomVO[] aggVO, boolean newCheckEcn) {
        return this.insert(aggVO, newCheckEcn, null, false);
    }

    /**
     * 带参数的BOM新增插入方法
     *
     * @param aggVO
     * @param newCheckEcn
     * @param orgins      跨组织复制原bom主键
     * @param param       无效参数，占位
     * @return
     */
    public AggBomVO[] insert(AggBomVO[] aggVO, boolean newCheckEcn, String[] origins, boolean param) {
        this.setCheckEcn(newCheckEcn);
        if (origins != null) {
            this.setOrigins(origins);
        }
        GCInsertBPTemplate<AggBomVO> bp = new GCInsertBPTemplate<AggBomVO>(BomPluginPoint.INSERT);
        // 执行前规则
        this.addBeforeRule(bp.getAroundProcesser());
        // 执行后规则
        this.addAfterRule(bp.getAroundProcesser());
        AggBomVO[] returnvo = bp.insert(aggVO);
        return returnvo;
    }

    /**
     * 带参的新增方法：是否支持ECN，是否支持审批流
     *
     * @param aggVO
     * @param isCheckEcn
     * @param isOnApprove
     * @return
     */
    public AggBomVO[] insert(AggBomVO[] aggVO, boolean isCheckEcn, boolean isOnApprove) {
        this.setCheckEcn(isCheckEcn);
        this.setOnApprove(isOnApprove);
        GCInsertBPTemplate<AggBomVO> bp = new GCInsertBPTemplate<AggBomVO>(BomPluginPoint.INSERT);
        // 执行前规则
        this.addBeforeRule(bp.getAroundProcesser());
        // 执行后规则
        this.addAfterRule(bp.getAroundProcesser());
        AggBomVO[] returnvo = bp.insert(aggVO);
        return returnvo;
    }

    /**
     * 新增前规则
     *
     * @param processor
     */
    @SuppressWarnings("unchecked")
    private void addBeforeRule(AroundProcesser<AggBomVO> processer) {
        // 补充默认值规则
        IRule<AggBomVO> rule = new FillInsertDataRule();
        processer.addBeforeRule(rule);

        // 组织停用校验
        rule = new OrgDisabledCheckRule(BomVO.PK_ORG, IOrgConstBasic.PRODUCEPLANTYPE);
        processer.addBeforeRule(rule);
        if (this.getCheckEcn()) {
            // 是否启用工程变更校验
            rule = new ECNOrgEnableCheckRule();
            processer.addBeforeRule(rule);
        }

        if (!this.getCheckEcn()) {
            // 数据检查并设置默认值
            rule = new BomSaveDataCheckRule();
            processer.addBeforeRule(rule);
        } else {
            // 数据检查并设置默认值
            rule = new BomSaveDataCheckRule(this.getCheckEcn());
            processer.addBeforeRule(rule);
        }

        // 行号检查
        rule = new MMRowNoCheckRule<AggBomVO>();
        processer.addBeforeRule(rule);

        // 物料信息检查
        rule = new BomMaterialCheckRule();
        processer.addBeforeRule(rule);

        // 长度检查
        rule = new FieldLengthCheckRule();
        processer.addBeforeRule(rule);

        // bom自定义项检查
        String[] prefixs = new String[] { "hvdef", "vdef", "vbdef", "vdef", "vdef", "vdef", "vdef" };
        Class<?>[] voClasses = new Class<?>[] { BomVO.class, BomItemVO.class, BomOutputsVO.class, BomActivityVO.class,
                BomWipVO.class, BomPosVO.class, BomReplVO.class };
        rule = new nc.impl.pubapp.bd.userdef.UserDefSaveRule<AggBomVO>(prefixs, voClasses);
        processer.addBeforeRule(rule);

        // 检查自由项
        rule = new MarAssistantSaveRule<AggBomVO>();
        processer.addBeforeRule(rule);

        // delete by qianzhx
        // BOM在非审批通过态可以支持存在多个默认 20150425
        // BOM默认版本规则
        // rule = new BomDefaultVersionCheckRule();
        // processer.addBeforeRule(rule);
        // edited by renshch 20150507
        // 定制BOM不支持审批流，最多一个默认，此规则已做调整，只对定制BOM处理
        //选配修改：这个规则仅处理定制BOM是否默认，定制BOM生成的时候已经处理过默认了，不需要再特殊校验，为了性能，先注释掉
        //rule = new BomDefaultVersionCheckRule();
        //processer.addBeforeRule(rule);

        // 默认卷积：在同一维度下只能有一个默认卷积（业务单元+物料+物料版本+bom类型）
        ConvolutionRule ctRule = new ConvolutionRule();
        processer.addBeforeRule(ctRule);

        // 是否委外：在同一维度下只能有一个委外BOM（业务单元+物料+物料版本）
        OutsourceRule osRule = new OutsourceRule();
        processer.addBeforeRule(osRule);

        // 子项物料启用序列号校验规则
        //此校验在BomSaveDataCheckRule里头已经校验，这里注释掉
        //rule = new SerialManageCheckRule();
        //processer.addBeforeRule(rule);

        // 定制/配置BOM材料页签的相关校验
        // 选配修改：去掉之前定制BOM关于特征码的校验，改到下面的rule里校验
        rule = new CfgBomAndCBomItemCheckRule();
        processer.addBeforeRule(rule);

        //选配修改：增加模型BOM/可选类BOM/定制BOM 设置状态和校验
        rule=new BomCheckForMatchRule();
        processer.addBeforeRule(rule);

        // 分配部分校验
        rule = new BomSaveAssignVersionCheckRule();
        processer.addBeforeRule(rule);
        // 物料表头自动分配
        rule = new MMAutoMaterialAssignRule<AggBomVO>(new String[] { BomVO.HCMATERIALVID, BomVO.HCMATERIALID }, null,
                null, new String[] { BomVO.PK_ORG }, null, null, BomVO.class, false);
        processer.addBeforeFinalRule(rule);
        // 表体材料物料自动分配
        rule = new MMAutoMaterialAssignRule<AggBomVO>(null,
                new String[] { BomItemVO.CMATERIALVID, BomItemVO.CMATERIALID }, null, null,
                new String[] { BomItemVO.PK_ORG }, null, BomItemVO.class, false);
        processer.addBeforeFinalRule(rule);
        // 表体联副产品物料自动分配
        rule = new MMAutoMaterialAssignRule<AggBomVO>(null,
                new String[] { BomOutputsVO.CMATERIALVID, BomOutputsVO.CMATERIALID }, null, null,
                new String[] { BomOutputsVO.PK_ORG }, null, BomOutputsVO.class, false);
        processer.addBeforeFinalRule(rule);
        // 孙表线上仓物料表体自动分配
//		rule = new MMAutoMaterialAssignRule<AggBomVO>(null, BomWipVO.G_CMATERIALVID, null,
//				new String[] { BomWipVO.PK_ORG });
//		processer.addBeforeFinalRule(rule);
//		// 孙表组装位置物料表体自动分配
//		rule = new MMAutoMaterialAssignRule<AggBomVO>(null, BomPosVO.G_CMATERIALVID, null,
//				new String[] { BomPosVO.PK_ORG });
//		processer.addBeforeFinalRule(rule);
//		// 孙表阶梯变动损耗物料表体自动分配
//		rule = new MMAutoMaterialAssignRule<AggBomVO>(null, BomLossVO.CLMATERIALVID, null,
//				new String[] { BomLossVO.PK_ORG });
//		processer.addBeforeFinalRule(rule);

        // 孙表替代料物料表体自动分配
        rule = new MMAutoMaterialAssignRule<AggBomVO>(null, null,
                new String[] { BomReplVO.CREPLMATERIALVID, BomReplVO.CREPLMATERIALOID }, null, null,
                new String[] { BomReplVO.PK_ORG }, BomReplVO.class, false);
        processer.addBeforeFinalRule(rule);
        // 注册事件源
        rule = new BomAddBeforeEventRule();
        processer.addBeforeRule(rule);

        if (this.origins != null) {
            // 替代方案-物料分配
            rule = new BomReplsMaterialAssignRule(this.origins);
            processer.addBeforeRule(rule);
        }
        //2025年6月13日09点38分 -- BOM保存时校验各BOM子件本业务单元库存组织是否启用项目，如果启用，则项目专用料自动启用（包含接口导入）
        rule =new BOMBprojectmaterialEnableRule();
        processer.addBeforeRule(rule);
    }

    /**
     * 新增后规则
     *
     * @param processor
     */
    private void addAfterRule(AroundProcesser<AggBomVO> processer) {
        // BOM版本检查规则
        IRule<AggBomVO> rule = new BomVersionCheckRule();
        processer.addAfterRule(rule);

        // 注册事件源
        rule = new BomAddAfterEventRule();
        processer.addAfterRule(rule);

    }

    public void setCheckEcn(boolean checkEcn) {
        this.checkEcn = checkEcn;
    }

    public boolean getCheckEcn() {
        return this.checkEcn;
    }

    public String[] getOrigins() {
        return origins;
    }

    public void setOrigins(String[] origins) {
        this.origins = origins;
    }

}
