ScrapOrShipManagerAction.java

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

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.i18n.I18nUtils;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.consts.CompletedssConstants;
import com.mycim.valueobject.ems.Carrier;
import com.mycim.valueobject.wip.BoxForDie;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.TransReason;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.CompletedssLotForm;
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 kang.zhang
 * @date 2019年9月27日
 * @description 完成品的报废出货相关操作 控制器
 */
public class ScrapOrShipManagerAction extends WipSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        return mapping.findForward("init");
    }

    public ActionForward back(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        CompletedssLotForm themform = (CompletedssLotForm) form;
        themform.setCacheShipBoxs("");
        themform.setCacheUnitLists("");
        return mapping.findForward("back");
    }

    public ActionForward getManagerPage(ActionMapping mapping, CompletedssLotForm themform, HttpServletRequest request,
                                        HttpServletResponse response) {
        String language = I18nUtils.getCurrentLanguage().toString();
        String scrap = request.getParameter("scrap");
        String unscrap = request.getParameter("unscrap");
        String ship = request.getParameter("ship");
        String unship = request.getParameter("unship");
        if (StringUtils.isNotBlank(scrap)) {
            Assert.isFalse(isNeedDivideBox(themform),
                           Errors.create().key(MessageIdList.COMPLETEDSSLOT_SEPARATE_BOX_AND_SCRAP)
                                 .content("Please separate the box and then discard!").build());
            themform.setOperationType(CompletedssConstants.COMPLETEDSS_SCRAP);
            if (StringUtils.equalsIgnoreCase("CN", language)) {
                themform.setPageTitle("报废");
            } else {
                themform.setPageTitle("discard");
            }
        } else if (StringUtils.isNotBlank(unscrap)) {
            Assert.isFalse(isNeedDivideBox(themform),
                           Errors.create().key(MessageIdList.COMPLETEDSSLOT_SEPARATE_BOX_THEN_CANCELSCRAP)
                                 .content("Please separate the box and then cancel " + "discard!").build());
            themform.setOperationType(CompletedssConstants.COMPLETEDSS_CANCLE_SCRAP);
            themform.setPageTitle("cancel discard");
        } else if (StringUtils.isNotBlank(ship)) {
            Assert.isFalse(isNeedDivideBox(themform),
                           Errors.create().key(MessageIdList.COMPLETEDSSLOT_SEPARATE_BOX_THEN_SHIP)
                                 .content("Please separate the box and then ship it " + "again!").build());
            themform.setOperationType(CompletedssConstants.COMPLETEDSS_SHIP);
            themform.setPageTitle("shipment");
        } else if (StringUtils.isNotBlank(unship)) {
            Assert.isFalse(isNeedDivideBox(themform),
                           Errors.create().key(MessageIdList.COMPLETEDSSLOT_SEPARATE_BOX_THEN_UNSHIP)
                                 .content("Please separate the box and then return it!").build());
            themform.setOperationType(CompletedssConstants.COMPLETEDSS_CANCLE_SHIP);
            themform.setPageTitle("退货");
        }
        String completedssType = themform.getCompletedssType();
        List<Map<String, Object>> usefulUnits = new ArrayList<Map<String, Object>>();
        if (CompletedssConstants.COMPLETEDSS_TYPE_DIE.equalsIgnoreCase(completedssType)) {// die类型
            usefulUnits = wipQueryService.getLotUnitsAndBoxsInfos(themform.getLotRrn());
            List<BoxForDie> dieBoxs = wipQueryService.getDieBoxsByLotRrn(themform.getLotRrn());
            List<BoxForDie> shipBoxs = new ArrayList<BoxForDie>();
            List<BoxForDie> cancleShipBoxs = new ArrayList<BoxForDie>();
            for (BoxForDie boxForDie : dieBoxs) {
                String boxType = boxForDie.getType();
                String boxStatus = boxForDie.getStatus();
                if (CompletedssConstants.BOX_TYPE_FORSTORAGE.equals(boxType)) {
                    if (CompletedssConstants.BOX_STATUS_FORSHIP.equals(boxStatus) && boxForDie.getRemainDieNum() > 0) {
                        shipBoxs.add(boxForDie);
                    } else if (CompletedssConstants.BOX_STATUS_FORUNSHIP.equals(boxStatus)) {
                        cancleShipBoxs.add(boxForDie);
                    }
                }
            }
            themform.setCacheShipBoxs(WebUtils.getCacheObj2String(shipBoxs));
            themform.setCacheCancleShipBoxs(WebUtils.getCacheObj2String(cancleShipBoxs));
            themform.setCacheUnitLists(WebUtils.getCacheObj2String(usefulUnits));
            request.setAttribute("shipBoxs", shipBoxs);// 出货的box
            request.setAttribute("cancleShipBoxs", cancleShipBoxs);// 退货的box
            request.setAttribute("unitLists", usefulUnits);
        } else {// wafer类型
            Lot lot = new Lot();
            lot.setLotRrn(themform.getLotRrn());
            List<Map<String, Object>> unitsByLot = wipQueryService.getUnitsByLot(lot);
            themform.setCacheUnitLists(WebUtils.getCacheObj2String(unitsByLot));
            request.setAttribute("unitLists", unitsByLot);
        }

        return mapping.findForward("managerPage");
    }

    public ActionForward scrapOrShip(ActionMapping mapping, CompletedssLotForm themform, HttpServletRequest request,
                                     HttpServletResponse response) {
        String user = LocalContext.getUserId();
        Long facilityRrn = LocalContext.getFacilityRrn();
        String scrapLot = request.getParameter("scrapLot");
        String canclescrap = request.getParameter("canclescrap");
        String shipLot = request.getParameter("shipLot");
        String cancleship = request.getParameter("cancleship");
        if (StringUtils.isNotBlank(scrapLot)) {
            // 报废
            themform.setOperationType(CompletedssConstants.COMPLETEDSS_SCRAP);
            if (CompletedssConstants.COMPLETEDSS_TYPE_DIE.equals(themform.getCompletedssType())) {
                return scrapOrShipForDie(mapping, themform, request, user);
            } else {
                return scrapOrShipForWafer(mapping, themform, request, user);
            }
        } else if (StringUtils.isNotBlank(canclescrap)) {
            // 取消报废
            themform.setOperationType(CompletedssConstants.COMPLETEDSS_CANCLE_SCRAP);
            if (CompletedssConstants.COMPLETEDSS_TYPE_DIE.equals(themform.getCompletedssType())) {
                return scrapOrShipForDie(mapping, themform, request, user);
            } else {
                return scrapOrShipForWafer(mapping, themform, request, user);
            }
        } else if (StringUtils.isNotBlank(shipLot)) {
            // 出货
            themform.setOperationType(CompletedssConstants.COMPLETEDSS_SHIP);
            if (CompletedssConstants.COMPLETEDSS_TYPE_DIE.equals(themform.getCompletedssType())) {
                return scrapOrShipForDie(mapping, themform, request, user);
            } else {
                return scrapOrShipForWafer(mapping, themform, request, user);
            }
        } else if (StringUtils.isNotBlank(cancleship)) {
            // 退货
            themform.setOperationType(CompletedssConstants.COMPLETEDSS_CANCLE_SHIP);
            if (CompletedssConstants.COMPLETEDSS_TYPE_DIE.equals(themform.getCompletedssType())) {
                return cancleShipForDie(mapping, themform, request, user);
            } else {
                return cancleShipForWafer(mapping, themform, request, user, facilityRrn);
            }
        }
        return mapping.findForward("init");
    }

    public ActionForward scrapOrShipForDie(ActionMapping mapping, CompletedssLotForm themform,
                                           HttpServletRequest request, String user) {
        if (StringUtils.isNotBlank(themform.getShipBoxId())) {
            themform.setShipBoxId(themform.getShipBoxId().trim());
        }
        String operationType = themform.getOperationType();
        // wafer信息并且包含wafer下面的盒子
        List<Map<String, Object>> usefulUnits = (List<Map<String, Object>>) WebUtils
                .getCacheString2Obj(themform.getCacheUnitLists());
        // 退货的盒子
        List<BoxForDie> cancleShipBoxs = (List<BoxForDie>) WebUtils
                .getCacheString2Obj(themform.getCacheCancleShipBoxs());
        // 被操作数量
        Map<String, Object> operationNumInfo = new HashMap<String, Object>();
        for (Map<String, Object> unitMap : usefulUnits) {
            // 取出unit对应的boxs
            List<BoxForDie> unitBoxs = (List<BoxForDie>) unitMap.get("unitBoxs");
            for (BoxForDie boxForDie : unitBoxs) {
                String boxId = boxForDie.getBoxId();
                String shipNum = request.getParameter(boxId);
                // 将所有box的操作数量放入map中 key为boxid value为操作数量
                operationNumInfo.put(boxId, shipNum);
            }
        }
        // 拿到退货的盒子操作数量并存入map中
        for (BoxForDie boxForDie : cancleShipBoxs) {
            String boxId = boxForDie.getBoxId();
            String shipNum = request.getParameter(boxId);
            operationNumInfo.put(boxId, shipNum);
        }
        // 判断出货盒号是否重复
        if (CompletedssConstants.COMPLETEDSS_SHIP.equals(operationType)) {
            Boolean repeatBoxInLot = isRepeatBoxInLot(themform.getShipBoxId(), themform.getLotRrn());
            Assert.isFalse(repeatBoxInLot, Errors.create().key(MessageIdList.COMPLETEDSSLOT_BOX_NUMBER_EXISTED).content(
                    "The same box number has already existed in the Lot, " + "please retype!").build());

        }
        String completedssType = themform.getCompletedssType();
        Long lotRrn = themform.getLotRrn();
        String comments = themform.getComments();
        // 被操作数量
        Map<String, Object> transInfo = new HashMap<String, Object>();
        transInfo.put("lotRrn", lotRrn);
        transInfo.put("completedssType", completedssType);
        transInfo.put("operationType", operationType);
        transInfo.put("comments", comments);
        transInfo.put("shipBoxId", themform.getShipBoxId());
        transInfo.put("usefulUnits", usefulUnits);
        transInfo.put("cancleShipBoxs", cancleShipBoxs);
        transInfo.put("operationNumInfo", operationNumInfo);
        transInfo.put("user", user);
        lotService.scrapOrShipForDie(transInfo);
        request.removeAttribute("unitLists");
        themform.setCacheUnitLists("");
        return mapping.findForward("success");
    }

    public ActionForward scrapOrShipForWafer(ActionMapping mapping, CompletedssLotForm themform,
                                             HttpServletRequest request, String user) {
        Long lotRrn = themform.getLotRrn();
        String completedssType = themform.getCompletedssType();
        String operationType = themform.getOperationType();
        String comments = themform.getComments();
        lotService.scrapOrShipForWafer(lotRrn, operationType, user, completedssType, comments);
        return mapping.findForward("success");
    }

    public ActionForward cancleShipForDie(ActionMapping mapping, CompletedssLotForm themform,
                                          HttpServletRequest request, String user) {
        List<BoxForDie> shipBoxs = (List<BoxForDie>) WebUtils.getCacheString2Obj(themform.getCacheShipBoxs());
        Map<String, Object> dieNumMap = new HashMap<String, Object>();
        for (BoxForDie boxForDie : shipBoxs) {
            String boxId = boxForDie.getBoxId();
            String canshipNum = request.getParameter(boxId);
            dieNumMap.put(boxId, canshipNum);
        }
        Long lotRrn = themform.getLotRrn();
        String completedssType = themform.getCompletedssType();
        String operationType = themform.getOperationType();
        String comments = themform.getComments();
        String cancleShipBoxId = themform.getCancleShipBoxId();
        Assert.isFalse(StringUtils.isBlank(cancleShipBoxId),
                       Errors.create().key(MessageIdList.COMPLETEDSSLOT_PLEASE_ENTER_RETURNBOX_NUMBER)
                             .content("Please enter the return box number!").build());
        cancleShipBoxId = cancleShipBoxId.toUpperCase().trim();
        Boolean repeatBoxInLot = isRepeatBoxInLot(cancleShipBoxId, lotRrn);
        // 检验盒号
        Assert.isFalse(repeatBoxInLot, Errors.create().key(MessageIdList.COMPLETEDSSLOT_BOX_NUMBER_EXISTED).content(
                "The same box number has already existed in the Lot, " + "please" + " retype!").build());
        Map<String, Object> transInfo = new HashMap<String, Object>();
        transInfo.put("lotRrn", lotRrn);
        transInfo.put("completedssType", completedssType);
        transInfo.put("operationType", operationType);
        transInfo.put("comments", comments);
        transInfo.put("user", user);
        transInfo.put("dieNumMap", dieNumMap);
        transInfo.put("shipBoxs", shipBoxs);
        transInfo.put("cancleShipBoxId", cancleShipBoxId);
        lotService.cancleShipForDie(transInfo);
        return mapping.findForward("success");
    }

    /**
     * 判断盒号是否已存在于批次下
     *
     * @param boxId
     * @param lotRrn
     * @return
     * @throws Exception
     * @date:2018年1月16日 下午3:57:11
     * @author Aiden
     */
    private Boolean isRepeatBoxInLot(String boxId, Long lotRrn) {
        Boolean isRepeat = false;
        List<BoxForDie> dieBoxsByLotRrn = wipQueryService.getDieBoxsByLotRrn(lotRrn);
        for (BoxForDie boxForDie : dieBoxsByLotRrn) {
            String boxIdExist = boxForDie.getBoxId();
            if (boxIdExist.equals(boxId)) {
                isRepeat = true;
                break;
            }

        }
        return isRepeat;
    }

    public ActionForward cancleShipForWafer(ActionMapping mapping, CompletedssLotForm themform,
                                            HttpServletRequest request, String user, Long facilityRrn) {
        String[] cancleShipWaferIds = themform.getCancleShipWaferIds();
        Assert.isFalse(cancleShipWaferIds == null || cancleShipWaferIds.length == 0,
                       Errors.create().key(MessageIdList.COMPLETEDSSLOT_PLEASE_CHOOSE_RETURN_RETURNWAFER)
                             .content("Please " + "choose " + "to" + " " + "return " + "the goods" + " WAFER!")
                             .build());
        Integer childUnitNum = cancleShipWaferIds.length;
        List<Map<String, Object>> unitLists = (List<Map<String, Object>>) WebUtils
                .getCacheString2Obj(themform.getCacheUnitLists());
        // wafer全选时,则是整批退货
        if (childUnitNum == unitLists.size()) {// 整批
            return scrapOrShipForWafer(mapping, themform, request, user);
        } else {// 部分退货
            return cancleShipForWaferPart(mapping, themform, request, user, facilityRrn);
        }
    }

    private ActionForward cancleShipForWaferPart(ActionMapping mapping, CompletedssLotForm themform,
                                                 HttpServletRequest request, String user, Long facilityRrn) {
        String language = I18nUtils.getCurrentLanguage().toString();
        Map<String, Object> valueMap = new HashMap<String, Object>();
        Long lotRrn = themform.getLotRrn();
        String[] cancleShipWaferIds = themform.getCancleShipWaferIds();
        Integer childUnitNum = cancleShipWaferIds.length;
        List<Map<String, Object>> unitLists = (List<Map<String, Object>>) WebUtils
                .getCacheString2Obj(themform.getCacheUnitLists());
        List<Map<String, Object>> allUnits = new ArrayList<Map<String, Object>>();// 全部转换
        for (Map<String, Object> map : unitLists) {
            Map<String, Object> unit = new HashMap<String, Object>();
            unit.put("position", map.get("POSITION_IN_CARRIER").toString());
            unit.put("dummyflag", map.get("DUMMY_FLAG"));
            unit.put("unitRrn", map.get("UNIT_RRN").toString());
            unit.put("lotid", themform.getLotId());
            unit.put("available", "1");
            unit.put("unitId", map.get("UNIT_ID"));
            allUnits.add(unit);
        }
        List<Map<String, Object>> unitsOfParentLot = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> unitsOfChildLot = new ArrayList<Map<String, Object>>();
        for (String waferId : cancleShipWaferIds) {// 分给子批的unit/wafer
            for (Map<String, Object> unit : allUnits) {
                if (waferId.equals(unit.get("unitId"))) {
                    unitsOfChildLot.add(unit);
                    break;
                }
            }
        }
        allUnits.removeAll(unitsOfChildLot);// 移除分出去的,剩下的就是母批的
        unitsOfParentLot = allUnits;
        Lot lot = lotQueryService.getLot(lotRrn);
        // 组装子批信息
        List<Map<String, Object>> childLots = new ArrayList<Map<String, Object>>();
        Map<String, Object> childLot = new HashMap<String, Object>();
        String childLotId = getCancleShipChildLotId(lot.getLotId());
        childLot.put("childLotId", childLotId);
        childLot.put("technologyRrn", lot.getProcessRrn());
        childLot.put("changeProcessFlag", "FALSE");
        childLot.put("intSplitQty", Double.valueOf(childUnitNum));
        childLot.put("units", unitsOfChildLot);
        childLot.put("splitQty", Double.valueOf(childUnitNum));
        Carrier carrier = new Carrier();// carrier信息
        carrier.setInstanceRrn(0L);
        carrier.setObject("ENTITY");
        carrier.setNamedSpace("MYCIM2");
        carrier.setInstanceId("DUMMY" + childLotId);
        carrier.setObjectType("CARRIER");
        carrier.setObjectSubtype("DUMMY");
        childLot.put("carrier", carrier);
        childLots.add(childLot);
        // 组装分批map信息
        lot.setTransPerformedby(user);
        valueMap.put("lot", lot);// 母批
        valueMap.put("childLots", childLots);// 子批
        valueMap.put("splitType", "permanent");
        valueMap.put("subSplitType", "");//
        valueMap.put("unitOfParent", unitsOfParentLot);// 母批unit
        valueMap.put("comment", themform.getComments());
        valueMap.put("facilityRrn", facilityRrn);
        valueMap.put("user", user);
        valueMap.put("reason", themform.getComments());

        String tranReasonStr = "";
        if (StringUtils.equalsIgnoreCase("CN", language)) {
            tranReasonStr = "批次退货";
        } else {
            tranReasonStr = "Lot return";
        }
        TransReason transReason = new TransReason();
        transReason.setReasonCode("UNSHIP");
        transReason.setReason(tranReasonStr);
        transReason.setResponsibility(user);
        transReason.setTransQty1(Double.valueOf(childUnitNum));
        valueMap.put("transReason", transReason);
        valueMap.put("isCancleShipLot", true);
        valueMap.put("lotRrn",String.valueOf(lot.getLotRrn()));
        // 执行分批
        lotService.splitLotWithTerminate(valueMap);
            /*ActionForward forward = mapping.findForward("back");
            ActionForward newForward = new ActionForward(forward);
            newForward.setPath(forward.getPath() + "?lotId=" + childLotId);*/
        request.setAttribute("lotId", childLotId);
        return mapping.findForward("success");
    }

    private String getCancleShipChildLotId(String lotId) {
        String childLotId = "";
        if (lotId.indexOf(".") == -1) {
            lotId = lotId + ".1";
        }
        lotId = lotQueryService.getMaxLotId(lotId.substring(0, lotId.indexOf(".") + 1));
        if (lotId.indexOf(".") == -1) {// 没有子批
            childLotId = lotId + ".01";
        } else {// 有子批
            childLotId = lotId.substring(0, lotId.indexOf("."));
            String lotEndStr = lotId.substring(lotId.indexOf(".") + 1, lotId.length());
            lotEndStr = (Integer.valueOf(lotEndStr) + 1) + "";
            if (lotEndStr.length() > 1) {
                childLotId = childLotId + "." + lotEndStr;
            } else {
                childLotId = childLotId + ".0" + lotEndStr;
            }
        }
        return childLotId;
    }

    /**
     * 判断是否需要卡控 先分盒 再 操作
     *
     * @param form
     * @return
     */
    public boolean isNeedDivideBox(CompletedssLotForm form) {
        String completedssType = form.getCompletedssType();
        Integer isDivideBox = form.getIsDivideBox();
        Integer isDivideBoxYes = Integer.valueOf(1);
        if (CompletedssConstants.COMPLETEDSS_TYPE_DIE.equals(completedssType) && !isDivideBoxYes.equals(isDivideBox)) {
            return true;
        }
        return false;
    }

}