AsmSetupAction.java

package com.mycim.webapp.actions;

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.context.spring.SpringContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.server.alarm.service.AlarmService;
import com.mycim.server.asm.service.AsmService;
import com.mycim.server.asm.service.WarehouseService;
import com.mycim.server.erp.service.ErpService;
import com.mycim.server.prp.service.PrpService;
import com.mycim.server.security.service.SecurityService;
import com.mycim.server.system.service.SysService;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.prp.Operation;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.forms.WarehouseInfoForm;

import javax.servlet.http.HttpServletRequest;

public class AsmSetupAction extends AbstractAction {

    protected static final String LOT_NUMBER_KEY = "lotNumber";

    protected static final String WAREHOUSE_ID_KEY = "warehouseId";

    protected static final String MATERIAL_ID_KEY = "materialId";

    protected static final String MATERIAL_RRN = "materialRrn";

    protected static final String MATERIAL_DESC = "materialDesc";

    protected static final String SUPPLIER_ID_KEY = "supplierId";

    protected static final String MANUFACTURER_ID_KEY = "manufacturerId";

    protected static final String EXPIRATION_DATE_KEY = "expirationDate";

    protected static final String PRODUCTION_DATE_KEY = "productionDate";

    protected static final String INCOMING_DATE_KEY = "incomingDate";

    protected static final String CHECK_DATE_KEY = "checkDate";

    protected static final String CHECK_USER_KEY = "checkUser";

    protected static final String CHECK_RESULT_KEY = "checkResult";

    protected static final String COMMENTS_KEY = "comments";

    protected static final String RECEIPT_QTY = "receiptQty";

    protected static final String STORE_UOM = "storeUOM";

    protected static final String QTY = "qty";

    protected static final String TRANS_TYPE = "transType";

    protected AsmService asmService = SpringContext.getBean(AsmService.class);

    protected AlarmService alarmService = SpringContext.getBean(AlarmService.class);

    protected WarehouseService warehouseService = SpringContext.getBean(WarehouseService.class);

    protected SecurityService securityService = SpringContext.getBean(SecurityService.class);

    protected SysService sysService = SpringContext.getBean(SysService.class);

    protected PrpService prpService = SpringContext.getBean(PrpService.class);

    protected ErpService erpService = SpringContext.getBean(ErpService.class);
    /**
     * check where date info is correct
     */
    protected void validateDateInfoForInventory(WarehouseInfoForm theform) throws Exception {
        StringBuilder sb = new StringBuilder();
        String addInfo = theform.getExpirationDate();

        if (StringUtils.isNotEmpty(addInfo)) {
            if (!DateUtils.isTimeYearMonthDay(addInfo)) {
                sb.append(I18nUtils.getMessage(MessageIdList.ASM_INVALID_DATE, "失效日期格式不正确,请检查!"));
            } else {
                long availabilityDays =
                        (DateUtils.stringToTimestamp(addInfo).getTime() - System.currentTimeMillis()) / 1000;
                if (availabilityDays <= 0) {
                    sb.append(I18nUtils.getMessage(MessageIdList.ASM_EXPIRY_DATE_INVALID, "失效日期无效,请输入自今日之后的日期!"));
                }
            }
        }

        String productionDate = theform.getProductionDate();
        if (!DateUtils.isTimeYearMonthDay(productionDate)) {
            sb.append(I18nUtils.getMessage(MessageIdList.ASM_INVALID_PRODUCTION_DATE, "生产日期格式不正确,请检查!"));
        } else {
            long availabilityDays =
                    (DateUtils.stringToTimestamp(productionDate).getTime() - System.currentTimeMillis()) / 1000;
            if (availabilityDays >= 0) {
                sb.append(
                        I18nUtils.getMessage(MessageIdList.ASM_EXPIRY_PRODUCTION_DATE_INVALID, "生产日期无效,请输入自今日之前的日期!"));
            }
        }

        String incomingDate = theform.getIncomingDate();
        if (!DateUtils.isTimeYearMonthDay(incomingDate)) {
            sb.append(I18nUtils.getMessage(MessageIdList.ASM_INVALID_INCOMING_DATE, "来料日期格式不正确,请检查!"));
        } else {
            long availabilityDays =
                    (DateUtils.stringToTimestamp(incomingDate).getTime() - System.currentTimeMillis()) / 1000;
            if (availabilityDays >= 0) {
                sb.append(I18nUtils.getMessage(MessageIdList.ASM_EXPIRY_INCOMING_DATE_INVALID, "来料日期无效,请输入自今日之前的日期!"));
            }
        }

        String checkDate = theform.getCheckDate();

        if (!DateUtils.isTimeYearMonthDay(checkDate)) {
            sb.append(I18nUtils.getMessage(MessageIdList.ASM_INVALID_CHECK_DATE, "检查日期格式不正确,请检查!"));
        } else {
            long availabilityDays =
                    (DateUtils.stringToTimestamp(checkDate).getTime() - System.currentTimeMillis()) / 1000;
            if (availabilityDays >= 0) {
                sb.append(I18nUtils.getMessage(MessageIdList.ASM_EXPIRY_CHECK_DATE_INVALID, "检查日期无效,请输入自今日之前的日期!"));
            }
        }

        if (DateUtils.isTimeYearMonthDay(productionDate) && DateUtils.isTimeYearMonthDay(incomingDate) &&
                DateUtils.stringToTimestamp(productionDate).getTime() >
                        DateUtils.stringToTimestamp(incomingDate).getTime()) {
            sb.append(I18nUtils.getMessage(MessageIdList.ASM_PRODUCTION_MUST_LATER_INCOMING, "生产日期不可晚于来料日期!"));
        }
        if (DateUtils.isTimeYearMonthDay(productionDate) && DateUtils.isTimeYearMonthDay(checkDate) &&
                DateUtils.stringToTimestamp(productionDate).getTime() >
                        DateUtils.stringToTimestamp(checkDate).getTime()) {
            sb.append(I18nUtils.getMessage(MessageIdList.ASM_PRODUCTION_MUST_LATER_INSPECTION, "生产日期不可晚于检查日期!"));
        }
        if (DateUtils.isTimeYearMonthDay(incomingDate) && DateUtils.isTimeYearMonthDay(checkDate) &&
                DateUtils.stringToTimestamp(incomingDate).getTime() >
                        DateUtils.stringToTimestamp(checkDate).getTime()) {
            sb.append(I18nUtils.getMessage(MessageIdList.ASM_INCOMING_MUST_LATER_INSPECTION, "来料日期不可晚于检查日期!"));
        }
        String msg = sb.toString();
        Assert.isFalse(StringUtils.isNotEmpty(msg), Errors.create().content(msg).build());
    }

    protected Operation getRequestWarehouse(WarehouseInfoForm theform, HttpServletRequest request) {
        String warehouseId = WebUtils.getParameterUpperCase("warehouseId", request);
        warehouseId = StringUtils.isEmpty(warehouseId) ? theform.getInstanceId() : warehouseId;
        Assert.isFalse(StringUtils.isEmpty(warehouseId), Errors.create().key(MessageIdList.ASM_MISSING_WAREHOUSE_ID)
                                                               .content("Warehouse Id can not be empty!").build());
        Long facilityRrn = LocalContext.getFacilityRrn();
        Operation warehouse = warehouseService.getWarehouse(facilityRrn, warehouseId);
        Assert.isFalse(warehouse == null || warehouse.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.ASM_MISSING_WAREHOUSE).content("Warehouse is not exist!")
                             .build());
        return warehouse;
    }

}