SorterModelConfirmAction.java

package com.mycim.webapp.actions.sort;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.framework.utils.msg.JsonUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.consts.SorterEnum;
import com.mycim.valueobject.runcard.util.RunCardUtils;
import com.mycim.valueobject.sorter.SortJobBean;
import com.mycim.valueobject.sorter.SorterDetailBean;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.Unit;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.forms.SortJobForm;
import org.apache.commons.collections.MapUtils;
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.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

public class SorterModelConfirmAction extends SortAbsAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        SortJobForm theform = (SortJobForm) form;
        Map parameters = (Map) request.getAttribute(SessionNames.PARAMETERSINFO_KEY);
        long jobRrn = com.mycim.framework.utils.lang.collections.MapUtils.getLongValue(parameters, "jobRrn");
        if (parameters == null) {
            parameters = (HashMap) WebUtils.getCacheString2Obj(theform.getCacheParametersInfo());
            request.setAttribute(SessionNames.PARAMETERSINFO_KEY, parameters);
        }
        List<Map> lots = (List<Map>) request.getAttribute(SessionNames.COLLECTION_KEY);
        Map lot = lots.get(0);
        Long carrierRrn = MapUtils.getLong(lot, "carrierRrn");
        Long lotRrn = MapUtils.getLong(lot, "lotRrn");
        Long entityRrn = MapUtils.getLong(parameters, "eqptRrn");
        // 通过Lot获取sorter job数据
        SortJobBean sortJobBean = sorterQueryService.getSortJobListByCarrierRrn(carrierRrn);
        if (sortJobBean.getMainJobRrn() == 0) {
            boolean holdFlag = lotService.checkMoveOutSorterJob(lotRrn, entityRrn);
            if (holdFlag) {
                return super.showRunningHoldInfoDetail(mapping, request, jobRrn);
            }
            return workflow(parameters, mapping, request, response);
        }
        Map<String, Object> lotInfo = new HashMap<>();


        request.setAttribute("readT7Code",false);

        //是否是读取来料T7code
        if(SorterEnum.Constant.READ_T7CODE.equals(sortJobBean.getJobType())){
            request.setAttribute("readT7Code",true);
            lotInfo = buildReadT7CodeSortJobDetail(sortJobBean);
        }else{
            lotInfo = buildSortJobDetail(sortJobBean);

        }
        lotInfo.put("allowConfirm", MARK);
        request.setAttribute("lotInfo", lotInfo);
        setCache4WFL(request, form);
        return mapping.getInputForward();
    }

    /**
     * 根据Sorter Job完成操作,继续流转Workflow
     */
    public ActionForward confirmOperation(HttpServletRequest request, ActionForm form, HttpServletResponse response,
                                          ActionMapping mapping) throws Exception {
        Map parameters = (Map) request.getAttribute(SessionNames.PARAMETERSINFO_KEY);
        SortJobForm theform = (SortJobForm) form;
        if (parameters == null) {
            parameters = (Map) WebUtils.getCacheString2Obj(theform.getCacheParametersInfo());
            request.setAttribute(SessionNames.PARAMETERSINFO_KEY, parameters);
        }
        if (request.getAttribute(SessionNames.JOB_KEY) == null) {
            request.setAttribute(SessionNames.JOB_KEY, WebUtils.getCacheString2Obj(theform.getCacheJob()));
        }
        if (request.getAttribute(SessionNames.COLLECTION_KEY) == null) {
            request.setAttribute(SessionNames.COLLECTION_KEY,
                                 WebUtils.getCacheString2Obj(theform.getCacheCollection()));
        }
        List<Map> lots = (List<Map>) request.getAttribute(SessionNames.COLLECTION_KEY);
        Map lot = lots.get(0);
        Lot lotInfo = lotInqService.getLot(MapUtils.getLong(lot, "lotRrn"));
        Long carrierRrn = MapUtils.getLong(lot, "carrierRrn");
        // 通过Lot获取sorter job数据
        SortJobBean sortJobBean = sorterQueryService.getSortJobListByCarrierRrn(carrierRrn);
        if (sortJobBean.getMainJobRrn() != 0) {
            if(StringUtils.equalsIgnoreCase(SorterEnum.Constant.READ_T7CODE,sortJobBean.getJobType())){
                List<Map> unitT7codeMapping = JsonUtils.toList(theform.getSourceListValues(), Map.class);
                List<Unit> units = new ArrayList<>();
                List<Unit> newUnits = new ArrayList<>();
                unitT7codeMapping.stream().forEach(data ->{
                    Unit unit = new Unit();
                    unit.setLotRrn(MapUtils.getLongValue(data,"lotRrn"));
                    unit.setLotId(MapUtils.getString(data,"lotId"));
                    unit.setUnitRrn(MapUtils.getLongValue(data,"unitRrn"));
                    unit.setUnitId(MapUtils.getString(data,"unitId"));
                    unit.setCustomerT7Code(MapUtils.getString(data,"customerT7Code"));
                    unit.setPositionInCarrier(MapUtils.getInteger(data,"position"));
                    units.add(unit);
                });
                //校验customer t7code
                sorterService.checkCustomerT7Code(units);
                sorterService.endReadT7Code(units);
            }
                sorterService.endSortJob(sortJobBean);
        }
        if(RunCardUtils.checkLotIdIsRunCardLot(lotInfo.getLotId())){
            return mapping.findForward(Constants.LOTLOCATION_KEY);
        }
        return workflow(parameters, mapping, request, response);
    }

    public ActionForward workflow(Map parameters, ActionMapping mapping, HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        parameters.put(SessionNames.RUNSTEP_FLAG, "0");
        // todo future action split
        request.getRequestDispatcher(mapping.findForward("workflow").getPath() + "?action=init")
               .forward(request, response);
        return null;
    }

}