package nc.bs.mmpsc.pscrecive.bp; 

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

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.base.MMArrayUtil;
import nc.util.mmf.framework.base.MMVOUtil;
import nc.util.mmf.framework.gc.GCUpdateBPTemplate;
import nc.vo.mmpsc.pscrecive.entity.AggPscreciveVO;
import nc.vo.mmpsc.pscrecive.entity.PscReciveTransTypeVO;
import nc.vo.mmpsc.pscrecive.util.QueryUtil;
import nc.vo.mmpsc.pscrecive.util.VOChangUtil;

/**
 * 收货单自动审核的BP
 * 
 * @since 6.0
 * @version 2014-01-27 15:02:41
 * @author computer
 */
public class PscreciveAutoApproveBP {
    /**
     * 单据的持久化
     * 
     * @param clientBills 单据VO
     * @param originBills 原始单据VO
     * @return 保存后的单据VO
     */
    public AggPscreciveVO[] approve(AggPscreciveVO[] clientBills) {
    	AggPscreciveVO[] approveVOs = this.filterAutoApprove(clientBills);
    	if (MMArrayUtil.isEmpty(approveVOs)) {
    		return clientBills;
    	}
    	AggPscreciveVO[] originBills = VOChangUtil.clone(approveVOs);
        // 调用修改模板
    	GCUpdateBPTemplate<AggPscreciveVO> bp = new GCUpdateBPTemplate<AggPscreciveVO>(PscrecivePluginPoint.APPROVE);

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

        // 执行后规则
        this.addAfterRule(bp.getAroundProcesser(), originBills);
        
        AggPscreciveVO[] returnBills = bp.update(clientBills, originBills);
        return new QueryUtil().queryAfterUpdate(returnBills);
    }

    /**
     * @param processer
     * @param originBills
     */
    private void addAfterRule(CompareAroundProcesser<AggPscreciveVO> processer, AggPscreciveVO[] originBills) {
    	//暂时没有规则
        //添加审批推送加工费结算单逻辑
        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> vrule = new ApproveVerifyStatusRule();
        processer.addBeforeRule(vrule);
        // 修改审核信息
        IRule<AggPscreciveVO> frule = new FillApprovePsnRule();
        processer.addBeforeRule(frule);
        // 更新审核状态
        IRule<AggPscreciveVO> updaterule = new ApproveStatusUpdateRule();
        processer.addBeforeRule(updaterule);
        // 自动业务关闭工序委外订单
        IRule<AggPscreciveVO> closerule = new CloseOrderRule();
        processer.addBeforeRule(closerule);
    }
    
    /**
     * 过滤出需要自动审核
     * 
     * @param nowBills
     * @return
     */
    private AggPscreciveVO[] filterAutoApprove(AggPscreciveVO[] nowBills) {
        if (nowBills == null || nowBills.length == 0) {
            return null;
        }
        List<String> typeid = new ArrayList<String>();
        for (AggPscreciveVO bill : nowBills) {
            typeid.add(bill.getHeadVO().getCtrantype());
        }
        // 查询哪些交易类型需要自动审核
        Map<String, Boolean> autoMap = this.isAutoApprove(typeid.toArray(new String[0]));
        // 过滤
        List<AggPscreciveVO> reList = new ArrayList<AggPscreciveVO>();
        for (AggPscreciveVO bill : nowBills) {
            Boolean is = autoMap.get(bill.getHeadVO().getCtrantype());
            if (is != null && is.booleanValue()) {
                reList.add(bill);
            }
        }
        if (reList.size() > 0) {
            return reList.toArray(new AggPscreciveVO[0]);
        }
        return null;
    }
    
    /**
     * 是否自动审核
     * 
     * @param ids
     * @return
     */
    private Map<String, Boolean> isAutoApprove(String[] ids) {
        // 查询
        PscReciveTransTypeVO[] types = MMVOUtil.query(PscReciveTransTypeVO.class, PscReciveTransTypeVO.CTRANTYPEID, ids);
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        if (MMArrayUtil.isEmpty(types)) {
            return map;
        }
        // 构造结果
        for (PscReciveTransTypeVO type : types) {
            if (type.getBautoapprove() != null && type.getBautoapprove().booleanValue()) {
                map.put(type.getCtrantypeid(), Boolean.TRUE);
            }
            else {
                map.put(type.getCtrantypeid(), Boolean.FALSE);
            }
        }
        return map;
    }
}
