LotChangeHoldAction.java

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

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.exception.SystemIllegalArgumentException;
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.consts.HoldCodeNames;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotRunCard;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.valueobject.wip.TransReason;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.HoldReleaseLotInfoForm;
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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Luopeng.Wang
 * @version 6.0.0
 * @date 2019/9/16
 **/
public class LotChangeHoldAction extends WipSetupAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        Lot lot = initLotBaseInfoForJsp(request, true, null, null);

        super.checkCstRemoved(Sets.newHashSet(lot.getLotRrn()));
        List<Map> holdInfos = new ArrayList<>();
        if (lot != null && lot.getLotRrn() > 0) {
            holdInfos = wipQueryService.getHoldReasons(lot.getLotRrn());
            request.setAttribute("lotId", lot.getLotId());
        }

        request.setAttribute("collection", holdInfos);

        if (StringUtils.equals(lot.getLotStatus(), LotStatus.HOLD)) {
            return mapping.findForward(Constants.CHANGE_HOLD_KEY);
        } else if (StringUtils.equals(lot.getLotStatus(), LotStatus.RUNNINGHOLD)) {
            return mapping.findForward(Constants.RUNNING_CHANGE_HOLD_KEY);
        } else {
            throw new SystemIllegalArgumentException(
                    Errors.create().key(MessageIdList.LOT_STATUS_NOT_ALLOW).content("Lot status not allow").build());
        }
    }

    public ActionForward changeHold(ActionMapping mapping, ActionForm form,
                                    HttpServletRequest request) throws Exception {
        HoldReleaseLotInfoForm theform = (HoldReleaseLotInfoForm) form;

        long facilityRrn = LocalContext.getFacilityRrn();
        String lotId = theform.getLotId();
        long userRrn = LocalContext.getUserRrn();
        String userId = LocalContext.getUserId();
        String transRrn = request.getParameter("transRrn");
        String oldHoldCode = request.getParameter("oldHoldCode");
        String oldReason = request.getParameter("oldReason");
        String reason = theform.getDepartment() + " Reason:" + "Holdcode is " + theform.getReasonCode() + ". " +
                theform.getReason();
        if (oldReason.startsWith("(TR)")) {
            reason = "(TR)" + reason;
        } else if (oldReason.startsWith("(FH)")) {
            reason = "(FH)" + reason;
        }

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

        Assert.isFalse(lot.getLotRrn() <= 0,
                       Errors.create().key(MessageIdList.LOT_LOTRRN_NOT_FOUND).content("Cannot Find Lot!").build());

        Assert.isTrue(StringUtils.equals(lot.getLotStatus(), LotStatus.HOLD),
                      Errors.create().key(MessageIdList.LOT_STATUS_NOT_ALLOW).content("Lot status not allow").build());

        Assert.isFalse(StringUtils.isEmpty(transRrn), Errors.create().key(MessageIdList.LOT_CHOSE_HOLD_REASON).content(
                "Please select which hold reason you want to change!").build());

        Assert.isTrue(StringUtils.equals(StringUtils.trim(theform.getHoldPassword()),
                                         StringUtils.trim(theform.getPasswordConfirm())),
                      Errors.create().key(MessageIdList.LOGIN_OLD_PWD_NULL)
                            .content("The original password is not correct, please confirm!").build());

        //卡控post future hold被hold住的lot
        checkPostFutureHold(lot.getLotRrn());

        if (StringUtils.contains(oldHoldCode, HoldCodeNames.SRCHOLD_KEY)) {
            List<LotRunCard> runcardList = lotRunCardQueryService.getEffectiveLotRuncardByLot(lot.getLotRrn());
            for (LotRunCard runcard : runcardList) {
                String lotFlowSeq = ctxExecService
                        .getFlowSeqByProcessInfo(lot.getProcessRrn(), lot.getProcessVersion(), lot.getOperationRrn(),
                                                 lot.getRouteRrn());

                Assert.isFalse(StringUtils.contains(oldReason, "Current Lot WaferCnt is not equal to SRC " +
                        "Lot WaferCnt,Please Check!"), Errors.create().key(MessageIdList.RC_WITHDRAW_FIRST)
                                                             .content("Please withdraw the RC {} first!")
                                                             .args(getInstanceId(runcard.getRunCardRrn())).build());
                Assert.isFalse(StringUtils.equals(lotFlowSeq, runcard.getSplitFlowSeq()),
                               Errors.create().content("Can not change SRCHOLD!").build());
            }
        }

        // 增加ocap hold卡控
        Assert.isFalse(StringUtils.contains(oldHoldCode, HoldCodeNames.OCAP_HOLD) &&
                               wipQueryService.isValidOcapHold(lot.getLotRrn(), oldReason),
                       Errors.create().content("Unable to change OCAP Hold!").build());

        TransReason transReason = new TransReason();
        transReason.setReasonCode(theform.getReasonCode());
        transReason.setReason(reason);

        transReason.setTransQty1(lot.getQty1());
        transReason.setTransQty2(lot.getQty2());
        transReason.setResponsibility(userId);

        HashMap holdInfo = new HashMap();
        holdInfo.put("lotRrn", new Long(lot.getLotRrn()).toString());
        holdInfo.put("lotId", lot.getLotId());
        holdInfo.put("lotStatus", lot.getLotStatus());
        holdInfo.put("transPerformedBy", userId);
        holdInfo.put("holdBy", userRrn + "");
        holdInfo.put("holdPassword", StringUtils.trim(theform.getHoldPassword()));
        holdInfo.put("transComments", theform.getLotComments());
        holdInfo.put("operation", lot.getOperationId());
        holdInfo.put("transRrn", transRrn);
        holdInfo.put("oldReason", oldReason);
        holdInfo.put("holdcode", theform.getReasonCode());
        holdInfo.put("transReason", transReason);

        //判断前台传入的hold是否一致
        boolean transError = true;
        List<Map> holdReasons = wipQueryService.getHoldReasons(lot.getLotRrn());
        for (Map holdReason : holdReasons) {
            String holdTransRrn = MapUtils.getString(holdReason, "transRrn");
            if (StringUtils.equals(holdTransRrn, transRrn)) {
                transError = false;

                long holdByRrn = MapUtils.getLongValue(holdReason, "holdByRrn");
                String reasonCode = MapUtils.getString(holdReason, "reasonCode");

                Assert.isTrue(
                        wipQueryService.hasChangeOrReleaseHoldPermission(facilityRrn, userRrn, holdByRrn, reasonCode),
                        Errors.create().content("Current user does not have permission to edit this hold info!")
                              .build());

                String holdPassword =
                        holdReason.get("holdPassword") != null ? ((String) holdReason.get("holdPassword")).trim() : "";
                String oldPassword =
                        request.getParameter("oldPassword") != null ? request.getParameter("oldPassword").trim() : "";
                Assert.isTrue(StringUtils.equals(holdPassword, oldPassword),
                              Errors.create().content("Invalid Old Password!").build());
            }
        }

        Assert.isFalse(transError,
                       Errors.create().key(MessageIdList.LOT_LOT_HAS_BEEN_RELEASED).content("Lot has been released!")
                             .build());
        // List<Lot> lockLots = new ArrayList<Lot>();
        // lockLots.add(lot);
        // checkAndCreateLotsTransLock(LocalContext.getUserRrn(), TransactionNames.LOCK_CHANGE_HOLD, lockLots,
        //                             "change hold in LotChangeHoldAction by " + userId);
        lotService.changeHoldLot(holdInfo);

        return mapping.findForward(Constants.LOTLOCATION_KEY);
    }

    public ActionForward runningChangeHold(ActionMapping mapping, ActionForm form,
                                           HttpServletRequest request) throws Exception {
        HoldReleaseLotInfoForm theform = (HoldReleaseLotInfoForm) form;

        long facilityRrn = LocalContext.getFacilityRrn();
        String lotId = theform.getLotId();
        long userRrn = LocalContext.getUserRrn();
        String userId = LocalContext.getUserId();
        String transRrn = request.getParameter("transRrn");
        String oldHoldCode = request.getParameter("oldHoldCode");
        String oldReason = request.getParameter("oldReason");
        String reason = theform.getDepartment() + " Reason:" + "Holdcode is " + theform.getReasonCode() + ". " +
                theform.getReason();
        if (oldReason.startsWith("(TR)")) {
            reason = "(TR)" + reason;
        } else if (oldReason.startsWith("(FH)")) {
            reason = "(FH)" + reason;
        }

        Assert.isFalse(StringUtils.equalsIgnoreCase(oldHoldCode, HoldCodeNames.SPC_HOLD_CODE) ||
                               StringUtils.contains(oldHoldCode, HoldCodeNames.OCAP_HOLD),
                       Errors.create().content("HoldCode:{} is not allowed to be modified").args(oldHoldCode).build());

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

        Assert.isFalse(lot.getLotRrn() <= 0,
                       Errors.create().key(MessageIdList.LOT_LOTRRN_NOT_FOUND).content("Cannot Find Lot!").build());

        Assert.isTrue(StringUtils.equals(lot.getLotStatus(), LotStatus.RUNNINGHOLD),
                      Errors.create().key(MessageIdList.LOT_STATUS_NOT_ALLOW).content("Lot status not allow").build());

        Assert.isFalse(StringUtils.isEmpty(transRrn), Errors.create().key(MessageIdList.LOT_CHOSE_HOLD_REASON).content(
                "Please select which hold reson you want to change!").build());

        Assert.isTrue(StringUtils.equals(StringUtils.trim(theform.getHoldPassword()),
                                         StringUtils.trim(theform.getPasswordConfirm())),
                      Errors.create().key(MessageIdList.LOGIN_OLD_PWD_NULL)
                            .content("The original password is not correct, please confirm!").build());

        //卡控post future hold被hold住的lot
        checkPostFutureHold(lot.getLotRrn());

        if (StringUtils.contains(oldHoldCode, HoldCodeNames.SRCHOLD_KEY) ||
                StringUtils.contains(oldHoldCode, HoldCodeNames.RRCHOLD_KEY)) {
            List<LotRunCard> runcardList = lotRunCardQueryService.getEffectiveLotRuncardByLot(lot.getLotRrn());
            for (LotRunCard runcard : runcardList) {
                Assert.isFalse(StringUtils.equals(lot.getFlowSeq(), runcard.getSplitFlowSeq()) &&
                                       (StringUtils.equalsIgnoreCase(runcard.getStatus(), "ACTIVE") ||
                                               StringUtils.equalsIgnoreCase(runcard.getStatus(), "SUBMIT")),
                               Errors.create().content("Can not change RRCHOLD!").build());
            }
        }

        TransReason transReason = new TransReason();
        transReason.setReasonCode(theform.getReasonCode());
        transReason.setReason(reason);

        transReason.setTransQty1(lot.getQty1());
        transReason.setTransQty2(lot.getQty2());
        transReason.setResponsibility(userId);

        HashMap holdInfo = new HashMap();
        holdInfo.put("lotRrn", new Long(lot.getLotRrn()).toString());
        holdInfo.put("lotId", lot.getLotId());
        holdInfo.put("lotStatus", lot.getLotStatus());
        holdInfo.put("transPerformedBy", userId);
        holdInfo.put("holdBy", userRrn + "");
        holdInfo.put("holdPassword", StringUtils.trim(theform.getHoldPassword()));
        holdInfo.put("transComments", theform.getLotComments());
        holdInfo.put("operation", lot.getOperationId());
        holdInfo.put("transRrn", transRrn);
        holdInfo.put("oldReason", oldReason);
        holdInfo.put("holdcode", theform.getReasonCode());
        holdInfo.put("transReason", transReason);
        holdInfo.put("byTransFlag", "Y");

        //判断前台传入的hold是否一致
        boolean transError = true;
        List<Map> holdReasons = wipQueryService.getHoldReasons(lot.getLotRrn());
        for (Map holdReason : holdReasons) {
            String holdTransRrn = MapUtils.getString(holdReason, "transRrn");
            if (StringUtils.equals(holdTransRrn, transRrn)) {
                transError = false;

                long holdByRrn = MapUtils.getLongValue(holdReason, "holdByRrn");
                String reasonCode = MapUtils.getString(holdReason, "reasonCode");

                Assert.isTrue(
                        wipQueryService.hasChangeOrReleaseHoldPermission(facilityRrn, userRrn, holdByRrn, reasonCode),
                        Errors.create().content("Current user does not have permission to edit this hold info!")
                              .build());

                String holdPassword =
                        holdReason.get("holdPassword") != null ? ((String) holdReason.get("holdPassword")).trim() : "";
                String oldPassword =
                        request.getParameter("oldPassword") != null ? request.getParameter("oldPassword").trim() : "";
                Assert.isTrue(StringUtils.equals(holdPassword, oldPassword),
                              Errors.create().content("Invalid Old Password!").build());
            }
        }

        Assert.isFalse(transError,
                       Errors.create().key(MessageIdList.LOT_LOT_HAS_BEEN_RELEASED).content("Lot has been released!")
                             .build());
        // List<Lot> lockLots = new ArrayList<Lot>();
        // lockLots.add(lot);
        // checkAndCreateLotsTransLock(LocalContext.getUserRrn(), TransactionNames.LOCK_CHANGE_RUNNINGHOLD, lockLots,
        //                             "change running hold in LotChangeHoldAction by " + userId);
        lotService.changeRunningHoldLot(holdInfo);

        return mapping.findForward(Constants.LOTLOCATION_KEY);
    }

}