AutoMonitorChangeLotRouteAction.java

package com.mycim.webapp.actions;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.i18n.I18nUtils;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.beans.BeanUtils;
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.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.automonitor.dto.AutoMonitorItemDTO;
import com.mycim.valueobject.automonitor.entity.AutoMonitorItemStep;
import com.mycim.valueobject.automonitor.entity.LotAutoMonitorInfo;
import com.mycim.valueobject.automonitor.entity.LotMonitorJobStore;
import com.mycim.valueobject.consts.ReferenceFileConst;
import com.mycim.valueobject.consts.TransactionNames;
import com.mycim.valueobject.prp.Operation;
import com.mycim.valueobject.security.User;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.valueobject.wip.TransReason;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.forms.RootForm;
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.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author finatice.yang
 */
public class AutoMonitorChangeLotRouteAction extends AutoMonitorAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        String lotId = WebUtils.getParameterUpperCase("lotId", request);
        Lot lot = lotInqService.getLot(lotId);
        Assert.state(LotStatus.isHold(lot.getLotStatus()),
                     Errors.create().key(MessageIdList.SCRAPLOT_STATUS_NOT_ALLOW).content("Lot status must be Hold!")
                           .build());
        return initPage(request, mapping);
    }

    public ActionForward initPage(HttpServletRequest request, ActionMapping mapping) {
        String lotId = WebUtils.getParameterUpperCase("lotId", request);
        Lot lot = lotInqService.getLot(lotId);
        LotMonitorJobStore lotMonitorJobStore = lotAutoMonitorInqService.getMonitorJob(lot.getLotRrn());
        LotAutoMonitorInfo monitorLotInfo = lotAutoMonitorInqService.getLotActiveAutoMonitorInfo(lot.getLotRrn());
        Map<String, Object> lotMapInfo = builidlotMapInfo(lot, monitorLotInfo);

        request.setAttribute("workflowRrn", lotMonitorJobStore.getWorkflowRrn());
        request.setAttribute("workflowVersion", lotMonitorJobStore.getWorkflowVersion());
        request.setAttribute("lotInfo", lotMapInfo);
        return mapping.findForward(Constants.INIT_KEY);
    }

    /**
     * 暂时空方法,不校验,方便以后添加
     */
    public String checkTargetProcessInfo(Map<String, Object> params) {
        return StringUtils.EMPTY;
    }

    public ActionForward changeAutoMonitorOperation(ActionMapping mapping, RootForm theform, HttpServletRequest request) {
        String lotId = WebUtils.getParameterUpperCase("lotId",request);
        Lot lot = lotInqService.getLot(lotId);

        String flowSeq = WebUtils.getParameter("flowSeq", request);
        Long workflowRrn = WebUtils.getParameterLong("workflowRrn", request);
        Integer workflowVersion = WebUtils.getParameterInt("workflowVersion", request);

        Long operationRrn = selectOperation(workflowRrn, workflowVersion, flowSeq);
        String wflStep = selectWflStep(workflowRrn, workflowVersion, operationRrn);

        User userInfo = securityService.getUser(LocalContext.getUserRrn());
        String deptExt = StringUtils.trimToUpperCase(theform.getDeptExt());
        String reason =
                theform.getReasonCode() + " " + deptExt + " " + userInfo.getUserName() + " " + theform.getReason();

        TransReason transReason = new TransReason();
        transReason.setReasonCategory(TransactionNames.CHANGEPROCESSFLOW_KEY);
        transReason.setReasonCode(deptExt);
        transReason.setReason(reason);
        transReason.setResponsibility(LocalContext.getUserId());
        transReason.setTransQty1(lot.getInt_qty1().doubleValue());
        transReason.setTransQty2(lot.getInt_qty2().doubleValue());

        if (StringUtils.isNotEmpty(wflStep)) {
            lotAutoMonitorReqService.changeAutoMonitorProcess(lot, wflStep, transReason);
            request.setAttribute("backLotInfo", "1");
        }

        return initPage(request, mapping);
    }

    private Long selectOperation(Long workflowRrn, Integer workflowVersion, String flowSeq) {
        AutoMonitorItemDTO itemDTO = autoMonitorItemInqService.getAutoMonitorItemInfo(workflowRrn, workflowVersion);
        Long operationRrn = null;
        List<AutoMonitorItemStep> stepList = itemDTO.getStepList();
        for (AutoMonitorItemStep step : stepList) {
            if (StringUtils.equals(step.getFlowSeq(), flowSeq)) {
                operationRrn = step.getOperationRrn();
                break;
            }
        }
        return operationRrn;
    }

    private String selectWflStep(Long workflowRrn, Integer workflowVersion, Long operationRrn) {
        String wflStep = StringUtils.EMPTY;
        if (operationRrn != null && operationRrn.longValue() > 0) {
            List<String[]> workflowTree = prpService.getWorkFlowTree(workflowRrn, workflowVersion, MapUtils.EMPTY_MAP);

            for (String[] node : workflowTree) {
                if (operationRrn.longValue() == NumberUtils.toLong(node[4])) {
                    wflStep = node[6];
                    break;
                }
            }
        }
        return wflStep;
    }

}