PersonalSpecialLotAction.java

package com.mycim.webapp.actions.lot.personalspeciallot;

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.beans.PropertyUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.framework.utils.msg.JsonUtils;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.security.User;
import com.mycim.valueobject.wip.Lot;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.PersonalSpecialLotForm;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fei.zeng
 * @Description
 * @date 2021/1/22
 **/
public class PersonalSpecialLotAction extends WipSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        if (StringUtils.isNotEmpty(WebUtils.getParameter(Constants.INIT_KEY, request))) {
            return addSpecialLotInit(mapping, form, request, response);
        }
        if (StringUtils.isNotEmpty(WebUtils.getParameter("modifyInit", request))) {
            return mapping.findForward("modifyInit");
        }
        return WebUtils.NULLActionForward;
    }

    public Map queryLotList(Map map) {
        Long facilityRrn = LocalContext.getFacilityRrn();

        Long pageNo = NumberUtils.toLong(MapUtils.getString(map, "pageNo"));
        Long pageSize = NumberUtils.toLong(MapUtils.getString(map, "pageSize"));

        Page page = new Page(pageNo, pageSize);
        Map<String, Object> condition = checkParameter(map, facilityRrn);

        page.setBaseInfo(condition);

        //查询时需排除已经设置过special lot 的批次
        page = lotService.getLotListByParam(page);

        Map msg = new HashMap();
        msg.put("success", "true");
        msg.put("resultInfo", page.getResults());
        msg.put("itemCount", page.getTotalItems());
        if (page.isFirstPage()) {
            msg.put("isFirst", "true");
            if (page.getTotalItems() == 0) {
                msg.put("isLast", "true");
            }
        }
        if (page.isLastPage()) {
            msg.put("isLast", "true");
        }

        return msg;
    }

    public void addSpecialLots(Map map) {
        String userId = LocalContext.getUserId();
        Long userRrn = LocalContext.getUserRrn();

        List specialLotRrns = JsonUtils.toObject((String) map.get("specialLotRrns"), List.class);
        Assert.isFalse(CollectionUtils.isEmpty(specialLotRrns),
                       Errors.create().content("Add Special Lots can not be empty!").build());
        List<Long> lotRrns = new ArrayList<>();
        for (Object object : specialLotRrns) {
            String string = StringUtils.toString(object);
            if (NumberUtils.isNumber(string)) {
                lotRrns.add(NumberUtils.toLong(string));
            }
        }
        lotService.addSpecialLot(lotRrns, userRrn, userId);
    }

    public Map querySpecialLotList(Map map) {
        Long facilityRrn = LocalContext.getFacilityRrn();
        Long userRrn = LocalContext.getUserRrn();
        Long pageNo = NumberUtils.toLong(MapUtils.getString(map, "pageNo"));
        Long pageSize = NumberUtils.toLong(MapUtils.getString(map, "pageSize"));

        Map<String, Object> condition = checkParameter(map, facilityRrn);
        condition.put("userRrn", userRrn);
        Page page = new Page(pageNo, pageSize);
        page.setBaseInfo(condition);

        page = lotService.getLotSpecialListByParam(page);

        Map msg = new HashMap();
        msg.put("success", "true");
        msg.put("resultInfo", page.getResults());
        msg.put("itemCount", page.getTotalItems());
        if (page.isFirstPage()) {
            msg.put("isFirst", "true");
            if (page.getTotalItems() == 0) {
                msg.put("isLast", "true");
            }
        }
        if (page.isLastPage()) {
            msg.put("isLast", "true");
        }

        return msg;
    }

    public void deleteSpecialLots(Map map) {
        String userId = LocalContext.getUserId();
        Long userRrn = LocalContext.getUserRrn();

        List specialLotRrns = JsonUtils.toObject((String) map.get("specialLotRrns"), List.class);
        Assert.isFalse(CollectionUtils.isEmpty(specialLotRrns),
                       Errors.create().content("Delete Special Lots can not be empty!").build());
        List<Long> lotRrns = new ArrayList<>();
        for (Object object : specialLotRrns) {
            String string = StringUtils.toString(object);
            if (NumberUtils.isNumber(string)) {
                lotRrns.add(NumberUtils.toLong(string));
            }
        }
        lotService.deleteSpecialLot(lotRrns, userRrn, userId);
    }

    private ActionForward addSpecialLotInit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                            HttpServletResponse response) {
        User user = new User(LocalContext.getUserId(),
                             getNamedSpace(ObjectList.USER_KEY, LocalContext.getFacilityRrn()), ObjectList.USER_KEY);
        user = securityService.getUser(user);
        PersonalSpecialLotForm personalSpecialLotForm = (PersonalSpecialLotForm) form;
        PropertyUtils.copyProperties(personalSpecialLotForm, user);
        return mapping.findForward(Constants.INIT_KEY);
    }

    private Map<String, Object> checkParameter(Map map, Long facilityRrn) {
        Map<String, Object> condition = new HashMap<>();

        String lotId = MapUtils.getString(map, "lotId").trim();
        if (StringUtils.isNotBlank(lotId)) {
            Lot lot = lotQueryService.getLot(lotId, facilityRrn);
            Assert.isFalse(lot.getLotRrn() <= 0, Errors.create().content("Lot: {} not exist!").args(lotId).build());
            condition.put("lotId", lotId);
        }
        String lotType = MapUtils.getString(map, "lotType").trim();
        String lotStatus = MapUtils.getString(map, "lotStatus").trim();
        String productId = StringUtils.trimToUpperCase(MapUtils.getString(map, "productId"));
        if (StringUtils.isNotBlank(productId)) {
            long productRrn = getInstanceRrn(productId, facilityRrn, ObjectList.PRODUCT_KEY);
            Assert.isFalse(productRrn <= 0, Errors.create().content("Product: {} not exist!").args(productId).build());
            condition.put("productRrn", productRrn);
        }
        String processId = MapUtils.getString(map, "processId").trim();
        if (StringUtils.isNotBlank(processId)) {
            long processRrn = getInstanceRrn(processId, facilityRrn, ObjectList.WFL_KEY);
            Assert.isFalse(processRrn <= 0, Errors.create().content("Process:{} not exist!").args(processId).build());
            condition.put("processRrn", processRrn);
        }
        String processVersion = MapUtils.getString(map, "processVersion").trim();
        Assert.isTrue(NumberUtils.isNumber(processVersion) || StringUtils.isBlank(processVersion),
                      Errors.create().content("processVersion: {},Integer is required !").args(processVersion).build());
        condition.put("processVersion", processVersion);
        condition.put("lotType", lotType);
        condition.put("lotStatus", lotStatus);

        return condition;
    }

}