PostFutureHoldByProductAction.java

package com.mycim.webapp.actions.lot.postfuturehold;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.alm.EenAction;
import com.mycim.valueobject.consts.ContextNames;
import com.mycim.valueobject.consts.EenActionType;
import com.mycim.valueobject.consts.HoldCodeNames;
import com.mycim.valueobject.prp.*;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.LotInfoForm;
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.lang.Override;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Sola
 * @date 2021/6/30
 **/
public class PostFutureHoldByProductAction extends WipSetupAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        String productId = theform.getInstanceId();
        Long facilityRrn = LocalContext.getFacilityRrn();

        Item item = new Item(productId, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);
        long productRrn = item.getInstanceRrn();

        //查询postFutureHoldByProductList
        String holdType = EenActionType.POST_FUTURE_HOLD;
        String holdCategory = EenActionType.FUTURE_HOLD_BY_PRODUCT;
        List<FutureHold> futureHoldListbyProduct = wipService.getFutureHoldListByProduct(facilityRrn,0,productRrn,holdType,holdCategory);
        for (FutureHold futureHoldInfo : futureHoldListbyProduct) {
            String processId = baseService.getNamedObjectId(futureHoldInfo.getProcessRrn());
            String routeId = baseService.getNamedObjectId(futureHoldInfo.getRouteRrn());
            String operationId = baseService.getNamedObjectId(futureHoldInfo.getOperationRrn());

            futureHoldInfo.setProcessId(processId);
            futureHoldInfo.setRouteId(routeId);
            futureHoldInfo.setOperationId(operationId);
            futureHoldInfo.setCreateUserId(baseService.getNamedObjectId(futureHoldInfo.getCreateUserRrn()));
            futureHoldInfo.setSeqNum(futureHoldInfo.getAttributeData1());
            Map condition = new HashMap<>();
            condition.put("productRrn",futureHoldInfo.getProductRrn());
            condition.put("processRrn",futureHoldInfo.getProcessRrn());
            condition.put("routeRrn",futureHoldInfo.getRouteRrn());
            condition.put("operationRrn",futureHoldInfo.getOperationRrn());
            condition.put("processVersion",futureHoldInfo.getProcessVersion());
            futureHoldInfo.setStepDesc(ctxExecService.getOperationDescByProcessInfo(condition));
            futureHoldInfo.setStageId(ctxExecService.getStageByProcessInfo(condition));

            String createTime=futureHoldInfo.getCreateTime().toString().substring(0, futureHoldInfo.getCreateTime().toString().indexOf("."));
            futureHoldInfo.setPostFHcreateTime(createTime);
        }

        theform.setEffectCount(null);
        theform.setDepartment(null);
        theform.setOperationId(null);
        theform.setInstanceDesc(item.getInstanceDesc());
        request.setAttribute("productRrn", productRrn);
        request.setAttribute("postfutureholdlistbyproduct", futureHoldListbyProduct);

        return mapping.findForward(Constants.INIT_KEY);
    }

    public ActionForward addFutureHold(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                       HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        Long facilityRrn = LocalContext.getFacilityRrn();
        Long userRrn = LocalContext.getUserRrn();
        String productId = theform.getInstanceId();
        Long processRrn = theform.getProcessRrn();
        String routeId = theform.getRouteId();
        String holdCode = theform.getReasonCode();
        String operationId = theform.getOperationId();
        String holdEffectCount = StringUtils.isEmpty(theform.getEffectCount()) ? "1" : theform.getEffectCount();
        String holdReason = theform.getDepartment() + " Reason:" + "Holdcode is " + theform.getReasonCode() + ". " +
                theform.getReason();
        int holdReasonLen = holdReason.replaceAll("[^\\x00-\\xff]", "***").length();
        Assert.isFalse(holdReasonLen > 256, Errors.create().key(MessageIdList.LOT_HOLDREASONTOOLONG)
                                                  .content("holdReason length can not be over 256!").build());

        Assert.isFalse(StringUtils.isEmpty(routeId), Errors.create().content("SubPlan ID can not be null!").build());

        Assert.isFalse(StringUtils.isEmpty(operationId), Errors.create().content("Step ID can not be null!").build());

        Assert.isFalse(StringUtils.isEmpty(holdCode), Errors.create().content("Hold Code can not be null!").build());

        checkRCHoldCode(holdCode);

        Item item = new Item(productId, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);
        long productRrn = item.getInstanceRrn();
        long routeRrn = this.getInstanceRrn(routeId, this.getNamedSpace(ObjectList.WFL_KEY, facilityRrn), ObjectList.WFL_KEY, ObjectList.ROUTE_KEY);
        long operationRrn = this.getInstanceRrn(operationId, this.getNamedSpace(ObjectList.OPERATION_KEY, facilityRrn), ObjectList.OPERATION_KEY);

        long productVersion = baseService.getNamedObject(productRrn).getCurrentVersion();
        long processVersion = prpService.getProcessVersion(processRrn);

        Map<String,Object> setupMap = new HashMap<>();
        setupMap.put("productRrn",productRrn);
        setupMap.put("productVersion",productVersion);
        setupMap.put("processRrn",processRrn);
        setupMap.put("routeRrn",routeRrn);
        setupMap.put("operationRrn",operationRrn);

        lotService.checkSetupPostFHbyProduct(setupMap);

        Map<String, Object> processInfo = new HashMap<>();
        processInfo.put("processRrn",processRrn);
        processInfo.put("routeRrn",routeRrn);
        processInfo.put("operationRrn",operationRrn);
        processInfo.put("processVersion",processVersion);
        String flowseq = ctxExecService.getFlowSeqByProcessInfo(processInfo);

        FutureHold futureHold = new FutureHold();
        futureHold.setFacilityRrn(facilityRrn);
        futureHold.setStatus(EenActionType.ACTIVE);
        futureHold.setProductRrn(productRrn);
        futureHold.setProductVersion(productVersion==0 ? 1 : productVersion);
        futureHold.setProcessRrn(processRrn);
        futureHold.setProcessVersion(processVersion);
        futureHold.setRouteRrn(routeRrn);
        futureHold.setRouteSeq("");
        futureHold.setOperationRrn(operationRrn);
        futureHold.setOperationSeq("");
        futureHold.setFlowSeq(flowseq);
        futureHold.setEffictiveCount(Long.valueOf(holdEffectCount));
        futureHold.setHoldType(EenActionType.POST_FUTURE_HOLD);
        futureHold.setHoldCategory(EenActionType.FUTURE_HOLD_BY_PRODUCT);
        futureHold.setHoldGroup(theform.getDepartment());
        futureHold.setHoldCode(theform.getReasonCode());
        futureHold.setHoldReason(theform.getDepartment() + " Reason:" + "Holdcode is " + theform.getReasonCode() + ". " +
                                         theform.getReason());
        futureHold.setHoldComment(theform.getReason());
        futureHold.setCreateUserRrn(userRrn);
        futureHold.setCreateTime(new Timestamp(System.currentTimeMillis()));
        futureHold.setUpdateUserRrn(userRrn);
        futureHold.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        futureHold.setLotRrn(0L);

        wipService.insertFutureHold(futureHold);

        return init(mapping, form, request, response);
    }

    public ActionForward deleteDispatch(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        String routeId = request.getParameter("routeId");
        String operationId = request.getParameter("operationId");
        String processId = request.getParameter("processId");
        String productId = theform.getInstanceId();
        String holdCode = request.getParameter("holdCode");
        String seqNum = request.getParameter("seqNum");

        request.setAttribute("productId", productId);
        request.setAttribute("processId", processId);
        request.setAttribute("routeId", routeId);
        request.setAttribute("operationId", operationId);
        request.setAttribute("holdCode",holdCode);
        request.setAttribute("seqNum",seqNum);

        return mapping.findForward(Constants.DELETE_KEY);
    }

    public ActionForward deleteFutureHold(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                          HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        String comments = theform.getLotComments();
        String routeId = request.getParameter("routeId");
        String operationId = request.getParameter("operationId");
        String productId = request.getParameter("productId");
        String processId = request.getParameter("processId");
        String holdCode = request.getParameter("holdCode");
        String seqNum = request.getParameter("seqNum");
        Long facilityRrn = LocalContext.getFacilityRrn();
        String userId = LocalContext.getUserId();
        long userRrn = LocalContext.getUserRrn();

        Item item = new Item(productId, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn), ObjectList.PRODUCT_KEY);
        item = (Item) getInstance(item);
        long productRrn = item.getInstanceRrn();
        long processRrn = this.getInstanceRrn(processId, this.getNamedSpace(ObjectList.WFL_KEY, facilityRrn), ObjectList.WFL_KEY, ObjectList.ROUTE_KEY,ObjectList.PROCESS_KEY);
        long routeRrn = this.getInstanceRrn(routeId, this.getNamedSpace(ObjectList.WFL_KEY, facilityRrn), ObjectList.WFL_KEY, ObjectList.ROUTE_KEY);
        long operationRrn = this.getInstanceRrn(operationId, this.getNamedSpace(ObjectList.OPERATION_KEY, facilityRrn), ObjectList.OPERATION_KEY);

        Integer productVersion = baseService.getNamedObject(productRrn).getCurrentVersion();
        Integer processVersion = prpService.getProcessVersion(processRrn);
        Map<String, Object> processInfo = new HashMap<>();
        processInfo.put("processRrn",processRrn);
        processInfo.put("routeRrn",routeRrn);
        processInfo.put("operationRrn",operationRrn);
        processInfo.put("processVersion",processVersion);
        String flowseq = ctxExecService.getFlowSeqByProcessInfo(processInfo);

        FutureHold futureHold = new FutureHold();
        futureHold.setFacilityRrn(facilityRrn);
        futureHold.setProductRrn(productRrn);
        futureHold.setProductVersion(Long.valueOf(productVersion));
        futureHold.setProcessRrn(processRrn);
        futureHold.setProcessVersion(Long.valueOf(processVersion));
        futureHold.setRouteRrn(routeRrn);
        futureHold.setOperationRrn(operationRrn);
        futureHold.setFlowSeq(flowseq);
        futureHold.setHoldType(EenActionType.POST_FUTURE_HOLD);
        futureHold.setHoldCategory(EenActionType.FUTURE_HOLD_BY_PRODUCT);
        futureHold.setHoldCode(holdCode);
        futureHold.setHoldComment(comments);
        futureHold.setAttributeData1(seqNum);
        wipService.deleteFutureHoldByProductOrLot(futureHold);

        return init(mapping, form, request, response);
    }

    public ActionForward checkSRCFutureHoldInfo(Map map) {

        Long facilityRrn = LocalContext.getFacilityRrn();
        String lotId = (String) map.get("lotId");
        String routeId = (String) map.get("routeId");
        String operationId = (String) map.get("operationId");

        long routeRrn = getInstanceRrn(routeId, getNamedSpace(routeId, facilityRrn), ObjectList.WFL_KEY,
                                       ObjectList.ROUTE_KEY, ObjectList.PROCEDURE_KEY);

        long operationRrn = getInstanceRrn(operationId, facilityRrn, ObjectList.OPERATION_KEY);
        Lot lot = lotQueryService.getLot(lotId, facilityRrn);

        String msg = "";
        List<Map> futureHoldList = getAllFutureHoldForLot(facilityRrn, lot);
        for (Map futureHold : futureHoldList) {
            String holdCode = MapUtils.getString(futureHold, "holdCode");
            if (StringUtils.equals(HoldCodeNames.SRCHOLD_KEY, holdCode)) {
                Assert.isFalse(routeRrn == MapUtils.getLongValue(futureHold, "routeRrn") &&
                                       operationRrn == MapUtils.getLongValue(futureHold, "operationRrn"),
                               Errors.create().content("This step had SRC hold!").build());
            }
        }

        return WebUtils.NULLActionForward;
    }

}