MergeWaferAction.java

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

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
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.valueobject.MessageIdList;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.valueobject.wip.TransReason;
import com.mycim.valueobject.wip.Unit;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.WaferMergeInfoForm;
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;

/**
 * @author Luopeng.Wang
 * @version 6.0.0
 * @date 2019/10/5
 **/
public class MergeWaferAction extends WipSetupAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        WaferMergeInfoForm theform = (WaferMergeInfoForm) form;
        Lot lot = initLotBaseInfoForJsp(request, false, "RELEASELOT", "RELEASELOT_CARRIER");

        List<Map> unitMapList = new ArrayList<>();
        if (lot != null && lot.getLotRrn() > 0) {
            Assert.isTrue(StringUtils.equalsIgnoreCase(LotStatus.HOLD, lot.getLotStatus()),
                          Errors.create().key(MessageIdList.SCRAPLOT_STATUS_NOT_ALLOW).content("lot status is not HOLD")
                                .build());

            unitMapList = wipQueryService.getUnitListByLot(lot.getLotRrn(), false);

            unitMapList = unitMapList.stream().map(unitMap -> {
                unitMap.put("childLotId", lot.getLotId());
                unitMap.put("carrierId", lot.getCarrierId());
                unitMap.put("qty1", lot.getQty1());
                unitMap.put("qty2", lot.getQty2());
                unitMap.put("productId", lot.getProductId());
                unitMap.put("processId", lot.getProcessId());
                unitMap.put("operationId", lot.getOperationId());
                unitMap.put("lotType", lot.getLotStatus());
                return unitMap;
            }).collect(Collectors.toList());

            String spliLotId = StringUtils.substringBeforeLast(lot.getLotId(), ".");
            theform.setSpliLotId(spliLotId);

            theform.setSourceListValues(parseToJsonString(unitMapList));

        }

        //childUnit的临时数据
        theform.setCacheCollection(WebUtils.getCacheObj2String(unitMapList));
        theform.setCacheCollection2(WebUtils.getCacheObj2String(unitMapList));

        request.setAttribute("collection", unitMapList);
        theform.setUnitId(null);
        theform.setReason("");
        return mapping.findForward("mergeWafer");

    }

    public ActionForward addWafer(ActionMapping mapping, WaferMergeInfoForm theform, HttpServletRequest request,
                                  HttpServletResponse response) {
        List childLots = (List) WebUtils.getCacheString2Obj(theform.getCacheCollection());
        String waferHidden = (String) request.getParameter("waferHidden");

        Assert.isFalse(StringUtils.isEmpty(theform.getUnitId()),
                       Errors.create().key(MessageIdList.WAFER_WAFER_ID_IS_EMPTY).content("wafer id empty").build());

        Unit unit = wipQueryService.getUnit(LocalContext.getFacilityRrn(), theform.getUnitId());
        Assert.isFalse(unit == null || StringUtils.isEmpty(unit.getUnitId()),
                       Errors.create().key(MessageIdList.WAFER_WAFER_NOT_EXSIT).content("wafer not exsit").build());
        for (Object unitMap : childLots) {
            String unitId = MapUtils.getString((Map) unitMap, "unitId");
            Assert.isFalse(StringUtils.equalsIgnoreCase(unitId, theform.getUnitId()),
                           Errors.create().key(MessageIdList.WAFER_WAFER_IS_EXSIT).content(" wafer exist").build());
        }

        Lot lot = lotQueryService.getLot(unit.getLotRrn().longValue());

        //卡控post future hold被hold住的lot
        checkPostFutureHold(lot.getLotRrn());

        if ((StringUtils.equals(lot.getLotStatus(), LotStatus.SCRAPPED) ||
                StringUtils.equals(lot.getLotStatus(), LotStatus.TERMINATED)) && StringUtils.equals(waferHidden, "0")) {
            initLotBaseInfoForJsp(request, false, "RELEASELOT", "RELEASELOT_CARRIER");
            request.setAttribute("collection", childLots);
            request.setAttribute("waferBool", "1");
            return (mapping.findForward("mergeWafer"));
        }

        // unit因为以前代码处理的时候需要Map类型的
        Map unitMap = BeanUtils.copyBeanToMap(unit);
        unitMap.put("childLotId", lot.getLotId());
        unitMap.put("carrierId", lot.getCarrierId());
        unitMap.put("qty1", lot.getQty1());
        unitMap.put("qty2", lot.getQty2());
        unitMap.put("productId", lot.getProductId());
        unitMap.put("processId", lot.getProcessId());
        unitMap.put("operationId", lot.getOperationId());
        unitMap.put("lotType", lot.getLotStatus());
        unitMap.put("position", unit.getPositionInCarrier() + "");
        unitMap.put("lotid", lot.getLotId());

        childLots = addUnitMap(childLots, unitMap);

        initLotBaseInfoForJsp(request, false, "RELEASELOT", "RELEASELOT_CARRIER");
        request.setAttribute("collection", childLots);
        theform.setCacheCollection(WebUtils.getCacheObj2String(childLots));

        List sortChildLots = new ArrayList();
        sortChildLots.addAll(childLots);
        sortChildLots(sortChildLots);
        theform.setSourceListValues(parseToJsonString(sortChildLots));
        theform.setUnitId(null);

        return (mapping.findForward("mergeWafer"));
    }

    public ActionForward updateWafer(ActionMapping mapping, WaferMergeInfoForm theform, HttpServletRequest request,
                                     HttpServletResponse response) {
        List childLots = (List) WebUtils.getCacheString2Obj(theform.getCacheCollection());
        List cacheChildLots = (List) WebUtils.getCacheString2Obj(theform.getCacheCollection2());
        Long facility = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();
        Lot lot = lotQueryService.getLot(theform.getLotId(), facility);

        Assert.isFalse(childLots.size() <= 0, Errors.create().key(MessageIdList.MERGELOT_NO_LOT_SELECTED_FOR_MERGE)
                                                    .content("No lot selected for merge").build());

        HashMap valueMap = new HashMap();
        valueMap.put("lot", lot);
        valueMap.put("childLots", childLots);
        valueMap.put("cacheChildLots", cacheChildLots);
        valueMap.put("comment", theform.getComment());
        valueMap.put("userId", user);
        // add for FMI reason Code
        TransReason transReason = new TransReason();
        String reason = theform.getReason();
        transReason.setReasonCode("");
        transReason.setResponsibility(user);
        transReason.setReason(reason);
        valueMap.put("transReason", transReason);

        lotService.mergeLotWafer(valueMap);

        return init(mapping, theform, request, response);
    }

    public ActionForward deleteWafer(ActionMapping mapping, WaferMergeInfoForm theform, HttpServletRequest request,
                                     HttpServletResponse response) {
        List childLots = (List) WebUtils.getCacheString2Obj(theform.getCacheCollection());
        String unitId = request.getParameter("unitId");
        String buildItem = request.getParameter("buildItem");

        childLots = (List) childLots.stream().filter(childLot -> !(
                StringUtils.equals(buildItem, MapUtils.getString((Map) childLot, "childLotId")) &
                        StringUtils.equals(unitId, MapUtils.getString((Map) childLot, "unitId"))))
                                    .collect(Collectors.toList());

        initLotBaseInfoForJsp(request, false, "RELEASELOT", "RELEASELOT_CARRIER");
        request.setAttribute("collection", childLots);
        theform.setCacheCollection(WebUtils.getCacheObj2String(childLots));
        theform.setSourceListValues(parseToJsonString(childLots));
        theform.setUnitId(null);

        return (mapping.findForward("mergeWafer"));
    }

    private void sortChildLots(List<Map> childLots) {
        Collections.sort(childLots, new Comparator() {

            @Override
            public int compare(Object paramObject1, Object paramObject2) {
                Map info1 = (Map) paramObject1;
                Map info2 = (Map) paramObject2;
                int seq1 = MapUtils.getIntValue(info1, "position");
                int seq2 = MapUtils.getIntValue(info2, "position");
                return (seq1 - seq2);
            }
        });
    }

    private List<Map> addUnitMap(List<Map> sources, Map unitMap) {

        String unitId = MapUtils.getString(unitMap, "unitId");
        int defaultPosition = Unit.getWaferDefaultPostion(unitId);

        List<Integer> positions = sources.stream().map(sourceMap -> {
            int position = MapUtils.getIntValue(sourceMap, "position");
            return position;
        }).sorted().collect(Collectors.toList());

        int cstSize = 25;
        if (defaultPosition > 0 && positions.contains(defaultPosition)) {
            Assert.isFalse(positions.size() >= cstSize,
                           Errors.create().key(MessageIdList.ENTITY_LOCATION_MISSING).content("entity location miss")
                                 .build());
            List<Integer> cstPositions = new ArrayList();
            for (int i = 1; i <= cstSize; i++) {
                cstPositions.add(i);
            }
            cstPositions.removeAll(positions);
            defaultPosition = cstPositions.get(0);
        }

        unitMap.put("position", defaultPosition + "");

        sources.add(unitMap);

        return sources;
    }

    private void mergeLot(Lot lot, Collection childLots, WaferMergeInfoForm theform, Long facility, String user) {

        Assert.isFalse(childLots.size() <= 0, Errors.create().key(MessageIdList.MERGELOT_NO_LOT_SELECTED_FOR_MERGE)
                                                    .content("No lot selected for merge").build());
        List sourceListValues = parseJsonStrAndRemoveIdlePostion(theform.getSourceListValues());

        Collection unitsOfParentLot = null;
        Collection newUnits = null;

        unitsOfParentLot = (List) WebUtils.getCacheString2Obj(theform.getCacheCollection2());
        Collection buildChildUnits = new ArrayList();

        unitsOfParentLot = buildChildUnits;

        newUnits = parseJsonStrAndRemoveIdlePostion(theform.getSourceListValues());

        HashMap valueMap = new HashMap();
        valueMap.put("lot", lot);
        valueMap.put("childLots", childLots);
        valueMap.put("unitOfParent", unitsOfParentLot);
        valueMap.put("comment", theform.getComment());
        valueMap.put("newUnits", newUnits);
        valueMap.put("userId", user);
        // add for FMI reason Code
        TransReason transReason = new TransReason();
        String reason = theform.getReason();

        transReason.setReasonCode("");

        transReason.setResponsibility(user);
        transReason.setReason(reason);
        valueMap.put("transReason", transReason);

        lotService.mergeLotWafer(valueMap);

    }

}