EntityPmScheduleViewMonthAction.java

/*
 *        @ Copyright 2001 FA Software;
 *        All right reserved. No part of this program may be reproduced or
 *        transmitted in any form or by any means, electronic or
 *        mechanical, including photocopying, recording, or by any
 *        information storage or retrieval system without written
 *        permission from FA Software, except for inclusion of brief
 *        quotations in a review.
 */
package com.mycim.webapp.actions.equipmentpm;


import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.time.DateUtils;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.ems.Entity;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.EmsSetupAction;
import com.mycim.webapp.forms.EntityPmScheduleMonthForm;
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.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

/**
 * 设备Pm 日历查看
 *
 * @author pinyan.song
 * @version 6.0.0
 * @date 2019-12-5 14:46
 **/
public class EntityPmScheduleViewMonthAction extends EmsSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        return cancel(mapping, form, request, response);
    }

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        EntityPmScheduleMonthForm theform = (EntityPmScheduleMonthForm) form;
        if (StringUtils.isNotEmpty(theform.getPageFrom())) {
            return mapping.findForward(theform.getPageFrom());
        } else {
            return super.cancel(mapping, form, request, response);
        }
    }

    public ActionForward view(ActionMapping mapping, EntityPmScheduleMonthForm theform, HttpServletRequest request,
                              HttpServletResponse response) {
        GregorianCalendar gc;
        if (request.getAttribute("gregorianCalendar") == null) {
            gc = new GregorianCalendar();
            request.setAttribute("gregorianCalendar", gc);
            request.setAttribute("queryTime", DateUtils.getNowTime());
        } else {
            gc = (GregorianCalendar) request.getAttribute("gregorianCalendar");
        }

        Entity entity = new Entity(theform.getEquipmentId(),
                                   getNamedSpace(ObjectList.ENTITY_KEY, LocalContext.getFacilityRrn()),
                                   ObjectList.ENTITY_KEY);
        entity = (Entity) getInstance(entity);

        theform.setEquipmentDesc(entity.getInstanceDesc());
        theform.setNamedSpace(entity.getNamedSpace());
        theform.setObjectType(entity.getObjectType());

        long ownerEntityRrn = 0L;
        getData(entity, ownerEntityRrn, request, gc);
        return mapping.findForward("success");
    }

    public ActionForward prev(ActionMapping mapping, EntityPmScheduleMonthForm theform, HttpServletRequest request,
                              HttpServletResponse response) {
        return processTime(mapping, theform, request, response, -1);
    }

    public ActionForward next(ActionMapping mapping, EntityPmScheduleMonthForm theform, HttpServletRequest request,
                              HttpServletResponse response) {
        return processTime(mapping, theform, request, response, 1);
    }

    private ActionForward processTime(ActionMapping mapping, EntityPmScheduleMonthForm theform,
                                      HttpServletRequest request, HttpServletResponse response, int time) {
        String queryTime = WebUtils.getParameter("queryTime", request);
        Date date = DateUtils.parse(queryTime);
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);

        gc.add(Calendar.MONTH, time);
        request.removeAttribute("scheduleMonthMap");
        request.setAttribute("gregorianCalendar", gc);
        request.setAttribute("queryTime", DateUtils.formatDate(gc.getTime()));
        return view(mapping, theform, request, response);
    }

    private void getData(Entity entity, long ownerEntityRrn, HttpServletRequest request, GregorianCalendar gc) {
        Map map = emsService
                .getPMScheduleByMonth(entity.getInstanceId(), gc.get(Calendar.YEAR), gc.get(Calendar.MONTH) + 1,
                                      ownerEntityRrn);
        request.setAttribute("scheduleMonthMap", this.getPmDetailString(map));
    }

}