PostFutureHoldByLotAction.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.MapUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.EenActionType;
import com.mycim.valueobject.consts.HoldCodeNames;
import com.mycim.valueobject.prp.FutureHold;
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.commons.compress.utils.Sets;
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.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Sola
 * @date 2021/6/30
 **/
public class PostFutureHoldByLotAction extends WipSetupAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        Long facilityRrn = LocalContext.getFacilityRrn();
        Lot lot = initLotBaseInfoForJsp(request, true, null, null);

        List<FutureHold> futureholdlistbylot = new ArrayList<>();
        List<FutureHold> futureholdlistbyProduct = new ArrayList<>();
        List<FutureHold> futureHoldList = new ArrayList<>();
        if (lot != null && lot.getLotRrn() > 0) {
            String holdType = EenActionType.POST_FUTURE_HOLD;
            String holdCategory = EenActionType.FUTURE_HOLD_BY_LOT;
            futureholdlistbylot = wipService.getFutureHoldListByLot(facilityRrn,lot.getLotRrn(),holdType,holdCategory);
            for (FutureHold futureholdbyLot : futureholdlistbylot) {
                futureholdbyLot.setCreateUserId(baseService.getNamedObjectId(futureholdbyLot.getCreateUserRrn()));
                futureholdbyLot.setFlag(EenActionType.FUTURE_HOLD_LOT_KEY);
                futureholdbyLot.setSeqNum(futureholdbyLot.getAttributeData1());
            }
            futureHoldList.addAll(futureholdlistbylot);
            holdCategory = EenActionType.FUTURE_HOLD_BY_PRODUCT;
            futureholdlistbyProduct = wipService.getFutureHoldListByProduct(facilityRrn,lot.getLotRrn(),lot.getProductRrn(),holdType,holdCategory);
            for (FutureHold futureholdbyProduct : futureholdlistbyProduct) {
                futureholdbyProduct.setCreateUserId(baseService.getNamedObjectId(futureholdbyProduct.getCreateUserRrn()));
                futureholdbyProduct.setFlag(EenActionType.FUTURE_HOLD_PRODUCT_KEY);
                futureholdbyProduct.setSeqNum(futureholdbyProduct.getAttributeData1());
            }
            futureHoldList.addAll(futureholdlistbyProduct);
            request.setAttribute("lotId", lot.getLotId());
        }

        Assert.isFalse(lot == null || !(StringUtils.equals(lot.getLotStatus(), LotStatus.WAITING) ||
                               StringUtils.equals(lot.getLotStatus(), LotStatus.HOLD) ||
                               StringUtils.equals(lot.getLotStatus(), LotStatus.RUNNING) ||
                               StringUtils.equals(lot.getLotStatus(), LotStatus.DISPATCH) ||
                               StringUtils.equals(lot.getLotStatus(), LotStatus.BANKED) ||
                               StringUtils.equals(lot.getLotStatus(), LotStatus.RUNNINGHOLD)),
                       Errors.create().key(MessageIdList.LOT_STATUS_NOT_ALLOW).content("Lot status not allow!")
                             .build());

        for (FutureHold futureHoldInfo : futureHoldList) {
            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);
            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);
        }

        super.checkCstRemoved(Sets.newHashSet(lot.getLotRrn()));

        theform.setEffectCount(null);
        theform.setDepartment(null);
        request.setAttribute("postfutureholdlistbylot", futureholdlistbylot);
        request.setAttribute("allpostfutureinfo", futureHoldList);

        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 lotId = theform.getLotId();
        String routeId = theform.getReturnRouteId();
        String processId = request.getParameter("baseProcessId");
        String flowSeq = request.getParameter("returnFlowSeq");
        String holdCode = theform.getReasonCode();
        String holdEffectCount = StringUtils.isEmpty(theform.getEffectCount()) ? "1" : theform.getEffectCount();
        String operationId = request.getParameter("returnOperationId");
        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(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);

        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);
        long processVersion = prpService.getProcessVersion(processRrn);

        Lot lot = lotQueryService.getLot(lotId, facilityRrn);

        Map<String,Object> setupMap = new HashMap<>();
        setupMap.put("processRrn",processRrn);
        setupMap.put("routeRrn",routeRrn);
        setupMap.put("operationRrn",operationRrn);
        lotService.checkSetupPostFHbyLot(lot,setupMap);

        FutureHold futureHold = new FutureHold();
        futureHold.setFacilityRrn(facilityRrn);
        futureHold.setStatus(EenActionType.ACTIVE);
        futureHold.setLotRrn(lot.getLotRrn());
        futureHold.setProductRrn(lot.getProductRrn());
        futureHold.setProductVersion(Long.valueOf(lot.getProductVersion()));
        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_LOT);
        futureHold.setHoldGroup(theform.getDepartment());
        futureHold.setHoldCode(theform.getReasonCode());
        futureHold.setHoldReason(theform.getDepartment() + " Reason:" + "Holdcode is " + theform.getReasonCode() + ". " +
                                         theform.getReason());
        String holdComment = theform.getReason().replaceAll(" ","");
        futureHold.setHoldComment(holdComment);
        futureHold.setCreateUserRrn(userRrn);
        futureHold.setCreateTime(new Timestamp(System.currentTimeMillis()));
        futureHold.setUpdateUserRrn(userRrn);
        futureHold.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        futureHold.setFlag("addFutureHold");

        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 lotId = theform.getLotId();
        String routeId = request.getParameter("routeId");
        String operationId = request.getParameter("operationId");
        String delSeq = request.getParameter("delSeq");
        String holdCode = request.getParameter("holdCode");
        String holdReason = request.getParameter("holdReason");
        String seqNum = request.getParameter("seqNum");

        request.setAttribute("routeId", routeId);
        request.setAttribute("operationId", operationId);
        request.setAttribute("lotId", lotId);
        request.setAttribute("delSeq", delSeq);
        request.setAttribute("holdCode", holdCode);
        request.setAttribute("seqNum", seqNum);
        if (StringUtils.isNotBlank(holdReason) && holdReason.indexOf(".") > -1) {
            request.setAttribute("holdReason", holdReason.substring(holdReason.indexOf(".") + 1));
        }

        return mapping.findForward(Constants.DELETE_KEY);
    }

    public ActionForward deleteFutureHold(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                          HttpServletResponse response) throws Exception {
        LotInfoForm theform = (LotInfoForm) form;
        String lotId = theform.getLotId();
        String comments = theform.getLotComments();
        String routeId = request.getParameter("routeId");
        String operationId = request.getParameter("operationId");
        Long facilityRrn = LocalContext.getFacilityRrn();
        long userRrn = LocalContext.getUserRrn();
        String userId = LocalContext.getUserId();
        String delSeq = request.getParameter("delSeq");
        String holdCode = request.getParameter("holdCode");
        String holdReason = request.getParameter("holdReason");
        String seqNum = request.getParameter("seqNum");
        if (StringUtils.isEmpty(holdReason)) {
            holdReason = StringUtils.EMPTY;
        }
        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);

        Lot lot = lotQueryService.getLot(lotId, facilityRrn);

        Assert.isFalse(lot.getLotRrn() <= 0,
                       Errors.create().key(MessageIdList.LOT_LOTRRN_NOT_FOUND).content("批次不存在!").build());
        FutureHold futureHold = new FutureHold();
        futureHold.setFacilityRrn(facilityRrn);
        futureHold.setProductRrn(lot.getProductRrn());
        futureHold.setProductVersion(Long.valueOf(lot.getProductVersion()));
        futureHold.setProcessRrn(lot.getProcessRrn());
        futureHold.setProcessVersion(Long.valueOf(lot.getProcessVersion()));
        futureHold.setRouteRrn(routeRrn);
        futureHold.setOperationRrn(operationRrn);
        futureHold.setFlowSeq(delSeq);
        futureHold.setHoldType(EenActionType.POST_FUTURE_HOLD);
        futureHold.setHoldCategory(EenActionType.FUTURE_HOLD_BY_LOT);
        futureHold.setHoldCode(holdCode);
        futureHold.setHoldReason(holdReason);
        futureHold.setHoldComment(comments);
        futureHold.setLotRrn(lot.getLotRrn());
        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;
    }

}