ErpBomAction.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.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 can.yang
 *
 * @date 2021/11/8
 */
public class ErpBomAction extends AsmSetupAction {
    private static final String MOVE_TYPE_REC = "311";
    private static final String MOVE_TYPE_RET = "312";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        WarehouseInfoForm theform = getForm(form);
        theform.setInstanceId("BOM_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.getReceiveOrReturnBomList(MOVE_TYPE_REC));
        return init(mapping, theform, request, response);
    }

    public ActionForward initReturn(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        request.setAttribute("initReturn", 1);
        WarehouseInfoForm theform = getForm(form);
        theform.setWarehouseInventoryList(erpService.getReceiveOrReturnBomList(MOVE_TYPE_RET));
        request.setAttribute("conversionTypeList", sysService
                .getAllReferenceValue(ReferenceDetailNames.TRANS_MATERIAL_TYPE, "#$#", "data_2_value"));

        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().key(MessageIdList.ERP_SELECT_BOM_RECEIVED).content("Please select the batch of materials to be received!").build());

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

    /**
     * 退回 物料
     */
    public ActionForward returnMaterial(ActionMapping mapping, WarehouseInfoForm form, HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {
        Operation requestWarehouse = getRequestWarehouse(form, request);
        form.setInstanceId(requestWarehouse.getInstanceId());
        form.setNamedSpace(requestWarehouse.getNamedSpace());
        form.setInstanceDesc(requestWarehouse.getInstanceDesc());
        String returnListStr = form.getReturnListStr();
        String[] returnArr = returnListStr.split(StringUtils.SEMICOLON);
        String conversionType = request.getParameter("conversionType");

        Assert.isFalse(returnArr.length == 0,
                       Errors.create().key(MessageIdList.ERP_SELECT_BOM_RETURN).content("Please select the batch of materials to be received!").build());
        erpService.batchReturnBom(new ArrayList<>(Arrays.asList(returnArr)), requestWarehouse, conversionType);

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

}