RunCardAction.java

package com.mycim.webapp.actions;

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.StringUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.framework.utils.msg.JsonUtils;
import com.mycim.framework.workflow.engine.WorkflowManagerService;
import com.mycim.quartz.service.QuartzOperateService;
import com.mycim.server.base.service.BaseService;
import com.mycim.server.edcchart.service.EdcChartService;
import com.mycim.server.rc.exec.service.RunCardExecService;
import com.mycim.server.reticle.service.ReticleService;
import com.mycim.server.runcard.service.RunCardLotInqService;
import com.mycim.server.runcard.service.RunCardService;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.VersionStatus;
import com.mycim.valueobject.wip.*;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.lot.ManualAndSortCheckAction;
import com.mycim.workflow.valueobject.WorkflowStepModel;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author pinyan.song
 * @version 6.0.0
 * @date 2019-12-20
 **/
public class RunCardAction extends ManualAndSortCheckAction {

    protected BaseService baseService = SpringContext.getBean(BaseService.class);

    protected RunCardService runCardService = SpringContext.getBean(RunCardService.class);

    protected RunCardExecService runCardExecService = SpringContext.getBean(RunCardExecService.class);

    protected RunCardLotInqService runCardLotInqService = SpringContext.getBean(RunCardLotInqService.class);

    protected QuartzOperateService quartzOperateService = SpringContext.getBean(QuartzOperateService.class);

    protected EdcChartService edcChartService = SpringContext.getBean(EdcChartService.class);

    protected ReticleService reticleService = SpringContext.getBean(ReticleService.class);

    protected WorkflowManagerService workflowManagerService = SpringContext.getBean(WorkflowManagerService.class);

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

    /**
     * Action 方法: getOperationInfoByStep
     */
    public Map<String, Object> getOperationInfoByStep(Map<String, Object> params) {
        return getOperationInfoByStepHandle(params);
    }

    /**
     * Action 方法: getOperationInfo
     */
    public Map<String, Object> getOperationInfo(Map<String, Object> params) {
        return getOperationInfoHandle(params);
    }

    /**
     * Action 方法: checkIfEquipmentIsNeedReticle
     */
    public Map<String, Object> checkIfEquipmentIsNeedReticle(Map<String, Object> params) {
        String equipmentId = MapUtils.getString(params, "equipmentId");
        Assert.isFalse(StringUtils.isEmpty(equipmentId),
                       Errors.create().content("Do not find the equipment, please check the equipment id!").build());

        return runCardService.checkIfEquipmentIsNeedReticle(equipmentId);
    }

    public Map<String, Object> checkIfEquipmentGroupIsNeedReticle(Map<String, Object> params) {
        String equipmentGroupId = MapUtils.getString(params, "equipmentGroupId");
        Assert.isFalse(StringUtils.isEmpty(equipmentGroupId),
                       Errors.create().content("Equipment group id is null!").build());

        long equipmentGroupRrn = getInstanceRrn(equipmentGroupId, LocalContext.getFacilityRrn(),
                                                ObjectList.ENTITYGROUP_KEY);
        Assert.isFalse(equipmentGroupRrn <= 0,
                       Errors.create().content("Do not find the equipment group, please check the equipment group id!")
                             .build());

        return runCardService.checkIfEquipmentGroupIsNeedReticle(equipmentGroupId);
    }

    public Map<String, Object> getUserSettingInfo(Map<String, Object> params) {
        String areaId = MapUtils.getString(params, "areaId");
        return runCardService.getUserSettingInfo(areaId);
    }

    public List<Map<String, String>> getComboxValue(Map<String, Object> params) {
        String type = MapUtils.getString(params, "type");
        return runCardService.getComboxValue(type);
    }

    private void createProcessScheduler(Map<String, Object> jobData) {
        Date date = new Date();
        String jobName = "ECN" + date.getTime();
        quartzOperateService.addJob(jobName, "com.mycim.quartz.jobs.ProcessScheduleJob", "DEFAULT",
                                    new Date(date.getTime() + 1), 0, 0, jobData);
    }

    protected String buildOrderBy(String sortJsonString) {
        StringBuilder orderBy = new StringBuilder();
        if (StringUtils.isNotEmpty(sortJsonString)) {
            List<Map<String, Object>> sort = JsonUtils.toObject(sortJsonString, List.class);
            for (Iterator<Map<String, Object>> iterator = sort.iterator(); iterator.hasNext(); ) {
                Map<String, Object> map = iterator.next();
                String property = MapUtils.getString(map, "property");
                String direction = MapUtils.getString(map, "direction");
                orderBy.append(property).append(" ").append(direction);
                if (iterator.hasNext()) {
                    orderBy.append(", ");
                }
            }
        }
        return orderBy.toString();
    }

