WarehouseInventoryManagementAction.java

package com.mycim.webapp.actions.warehouse;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.jdbc.Page;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.consts.ReferenceDetailNames;
import com.mycim.valueobject.consts.ReferenceFileConst;
import com.mycim.valueobject.inv.LotInventoryDO;
import com.mycim.valueobject.inv.MaterialDO;
import com.mycim.valueobject.prp.Operation;
import com.mycim.valueobject.security.User;
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.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 库房库存物料管理
 *
 * @author yanbing.chen
 * @date 2019/9/3
 * @since 1.8
 **/
public class WarehouseInventoryManagementAction extends AsmSetupAction {

    @Override
    public String inItMethod() {
        return "viewInventory";
    }

    public Map queryInventory(Map map) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String warehouseId = MapUtils.getString(map, "instanceId");
        String materialId = MapUtils.getString(map, "materialId");
        String lotNumber = MapUtils.getString(map, "lotNumber");
        Assert.isFalse(StringUtils.isEmpty(warehouseId) || StringUtils.isEmpty(materialId),
                       Errors.create().key(MessageIdList.ASM_PARAMETER_MISSING).content("参数丢失,请重新登陆!").build());
        // 获取库房信息
        Operation warehouse = warehouseService.getWarehouse(facilityRrn, warehouseId);

        // 获取物料信息
        MaterialDO material = asmService.getMaterial(facilityRrn, materialId);

        int pageNo = NumberUtils.toInt(map.get("currentPage").toString());
        if (pageNo <= 0) {
            pageNo = 1;
        }
        Page page = new Page();
        page.setPageSize(30);
        page.setPageNo(pageNo);

        page = warehouseService
                .pageLotInventoryListInWarehouse(page, material.getInstanceRrn(), warehouse.getInstanceRrn(),
                                                 lotNumber);

        Map<String, Object> result = new HashMap<>();

        result.put("materialDesc", material.getInstanceDesc());
        result.put("materialType", sysService
                .referenceDetailExchangeNull(ReferenceDetailNames.MATERIAL_TYPE, material.getObjectSubtype(),
                                             ReferenceFileConst.DATA_1_VALUE));
        result.put("materialSubType", sysService
                .referenceDetailExchangeNull(ReferenceDetailNames.MATERIAL_SUB_TYPE, material.getItemClass(),
                                             ReferenceFileConst.DATA_1_VALUE));
        result.put("storeUOM", sysService
                .referenceDetailExchangeNull(ReferenceDetailNames.UNIT_OF_MEASURE_FOR_BANK, material.getStoreUom(),
                                             ReferenceFileConst.DATA_1_VALUE));
        result.put("pageSize", page.getNextPage());
        result.put("pageNum", page.getPageNo());
        result.put("maxPage", page.getTotalPages());
        result.put("startRowNum", page.getStartRow());

        result.put("isFirstPage", page.isFirstPage());
        result.put("hasPreviousPage", page.isHasPrePage());
        result.put("isLastPage", page.isLastPage());
        result.put("hasNextPage", page.isHasNextPage());

