package nc.bs.mmpsc.pscrecive.bp; 

import nc.bs.mmpsc.pscrecive.plugin.bpplugin.PscrecivePluginPoint;
import nc.bs.mmpsc.pscrecive.rule.*;
import nc.bs.mmpub.rule.MMVOSagaFrozenValidateRule;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.impl.pubapp.pattern.rule.processer.CompareAroundProcesser;
import nc.util.mmf.framework.gc.GCUpdateBPTemplate;
import nc.vo.mmpsc.pscrecive.entity.AggPscreciveVO;

/**
 * 单据审核的BP
 * 
 * @since 6.0
 * @version 2014-01-27 15:02:41
 * @author computer
 */
public class PscreciveApproveBP {
    /**
     * 单据的持久化
     * 
     * @param clientBills 单据VO
     * @param originBills 原始单据VO
     * @return 保存后的单据VO
     */
    public AggPscreciveVO[] approve(AggPscreciveVO[] clientBills, AggPscreciveVO[] originBills) {
        // 调用修改模板
    	GCUpdateBPTemplate<AggPscreciveVO> bp = new GCUpdateBPTemplate<AggPscreciveVO>(PscrecivePluginPoint.APPROVE);

        // 执行前规则
        this.addBeforeRule(bp.getAroundProcesser());

        // 执行后规则
        this.addAfterRule(bp.getAroundProcesser(), originBills);

        return bp.update(clientBills, originBills);
    }

    /**
     * @param processer
     * @param originBills
     */
    private void addAfterRule(CompareAroundProcesser<AggPscreciveVO> processer, AggPscreciveVO[] originBills) {
    	// 自动业务关闭工序委外订单
        IRule<AggPscreciveVO> closerule = new CloseOrderRule();
        processer.addAfterRule(closerule);
        //添加审批推送加工费结算单逻辑
        IRule<AggPscreciveVO> pushrule = new Push55E6Rule();
        processer.addAfterFinalRule(pushrule);
    }

    /**
     * @param processer
     */
    private void addBeforeRule(CompareAroundProcesser<AggPscreciveVO> processer) {
    	// 校验冻结状态 added by zhanghr
    	IRule<AggPscreciveVO> checkFrozenRule = new MMVOSagaFrozenValidateRule<AggPscreciveVO>(true);
    	processer.addBeforeRule(checkFrozenRule);
        // 检查状态
    	IRule<AggPscreciveVO> irule = new ApproveBillStatusRule();
        processer.addBeforeRule(irule);
        // 工序委外订单状态检查规则
        IRule<AggPscreciveVO> orderCheckRule = new CheckPscOrderStatusRule();
        processer.addBeforeRule(orderCheckRule);
        // 补充核销状态
        IRule<AggPscreciveVO> vrule = new ApproveVerifyStatusRule();
        processer.addBeforeRule(vrule);
        // 修改审核信息
        IRule<AggPscreciveVO> frule = new FillApprovePsnRule();
        processer.addBeforeRule(frule);
        // 更新审核状态
        IRule<AggPscreciveVO> updaterule = new ApproveStatusUpdateRule();
        processer.addBeforeRule(updaterule);
    }
}
