markNumberAction.java

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

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.beans.BeanUtils;
import com.mycim.framework.utils.beans.PropertyUtils;
import com.mycim.framework.utils.lang.StringUtils;
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.SessionNames;
import com.mycim.valueobject.wip.Lot;
import com.mycim.valueobject.wip.Unit;
import com.mycim.webapp.Constants;
import com.mycim.webapp.TemplateLocation;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.WipSetupAction;
import com.mycim.webapp.forms.lot.LotInfoForm;
import com.mycim.webapp.forms.lot.LotMarkNumberInfoForm;
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhi.cai
 * @version 6.0.0
 * @date 2020/3/6
 **/
public class markNumberAction extends WipSetupAction {

    public static final String MARK_NUMBER_KEY = "MARKNUMBER_KEY";

    public static final String MARK_NUMBER_IMPORT = "MARKNUMBER_IMPORT";

    public static final String MARK_NUMBER_SUFFIX = "_MARKNUMBER";

    public static final String MARK_NUMBER_COMMENT_SUFFIX = "_MARKNUMBERCOMMENT";


    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {

        // redirect to the login page if there is no session
        long facilityRrn = LocalContext.getFacilityRrn();
        if (form == null) {
            form = new LotInfoForm();
        }

        LotMarkNumberInfoForm theform = (LotMarkNumberInfoForm) form;

        theform.setPages(new Integer(1));

        String lotId = request.getParameter("lotId");

        Lot lot = lotQueryService.getLot(lotId, facilityRrn);


        Assert.notNull(lot, Errors.create().key(MessageIdList.LOT_ID_EMPTY).content("The lot is empty!").build());


        if (StringUtils.isNotBlank((String) request.getAttribute("target"))) {
            theform.setCacheTarget((String) request.getAttribute("target"));
        }

        // Copy value object properties to form bean.
        LotMarkNumberInfoForm lotInfoForm = new LotMarkNumberInfoForm();

        PropertyUtils.copyProperties(lotInfoForm, lot);
        Map conditionMap = BeanUtils.copyBeanToMap(lot);
        initLotBaseInfoForJsp(request, true, null, null);
        theform.setUnitListJson(parseToJsonString(wipQueryService.getUnitListByLot(lot.getLotRrn(), false)));
        //theform.setSourceListValues(parseToJsonString(wipQueryService.getUnitListByLot(lot.getLotRrn())));
        //lotInfoForm.setSourceListValues(theform.getSourceListValues());
        request.setAttribute(SessionNames.COLLECTION2_KEY, parseToSimplifyCollection(theform.getUnitListJson()));

        lotInfoForm.setRemainQty(lot.getQty1());


        theform.setCacheLot(WebUtils.getCacheObj2String(lot));

        request.setAttribute(SessionNames.COLLECTION_KEY, new ArrayList());
        theform.setTransId(Constants.MODIFY_KEY);
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, lotInfoForm);
        theform.setCacheLotInfo(WebUtils.getCacheObj2String(lotInfoForm));