        result.put("inventoryList", page.getResults());
        return result;
    }

    public ActionForward viewInventory(ActionMapping mapping, HttpServletRequest request, WarehouseInfoForm theform) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String warehouseId = StringUtils.trimToUpperCase(theform.getInstanceId());
        String materialId = StringUtils.trimToUpperCase(theform.getMaterialId());
        String lotNumber = WebUtils.getParameter(LOT_NUMBER_KEY, request);

        // 获取库房信息
        Operation warehouse = warehouseService.getWarehouse(facilityRrn, warehouseId);

        // 获取物料信息
        MaterialDO material = asmService.getMaterial(facilityRrn, materialId);

        LotInventoryDO lotInventory = warehouseService
                .getLotInventory(lotNumber, material.getInstanceRrn(), warehouse.getInstanceRrn());
        lotInventory = lotInventory == null ? new LotInventoryDO() : lotInventory;
        theform.setMaterialDesc(material.getInstanceDesc());

        theform.setLotNumber(lotInventory.getLotNumber());
        theform.setTotalQty(lotInventory.getTotalQuantity());
        SimpleDateFormat formatter = new SimpleDateFormat(DateUtils.DATE_FORMAT4DAY);
        String receiptDate = "";
        if (lotInventory.getReceiptDate() != null) {
            receiptDate = formatter.format(lotInventory.getReceiptDate());
        }
        theform.setReceiptDate(receiptDate);
        String productionDate = "";
        if (lotInventory.getProductionDate() != null) {
            productionDate = formatter.format(lotInventory.getProductionDate());
        }
        theform.setProductionDate(productionDate);
        String incomingDate = "";
        if (lotInventory.getIncomingDate() != null) {
            incomingDate = formatter.format(lotInventory.getIncomingDate());
        }
        theform.setIncomingDate(incomingDate);
        String checkDate = "";
        if (lotInventory.getCheckDate() != null) {
            checkDate = formatter.format(lotInventory.getCheckDate());
        }
        theform.setCheckDate(checkDate);
        theform.setCheckUser(lotInventory.getCheckUser());
        theform.setCheckResult(lotInventory.getCheckResult());
        theform.setStatus(lotInventory.getStatus());

        String expirationDate = "";
        if (lotInventory.getExpirationDate() != null) {
            expirationDate = formatter.format(lotInventory.getExpirationDate());
        }
        theform.setExpirationDate(expirationDate);
        theform.setSupplierId(lotInventory.getAttributeData1());
        theform.setManufacturerId(lotInventory.getAttributeData2());

        return mapping.findForward(Constants.VIEW_KEY);
    }

    public ActionForward modify(ActionMapping mapping, WarehouseInfoForm theform) throws Exception {
        long facilityRrn = LocalContext.getFacilityRrn();
        String id = StringUtils.trimToUpperCase(theform.getInstanceId());
        Operation warehouse = warehouseService.getWarehouse(facilityRrn, id);

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

        String lotNumber = StringUtils.trimToUpperCase(theform.getLotNumber());

        String userInfo = StringUtils.trimToUpperCase(theform.getCheckUser()).split(" ")[0];
        User user = securityService.getUser(userInfo, facilityRrn);
        userInfo = StringUtils.isEmpty(user.getUserName()) ? userInfo : userInfo + " " + user.getUserName();
        validateDateInfoForInventory(theform);


        LotInventoryDO lotInventory = warehouseService
                .getLotInventory(lotNumber, material.getInstanceRrn(), warehouse.getInstanceRrn());

        lotInventory.setCheckUser(StringUtils.isEmpty(userInfo) ? lotInventory.getCheckUser() : userInfo);

        lotInventory.setExpirationDate(DateUtils.stringToTimestamp(theform.getExpirationDate()));
        lotInventory.setProductionDate(DateUtils.stringToTimestamp(theform.getProductionDate()));
        lotInventory.setIncomingDate(DateUtils.stringToTimestamp(theform.getIncomingDate()));
        lotInventory.setCheckDate(DateUtils.stringToTimestamp(theform.getCheckDate()));
        lotInventory.setCheckResult(theform.getCheckResult());

        lotInventory.setAttributeData1(theform.getSupplierId());
        lotInventory.setAttributeData2(theform.getManufacturerId());

        warehouseService.updateLotInventory(lotInventory);

        theform.setLotNumber(lotInventory.getLotNumber());
        theform.setTotalQty(lotInventory.getTotalQuantity());
        SimpleDateFormat formatter = new SimpleDateFormat(DateUtils.DATE_FORMAT4DAY);
        String receiptDate = "";
        if (lotInventory.getReceiptDate() != null) {
            receiptDate = formatter.format(lotInventory.getReceiptDate());
        }
        theform.setReceiptDate(receiptDate);
        theform.setStatus(lotInventory.getStatus());

        String expirationDate = "";
        if (lotInventory.getExpirationDate() != null) {
            expirationDate = formatter.format(lotInventory.getExpirationDate());
        }
        theform.setExpirationDate(expirationDate);
        theform.setSupplierId(lotInventory.getAttributeData1());
        theform.setManufacturerId(lotInventory.getAttributeData2());

        return mapping.findForward(Constants.VIEW_KEY);
    }

    public ActionForward delete(ActionMapping mapping, WarehouseInfoForm theform) {
        long facilityRrn = LocalContext.getFacilityRrn();
        Assert.isFalse(StringUtils.isEmpty(theform.getInstanceId()) || StringUtils.isEmpty(theform.getMaterialId()) ||
                               StringUtils.isEmpty(theform.getLotNumber()),
                       Errors.create().key(MessageIdList.ASM_PARAMETER_MISSING).content("参数丢失,请重新登陆!").build());

        String warehouseId = StringUtils.trimToUpperCase(theform.getInstanceId());
        String materialId = StringUtils.trimToUpperCase(theform.getMaterialId());
        String lotNumber = StringUtils.trimToUpperCase(theform.getLotNumber());

        // 获取库房信息
        Operation warehouse = warehouseService.getWarehouse(facilityRrn, warehouseId);

        // 获取物料信息
        MaterialDO material = asmService.getMaterial(facilityRrn, materialId);

        warehouseService.deleteLotInventory(lotNumber, material.getInstanceRrn(), warehouse.getInstanceRrn());

        return mapping.findForward(Constants.MEMBERS_KEY);
    }

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

}