RecoveryRunCardAction.java

package com.mycim.webapp.actions.recoveryruncard;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.context.spring.SpringContext;
import com.mycim.framework.utils.lang.BooleanUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.server.runcard.service.RecoveryRunCardService;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.consts.StepTypeConst;
import com.mycim.valueobject.consts.VersionStatus;
import com.mycim.valueobject.ems.Equipment;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotRunCard;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.RunCardAction;
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.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author finatice.yang
 */
public class RecoveryRunCardAction extends RunCardAction {

    private static final String IS_VIEW_FLAG = "isView";

    private static final String OBJECT_TYPE_P = "P";

    protected RecoveryRunCardService recoveryRunCardService = SpringContext.getBean(RecoveryRunCardService.class);

    /**
     * Action 方法: init
     */
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        String[] types = new String[]{"RELATED_MODULE_GROUP"};
        registerOptionType(LocalContext.getFacilityRrn(), Arrays.asList(types), request);
        return mapping.getInputForward();
    }

    /**
     * Action 方法: queryList
     */
    public Map<String, Object> queryList(Map<String, Object> params) {
        int pageSize = MapUtils.getIntValue(params, "limit");
        int pageNo = MapUtils.getIntValue(params, "page");
        String sortJsonString = MapUtils.getString(params, "sort");

        params.put("orderBy", buildOrderBy(sortJsonString));

        return recoveryRunCardService.queryRunCardPage(pageSize, pageNo, params);
    }

    /**
     * Action 方法: addRunCard
     */
    public Map<String, Object> addRunCard() {
        return recoveryRunCardService.addRunCard();
    }

    /**
     * Action 方法: runCardDetail
     */
    public ActionForward runCardDetail(ActionMapping mapping, HttpServletRequest request) {
        String[] types = new String[]{"RELATED_MODULE_GROUP", "RELATED_MODULE_MANAGER", "POLLUTIONLEVEL",
                "WORKAREATYPE", "PROCESSLOCATION", "RRCHOLDCODE", "STAGEID", "$HOLD_RRC_SUPERVISOR"};
        setForwardParametersToRequest(request, types);

        if (BooleanUtils.toBoolean(WebUtils.getParameter("isView", request))) {
            return mapping.findForward("runCardView");
        } else {
            return mapping.findForward("runCardSettings");
        }
    }

    /**
     * Action 方法: getRunCardInfo
     */
    public Map<String, Object> getRunCardInfo(Map<String, Object> params) {
        Long scrRrn = MapUtils.getLong(params, "runCardRrn");
        Assert.isFalse(scrRrn == null || scrRrn <= 0,
                       Errors.create().content("The parameter is missing. Please login again.").build());
        return recoveryRunCardService.getLotRunCardInfo(scrRrn);
    }

    /**
     * Action 方法: getLotWithUnitInfo
     */
    public Map<String, Object> getLotWithUnitInfo(Map<String, Object> params) {
        String lotId = MapUtils.getString(params, "lotId");
        Assert.isFalse(StringUtils.isEmpty(lotId),
                       Errors.create().content("The parameter is missing. Please login again.").build());

        Lot lot = lotQueryService.getLot(lotId);
        Assert.isFalse(lot == null,
                       Errors.create().key(MessageIdList.LOT_MISSING_ID).content("The lot is missing.").build());

        verifyLotInfo(lot);

        return recoveryRunCardService.getLotWithUnitInfo(lot);
    }

    /**
     * Action 方法: saveRecoveryRunCard
     */
    public void saveRecoveryRunCard(Map<String, Object> params) {
        recoveryRunCardService.saveRecoveryRunCard(params);
    }

    /**
     * Action 方法: deleteRunCard
     */
    public void deleteRunCard(Map<String, Object> params) {
        deleteRunCardHandle(params);
    }

    /**
     * Action 方法: frozenRunCard
     */
    public void frozenRunCard(Map<String, Object> params) {
        Long runCardRrn = MapUtils.getLong(params, "runCardRrn");
        Assert.isFalse(runCardRrn == null || runCardRrn <= 0,
                       Errors.create().content("The parameter is missing. Please login again.").build());

        LotRunCard lotRunCard = lotRunCardQueryService.getLotRunCard(runCardRrn);
        Assert.isTrue(securityService.isAdminRole(LocalContext.getFacilityRrn(), LocalContext.getUserRrn()) ||
                              StringUtils.equals(lotRunCard.getCreatedUserId(), LocalContext.getUserId()),
                      Errors.create().content("Permission denied! The user is not the creator of this RRC!").build());
        Assert.isTrue(StringUtils.equals(VersionStatus.DRAFT_KEY, lotRunCard.getStatus()),
                      Errors.create().content("Error status, can not submit!").build());
        Assert.isFalse((lotRunCard.getLotRrn() == null || lotRunCard.getLotRrn() <= 0),
                       Errors.create().content("Lot can not be empty!").build());
        Lot lot = lotQueryService.getLot(lotRunCard.getLotRrn());
        verifyLotInfo(lot);

        recoveryRunCardService.frozenRunCard(lotRunCard);
    }

    /**
     * Action 方法: approveRunCard
     */
    public void approveRunCard(Map<String, Object> params) {
        recoveryRunCardService.approveRunCard(params);
    }

    /**
     * Action 方法: withdrawRunCard
     */
    public void withdrawRunCard(Map<String, Object> params) {
        recoveryRunCardService.withdrawRunCard(params, "Auto Change Hold by RC Withdraw");
    }

    /**
     * Action 方法: getStationIdList
     *
     *@author finatice.yang
     *@date 2021/6/15
     *@version 6.0.0
     **/
    public List<Map<String,String>> getStationIdList(Map<String,Object> params) {
        String eqptId = MapUtils.getString(params, "eqptId");
        return recoveryRunCardService.getStationIdListByEqptId(eqptId);
    }

    /**
     * Action 方法: rejectRunCard
     */
    public void rejectRunCard(Map<String, Object> params) {
        String transComments = MapUtils.getString(params, "transComments");
        Assert.isFalse(StringUtils.isEmpty(transComments),
                       Errors.create().content("Reject comments cannot be empty.").build());
        recoveryRunCardService.rejectRunCard(params, transComments);
    }

    /**
     * Action 方法: viewRunCard
     */
    public Map<String, Object> viewRunCard(Map<String, Object> params) {
        Long scrRrn = MapUtils.getLong(params, "runCardRrn");
        Assert.isFalse(scrRrn == null || scrRrn <= 0,
                       Errors.create().content("The parameter is missing. Please login again.").build());

        return recoveryRunCardService.viewRunCard(scrRrn);
    }

    public ActionForward printRunCardDetail(ActionMapping mapping, HttpServletRequest request) {
        String[] types = new String[]{"RELATED_MODULE_GROUP", "RELATED_MODULE_MANAGER", "POLLUTIONLEVEL",
                                      "WORKAREATYPE", "PROCESSLOCATION", "RRCHOLDCODE", "STAGEID",
                                      "$HOLD_RRC_SUPERVISOR"};
        setForwardParametersToRequest(request, types);

        return mapping.findForward("printRunCard");

    }

    @Override
    protected Boolean hasPermissionToWithdraw(LotRunCard lotRunCard) {
        return recoveryRunCardService.hasPermissionToWithdraw(lotRunCard);
    }

    @Override
    protected void verifyLotInfo(Lot lot) {
        Assert.isTrue(LotStatus.RUNNINGHOLD.equalsIgnoreCase(lot.getLotStatus()),
                      Errors.create().content("Lot status must be " + "RUNNINGHOLD!").build());

        Assert.isFalse((StringUtils.isNotEmpty(lot.getReworkCategory()) ||
                               (lot.getReworkTransRrn() != null && lot.getReworkTransRrn() > 0)),
                       Errors.create().content("Lot is in rework!").build());

        String operationType = lotQueryService.getOperationType(lot);
        Assert.isTrue(StepTypeConst.isProcessingStep(operationType),
                      Errors.create().content("Lot is not in the P type step!").build());

        Equipment equipment = emsService.getEquipment(lot.getEqptRrn());
        Assert.isTrue(OBJECT_TYPE_P.equalsIgnoreCase(equipment.getObjectSubtype()),
                      Errors.create().content("Lot is not in the " + "P type " + "equipment!").build());

        Assert.state(!StringUtils.equalsIgnoreCase("1", equipment.getSorterFlag()),
                     Errors.create().key(MessageIdList.EQUIPMENT_IS_SORTER_EQPT)
                           .content("{} is Sorter equipment,Don't allow RRC").args(equipment.getInstanceId()).build());

        Assert.state(!diffBatchQueryService.checkLotInBatch(lot.getLotRrn()),
                     Errors.create().key(MessageIdList.LOT_IN_BATCH).content("Lot in batch!").build());
        wipCheckService.checkPiLotNormalFunction(lot.getLotRrn(), lot.getBasedLotRrn());
    }
}