//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.bs.mmpac.pmo.pac0002.bp;

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

import nc.bs.mmpac.pmo.pac0002.bp.rule.AfterPmoSyncJmQMSRule;
import nc.bs.mmpac.pmo.pac0002.bp.service.PMOBPService;
import nc.bs.mmpac.pmo.pac0002.bp.util.PMOBPUtil;
import nc.bs.mmpac.pmo.pac0002.pluginpoint.PMOPluginPoint;
import nc.bs.mmpac.pmo.pac0002.rule.PMOFilterBillStatusRule;
import nc.bs.mmpac.pmo.pac0002.rule.PMOFilterRowStatusRule;
import nc.bs.mmpac.pmo.pac0002.rule.check.PMOCheckPutCancelActApplyRule;
import nc.bs.mmpac.pmo.pac0002.rule.check.PMOCheckPutCancelBatchBindRule;
import nc.bs.mmpac.pmo.pac0002.rule.check.PMOCheckPutCancelDispatchNumRule;
import nc.bs.mmpac.pmo.pac0002.rule.check.PMOCheckPutCancelHoldNumRule;
import nc.bs.mmpac.pmo.pac0002.rule.check.PMOCheckPutCancelOperationRepRule;
import nc.bs.mmpac.pmo.pac0002.rule.check.PMOCheckPutCancelWrNumRule;
import nc.bs.mmpac.pmo.pac0002.rule.check.PMOPutCancelCheckPutPlanStatusRule;
import nc.bs.mmpac.pmo.pac0002.rule.fill.PMOFillPutValueRule;
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.GCBillTransferTool;
import nc.util.mmf.framework.gc.GCBillUpdate;
import nc.util.mmf.framework.gc.GCUpdateBPTemplate;
import nc.vo.mmpac.pmo.pac0002.constant.PMOConstLang;
import nc.vo.mmpac.pmo.pac0002.entity.PMOAggVO;
import nc.vo.mmpac.pmo.pac0002.entity.PMOItemVO;
import nc.vo.mmpac.pmo.pac0002.enumeration.PMOFBillstatusEnum;
import nc.vo.mmpac.pmo.pac0002.enumeration.PMOFItemStatusEnum;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class PMORowPutCancelBP {
    public PMORowPutCancelBP() {
    }

    public PMOAggVO[] rowputcancel(PMOAggVO[] vos) {
        GCUpdateBPTemplate<PMOAggVO> bp = new GCUpdateBPTemplate(PMOPluginPoint.ROWUNPUT);
        CompareAroundProcesser<PMOAggVO> processer = bp.getAroundProcesser();
        this.addRowPutCancelBeforeRule(processer);
        PMOAggVO[] returnvos = (PMOAggVO[])processer.before(vos, vos);
        List<PMOItemVO> upditemList = new ArrayList();
        Map<String, PMOItemVO> upditemMap = new HashMap();

        for(PMOAggVO aggvo : returnvos) {
            PMOItemVO[] items = aggvo.getChildrenVO();

            for(PMOItemVO item : items) {
                upditemList.add(item);
                upditemMap.put(item.getCmoid(), item);
            }
        }

        PMOAggVO aggvo = new PMOAggVO();

        try {
            aggvo = PMOBPService.getIPMOQueryService().queryByPk(vos[0].getParentVO().getCpmohid());
        } catch (BusinessException e) {
            ExceptionUtils.wrappException(e);
        }

        PMOItemVO[] olditems = aggvo.getChildrenVO();
        List<PMOItemVO> tempitems = new ArrayList();

        for(PMOItemVO item : olditems) {
            if (!upditemMap.containsKey(item.getCmoid())) {
                tempitems.add(item);
            } else {
                ((PMOItemVO)upditemMap.get(item.getCmoid())).setStatus(1);
                PMOBPUtil.setGrandVOStatusUpdate((PMOItemVO)upditemMap.get(item.getCmoid()));
                tempitems.add((PMOItemVO)upditemMap.get(item.getCmoid()));
            }
        }

        aggvo.setChildrenVO((CircularlyAccessibleValueObject[])tempitems.toArray(new PMOItemVO[tempitems.size()]));
        PMOAggVO[] aggvos = new PMOAggVO[]{aggvo};
        GCBillTransferTool<PMOAggVO> transferTool = new GCBillTransferTool(aggvos);
        PMOAggVO[] originBills = (PMOAggVO[])transferTool.getOriginBills();
        GCBillUpdate<PMOAggVO> billupdate = new GCBillUpdate();
        billupdate.update(aggvos, originBills);
        this.addRowPutCancelAfterRule(processer);
        processer.after(returnvos, returnvos);
        return aggvos;
    }

    private void addRowPutCancelBeforeRule(CompareAroundProcesser<PMOAggVO> processer) {
        IRule<PMOAggVO> checkFrozenRule = new MMVOSagaFrozenValidateRule(true);
        processer.addBeforeRule(checkFrozenRule);
        processer.addBeforeRule(new PMOFilterBillStatusRule(PMOConstLang.getMSG_ACTION_NO_AUDIT(), PMOFBillstatusEnum.AUDIT));
        processer.addBeforeRule(new PMOFilterRowStatusRule(PMOConstLang.getMSG_RULE_NO_ROWDATA(), PMOFItemStatusEnum.PUT));
        processer.addBeforeRule(new PMOCheckPutCancelWrNumRule());
        processer.addBeforeRule(new PMOCheckPutCancelActApplyRule());
        processer.addBeforeRule(new PMOCheckPutCancelHoldNumRule());
        processer.addBeforeRule(new PMOCheckPutCancelBatchBindRule());
        processer.addBeforeRule(new PMOFillPutValueRule(true));
        processer.addBeforeRule(new PMOCheckPutCancelDispatchNumRule());
        processer.addBeforeRule(new PMOCheckPutCancelOperationRepRule());
        IRule<PMOAggVO> putCancelCheckPutPlanStatusRule = new PMOPutCancelCheckPutPlanStatusRule();
        processer.addBeforeRule(putCancelCheckPutPlanStatusRule);
    }

    private void addRowPutCancelAfterRule(CompareAroundProcesser<PMOAggVO> processer) {
        IRule<PMOAggVO> rule = new AfterPmoSyncJmQMSRule("putCancel");
        processer.addAfterRule(rule);
    }
}