    protected void setForwardParametersToRequest(HttpServletRequest request, String[] types) {
        registerOptionType(LocalContext.getFacilityRrn(), Arrays.asList(types), request);

        long runCardRrn = WebUtils.getParameterLong("runCardRrn", request);
        LotRunCard lotRunCard = lotRunCardQueryService.getLotRunCard(runCardRrn);
        Assert.notNull(lotRunCard, Errors.create().content("Run Card is not exists. Please reselect!").build());
        request.setAttribute("runCardRrn", lotRunCard.getRunCardRrn());
        request.setAttribute("runCardType", lotRunCard.getRunCardType());
        request.setAttribute("queueSequence", WebUtils.getParameter("queueSequence", request));
        request.setAttribute("isReview", WebUtils.getParameter("isReview", request));
        request.setAttribute("isActive", VersionStatus.ACTIVE_KEY.equalsIgnoreCase(lotRunCard.getStatus()));
        request.setAttribute("canDoWithdraw", recoveryRunCardService.canDoWithdraw(lotRunCard.getStatus()));
        request.setAttribute("hasPermissionToWithdraw", hasPermissionToWithdraw(lotRunCard));
        request.setAttribute("isPrint", WebUtils.getParameter("isPrint", request));
    }

    /**
     * 子类重写
     */
    protected Boolean hasPermissionToWithdraw(LotRunCard lotRunCard) {
        return null;
    }

    /**
     * 子类重写该方法
     */
    protected void verifyLotInfo(Lot lot) {
    }

    protected void deleteRunCardHandle(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());
        LotRunCard lotRunCard = lotRunCardQueryService.getLotRunCard(scrRrn);
        Assert.isTrue(hasPermissionToWithdraw(lotRunCard),
                      Errors.create().content("Permission denied! The user is not the creator of this RC!").build());
        Assert.isTrue(StringUtils.equals(VersionStatus.DRAFT_KEY, lotRunCard.getStatus()),
                      Errors.create().content("This SRC has submit, please reopen this page!").build());
        runCardService.deleteLotRunCard(scrRrn);
    }

    protected Lot initLotBaseInfoForJsp(HttpServletRequest request) {
        String lotId = request.getParameter("lotId");
        Lot lot = lotQueryService.getLot(lotId);
        RunCardLotInfo runCardLotInfo = runCardLotInqService.getRunCardLotInfo(lot.getLotRrn());
        request.setAttribute("lotInfo", runCardLotInfo);
        return lot;
    }

    protected Map<String, Object> getOperationInfoHandle(Map<String, Object> params) {
        long productRrn = MapUtils.getLong(params, "productRrn");
        String wflStepPath = MapUtils.getString(params, "wflStepPath");
        Assert.isFalse(StringUtils.isEmpty(wflStepPath) || productRrn <= 0,
                       Errors.create().content("The parameter is missing. " + "Please login again.").build());

        String[] stepPath = wipWorkflowQueryService.parseWflStepPath(wflStepPath);
        long operationStepRrn = NumberUtils.toLong(stepPath[5]);
        WorkflowStepModel workflowStepModel = workflowManagerService.getWorkflowStepModel(operationStepRrn);
        long operationRrn = NumberUtils.toLong(StringUtils.substring(workflowStepModel.getParameterValue1(), 6));

        if (operationRrn <= 0) {
            return MapUtils.EMPTY_MAP;
        }

        return splitRunCardService.getOperationInfo(productRrn, wflStepPath, operationRrn);
    }

    protected Map<String, Object> getOperationInfoByStepHandle(Map<String, Object> params) {
        String operationId = MapUtils.getString(params, "operationId");
        Assert.isFalse(StringUtils.isEmpty(operationId),
                       Errors.create().content("The parameter is missing. Please login again.").build());
        return runCardService.getOperationInfo(operationId);
    }

    protected void invokeProcessScheduler(long ecnRrn) {
        Map<String, Object> jobInfo = MapUtils.newHashMap();
        jobInfo.put("ECN_RRN", ecnRrn + "");
        createProcessScheduler(jobInfo);
    }

    protected void doCloseRunCard(Lot lot) {
        LotRunCardStore lotStore = lotRunCardQueryService.getSplitRunCardLotStore(lot.getLotRrn());
        String mainLotId = lotStore.getMainLotId();
        Lot mainLot = lotQueryService.getLot(mainLotId);

        if ((StringUtils.equals(LotStatus.RUNNINGHOLD, lotStore.getStatus()) ||
                StringUtils.equals(LotStatus.RUNNING, lotStore.getStatus()))) {
            recoveryRunCardService.doCloseRecoveryRunCard(mainLot);
        } else {
            splitRunCardService.doCloseSplitRunCard(mainLot);
        }

        lotService.handlePreFutureHold(new HandleLot(mainLot, "", null));
        // splitRunCardService.doStartSplitRunCard(LocalContext.getUserId(), mainLot);
    }

}