EntityPmScheduleQueryAction.java

package com.mycim.webapp.actions.equipmentpm;

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.MiscUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.ems.EntityCounter;
import com.mycim.valueobject.ems.PmSchedule;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.EmsSetupAction;
import com.mycim.webapp.forms.EntityPmScheduleForm;
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.*;

/**
 * 设备PM 查询
 *
 * @author pinyan.song
 * @version 6.0.0
 * @date 2019-12-4 15:58
 **/
public class EntityPmScheduleQueryAction extends EmsSetupAction {

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

        if (WebUtils.getParameterBoolean("byOpen", request)) {
            request.setAttribute("byOpen", true);
        } else {
            request.removeAttribute("byOpen");
        }

        return mapping.getInputForward();
    }

    public ActionForward query(ActionMapping mapping, EntityPmScheduleForm theform, HttpServletRequest request,
                               HttpServletResponse response) {
        String query = WebUtils.getParameter(Constants.QUERY_KEY, request);
        Long facilityRrn = LocalContext.getFacilityRrn();

        if (StringUtils.equalsIgnoreCase(query, "ACTIVE")) {
            theform.setPmScheduleDateFrom(DateUtils.formatDate(new Date(), DateUtils.DATE_FORMAT4DAY));
        }
        Map<String, Object> conditions = new HashMap<>();
        conditions.put("facilityRrn", facilityRrn);

        long entityRrn = 0L;

        // 计数器的分页查询
        if (StringUtils.isNotBlank(theform.getEquipmentId())) {
            entityRrn = getInstanceRrn(theform.getEquipmentId(), getNamedSpace(ObjectList.ENTITY_KEY, facilityRrn),
                                       ObjectList.ENTITY_KEY);
            Assert.isFalse(entityRrn == 0,
                           Errors.create().key(MessageIdList.EQUIPMENT_ID_NOT_EXIST).content("EQP ID is not exist!")
                                 .build());
            conditions.put("entityRrn", entityRrn);
        }

        Page page = theform.getTimePage();
        page.setPageSize(10);
        page.setPageNo(theform.getTimeThisPage());
        Page resultPage = emsService.getEntityCountersByPageQuery(entityRrn, page);
        List<EntityCounter> pmCounterList = new ArrayList<EntityCounter>(resultPage.getResults());
        theform.setTimePage(resultPage);

        conditions.put("pmScheduleDateFrom", theform.getPmScheduleDateFrom());
        conditions.put("pmScheduleDateTo", theform.getPmScheduleDateTo());
        conditions.put("eventType", "PM");

        Page pmPage = theform.getPmPage();
        pmPage.setPageSize(10);
        pmPage.setPageNo(theform.getPmThisPage());
        Page pmSchedulesByPageQuery = emsService.getPmSchedulesByPageQuery(conditions, pmPage);
        theform.setPmPage(pmSchedulesByPageQuery);
        List<PmSchedule> pmScheduleList = new ArrayList<PmSchedule>(pmSchedulesByPageQuery.getResults());

        conditions.put("eventType", "SQC");
        Page sqcPage = theform.getSqcPage();
        sqcPage.setPageSize(10);
        sqcPage.setPageNo(theform.getSqcThisPage());
        Page sqcSchedulesByPageQuery = emsService.getPmSchedulesByPageQuery(conditions, sqcPage);
        List<PmSchedule> sqcScheduleList = new ArrayList<PmSchedule>(sqcSchedulesByPageQuery.getResults());
        theform.setSqcPage(sqcSchedulesByPageQuery);


        request.setAttribute("pmScheduleList", addSchedulesMessage(pmScheduleList));
        request.setAttribute("sqcScheduleList", addSchedulesMessage(sqcScheduleList));
        request.setAttribute("pmCounterList", addEntityCounterMessage(pmCounterList));

        processItemAction(request, theform);

        return init(mapping, theform, request, response);
    }

    private Object addEntityCounterMessage(List<EntityCounter> pmCounterList) {
        List<EntityCounter> listCounter = new ArrayList<>();
        for (EntityCounter entityCounter : pmCounterList) {
            if ((entityCounter.getChecklistRrn() != null) && (entityCounter.getChecklistRrn() > 0)) {
                entityCounter.setChecklistId(getInstanceId(entityCounter.getChecklistRrn()));
            }
            listCounter.add(entityCounter);
        }

        return listCounter;
    }

    private Object addSchedulesMessage(List<PmSchedule> pmScheduleList) {
        List<PmSchedule> list = new ArrayList<>();
        for (PmSchedule pmSchedule : pmScheduleList) {
            if (MiscUtils.parseSQL(pmSchedule.getChecklistRrn()) > 0) {
                pmSchedule.setChecklistId(getInstanceId(MiscUtils.parseSQL(pmSchedule.getChecklistRrn())));
            }
            if (pmSchedule.getEntityRrn() > 0) {
                pmSchedule.setEquipmentId(getInstanceId(pmSchedule.getEntityRrn()));
            }
            list.add(pmSchedule);
        }
        return list;
    }

}