AutoMonitorMergeLotAction.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.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.utils.CheckRegexUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.automonitor.dto.LotAutoMonitorQueryDTO;
import com.mycim.valueobject.automonitor.entity.LotAutoMonitorInfo;
import com.mycim.valueobject.automonitor.entity.MonitorCarrierMapping;
import com.mycim.valueobject.ems.Carrier;
import com.mycim.valueobject.sorter.SortJobBean;
import com.mycim.valueobject.sorter.SorterBean;
import com.mycim.valueobject.wip.*;
import com.mycim.valueobject.wip.responseModel.SortJobResponse;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.forms.NpwSplitMergeInfoForm;
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.*;

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

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        NpwSplitMergeInfoForm theform = (NpwSplitMergeInfoForm) form;
        return mergeLotPage(mapping, theform, request, response);
    }

    public ActionForward mergeLotPage(ActionMapping mapping, NpwSplitMergeInfoForm theform, HttpServletRequest request,
                                      HttpServletResponse response) {
        String lotId = request.getParameter("lotId");
        if (StringUtils.isBlank(lotId)) {
            lotId = (String) request.getAttribute("lotId");
        }
        Long lotRrn = lotQueryService.getLotRrn(lotId);
        LotAutoMonitorInfo lotAutoMonitorInfo = lotAutoMonitorInqService.getLotActiveAutoMonitorInfo(lotRrn);
        Long childLotRrn = lotAutoMonitorInfo.getLotRrn();
        Lot childLot = lotInqService.getLot(childLotRrn);
        Long parentLotRrn = lotAutoMonitorInfo.getBaseLotRrn();
        Lot parentLot = lotInqService.getLot(parentLotRrn);

        List<Unit> childUnitList = wipQueryService.getUnitList(childLotRrn);
        if (lotAutoMonitorInfo.getMonitorCarrierRrn() != null &&
                lotAutoMonitorInfo.getMonitorCarrierRrn().longValue() > 0) {
            List<MonitorCarrierMapping> carrierMapping = lotAutoMonitorInqService
                    .getMonitorCarrierMappings(lotAutoMonitorInfo.getMonitorCarrierRrn());
            for (Unit childUnit : childUnitList) {
                for (MonitorCarrierMapping monitorMapping : carrierMapping) {
                    if (childUnit.getUnitRrn() == monitorMapping.getUnitRrn().longValue()) {
                        childUnit.setLotId(childLot.getLotId());
                        childUnit.setPositionInCarrier(monitorMapping.getSourcePosition());
                    }
                }
            }
        }

        List<Unit> allUnitList = new ArrayList<>();
        allUnitList.addAll(childUnitList);
        List<Long> allBaseCarrierLotRrnList = carrierService.getInUseCarrierLotRrnList(parentLot.getCarrierRrn());
        for (Long lotRrnTemp : allBaseCarrierLotRrnList) {
            String lotIdTemp = lotInqService.getLotId(lotRrnTemp);
            List<Unit> unitList = wipQueryService.getUnitList(lotRrnTemp);
            unitList.stream().forEach(unit -> {
                unit.setLotId(lotIdTemp);
            });
            allUnitList.addAll(unitList);
        }

        List<Map> unitList = new ArrayList<>();
        Carrier baseCarrier = carrierService.getCarrier(parentLot.getCarrierRrn());
        for (long i = 1; i <= baseCarrier.getSlotCount().longValue(); i++) {
            Map<String, Object> unitMap = null;
            for (Unit unit : allUnitList) {
                if (unit.getPositionInCarrier().longValue() == i) {
                    unitMap = buildUnitMap(unit.getPositionInCarrier(), unit.getUnitId(), unit.getUnitRrn(),
                                           unit.getLotId(), "");
                }
            }
            if (unitMap == null) {
                unitMap = buildUnitMap(i, "", 0L, "", "");
            }
            unitList.add(unitMap);
        }
        theform.setSourceListValues(parseToJsonString(unitList));

        theform.setTransId(Constants.MODIFY_KEY);
        parentLot.setTrackUnitFlag("1");
        theform.setBaseLotId(lotAutoMonitorInfo.getBaseLotId());
        theform.setCarrierId(childLot.getCarrierId());
        theform.setBaseCarrierId(parentLot.getCarrierId());
        request.setAttribute("showCarrierFlag", "1");
        request.setAttribute("lotId",lotId);
        request.setAttribute("baseLotRrn",lotAutoMonitorInfo.getBaseLotRrn());
        request.setAttribute("sourceCarrierRrn",childLot.getCarrierRrn());
        request.setAttribute("targetCarrierRrn",parentLot.getCarrierRrn());
        return (mapping.findForward(Constants.INIT_KEY));
    }

    public ActionForward saveMergeLots(ActionMapping mapping, NpwSplitMergeInfoForm theform, HttpServletRequest request,
                                       HttpServletResponse response) {

        String childLotId = WebUtils.getParameterUpperCase("lotId", request);
        Lot childLot = lotInqService.getLot(childLotId);

        String parentLotId = WebUtils.getParameterUpperCase("baseLotId", request);
        Lot parentLot = lotInqService.getLot(parentLotId);

        Assert.state(childLot.getCarrierRrn().longValue() != parentLot.getCarrierRrn().longValue(),
                     Errors.create().content("The lot is in mother cassette.").build());

        List<SorterBean> sorterList = sorterQueryService
                .getSorterListByCarrier(childLot.getCarrierRrn(), parentLot.getCarrierRrn());
        Assert.state(CollectionUtils.isEmpty(sorterList),
                     Errors.create().key(MessageIdList.SORTER_CST_HAVE_JOB).build());

        LotAutoMonitorInfo monitorInfo = lotAutoMonitorInqService.getLotActiveAutoMonitorInfo(childLot.getLotRrn());
        List<Map<String, Object>> units = new ArrayList<>();
        if (monitorInfo != null) {
            List<Unit> unitList = wipQueryService.getUnitList(childLot.getLotRrn());

            if (monitorInfo.getMonitorCarrierRrn() != null && monitorInfo.getMonitorCarrierRrn().longValue() > 0) {
                List<MonitorCarrierMapping> carrierMapping = lotAutoMonitorInqService
                        .getMonitorCarrierMappings(monitorInfo.getMonitorCarrierRrn());

                for (Unit unit : unitList) {
                    for (MonitorCarrierMapping monitorMapping : carrierMapping) {
                        if (unit.getUnitRrn() == monitorMapping.getUnitRrn().longValue()) {
                            Map<String, Object> map = MapUtils.newHashMap();
                            map.put("position", monitorMapping.getSourcePosition().toString());
                            map.put("unitId", unit.getUnitId());
                            map.put("unitRrn", unit.getUnitRrn() + "");
                            map.put("dummyflag", unit.getDummyFlag());
                            map.put("available", "1");
                            map.put("lotId", childLot.getLotId());
                            units.add(map);
                        }
                    }
                }
            }
        }

        if (childLot.getCarrierRrn().longValue() != parentLot.getCarrierRrn().longValue()) {
            // child CST change to mother CST and carrier shared flag is child lot id
            childLot.setCarrierSharedFlag(childLot.getLotId());
            lotAutoMonitorReqService.exchangeAutoMonitorLotCarrier(monitorInfo, parentLot.getCarrierRrn());
        }

        request.setAttribute("lotId", childLotId);
        request.setAttribute("mergeOver", "1");

        return (mapping.findForward(Constants.INIT_KEY));
    }

    public ActionForward createMergeSort(ActionMapping mapping, NpwSplitMergeInfoForm theform,
                                         HttpServletRequest request, HttpServletResponse response) {

        String childLotId = WebUtils.getParameterUpperCase("lotId", request);
        Lot childLot = lotInqService.getLot(childLotId);

        String parentLotId = WebUtils.getParameterUpperCase("baseLotId", request);
        Lot parentLot = lotInqService.getLot(parentLotId);

        Assert.state(childLot.getCarrierRrn().longValue() != parentLot.getCarrierRrn().longValue(),
                     Errors.create().key(MessageIdList.AUTOMONITOR_LOT_IN_BASE_CARRIER).build());

        List<SortJobBean> sortJobBeanList =  sorterQueryService.getSortJobListByCarrierRrn(childLot.getCarrierRrn(),
                                                                                           parentLot.getCarrierRrn());
        Assert.state(CollectionUtils.isNotEmpty(sortJobBeanList),
                     Errors.create().key(MessageIdList.SOURCE_CARRIER_BE_OCCUPIED).build());

        sorterService.addAutoMonitorMergeSortJob(parentLot.getCarrierRrn(),childLot.getCarrierRrn());

        request.setAttribute("lotId", childLotId);
        request.setAttribute("mergeOver", "1");

        return (mapping.findForward(Constants.INIT_KEY));
    }

    public Map initCreateMergeSorterPage(Map params) {
        Long baseLotRrn = MapUtils.getLong(params, "baseLotRrn");
        Long sourceCarrierRrn = MapUtils.getLong(params, "sourceCarrierRrn");
        Long targetCarrierRrn = MapUtils.getLong(params, "targetCarrierRrn");

        Assert.state(Objects.nonNull(baseLotRrn) && baseLotRrn.longValue() > 0,
                     Errors.create().key(MessageIdList.SYSTEM_MISSING_PARAMETER).build());

        Assert.state(sourceCarrierRrn.longValue() != targetCarrierRrn.longValue(),
                     Errors.create().key(MessageIdList.AUTOMONITOR_LOT_IN_BASE_CARRIER).build());

        List<SorterBean> sorterList = sorterQueryService.getSorterListByCarrier(sourceCarrierRrn, targetCarrierRrn);
        Assert.state(CollectionUtils.isEmpty(sorterList),
                     Errors.create().key(MessageIdList.SORTER_CST_HAVE_JOB).build());

        LotAutoMonitorQueryDTO queryDTO = new LotAutoMonitorQueryDTO();
        queryDTO.setBaseLotRrn(baseLotRrn);
        List<LotAutoMonitorInfo> monitorLotInfos = lotAutoMonitorInqService.getLotMonitorLotList(queryDTO);

        List<MonitorCarrierMapping> mappings = lotAutoMonitorInqService.getMonitorCarrierMappings(sourceCarrierRrn);

        List<Map<String,Object>> lotMapList = new ArrayList<>();
        List<Map<String, Object>> unitMapList = new ArrayList<>();
        for (LotAutoMonitorInfo lotMonitorInfo : monitorLotInfos) {
            if (lotMonitorInfo.getCarrierRrn().longValue() == sourceCarrierRrn.longValue()) {
                List<Unit> unitList = wipQueryService.getUnitList(lotMonitorInfo.getLotRrn());
                for (Unit unit : unitList) {
                    Map<String, Object> unitMap = buildUnitMap(unit, mappings);
                    unitMapList.add(unitMap);
                }

                Map<String,Object> lotMap = MapUtils.newHashMap();
                lotMap.put("lotRrn",lotMonitorInfo.getLotRrn());
                lotMap.put("lotId",lotMonitorInfo.getLotId());
                lotMap.put("lotStatus",lotMonitorInfo.getLotStatus());
                lotMap.put("qty1",lotMonitorInfo.getQty1().intValue());
                lotMapList.add(lotMap);
            }
        }

        Map resultMap = MapUtils.newHashMap();
        resultMap.put("lotList", lotMapList);
        resultMap.put("unitList", unitMapList);
        resultMap.put("sourceCarrierId",getInstanceId(sourceCarrierRrn));
        resultMap.put("sourceCarrierRrn",sourceCarrierRrn);
        resultMap.put("targetCarrierId",getInstanceId(targetCarrierRrn));
        resultMap.put("targetCarrierRrn",targetCarrierRrn);
        return resultMap;
    }

    public Map createMergeSorterNew(Map params) {
        Assert.state(StringUtils.isNotBlank(MapUtils.getString(params,"lotRrns")),
                     Errors.create().key(MessageIdList.BATCH_NOT_SELECT_LOT).build());

        String[] lotRrns = StringUtils
                .split(MapUtils.getString(params, "lotRrns"), CheckRegexUtils.LOT_SAMPLING_SPLIT_KEY);

        Long sourceCarrierRrn = MapUtils.getLong(params,"sourceCarrierRrn");
        Long targetCarrierRrn = MapUtils.getLong(params,"targetCarrierRrn");

        List<SortJobBean> sortJobBeanList = sorterQueryService
                .getSortJobListByCarrierRrn(sourceCarrierRrn, targetCarrierRrn);
        Assert.state(CollectionUtils.isNotEmpty(sortJobBeanList),
                     Errors.create().key(MessageIdList.SOURCE_CARRIER_BE_OCCUPIED).build());

        List<Lot> lotList = new ArrayList<>();
        for (String lotRrn : lotRrns) {
            Lot lot = lotInqService.getLot(NumberUtils.toLong(lotRrn));
            if (Objects.nonNull(lot)) {
                lotList.add(lot);
            }
        }

        if (CollectionUtils.isNotEmpty(lotList)) {
            sorterService.addAutoMonitorMergeSortJob(lotList, targetCarrierRrn, sourceCarrierRrn);
        }

        Map resultMap = MapUtils.newHashMap();
        resultMap.put("success", true);
        return resultMap;
    }

    private Map<String, Object> buildUnitMap(Unit unit, List<MonitorCarrierMapping> mappings) {
        Map<String, Object> unitMap = MapUtils.newHashMap();
        // unitMap.put("position", unit.getPositionInCarrier());
        unitMap.put("unitId", unit.getUnitId());
        unitMap.put("unitRrn", unit.getUnitRrn());
        unitMap.put("lotId", unit.getLotId());
        unitMap.put("lotRrn", unit.getLotRrn());

        for (MonitorCarrierMapping mapping : mappings) {
            if (mapping.getUnitRrn().longValue() == unit.getUnitRrn()) {
                unitMap.put("position", mapping.getSourcePosition());
            }
        }
        return unitMap;
    }

    private Map<String, Object> buildUnitMap(long position, String unitId, Long unitRrn, String lotId,
                                             String chooseFlag) {
        Map<String, Object> unitMap = MapUtils.newHashMap();
        unitMap.put("position", position);
        unitMap.put("unitId", unitId);
        unitMap.put("unitRrn", unitRrn);
        unitMap.put("lotId", lotId);
        unitMap.put("chooseFlag", chooseFlag);
        return unitMap;
    }

}