DoChecklistAction.java

package com.mycim.webapp.actions.checklist;


import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.beans.PropertyUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.SessionNames;
import com.mycim.valueobject.ems.ChecklistJob;
import com.mycim.valueobject.ems.ChecklistJobData;
import com.mycim.valueobject.ems.Entity;
import com.mycim.valueobject.ems.PmSchedule;
import com.mycim.valueobject.prp.BORResource;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.EmsSetupAction;
import com.mycim.webapp.forms.DoChecklistInfoForm;
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.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author pinyan.song
 * @version 6.0.0
 * @date 2019-11-29 17:41
 **/
public class DoChecklistAction extends EmsSetupAction {
    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) {
        DoChecklistInfoForm theform = (DoChecklistInfoForm) form;
        ChecklistJob checklistJob = getChecklistJob(theform);
        theform.setChecklistJobState(checklistJob.getChecklistJobState());
        request.setAttribute(SessionNames.CHECKLISTJOB_KEY, checklistJob);
        return mapping.getInputForward();
    }

    public ActionForward cancelCheck(ActionMapping mapping, DoChecklistInfoForm theform, HttpServletRequest request,
                                     HttpServletResponse response) throws Exception {

        ChecklistJob checklistJob = getChecklistJob(theform);
        Entity entity = new Entity(theform.getEntityId(),
                                   getNamedSpace(ObjectList.ENTITY_KEY, LocalContext.getFacilityRrn()),
                                   ObjectList.ENTITY_KEY);
        entity = (Entity) getInstance(entity);
        entity.setPmschedules(emsService.getPmSchedulesOfEntity(entity.getInstanceRrn()));

        checklistJob.setChecklistJobState(CANCELED);
        checklistJob.setCompletedTimestamp(new Timestamp(System.currentTimeMillis()));
        String user = LocalContext.getUserId();
        checklistJob.setTransPerformedby(user);
        checklistJob.setCompletePerformedBy(user);
        checklistJob.setComments(theform.getCancelComments());

        emsService.cancelCheckListJob(entity.getInstanceRrn(), checklistJob);

        updatePmNextTriggerTime(checklistJob, entity);

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


    public ActionForward completed(ActionMapping mapping, DoChecklistInfoForm theform, HttpServletRequest request,
                                   HttpServletResponse response) {
        ChecklistJob checklistJob = getChecklistJob(theform);
        String id = theform.getEntityId().trim().toUpperCase();
        Entity entity = new Entity(id, getNamedSpace(ObjectList.ENTITY_KEY, LocalContext.getFacilityRrn()),
                                   ObjectList.ENTITY_KEY);
        entity = (Entity) getInstance(entity);
        Assert.isFalse(entity == null || entity.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("{} 没有找到对象!").args("EQP")
                             .build());
        // set pmschedule of entity
        entity.setPmschedules(emsService.getPmSchedulesOfEntity(entity.getInstanceRrn()));

        completeChecklistJob(checklistJob, entity);
        updatePmNextTriggerTime(checklistJob, entity);

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

    public ActionForward view(ActionMapping mapping, DoChecklistInfoForm theform, HttpServletRequest request,
                              HttpServletResponse response) {
        ChecklistJob checklistJob = getChecklistJob(theform);
        ChecklistJobData checklistJobData = getChecklistItemAndSetSequence(checklistJob, request);
        PropertyUtils.copyProperties(theform, checklistJobData);
        setBorResources(checklistJobData, request);
        String dataValue = checklistJobData.getDataValue();

        Assert.isFalse(dataValue == null,
                       Errors.create().key(MessageIdList.ENTITYPM_INVALID_CHECKLIST_DATA).content("此检查单没有数据").build());

        StringTokenizer st = new StringTokenizer(dataValue, ",", false);
        request.setAttribute("dataValue", st);

        return (mapping.findForward("dochecklistitementry"));
    }

    public ActionForward done(ActionMapping mapping, DoChecklistInfoForm doChecklistInfoForm,
                              HttpServletRequest request, HttpServletResponse response) {
        ChecklistJob checklistJob = getChecklistJob(doChecklistInfoForm);
        ChecklistJobData checklistJobData = getChecklistItemAndSetSequence(checklistJob, request);

        PropertyUtils.copyProperties(doChecklistInfoForm, checklistJobData);

        assert checklistJobData != null;

        if (checklistJobData.getConsumedTransRrn() != null) {
            Long dcolRrn = checklistJobData.getDcolRrn();
            boolean result = (dcolRrn != null && dcolRrn > 0) || (checklistJobData.getParameterSetRrn() == null) ||
                    checklistJobData.getParameterSetRrn() == 0L;
            if (result) {
                checklistJobData.setCompletedFlag("1");
                checklistJobData.setCompletedTimestamp(new Timestamp(System.currentTimeMillis()).toString());
                emsService.updateChecklistJobData(checklistJobData);
                return init(mapping, doChecklistInfoForm, request, response);
            } else {
                //todo 看逻辑,为了获取dcolRrn需要去edcEditAction. 可优化.根据逻辑,目前parameterRrn永远为null或者0,此else不会触发
                request.setAttribute("parameterSetRrn", checklistJobData.getParameterSetRrn());
                return mapping.findForward("dochecklistitemdcol");
            }
        } else {
            setBorResources(checklistJobData, request);
            return mapping.findForward("dochecklistitementry");
        }
    }

    private void setBorResources(ChecklistJobData checklistJobData, HttpServletRequest request) {
        List<BORResource> borResources = new ArrayList<>();
        if ((checklistJobData.getBorRrn() != null) && (checklistJobData.getBorRrn() > 0)) {
            borResources = this.getBorResources(checklistJobData);
        }
        request.setAttribute(SessionNames.COLLECTION_KEY, borResources);
    }

    private void completeChecklistJob(ChecklistJob checklistJob, Entity entity) {
        checklistJob.setChecklistJobState(COMPLETED);

        checklistJob.setCompletedTimestamp(new Timestamp(System.currentTimeMillis()));

        checklistJob.setTransPerformedby(LocalContext.getUserId());
        checklistJob.setCompletePerformedBy(LocalContext.getUserId());
        checklistJob.setTransId(Constants.COMPLETED_KEY);

        boolean logRights = false;
        if (StringUtils.equalsIgnoreCase(ObjectList.CARRIER_KEY, checklistJob.getEntityType())) {
            logRights = true;
        } else {
            logRights = emsService
                    .checkLogRights(checklistJob.getEntityId(), getInstanceId(checklistJob.getEndEventRrn()),
                                    LocalContext.getUserRrn(), ObjectList.EQUIPMENT_KEY);
        }

        Assert.isTrue(logRights, Errors.create().key(MessageIdList.ENTITYPM_NO_ACCESS_OR_STATUS_ERROR)
                                       .content("你无权登录该事件或设备状态与该事件不符,不能进行检查作业").build());

        emsService.completeChecklistJob(checklistJob, LocalContext.getUserRrn());
    }

    private void updatePmNextTriggerTime(ChecklistJob checklistJob, Entity entity) {
        if (checklistJob.getSequenceNumber() != null && checklistJob.getSequenceNumber() > 0) {
            boolean exist = false;
            List<PmSchedule> pmSchedules = (List<PmSchedule>) entity.getPmschedules();
            PmSchedule pmSchedule = new PmSchedule();
            for (PmSchedule p : pmSchedules) {
                if (p.getSequenceNumber().equals(checklistJob.getSequenceNumber())) {
                    pmSchedule = p;
                    pmSchedule.setEntityRrn(entity.getInstanceRrn());
                    exist = true;
                    break;
                }
            }

            if (exist) {
                if ("1".equalsIgnoreCase(pmSchedule.getBasedOnPmFlag()) ||
                        "Y".equalsIgnoreCase(pmSchedule.getBasedOnPmFlag())) {
                    // to update next pm time
                    pmSchedule = emsService.updateNextPMTime(pmSchedule);
                    checklistJob.setChecklistJobState(STARTED);
                    // to update quartz
                    this.deleteSchedule(pmSchedule, entity);
                    this.addSchedule(pmSchedule, entity, checklistJob);
                } else {
                    // to update repeatquartz
                    if ("0".equals(pmSchedule.getFrequence())) {// 如果频率为0,无下次repeat
                        this.deleteRepeatSchedule(pmSchedule, entity);
                    } else {
                        this.deleteRepeatSchedule(pmSchedule, entity);
                        this.addRepeatSchedule(pmSchedule, entity);
                    }
                }
            }
        }
    }

}