SdsMesProductMappingAction.java

package com.mycim.webapp.actions.workOrder;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
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.erp.workOrder.SdsWorkOrderInfo;
import com.mycim.valueobject.erp.workOrder.SdsWorkOrderMaterialInfo;
import com.mycim.valueobject.prp.WorkOrder;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.PrpSetupAction;
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;

/**
 * @Desc:
 * @Author: zengf
 * @Date: 2020/9/18
 */

public class SdsMesProductMappingAction extends PrpSetupAction {

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

        return WebUtils.NULLActionForward;
    }

    public void productMapping(ActionMapping mapping, HttpServletRequest request, HttpServletResponse response,
                               Map map) {
        String userId = LocalContext.getUserId();
        String productId = StringUtils.trim(MapUtils.getString(map, "productId"));
        String sdsProductId = StringUtils.trim(MapUtils.getString(map, "sdsProductId"));
        Assert.isFalse(StringUtils.isEmpty(productId),
                       Errors.create().content("MES Product ID cannot be empty!").build());
        Assert.isFalse(prpService.getProductRrnById(productId) < 1,
                       Errors.create().content("MES Product ID not exist!").build());
        Assert.isFalse(StringUtils.isEmpty(sdsProductId),
                       Errors.create().content("SDS Product ID cannot be empty!").build());

        workOrderService.insertProductMapping(productId, sdsProductId, userId);
    }

    public Map query(ActionMapping mapping, HttpServletRequest request, HttpServletResponse response, Map map) {
        String serchworkorderId = (String) map.get("workorderId");
        List<SdsWorkOrderInfo> sdsWorkOrderInfos = workOrderService.getSdsWorkOrderInfoByWorkOrderId(serchworkorderId);
        List<Map> result = new ArrayList<>();
        Map<String, String[]> orderToProductCatch = new HashMap<String, String[]>();
        Map<String, Object> showInfo;
        for (SdsWorkOrderInfo sdsWorkOrderInfo : sdsWorkOrderInfos) {
            showInfo = new HashMap<>();
            String workorderId = sdsWorkOrderInfo.getWorkorderId();
            if (orderToProductCatch.get("workorderId") == null) {
                WorkOrder workOrderById = workOrderService.getWorkOrderById(workorderId);
                String productId = workOrderById == null ? "" : workOrderById.getProductId();
                if (StringUtils.isEmpty(productId)) {
                    List<String> pds = workOrderService.getMesProductIdBySdsProductId(sdsWorkOrderInfo.getSdsProduct());
                    orderToProductCatch.put(workorderId, pds.toArray(new String[0]));
                } else {
                    orderToProductCatch.put(workorderId, new String[]{productId});
                }

            }
            showInfo.put("workorderIdToProduct", orderToProductCatch.get(workorderId));
            showInfo.put("workorderRrn", sdsWorkOrderInfo.getWorkorderRrn());
            showInfo.put("workorderId", workorderId);
            showInfo.put("workorderPriority", sdsWorkOrderInfo.getWorkorderPriority());
            showInfo.put("sdsProduct", sdsWorkOrderInfo.getSdsProduct());
            showInfo.put("startDayStr", sdsWorkOrderInfo.getStartDayStr());
            showInfo.put("requireDayStr", sdsWorkOrderInfo.getRequireDayStr());
            showInfo.put("orderDieQty", sdsWorkOrderInfo.getOrderDieQty());
            showInfo.put("dayStr", sdsWorkOrderInfo.getDayStr());
            showInfo.put("dueDateStr", sdsWorkOrderInfo.getDueDateStr());
            result.add(showInfo);
        }
        Map resultMsg = new HashMap<>();

        resultMsg.put("showInfos", result);
        return resultMsg;
    }

    public ActionForward showSdsWaferInfo(ActionMapping mapping, HttpServletRequest request) {
        long workorderRrn = NumberUtils.toLong(request.getParameter("workorderRrn"), 0L);
        List<SdsWorkOrderMaterialInfo> list = workOrderService.getSdsMaterialInfoByWorkOrderId(workorderRrn);
        request.setAttribute("list", list);
        return mapping.findForward("sdsWorkorderMaterial");
    }

    public void mappingReceive(ActionMapping mapping, HttpServletRequest request, HttpServletResponse response,
                               Map map) {
        List<HashMap> workorderIds = JsonUtils.toList(MapUtils.getString(map, "workorderIds"), HashMap.class);
        Assert.isFalse(CollectionUtils.isEmpty(workorderIds), Errors.create().content("所选工单不能为空!").build());
        Map<String, String> checkMap = new HashMap<String, String>();
        for (Map map2 : workorderIds) {
            String wId = MapUtils.getString(map2, "workorderId");
            Assert.isFalse(StringUtils.isEmpty(MapUtils.getString(map2, "mesProduct")),
                           Errors.create().content("Check product," + "workorderId:{}").args(wId).build());
            if (checkMap.get(wId) == null) {
                checkMap.put(wId, MapUtils.getString(map2, "mesProduct"));
            } else {
                Assert.isTrue(
                        StringUtils.equals(MapUtils.getString(map2, "mesProduct"), MapUtils.getString(checkMap, wId)),
                        Errors.create().content("Check product,workorderId:").args(wId).build());
            }
        }
        workOrderService.mappingReceive(workorderIds);
    }

    public Map querySdsMes(ActionMapping mapping, HttpServletRequest request, HttpServletResponse response, Map map) {
        String productId = (String) map.get("productId");
        List<Map> sdsMesInfos = workOrderService.getSdsMesMappingInfoByProductId(productId);

        Map resultMsg = new HashMap<>();
        resultMsg.put("showInfos", sdsMesInfos);
        return resultMsg;
    }

    public void deleteProductMapping(Map map) {
        String sdsproduct = MapUtils.getString(map, "sdsproduct");
        String mesproduct = MapUtils.getString(map, "mesproduct");
        Assert.isFalse(workOrderService.checkAreadySdsProReceive(sdsproduct),
                       Errors.create().content("{} aready reiveed,can not delete!").args(sdsproduct).build());
        workOrderService.deleteProductMapping(mesproduct, sdsproduct);
    }

}