        return mapping.findForward(Constants.INIT_KEY);
    }


    public Map updateUnitMarkNumber(Map params) {
        Map result = new HashMap();
        long facilityRrn = LocalContext.getFacilityRrn();
        String lotId = MapUtils.getString(params, "lotId").trim();
        Lot lot = lotQueryService.getLot(lotId, facilityRrn);
        List<Map> unitListMap = wipQueryService.getUnitListByLot(lot.getLotRrn());
        List<Unit> unitList = unitListMap.stream().map(map -> {
            String unitId = MapUtils.getString(map, "unitId");
            long unitRrn = MapUtils.getLong(map, "unitRrn");
            String markNumber = MapUtils.getString(params, unitId + MARK_NUMBER_SUFFIX);
            String markNumberComment = MapUtils.getString(params, unitId + MARK_NUMBER_COMMENT_SUFFIX);
            map.put("markNumber", markNumber);
            Unit unit = wipQueryService.getUnit(unitRrn);
            unit.setMarkNumber(markNumber);
            unit.setMarkNumberComment(markNumberComment);
            return unit;
        }).collect(Collectors.toList());
        wipService.updateUnitMarkNumber(unitList, MARK_NUMBER_KEY);
        result.put("success", "success");
        return result;
    }

    public Map importFromExcel(Map params) {
        Map result = new HashMap();
        long facilityRrn = LocalContext.getFacilityRrn();
        List<List<String>> list = (List) params.get("Sheet1");
        String lotId = String.valueOf(list.get(0).get(1)).trim();
        Lot lot = lotQueryService.getLot(lotId, facilityRrn);
        List<Unit> unitList = new ArrayList<>();
        Assert.isFalse(lot == null || lot.getLotRrn() < 1, Errors.create().key(MessageIdList.MARKNUMBER_NOT_FIND_LOT)
                                                                 .content("Cannot find this lot:{},Please check " +
                                                                                  "lot ID").args(lotId).build());
        for (int i = 2; i < list.size(); i++) {
            if (list.get(i) == null || list.get(i).size() == 0) {
                continue;
            }
            String unitId = String.valueOf(list.get(i).get(0));
            String markNumber = String.valueOf(list.get(i).get(1));
            String markNumberComment = String.valueOf(list.get(i).get(2));
            Unit unit = wipQueryService.getUnit(LocalContext.getFacilityRrn(), unitId);
            unit.setLotRrn(lot.getLotRrn());
            unit.setUnitId(unitId);
            unit.setMarkNumber(markNumber);
            unit.setMarkNumberComment(markNumberComment);
            unitList.add(unit);
        }
        wipService.updateUnitMarkNumber(unitList, MARK_NUMBER_IMPORT);
        result.put("success", "success");
        return result;
    }


    public ActionForward exportWaferMarkNumber(ActionMapping mapping, LotMarkNumberInfoForm theform,
                                               HttpServletRequest request,
                                               HttpServletResponse response) throws Exception {
        String language = I18nUtils.getCurrentLanguage().toString();
        Lot lot = (Lot) WebUtils.getCacheString2Obj(theform.getCacheLot());
        List<Map> unitList = wipQueryService.getUnitListByLot(lot.getLotRrn());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String fileName = "WaferMarkNumber_" + DateUtils.getNowTime(DateUtils.DATE_FORMAT4NOSPLICING) + ".xlsx";
        Map<String, Object> titles = WebUtils.getExportTitles(request);
        if (StringUtils.equalsIgnoreCase("CN", language)) {
            titles.put("lotId", theform.getLotId());
            titles.put("lotIdTitle", "批次号");
            titles.put("waferIdTitle", "晶圆号");
            titles.put("markNumberTitle", "刻号");
            titles.put("markNumberCommentTitle", "备注");
        } else {
            titles.put("lotId", theform.getLotId());
            titles.put("lotIdTitle", "Lot ID");
            titles.put("waferIdTitle", "Wafer ID");
            titles.put("markNumberTitle", "Mark Number");
            titles.put("markNumberCommentTitle", "Comment");
        }
        WebUtils.exportExcel(fileName, titles, unitList, TemplateLocation.WAFER_MARK_NUMBER_TEMPLATE, response);
        return WebUtils.NULLActionForward;
    }

    public ActionForward qryMarkNumberHistoryDetail(ActionMapping mapping, LotMarkNumberInfoForm theform,
                                                    HttpServletRequest request, HttpServletResponse response) {
        String transRrn = request.getParameter("transRrn");
        String lotRrn = request.getParameter("lotRrn");
        List<Map> list = wipQueryService.getUnitListHistory(new Long(transRrn));
        Lot lot = lotQueryService.getLot(lotRrn);
        LotMarkNumberInfoForm lotInfoForm = new LotMarkNumberInfoForm();
        PropertyUtils.copyProperties(lotInfoForm, lot);

        theform.setUnitListJson(parseToJsonString(list));
        request.setAttribute(SessionNames.COLLECTION2_KEY, parseToSimplifyCollection(theform.getUnitListJson()));
        request.setAttribute(SessionNames.LOT_INFO_FORM_KEY, lotInfoForm);
        theform.setCacheLotInfo(WebUtils.getCacheObj2String(lotInfoForm));
        theform.setCacheLot(WebUtils.getCacheObj2String(lot));

        return mapping.findForward("history");
    }

}