LotLoopInfoAction.java

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

import com.mycim.framework.jdbc.Page;
import com.mycim.framework.logging.Logger;
import com.mycim.framework.logging.LoggerFactory;
import com.mycim.framework.utils.beans.BeanUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.valueobject.prp.LotRecycledInfo;
import com.mycim.valueobject.prp.ProcessLoopInfo;
import com.mycim.valueobject.wip.Lot;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.WipSetupAction;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class LotLoopInfoAction extends WipSetupAction {

    private static final Logger logger = LoggerFactory.getLogger(LotLoopInfoAction.class);

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {

        return mapping.findForward(Constants.INIT_KEY);
    }

    public Map queryLotLootInfo(Map param) {
        Map result = new HashMap();
        String lotId = MapUtils.getString(param, "lotId");
        Lot lot = lotQueryService.getLot(lotId);
        if (lot == null || lot.getLotRrn() <= 0 || lot.getInt_qty1() <= 0) {
            return result;
        }
        List<ProcessLoopInfo> processLoopList = prpService
                .processLoopInfoList(lot.getProcessId(), lot.getProcessVersion());
        List<Map> lotRecycledInfos = lotQueryService
                .queryLotLoopInfo(lot.getLotRrn(), lot.getProcessRrn(), lot.getProcessVersion());

        buildLotRecycledInfos(lotRecycledInfos, processLoopList, lot.getLotRrn());

        result.put("dataList", lotRecycledInfos);
        return result;
    }

    public Map queryUnitLootInfo(Map param) {
        Map result = new HashMap();
        Long lotRrn = MapUtils.getLongValue(param, "lotRrn");
        Lot lot = lotQueryService.getLot(lotRrn);
        if (lot == null || lot.getLotRrn() <= 0 || lot.getInt_qty1() <= 0) {
            return result;
        }
        boolean isParentLot = lot.getLotRrn() == lot.getBasedLotRrn();
        LotRecycledInfo lotRecycledInfo = new LotRecycledInfo();
        BeanUtils.copyMapToBean(lotRecycledInfo, param);
        List<Map> unitRecycledInfos = lotQueryService.queryUnitLoopInfo(lotRecycledInfo);
        if (CollectionUtils.isEmpty(unitRecycledInfos)) {
            unitRecycledInfos = wipQueryService.getUnitsByParentLotRrn(lotRecycledInfo.getLotRrn());
        }
        unitRecycledInfos = buildUnitLoopInfo(rePositionUnitList(unitRecycledInfos), param, isParentLot);
        result.put("dataList", unitRecycledInfos);
        return result;
    }

    public Map queryLotLootHistoryInfo(Map param) {
        Map result = new HashMap();
        String lotId = MapUtils.getString(param, "lotId");
        Long limit = MapUtils.getLongValue(param, "limit");
        Long pageNo = MapUtils.getLongValue(param, "page");
        Lot lot = lotQueryService.getLot(lotId);
        if (lot == null || lot.getLotRrn() <= 0) {
            return result;
        }
        Page page = new Page(pageNo, limit);
        page = lotQueryService.queryLotLoopHistoryInfo(page, lot.getLotRrn(), lot.getProcessRrn());

        result.put("totalItems", page.getTotalItems());
        result.put("dataList", page.getResults());
        return result;
    }

    private void buildLotRecycledInfos(List<Map> lotRecycledInfos, List<ProcessLoopInfo> processLoopList, long lotRrn) {
        List<Map> temp = processLoopList.stream().filter(processLoopInfo -> {
            for (Map lotRecycledInfo : lotRecycledInfos) {
                long processRrn = MapUtils.getLongValue(lotRecycledInfo, "processRrn");
                int processVersion = MapUtils.getIntValue(lotRecycledInfo, "processVersion");
                long startRouteRrn = MapUtils.getLongValue(lotRecycledInfo, "startRouteRrn");
                long startOperationRrn = MapUtils.getLongValue(lotRecycledInfo, "startOperationRrn");
                long endRouteRrn = MapUtils.getLongValue(lotRecycledInfo, "endRouteRrn");
                long endOperationRrn = MapUtils.getLongValue(lotRecycledInfo, "endOperationRrn");
                if (processLoopInfo.getProcessRrn() == processRrn &&
                        processLoopInfo.getProcessVersion() == processVersion &&
                        processLoopInfo.getRouteStartRrn() == startRouteRrn &&
                        processLoopInfo.getOperationStartRrn() == startOperationRrn &&
                        processLoopInfo.getRouteEndRrn() == endRouteRrn &&
                        processLoopInfo.getOperationEndRrn() == endOperationRrn) {
                    return false;
                }
            }
            return true;
        }).map(processLoopInfo -> {
            Map map = new HashMap();
            map = BeanUtils.copyBeanToMap(processLoopInfo);
            map.put("processId", baseService.getNamedObjectId(processLoopInfo.getProcessRrn()));
            map.put("processRrn", processLoopInfo.getProcessRrn());
            map.put("processVersion", processLoopInfo.getProcessVersion());
            map.put("startRouteId", processLoopInfo.getRouteStartId());
            map.put("startOperationId", processLoopInfo.getOperationStartId());
            map.put("endRouteId", processLoopInfo.getRouteEndId());
            map.put("endOperationId", processLoopInfo.getOperationEndId());
            map.put("startRouteRrn", processLoopInfo.getRouteStartRrn());
            map.put("startOperationRrn", processLoopInfo.getOperationStartRrn());
            map.put("endRouteRrn", processLoopInfo.getRouteEndRrn());
            map.put("endOperationRrn", processLoopInfo.getOperationEndRrn());
            map.put("startSeq", processLoopInfo.getStartFlowSeq());
            map.put("endSeq", processLoopInfo.getEndFlowSeq());
            map.put("remainderLoopCount", MapUtils.getLongValue(map, "loopCount"));
            map.put("sumLoopCount", MapUtils.getLongValue(map, "loopCount"));
            map.put("loopCount", MapUtils.getLongValue(map, "0"));
            return map;
        }).collect(Collectors.toList());

        lotRecycledInfos.addAll(temp);
        lotRecycledInfos.forEach(map -> {
            map.put("lotRrn", lotRrn);
        });
    }

    private List<Map> buildUnitLoopInfo(List<Map> unitList, Map param, boolean isParentLot) {
        return unitList.stream().map(unit -> {
            String loopCount = MapUtils.getString(unit, "loopCount");
            String remainderLoopCount = MapUtils.getString(unit, "remainderLoopCount");
            if (StringUtils.isNotBlank(MapUtils.getString(unit, "unitId")) || isParentLot) {
                unit.putAll(param);
            }
            if (StringUtils.isBlank(loopCount) && StringUtils.isNotBlank(MapUtils.getString(unit, "unitId"))) {
                unit.put("loopCount", 0);
            } else if (StringUtils.isNotBlank(loopCount)) {
                unit.put("loopCount", loopCount);
                unit.put("remainderLoopCount", remainderLoopCount);
            }
            return unit;
        }).collect(Collectors.toList());
    }

}