BatchBankInAction.java

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

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.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.NamedObject;
import com.mycim.valueobject.consts.CarrierType;
import com.mycim.valueobject.consts.TransactionNames;
import com.mycim.valueobject.ems.Carrier;
import com.mycim.valueobject.prp.Operation;
import com.mycim.valueobject.prp.ProcessPlanning;
import com.mycim.valueobject.prp.ProcessVersion;
import com.mycim.valueobject.prp.ProductVersion;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.valueobject.wip.TransReason;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.BankInfoForm;
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 can.yang
 * @date 2021.7.29
 */
public class BatchBankInAction extends WipSetupAction {
    private final String WARE_HOUSE_ID = "WAFER START BANK";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        BankInfoForm theform = (BankInfoForm) form;
        theform.setOldOperationId(WARE_HOUSE_ID);
        theform.setBankName(WARE_HOUSE_ID);
        return mapping.getInputForward();
    }

    public Map<String, Object> queryProcess(Map map) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String productId = StringUtils.trimToUpperCase(MapUtils.getString(map, "productId"));
        Assert.isFalse(StringUtils.isBlank(productId),
                       Errors.create().key(MessageIdList.BATCHFUTUREHOLDLOT_SELECT_PRODUCTID)
                             .content("The Part Have Not Select Or KeyIn!").build());

        long productRrn = getInstanceRrn(productId, getNamedSpace(ObjectList.PRODUCT_KEY, facilityRrn),
                                         ObjectList.PRODUCT_KEY);
        Assert.isFalse(productRrn <= 0,
                       Errors.create().key(MessageIdList.PRODUCT_PRODUCT_MISSING).content("产品不存在!").build());

        // List<Map> processIdList = new ArrayList<>();
        List<String> processIds = prpService.getProcessIdsByProductRrn(productRrn);
        /*for (String processId : processIds) {
            Map processIdMap = new HashMap(processIds.size());
            processIdMap.put("processId", processId);
            processIdMap.put("processRrn", getInstanceRrn(processId, facilityRrn, ObjectList.WFL_KEY));
            processIdList.add(processIdMap);
        }*/
        List<ProductVersion> productVersions = productService.getProductVersions(productRrn);

        List<Map<String, String>> buildProductVersions = new ArrayList<>();
        productVersions.forEach(productVersion -> {
            String key = String.valueOf(productVersion.getInstanceVersion());
            String text = key + "(" + productVersion.getVersionStatus() + ")";
            Map<String, String> m = new LinkedHashMap<>(2);
            m.put("key", key);
            m.put("text", text);
            buildProductVersions.add(m);
        });

        Map<String, Object> result = new HashMap();
        result.put("processIds", processIds);
        result.put("productId", productId);
        result.put("productRrn", productRrn);
        result.put("productVersions", buildProductVersions);
        return result;
    }

    public Map<String, Object> queryLots(Map map) {
        long facilityRrn = LocalContext.getFacilityRrn();
        Map actionMap = new HashMap();
        String productId = MapUtils.getString(map, "productId");
        if (StringUtils.isNotEmptyTrim(productId)) {
            productId = productId.trim();
            long productRrn = this.getInstanceRrn(productId, facilityRrn, ObjectList.PRODUCT_KEY);
            Assert.isFalse(productRrn <= 0,
                           Errors.create().key(MessageIdList.PRODUCT_PRODUCT_MISSING).content("Product not exist!")
                                 .build());
            actionMap.put("productRrn", new Long(productRrn));
        }
        String processId = MapUtils.getString(map, "processId");
        if (StringUtils.isNotEmptyTrim(processId)) {
            processId = processId.trim();
            long processRrn = this.getInstanceRrn(processId, this.getNamedSpace("WFL", facilityRrn), "WFL", "ROUTE");
            Assert.isFalse(processRrn <= 0,
                           Errors.create().key(MessageIdList.PROCESS_PROCESS_MISSING).content("Process not exist!")
                                 .build());
            actionMap.put("processRrn", new Long(processRrn));
        }
        Integer processVer = MapUtils.getInteger(map, "processVersion");
        actionMap.put("processVersion", processVer);
        Integer productVer = MapUtils.getInteger(map, "productVersion");
        actionMap.put("productVersion", productVer);
        String lotId = MapUtils.getString(map, "lotId");
        if (StringUtils.isNotEmptyTrim(lotId)){
            lotId = StringUtils.replace(lotId, "*", "%").toString().trim().toUpperCase();
            actionMap.put("lotId", lotId);
        }

        String operationId = MapUtils.getString(map, "operationId");
        if (StringUtils.isNotEmptyTrim(operationId)) {
            operationId = operationId.trim();
            long operationRrn = this
                    .getInstanceRrn(operationId, this.getNamedSpace("OPERATION", LocalContext.getFacilityRrn()),
                                    "OPERATION");
            Assert.isFalse(operationRrn <= 0,
                           Errors.create().key(MessageIdList.INSTRUCTIONEDIT_OPERATION_MISSING).content("步骤号不存在!")
                                 .build());
            actionMap.put("operationRrn", new Long(operationRrn));
        }
        Assert.isFalse(MapUtils.isEmpty(actionMap),
                       Errors.create().key(MessageIdList.BATCHBANKMUST_SELECT_ITEM).content("Must select one Item!")
                             .build());
        actionMap.put("lotStatus", LotStatus.HOLD);
        List<Map> batchBankInLots = lotQueryService.getBatchBankInLots(actionMap);
        Map<String, Object> result = new HashMap();
        result.put("lots", batchBankInLots);

        return result;
    }

    public Map<String, Object> batchBankInLotInfo(ActionMapping mapping, HttpServletRequest request, Map map) {
        List<Map> bankInLots = (List<Map>) map.get("dataArray");
        Assert.isFalse(bankInLots.size() <= 0,
                       Errors.create().key(MessageIdList.BATCHBANKSELECT_LOT).content("Please select lot id!").build());
        List<Map> batchBankInInfos = new ArrayList<>();
        ArrayList<String> lotRrns = new ArrayList<>();

        for (Map lots : bankInLots) {
            long lotRrn = MapUtils.getLongValue(lots, "lotRrn");
            Lot lot = lotQueryService.getLot(lotRrn);
            validateLot(lot);
            lots.putAll(map);
            Map bankMap = bankInfo(lots, lot);
            lotRrns.add(StringUtils.toString(lot.getLotRrn()));
            batchBankInInfos.add(bankMap);
        }
        lotService.batchBankIn(batchBankInInfos, lotRrns);
        Map<String, Object> result = new HashMap();
        result.put("bankInfos", WebUtils.getCacheObj2String(batchBankInInfos));
        return result;
    }

    private Map bankInfo(Map map, Lot lot) {
        String user = LocalContext.getUserId();
        long facilityRrn = LocalContext.getFacilityRrn();
        long operationRrn = getInstanceRrn(MapUtils.getString(map, "oldOperationId"), facilityRrn,
                                           ObjectList.OPERATION_KEY);
        Map transInfo = new HashMap();
        transInfo.put("user", user);
        transInfo.put("referenceDocument", MapUtils.getString(map, "referenceDocument"));
        transInfo.put("comments", MapUtils.getString(map, "transComments"));
        transInfo.put("warehouseRrn", operationRrn);
        transInfo.put("transId", TransactionNames.BANKIN_KEY);
        TransReason transReason = new TransReason();
        transInfo.put("transReason", transReason);
        List listRrnLots = new ArrayList();

        listRrnLots.add(new Long(lot.getLotRrn()));

        transInfo.put("lots", listRrnLots);
        return transInfo;
    }

    private void validateLot(Lot lot) {
        long facilityRrn = LocalContext.getFacilityRrn();
        long operationRrn = getInstanceRrn(WARE_HOUSE_ID, facilityRrn, ObjectList.OPERATION_KEY);
        Operation operation = prpService.getOperation(operationRrn);
        Assert.isTrue(lot.getLotRrn() != 0,
                      Errors.create().key(MessageIdList.BANK_INVALID_LOT).content("Invalid lot Id!").build());
        Assert.isFalse(operation == null || operation.getStoreFlag() == null ||
                               operation.getStoreFlag().trim().equalsIgnoreCase("0"),
                       Errors.create().key(MessageIdList.BANK_NOT_STEP)
                             .content("Not found step or step not store step!").build());

        NamedObject namedObject = baseService.isExisted(operationRrn);
        String operationSubType = namedObject.getObjectSubtype();
        Assert.isFalse(operationSubType == null || !"LOT_BANK".equalsIgnoreCase(operationSubType),
                       Errors.create().key(MessageIdList.BANK_HOUSE_NOT_BANK)
                             .content("This warehouse is not a lot bank!").build());
        Assert.isTrue(StringUtils.equalsIgnoreCase(lot.getLotStatus(), LotStatus.HOLD),
                      Errors.create().key(MessageIdList.BANK_STATUS_NOT_HOLD)
                            .content("The lot is not hold and can not be bank in!").build());
        Carrier carrier = carrierService.getCarrier(lot.getCarrierRrn());
        Assert.isFalse(carrier == null || carrier.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.BANK_NOT_CARRIER)
                             .content("Can not find this carrier of " + "Lot!").build());

        Assert.isFalse(StringUtils.equalsIgnoreCase(carrier.getObjectSubtype(), CarrierType.DUMMY),
                       Errors.create().key(MessageIdList.BANK_CARRIER_REMOVED)
                             .content("This carrier of Lot is already removed!").build());
        //check sortJob
        checkWaitJobs(lot.getCarrierRrn(), 0L, 0L, null);
        Assert.isFalse(diffBatchQueryService.checkLotInBatch(lot.getLotRrn()),
                       Errors.create().key(MessageIdList.BANK_LOT_BATCH).content("Lot in batch!").build());
        //check post future hold
        checkPostFutureHold(lot.getLotRrn());

    }

    public List<Map<String, String>> getProcessVersionByProcess(Map map) {
        List<Map<String, String>> result = new ArrayList<>();
        String processId = MapUtils.getString(map, "processId");
        if (StringUtils.isNotBlank(processId)) {
            processId = processId.trim();
            long processRrn = this
                    .getInstanceRrn(processId, this.getNamedSpace("WFL", LocalContext.getFacilityRrn()), "WFL",
                                    "ROUTE");
            Assert.isFalse(processRrn <= 0,
                           Errors.create().key(MessageIdList.PROCESS_PROCESS_MISSING).content("Process not exist!")
                                 .build());
            ProcessPlanning processPlanning = new ProcessPlanning(processRrn);
            List<ProcessVersion> processVersions = prpService.getProcessVersions(processPlanning);
            Assert.isFalse(CollectionUtils.isEmpty(processVersions),
                           Errors.create().key(MessageIdList.PROCESS_NO_VERSION)
                                 .content("There is no version under the " + "process!").build());
            processVersions.forEach(processVersion -> {
                String key = String.valueOf(processVersion.getInstanceVersion());
                String text = key + "(" + processVersion.getVersionStatus() + ")";
                Map<String, String> m = new LinkedHashMap<>(2);
                m.put("key", key);
                m.put("text", text);
                result.add(m);
            });
        }
        return result;
    }


}