ErpMaterialAction.java

package com.mycim.webapp.actions.material;

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.ReferenceDetailNames;
import com.mycim.valueobject.consts.ReferenceFileConst;
import com.mycim.valueobject.inv.MaterialDO;
import com.mycim.valueobject.inv.value.WarehouseNames;
import com.mycim.valueobject.prp.Operation;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.AsmSetupAction;
import com.mycim.webapp.forms.WarehouseInfoForm;
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 yibing.liu
 * @version 1.0
 * @date 2021/5/25
 */
public class ErpMaterialAction extends AsmSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        WarehouseInfoForm theform = getForm(form);
        theform.setInstanceId("WAFER_BANK");
        Operation warehouse = getRequestWarehouse(theform, request);
        request.setAttribute("instanceId", warehouse.getInstanceId());
        theform.setInstanceId(warehouse.getInstanceId());
        theform.setNamedSpace(warehouse.getNamedSpace());
        theform.setInstanceDesc(warehouse.getInstanceDesc());
        if (request.getAttribute("initReceive") != null){
            return mapping.findForward("receive");
        }
        if (request.getAttribute("initReturn") != null) {
            return mapping.findForward("return");
        }
        Map<String, String> condition = new HashMap<>();
        condition.put("FROM_TYPE", "ERP");
        List<Map<String, String>> warehouseInventoryList = warehouseService
                .getWarehouseInventoryListForMaterialWithExt(warehouse.getInstanceRrn(), condition);
        theform.setWarehouseInventoryList(warehouseInventoryList==null?new ArrayList<>():warehouseInventoryList);
        theform.setMaterialId("");
        theform.setItemClass("");
        theform.setMaterialType("");
        theform.setTransformType("");

        theform.setTransId(Constants.MODIFY_KEY);

        return mapping.findForward("init");
    }

    public ActionForward initReceive(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        request.setAttribute("initReceive", 1);
        WarehouseInfoForm theform = getForm(form);
        theform.setWarehouseInventoryList(erpService.getCanReceiveMaterialList());
        return init(mapping, theform, request, response);
    }

    public ActionForward initReturn(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        request.setAttribute("initReturn", 1);

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


    /**
     * 接收 SAP 同步的物料 至 MES 的物料仓库
     */
    public ActionForward receiveMaterial(ActionMapping mapping, WarehouseInfoForm theform, HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {
        Operation warehouse = getRequestWarehouse(theform, request);
        theform.setInstanceId(warehouse.getInstanceId());
        theform.setNamedSpace(warehouse.getNamedSpace());
        theform.setInstanceDesc(warehouse.getInstanceDesc());
        String receiveListStr = theform.getReceiveListStr();
        String[] receiveArr = receiveListStr.split(StringUtils.SEMICOLON);
        Assert.isFalse(receiveArr.length == 0,
                       Errors.create().content("Please select the batch of materials to be received!").build());

        erpService.batchReceiveMaterial(new ArrayList<>(Arrays.asList(receiveArr)), warehouse);
        WebUtils.setSuccessMsg(request);
        return init(mapping, theform, request, response);
    }

    /**
     * 退回 SAP 的物料请求至中间表
     */
    public ActionForward returnMaterial(ActionMapping mapping, WarehouseInfoForm form, HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {
        return init(mapping, form, request, response);
    }

    public Map queryMaterialForConversion(WarehouseInfoForm theform) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String id = theform.getWarehouseId();
        Operation warehouse = warehouseService.getWarehouse(facilityRrn, id);

        id = theform.getMaterialId();
        MaterialDO material = asmService.getAndCheckMaterial(facilityRrn, id);

        String lotNumber = theform.getLotNumber();
        String transType = theform.getTransType();

        List<Map<String, String>> conversionList = warehouseService
                .getLotInventoryListForConversion4Erp(lotNumber, material.getInstanceRrn(), warehouse.getInstanceRrn());
        Map<String, Object> result = new HashMap<>();

        result.put(MATERIAL_DESC, material.getInstanceDesc());
        result.put(STORE_UOM, sysService
                .referenceDetailExchangeNull(ReferenceDetailNames.UNIT_OF_MEASURE_FOR_BANK, material.getStoreUom(),
                                             ReferenceFileConst.DATA_1_VALUE));
        result.put("conversionType", sysService
                .getAllReferenceValue(ReferenceDetailNames.TRANS_MATERIAL_TYPE, transType, "data_2_value"));
        result.put("conversionList", conversionList);
        return result;
    }

    public String materialConvert(Map map) throws Exception {
        long facilityRrn = LocalContext.getFacilityRrn();
        String userId = LocalContext.getUserId();
        String warehouseId = map.get("warehouseId").toString();
        String materialId = map.get("materialId").toString();
        String sourceType = map.get("sourceType").toString();
        List<Map<String, Object>> conversionList = (List<Map<String, Object>>) map.get("conversionList");
        Assert.isFalse(StringUtils.isEmpty(warehouseId) || StringUtils.isEmpty(materialId) || conversionList == null ||
                               conversionList.isEmpty(),
                       Errors.create().key(MessageIdList.ASM_PARAMETER_MISSING).content("参数丢失,请重新登陆!").build());
        // warehouseService
        //         .convertMaterialFromWarehouse(facilityRrn, userId, warehouseId, materialId, sourceType, conversionList);
        for (Map<String,Object> m:conversionList){
            m.put("key1", sourceType);
        }
        erpService.recordMaterialReturnLog(warehouseId, materialId, sourceType, conversionList);

        return I18nUtils.getMessage(MessageIdList.ASM_TYPE_CONVERSION_SUCCESS);
    }
}