WarehouseConsumptionAction.java

package com.mycim.webapp.actions.warehouse;

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.framework.utils.lang.math.NumberUtils;
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.WarehouseInventoryDO;
import com.mycim.valueobject.prp.Operation;
import com.mycim.webapp.actions.AsmSetupAction;
import com.mycim.webapp.forms.WarehouseInfoForm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库房消耗
 *
 * @author yanbing.chen
 * @date 2019/9/3
 * @since 1.8
 **/
public class WarehouseConsumptionAction extends AsmSetupAction {


    public String manualConsume(Map map) throws Exception {
        long facilityRrn = LocalContext.getFacilityRrn();
        String userId = LocalContext.getUserId();
        String warehouseId = map.get(WAREHOUSE_ID_KEY).toString().toUpperCase();
        String materialId = map.get(MATERIAL_ID_KEY).toString().toUpperCase();
        List<Map<String, Object>> cunsumptionList = (List<Map<String, Object>>) map.get("cunsumptionList");
        Assert.isFalse(StringUtils.isEmpty(warehouseId) || StringUtils.isEmpty(materialId) || cunsumptionList == null ||
                               cunsumptionList.isEmpty(),
                       Errors.create().key(MessageIdList.ASM_PARAMETER_MISSING).content("参数丢失,请重新登陆!").build());

        warehouseService.consumeMaterialFromWarehouse(facilityRrn, userId, warehouseId, materialId, cunsumptionList);

        return I18nUtils.getMessage(MessageIdList.ASM_MANUAL_CONSUMPTION_SUCCESS);
    }

    public Map queryMaterialForCunsumption(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();

        List<Map<String, String>> cunsumptionList = warehouseService
                .getLotInventoryListForConsumption(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("cunsumptionList", cunsumptionList);
        return result;
    }

    public void checkQuantityOfWarning(WarehouseInfoForm theform) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String id = "";
        MaterialDO material = null;
        Map<String, Object> resultMap = new HashMap<String, Object>();
        // error
        id = theform.getMaterialId();
        material = asmService.getAndCheckMaterial(facilityRrn, id);
        // warning
        if (material.getQuantityOfWarning() != null) {
            id = theform.getWarehouseId();
            Operation warehouse = warehouseService.getWarehouse(facilityRrn, id);
            warehouseService
                    .checkWarehouseInventoryQuantityOfWarning(warehouse.getInstanceRrn(), material.getInstanceRrn(),
                                                              material.getQuantityOfWarning(),
                                                              NumberUtils.toDouble(theform.getTotalQty().toString()));
        }
    }

    public Map checkQuantityOfWarningForCode(WarehouseInfoForm theform) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String id = "";
        MaterialDO material = null;
        // error
        id = theform.getMaterialId();
        material = asmService.getAndCheckMaterial(facilityRrn, id);
        // warning
        Map<String, Object> result = new HashMap<>();
        if (material.getQuantityOfWarning() != null) {
            id = theform.getWarehouseId();
            Operation warehouse = warehouseService.getWarehouse(facilityRrn, id);
            Double totalConsumeQty = NumberUtils.toDouble(theform.getTotalQty().toString());
            Double quantityOfWarning = material.getQuantityOfWarning();
            WarehouseInventoryDO warehouseInventory = warehouseService
                    .checkWarehouseInventoryQuantityOfWarningForCode(warehouse.getInstanceRrn(),
                                                                     material.getInstanceRrn());

            double totalInWarehouse = warehouseInventory.getTotalQuantity();
            double afterConsumeTotal = totalInWarehouse - totalConsumeQty;
            if (afterConsumeTotal < 0) {
                String message = "消耗物料后,库存数量将低于现有库存数量(" + String.format("%.2f", totalInWarehouse) + "), " +
                        String.format("%.2f", afterConsumeTotal) + ", 请先补充物料!";
                result.put("message", message);
                result.put("code", 10000);
            } else {
                if (quantityOfWarning != 0 && afterConsumeTotal < quantityOfWarning) {
                    double difference = quantityOfWarning - afterConsumeTotal;
                    String message = "消耗物料后,库存数量将低于安全库存数量(" + String.format("%.2f", quantityOfWarning) + "), " +
                            String.format("%.2f", difference) + ", 请及时补充物料!";
                    result.put("message", message);
                    result.put("code", 20000);
                }
            }
        }
        return result;
    }

}