ProductAutoSplitMergeSaveAction.java

package com.mycim.webapp.actions.product;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.beans.PropertyUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.prp.AutoSplitMergeInfo;
import com.mycim.valueobject.prp.Item;
import com.mycim.valueobject.wip.AutoSplitMergeLot;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.PrpSetupAction;
import com.mycim.webapp.forms.ProductInfoForm;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品定义
 *
 * @author sandy
 * @version 6.0.0
 * @date 2019/9/5
 **/
public class ProductAutoSplitMergeSaveAction extends PrpSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        ProductInfoForm productInfoForm = (ProductInfoForm) form;
        productInfoForm.setOperationSuccess("");

        String id = "";
        if ((request.getParameter(Constants.MODIFY_KEY) != null) &&
                request.getParameter(Constants.MODIFY_KEY).equals("Y")) {
            id = productInfoForm.getInstanceId();

        } else if (WebUtils.getParameterBoolean(Constants.NAV_KEY, request)) {
            return (mapping.findForward("autoSplitMerge"));
        } else {
            id = StringUtils.trimToUpperCase(productInfoForm.getInstanceId());
        }

        Long facilityRrn = LocalContext.getFacilityRrn();
        Item item = new Item(id, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);
        PropertyUtils.copyProperties(productInfoForm, item);
        List technologys = new ArrayList();
        List<Map<String, String>> materials = new ArrayList<>();

        if (item.getInstanceRrn() <= 0) {
            productInfoForm.setTransId(Constants.CREATE_KEY);

            request.setAttribute("technologys", technologys);
            request.setAttribute("objectType", item.getObjectType());
            productInfoForm.setMaterialList(materials);

            return (mapping.findForward("autoSplitMerge"));
        }
        technologys = this.getProductTechnologysByItem(item);
        item.setProductTechnologys(technologys);

        materials = getProductAndMaterialRelationForShowByProductRrn(item.getInstanceRrn());

        request.setAttribute("initTechnologys", technologys);
        request.setAttribute("technologys", technologys);
        request.setAttribute("objectType", item.getObjectType());

        productInfoForm.setMaterialList(materials);
        request.setAttribute(SessionNames.ITEM_KEY, item);

        productInfoForm.setAutoSplitMergeInfoList(getAutoSplitMergeInfoByProduct(item.getInstanceRrn()));// 物料信息
        productInfoForm.setTransId(Constants.MODIFY_KEY);
        return (mapping.findForward("autoSplitMerge"));
    }

    public ActionForward addAutoSplitMerge(ActionMapping mapping, ProductInfoForm theform, HttpServletRequest request,
                                           HttpServletResponse response) throws Exception {
        List technologys = new ArrayList();
        List sapphires = new ArrayList();
        List debondProducts = new ArrayList();
        theform.setOperationSuccess("");
        Long facilityRrn = LocalContext.getFacilityRrn();
        Item item = new Item(theform.getInstanceId(), getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn),
                             ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);

        Map technologyMap = new HashMap();
        technologyMap.put("productrrn", new Long(item.getInstanceRrn()));

        Map reticleGroupMap = new HashMap();
        reticleGroupMap.put("productrrn", new Long(item.getInstanceRrn()));

        item.setTransPerformedby(LocalContext.getUserId());

        // 获取流程版本号,要和望远镜获取的方式一致!
        int processVer = prpService
                .getLastAvaliableProcessVersion(item.getInstanceRrn(), theform.getSubconProcessRrn());
        boolean aplitIsFirstStepInProcess = prpService
                .isFirstStepInProcess(item.getInstanceRrn(), theform.getAutoSplitProcessRrn(), processVer,
                                      theform.getAutoSplitRouteRrn(), theform.getAutoSplitOperationRrn());
        boolean mergeIsFirstStepInProcess = prpService
                .isFirstStepInProcess(item.getInstanceRrn(), theform.getAutoMergeProcessRrn(), processVer,
                                      theform.getAutoMergeRouteRrn(), theform.getAutoMergeOperationRrn());
        Assert.isFalse(aplitIsFirstStepInProcess || mergeIsFirstStepInProcess,
                       Errors.create().key(MessageIdList.PRODUCT_STEPID_CANNOT_BE_FIRSTSTEP).content("流程第一步错误")
                             .build());
        //check分合批步骤先后问题,重复添加问题,嵌套问题
        checkSplitMergeFlowSeq(item.getInstanceRrn(), theform.getAutoSplitProcessRrn(), theform.getAutoSplitFlowSeq(),
                               theform.getAutoMergeFlowSeq(), theform.getAutosplitQty());
        AutoSplitMergeInfo splitMergeMsg = makeMsg4AutoSplitMerge(theform, item);
        prpService.addAutoSplitMergeInfo(splitMergeMsg);
        theform.setAutoSplitOperationId(StringUtils.EMPTY);
        theform.setAutoMergeOperationId(StringUtils.EMPTY);
        theform.setAutosplitQty(null);
        theform.setTransId(Constants.MODIFY_KEY);

        theform.setAutoSplitMergeInfoList(getAutoSplitMergeInfoByProduct(item.getInstanceRrn()));// 物料信息

        technologys = this.getProductTechnologysByItem(item);
        request.setAttribute("technologys", technologys); // 工艺流程信息
        return mapping.findForward("autoSplitMerge");
    }

    public ActionForward deleteAutoSplitMergeStep(ActionMapping mapping, ProductInfoForm productInfoForm,
                                                  HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        Long productRrn = WebUtils.getParameterLong("productRrn", request);
        Long processRrn = WebUtils.getParameterLong("processRrn", request);
        Long splitOperationRrn = WebUtils.getParameterLong("splitOperationRrn", request);
        Long mergeOperationRrn = WebUtils.getParameterLong("mergeOperationRrn", request);
        List<AutoSplitMergeLot> lots = prpService
                .getAutoSplitMergeLot(productRrn, processRrn, splitOperationRrn, mergeOperationRrn);
        if (lots != null) {
            for (AutoSplitMergeLot autoSplitMergeLot : lots) {
                Assert.isFalse(
                        AutoSplitMergeLot.StatusCode.SPLITED.toString().equals(autoSplitMergeLot.getSplitMergeStatus()),
                        Errors.create().key(MessageIdList.PRODUCT_DELETEAUTOSPLITMERGE)
                              .content("This product has start auto split lots!").build());
            }
        }
        prpService.deleteAutoSplitMergeStep(productRrn, processRrn, splitOperationRrn, mergeOperationRrn);

        String id = StringUtils.trimToUpperCase(productInfoForm.getInstanceId());
        Long facilityRrn = LocalContext.getFacilityRrn();
        Item item = new Item(id, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);
        PropertyUtils.copyProperties(productInfoForm, item);
        List technologys = new ArrayList();
        List<Map<String, String>> materials = new ArrayList<>();
        technologys = this.getProductTechnologysByItem(item);
        item.setProductTechnologys(technologys);

        materials = getProductAndMaterialRelationForShowByProductRrn(item.getInstanceRrn());

        request.setAttribute("initTechnologys", technologys);
        request.setAttribute("technologys", technologys);
        request.setAttribute("objectType", item.getObjectType());

        productInfoForm.setMaterialList(materials);
        request.setAttribute(SessionNames.ITEM_KEY, item);

        productInfoForm.setAutoSplitMergeInfoList(getAutoSplitMergeInfoByProduct(item.getInstanceRrn()));// 物料信息
        productInfoForm.setTransId(Constants.MODIFY_KEY);
        return (mapping.findForward("autoSplitMerge"));
    }

    private List<AutoSplitMergeInfo> getAutoSplitMergeInfoByProduct(long productRrn) {
        return prpService.getAutoSplitMergeInfoByProduct(productRrn);
    }

    private AutoSplitMergeInfo makeMsg4AutoSplitMerge(ProductInfoForm theform, Item item) {
        AutoSplitMergeInfo info = new AutoSplitMergeInfo();

        info.setProductRrn(item.getInstanceRrn());
        info.setProcessRrn(theform.getAutoSplitProcessRrn());
        info.setSplitRouteRrn(theform.getAutoSplitRouteRrn());
        info.setSplitOperationRrn(theform.getAutoSplitOperationRrn());
        info.setSplitFlowSeq(theform.getAutoSplitFlowSeq());
        info.setMergeRouteRrn(theform.getAutoMergeRouteRrn());
        info.setMergeOperationRrn(theform.getAutoMergeOperationRrn());
        info.setMergeFlowSeq(theform.getAutoMergeFlowSeq());
        info.setQty(theform.getAutosplitQty());

        return info;
    }

    /**
     * check分合批步骤先后问题,重复添加问题,嵌套问题
     *
     * @param
     */
    private void checkSplitMergeFlowSeq(long productRrn, Long processRrn, String autoSplitFlowSeq,
                                        String autoMergeFlowSeq, Integer qty) {
        List<AutoSplitMergeInfo> existList = prpService.getAutoSplitMergeInfo(productRrn, processRrn);

        if (autoSplitFlowSeq.indexOf(".") > -1) {
            String[] spliArr = autoSplitFlowSeq.split("\\.");
            String[] mergArr = autoMergeFlowSeq.split("\\.");
            long spliLong = Long.valueOf(spliArr[0] + spliArr[1]);
            long mergLong = Long.valueOf(mergArr[0] + mergArr[1]);
            //分合批同一步骤问题
            Assert.isFalse(spliLong == mergLong,
                           Errors.create().content("The split step and merge step cannot be set at the " + "same step!")
                                 .build());
            //分合批步骤先后问题
            Assert.isFalse(spliLong > mergLong, Errors.create().key(MessageIdList.PRODUCT_CHECKSPLITANDMERGESTEP)
                                                      .content("Please check split/merge flowSEQ").build());
            //嵌套问题
            for (AutoSplitMergeInfo info : existList) {
                String[] existSpliArr = info.getSplitFlowSeq().split("\\.");
                String[] existMergArr = info.getMergeFlowSeq().split("\\.");
                long existSpliLong = Long.valueOf(existSpliArr[0] + existSpliArr[1]);
                long existMergLong = Long.valueOf(existMergArr[0] + existMergArr[1]);

                Assert.isFalse((existSpliLong <= spliLong && spliLong <= existMergLong) ||
                                       (existSpliLong <= mergLong && mergLong <= existMergLong) ||
                                       (existSpliLong > spliLong && existMergLong < mergLong),
                               Errors.create().key(MessageIdList.PRODUCT_CHECKSPLITANDMERGESTEPNEST)
                                     .content("Please check split/merge flowSEQ nested").build());
            }

        } else {
            long spliLong = Long.valueOf(autoSplitFlowSeq);
            long mergLong = Long.valueOf(autoMergeFlowSeq);
            //分合批同一步骤问题
            Assert.isFalse(spliLong == mergLong,
                           Errors.create().content("The split step and merge step cannot be set at the " + "same step!")
                                 .build());
            //分合批步骤先后问题
            Assert.isFalse(spliLong > mergLong, Errors.create().key(MessageIdList.PRODUCT_CHECKSPLITANDMERGESTEP)
                                                      .content("Please check split/merge flowSEQ").build());
            //嵌套问题
            for (AutoSplitMergeInfo info : existList) {
                long existSpliLong = Long.valueOf(info.getSplitFlowSeq());
                long existMergLong = Long.valueOf(info.getMergeFlowSeq());
                Assert.isFalse((existSpliLong <= spliLong && spliLong <= existMergLong) ||
                                       (existSpliLong <= mergLong && mergLong <= existMergLong) ||
                                       (existSpliLong > spliLong && existMergLong < mergLong),
                               Errors.create().key(MessageIdList.PRODUCT_CHECKSPLITANDMERGESTEPNEST)
                                     .content("Please check split/merge flowSEQ nested").build());
            }

        }
        Assert.isFalse(qty == null || qty <= 0 || qty > 25,
                       Errors.create().key(MessageIdList.PRODUCT_CHECKAUTOSPLITQTY).content("Please check qty")
                             .build());
    }

}