SplitRunCardMergeAction.java

package com.mycim.webapp.actions.splitruncardlot;

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.valueobject.MessageIdList;
import com.mycim.valueobject.runcard.util.RunCardStoreSubStatus;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotRunCardStore;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.valueobject.wip.Unit;
import com.mycim.webapp.actions.RunCardAction;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author pinyan.song
 * @version 6.0.0
 * @date 2020-2-25
 **/
public class SplitRunCardMergeAction extends RunCardAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        String lotId = request.getParameter("lotId");
        long lotRrn = lotQueryService.getLotRrn(lotId);

        String[] lotIdArr = buildChildAndParentLotId4RunCard(lotRrn, lotId);
        String childLotId = lotIdArr[0];
        String parentLotId = lotIdArr[1];

        // Lot childLot = getLot(childLotId, facilityRrn);
        // Lot parentLot = getLot(parentLotId, facilityRrn);
        Assert.isFalse(StringUtils.isEmpty(childLotId), Errors.create().content("Parent lot not finish").build());
        Map<String, Object> childLotMap = lotRunCardQueryService.getRCLotInfoMap(childLotId);
        Map<String, Object> parentLotMap = lotRunCardQueryService.getRCLotInfoMap(parentLotId);

        //check flip type
        String childFlipType = MapUtils.getString(childLotMap, "flipType");
        String parentFlipType = MapUtils.getString(parentLotMap, "flipType");
        this.checkFlip4ChildAndParentLot(parentFlipType, childFlipType);
        this.checkWaitJobs(MapUtils.getLongValue(childLotMap, "carrierRrn"), 0L, 0L, null);
        // 增加卡控parentLot 的cst 是否有未完成的sort job
        this.checkWaitJobs(MapUtils.getLongValue(parentLotMap, "carrierRrn"), 0L, 0L, null);

        List<Unit> childUnitList = wipQueryService.getUnitList(MapUtils.getLong(childLotMap, "lotRrn"));
        List<Unit> parentUnitList = wipQueryService.getUnitList(MapUtils.getLong(parentLotMap, "lotRrn"));

        List<Map> mergedUnits = new ArrayList<>();

        for (Unit unit : parentUnitList) {
            mergedUnits.add(getUnitInfo(unit.getUnitId(), parentLotId, MapUtils.getLong(parentLotMap, "lotRrn")));
        }
        for (Unit unit : childUnitList) {
            mergedUnits.add(getUnitInfo(unit.getUnitId(), childLotId, MapUtils.getLong(childLotMap, "lotRrn")));
        }

        mergedUnits = orderUnitInfoList(mergedUnits);

        request.setAttribute("parentLot", parentLotMap);
        request.setAttribute("childLot", childLotMap);
        request.setAttribute("lotId", lotId);
        request.setAttribute("mergedUnits", parseToJsonString(mergedUnits));

        return mapping.getInputForward();
    }

    /**
     * mergeLot
     */
    public ActionForward mergeLots(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                   HttpServletResponse response) throws Exception {
        String parentLotId = request.getParameter("parentLotId");
        String childLotId = request.getParameter("childLotId");

        Lot parentLot = lotQueryService.getLot(parentLotId);
        Lot childLot = lotQueryService.getLot(childLotId);
        //增加非空验证
        Assert.isFalse(parentLot == null || childLot == null,
                       Errors.create().key(MessageIdList.LOT_LOT_NOT_EXIST_OR_CAST).build());

        Assert.isTrue(LotStatus.isRunCardWaitMerge(parentLot.getLotStatus()) &&
                              LotStatus.isRunCardWaitMerge(childLot.getLotStatus()),
                      Errors.create().key(MessageIdList.LOT_STATUS_NOT_ALLOW).build());

        this.checkQTime(parentLot.getLotRrn(), childLot.getLotRrn());

        //check flip type
        checkFlip4ChildAndParentLot(parentLot.getFlipType(), childLot.getFlipType());

        //按照业务逻辑。合并操作 需要锁 父子
        List<String> lotRrns = new ArrayList<String>(){{
                        add(StringUtils.toString(parentLot.getLotRrn()));
                        add(StringUtils.toString(childLot.getLotRrn()));
        }};
        lotRunCardService.mergeSplitRunCardLot(parentLot, childLot, LocalContext.getUserId(),lotRrns);

        request.setAttribute("lotId", parentLotId);
        request.getRequestDispatcher("splitRunCardLotInfo.do?action=init&lotId=" + parentLotId)
               .forward(request, response);
        return null;
    }

    private Map<String, Object> getUnitInfo(String unitId, String lotId, Long lotRrn) {
        long facilityRrn = LocalContext.getFacilityRrn();
        Unit unit = wipQueryService.getUnit(facilityRrn, unitId);
        Map<String, Object> unitMap = new HashMap<String, Object>();
        unitMap.put("position", unit.getPositionInCarrier());
        unitMap.put("oldPostionInCarrier", unit.getPositionInCarrier());
        unitMap.put("postionInCarrier", unit.getPositionInCarrier());
        unitMap.put("lotId", lotId);
        unitMap.put("lotid", lotId);
        unitMap.put("lotRrn", lotRrn);
        unitMap.put("unitRrn", unit.getUnitRrn());
        unitMap.put("unitId", unit.getUnitId());
        unitMap.put("dummyflag", unit.getDummyFlag());
        return unitMap;
    }

    private List<Map> orderUnitInfoList(List<Map> unitList) {
        List<Map> newList = new ArrayList<Map>();

        int size = unitList.size();
        for (int i = 0; i < size; i++) {
            int seq = -1;
            int position = -1;
            for (int j = 0; j < unitList.size(); j++) {
                Map<String, Object> unit = unitList.get(j);
                if (seq == -1 || MapUtils.getIntValue(unit, "position") < position) {
                    seq = j;
                    position = MapUtils.getIntValue(unit, "position");
                }
            }

            if (seq >= 0) {
                Map<String, Object> unit = unitList.get(seq);
                String unitId = MapUtils.getString(unit, "unitId");
                if (StringUtils.startsWith(unitId, "RC-")) {
                    unitId = StringUtils.substring(unitId, 3);
                    unit.put("unitId", unitId);
                }
                String lotId = MapUtils.getString(unit, "lotId");
                if (StringUtils.contains(lotId, ".MAIN")) {
                    lotId = StringUtils.substring(lotId, 0, StringUtils.indexOf(lotId, ".MAIN"));
                    unit.put("lotId", lotId);
                }
                newList.add(unit);
                unitList.remove(seq);
            }
        }

        return newList;
    }


}