LotOutFactoryEditAction.java

package com.mycim.webapp.actions.outfactory.outsourcing;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.exception.SystemIllegalArgumentException;
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.time.DateUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.consts.TransactionNames;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.LotStatus;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.outfactory.LotOutFactoryForm;
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.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author liuji.li
 * @version 6.0.0
 * @date 2019/10/24
 **/
public class LotOutFactoryEditAction extends WipSetupAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        return initLotOutFactory(mapping, form, request, response);
    }

    public ActionForward initLotOutFactory(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                           HttpServletResponse response) {
        LotOutFactoryForm lotOutFactoryForm = (LotOutFactoryForm) request.getAttribute("lotOutFactoryForm");
        if (lotOutFactoryForm == null) {
            lotOutFactoryForm = (LotOutFactoryForm) form;
        }
        List<Map> ofoLots = (List<Map>) WebUtils.getCacheString2Obj(lotOutFactoryForm.getCacheCurrentlots());
        if (ofoLots == null) {
            ofoLots = new ArrayList<>();
        }
        request.setAttribute("ofoLots", ofoLots);
        request.setAttribute("outFactoryRrn", lotOutFactoryForm.getOutFactoryRrn());
        return mapping.getInputForward();
    }

    public ActionForward addOfoLots(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                    HttpServletResponse response) {
        LotOutFactoryForm lotOutFactoryForm = (LotOutFactoryForm) form;
        String lotOfoId = lotOutFactoryForm.getLotOfoId();
        String outFactoryPlanTime = lotOutFactoryForm.getOutFactoryPlanTime();
        List<Map> ofoLots = (List<Map>) WebUtils.getCacheString2Obj(lotOutFactoryForm.getCacheCurrentlots());
        if (ofoLots == null) {
            ofoLots = new ArrayList<>();
        }
        if (StringUtils.isNotBlank(lotOfoId)) {
            Lot lot = lotQueryService.getLot(lotOfoId.trim().toUpperCase());
            long lotRrn = lot.getLotRrn();
            Map<String, Object> lotMap = new HashMap<String, Object>();
            Assert.isFalse(lotRrn < 1, Errors.create().key(MessageIdList.LOTOUTFACTORY_LOTID_ERROR)
                                             .content("lot Id does not exist!").build());
            String lotStatus = lot.getLotStatus();
            Assert.isFalse(StringUtils.isBlank(lotStatus) || !lotStatus.equalsIgnoreCase(LotStatus.BANKED),
                           Errors.create().key(MessageIdList.LOTOUTFACTORY_STATUS_ERROR)
                                 .content("The Lot state must be BANKED!").build());
            // 检查是否重复添加
            for (Map<String, Object> map : ofoLots) {
                long lotRrnE = MapUtils.getLongValue(map, "lotRrn");
                Assert.isFalse(lotRrnE == lotRrn, Errors.create().key(MessageIdList.LOTOUTFACTORY_NOT_REPEAT)
                                                        .content("Please do not repeat the submission!").build());
            }
            lotMap.put("lot", lot);
            lotMap.put("lotId", lot.getLotId());
            lotMap.put("productId", lot.getProductId());
            lotMap.put("processId", lot.getProcessId());
            lotMap.put("lotRrn", lotRrn);
            lotMap.put("opComments", lotOutFactoryForm.getOpComments());
            if (StringUtils.isNotBlank(outFactoryPlanTime)) {
                SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATE_FORMAT4DAY);
                Date planDate = null;
                try {
                    planDate = format.parse(outFactoryPlanTime);
                } catch (Exception e) {
                    throw new SystemIllegalArgumentException(
                            Errors.create().key(MessageIdList.LOTOUTFACTORY_DATA_WRONG).content("Date format is wrong!")
                                  .build());
                }
                Timestamp planTs = new Timestamp(planDate.getTime());
                Timestamp currentTime = new Timestamp(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
                if (planTs.before(currentTime)) {
                    throw new SystemIllegalArgumentException(Errors.create().key(MessageIdList.LOTOUTFACTORY_TIME_ERROR)
                                                                   .content(
                                                                           "The expected return time should not" + " " +
                                                                                   "be less than the current time!")
                                                                   .build());
                }
                lotMap.put("outFactoryPlanTime", planTs);
            }
            ofoLots.add(lotMap);
            lotOutFactoryForm.setCacheCurrentlots(WebUtils.getCacheObj2String(ofoLots));
            lotOutFactoryForm.setLotOfoId("");
            lotOutFactoryForm.setOutFactoryPlanTime("");
            lotOutFactoryForm.setOpComments("");
            request.setAttribute("ofoLots", ofoLots);
            request.setAttribute("outFactoryRrn", lotOutFactoryForm.getOutFactoryRrn());
        }
        return mapping.getInputForward();
    }

    public ActionForward deleteLot(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                   HttpServletResponse response) {
        LotOutFactoryForm lotOutFactoryForm = (LotOutFactoryForm) form;
        List<Map> ofoLots = (List<Map>) WebUtils.getCacheString2Obj(lotOutFactoryForm.getCacheCurrentlots());
        String deleteLotRrn = request.getParameter("deleteLotRrn");
        for (Map<String, Object> map : ofoLots) {
            String lotRrn = map.get("lotRrn").toString();
            if (lotRrn.equals(deleteLotRrn)) {
                ofoLots.remove(map);
                break;
            }
        }
        lotOutFactoryForm.setCacheCurrentlots(WebUtils.getCacheObj2String(ofoLots));
        request.setAttribute("ofoLots", ofoLots);
        request.setAttribute("outFactoryRrn", lotOutFactoryForm.getOutFactoryRrn());
        return mapping.getInputForward();
    }

    public ActionForward fsendLotOutFactory(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                            HttpServletResponse response) {
        String user = LocalContext.getUserId();
        LotOutFactoryForm lotOutFactoryForm = (LotOutFactoryForm) form;
        List<Map> ofoLots = (List<Map>) WebUtils.getCacheString2Obj(lotOutFactoryForm.getCacheCurrentlots());
        Map<String, Object> transInfos = new HashMap();
        Assert.isFalse(CollectionUtils.isEmpty(ofoLots),
                       Errors.create().key(MessageIdList.LOTOUTFACTORY_LOTS_WRONG).content("Please add lot first!")
                             .build());
        for (Object lot : ofoLots) {
            Map lotMap = (Map) lot;
            // TODO erp
            //  erpService.lotInfoForSubcon(MapUtils.getString(lotMap, "lotId"), user, facilityRrn);
        }
        transInfos.put("user", user);
        transInfos.put("ofoRrn", lotOutFactoryForm.getOutFactoryRrn());
        transInfos.put("transId", TransactionNames.TRANSID_FSEND);
        transInfos.put("lots", ofoLots);
        lotService.fsendLots(transInfos);

        request.setAttribute("operationSuccess", true);
        lotOutFactoryForm.setLotOfoId("");
        lotOutFactoryForm.setOutFactoryPlanTime("");
        lotOutFactoryForm.setOpComments("");
        request.setAttribute("outFactoryRrn", lotOutFactoryForm.getOutFactoryRrn());
        request.setAttribute("ofoLots", new ArrayList<>());
        lotOutFactoryForm.setCacheCurrentlots(WebUtils.getCacheObj2String(new ArrayList<>()));
        return mapping.getInputForward();
    }

}