# ResistStatusManagerImpl.java

    package com.mycim.server.resist.manager.impl;

    import com.fa.sesa.exception.Assert;
    import com.fa.sesa.exception.Errors;
    import com.fa.sesa.threadlocal.LocalContext;
    import com.mycim.framework.utils.lang.StringUtils;
    import com.mycim.framework.utils.lang.collections.MapUtils;
    import com.mycim.server.base.manager.EventManager;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.base.manager.TransactionLogManager;
    import com.mycim.server.ems.manager.EntityManager;
    import com.mycim.server.resist.dao.ResistStatusDao;
    import com.mycim.server.resist.manager.ResistManager;
    import com.mycim.server.resist.manager.ResistStatusManager;
    import com.mycim.valueobject.MessageIdList;
    import com.mycim.valueobject.ObjectList;
    import com.mycim.valueobject.bas.TransactionLog;
    import com.mycim.valueobject.consts.ObjectStatus;
    import com.mycim.valueobject.consts.TransactionNames;
    import com.mycim.valueobject.ems.*;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.*;

    /**
     * @author can.yang
     */
    @Transactional
    @Service
    public class ResistStatusManagerImpl implements ResistStatusManager {

        @Autowired
        ResistStatusDao resistStatusDao;

        @Autowired
        NamedObjectManager namedObjectManager;

        @Autowired
        TransactionLogManager transactionLogManager;

        @Autowired
        EventManager eventManager;

        @Autowired
        EntityManager entityManager;

        @Autowired
        ResistManager resistManager;

        @Override
        public void insertResistStatus(Long eventRrn, ResistStatus resistStatus, TransactionLog transactionLog) {

            resistStatusDao.insertResistStatus(eventRrn, resistStatus, transactionLog);
        }

        @Override
        public void initInsertResistStatusH(ResistStatus resistStatus, TransactionLog transactionLog) {

            resistStatusDao.initInsertResistStatusH(resistStatus, transactionLog);
        }

        @Override
        public void updateResistStatus(ResistStatus resistStatus, TransactionLog transactionLog) {
            resistStatusDao.updateResistStatus(resistStatus, transactionLog);
        }

        @Override
        public ResistStatus getResistStatus(ResistStatus resistStatus) {
            return resistStatusDao.getResistStatus(resistStatus);
        }

        @Override
        public String getPreResistStatus(ResistStatus resistStatus) {
            return resistStatusDao.getPreResistStatus(resistStatus);
        }

        @Override
        public void initResistStatus(String eventId, Resist resist) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(LocalContext.getUserId(), "INITIALIZE");
            Long eventRrn = new Long(
                    namedObjectManager.getNamedObjectRrn(eventId, resist.getNamedSpace(), ObjectList.EVENT_KEY));
            Assert.isFalse(eventRrn == 0, Errors.create().content("Event: {} not found").args(eventId).build());
            ResistStatus resistStatus = new ResistStatus();
            resistStatus.setResistSysRrn(resist.getInstanceRrn());
            //插入RESIST_STATUS表中
            insertResistStatus(eventRrn, resistStatus, transactionLog);
            //插入RESIST_STATUS_表中
            initInsertResistStatusH(resistStatus, transactionLog);
            HashMap<String, Object> map = MapUtils.newHashMap();

            Event event = new Event();
            event.setInstanceRrn(eventRrn);

            event.setEntityType(resist.getObjectType());
            event = eventManager.getEvent(event);

            map.put("transrrn", transactionLog.getTransRrn());
            map.put("lastUpdateTime", transactionLog.getTransStartTimestamp());
            map.put("entityrrn", resist.getInstanceRrn());
            map.put("eventrrn", eventRrn);
            map.put("entityavailablebefore", event.getValidAvailability());
            map.put("entityavailableafter", event.getTargetAvailability());
            entityManager.insertEntityEventHistoty(map);

            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public long changeResistStatus(Resist resist, String objectType, String eventId, String comments) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(LocalContext.getUserId(), TransactionNames.LOGEVENT_KEY);
            long transRrn = transactionLog.getTransRrn();

            Event event = new Event();
            long eventRrn = namedObjectManager.getNamedObjectRrn(eventId, namedObjectManager
                    .getNamedSpace(LocalContext.getFacilityRrn(), ObjectList.EVENT_KEY), ObjectList.EVENT_KEY);
            event.setInstanceRrn(eventRrn);
            event.setEntityType(objectType);
            event = eventManager.getEvent(event);
            Assert.isFalse(event == null || event.getInstanceRrn() <= 0,
                           Errors.create().content("Event ID not found!").build());

            List<EventModel> resistEventModels = resistManager.getResistEventModels(eventRrn, resist.getInstanceRrn());
            event.setEventModels(resistEventModels);
            List<ResistStatus> resistStatuss = new ArrayList();
            List<EventModel> eventModels = (List<EventModel>) event.getEventModels();
            for (EventModel eventModel : eventModels) {
                ResistStatus resistStatus = new ResistStatus();
                resistStatus.setStatusGroupId(eventModel.getStatusGroupId());
                resistStatus.setCurrentStatus(eventModel.getTargetStatus());
                resistStatus.setStatusBefore(eventModel.getCurrentStatus());
                resistStatus.setResistSysRrn(resist.getInstanceRrn());
                resistStatuss.add(resistStatus);
            }
            //check Event
            checkResistEnableLogEvent(eventRrn, resist.getInstanceRrn());

            HashMap<String, Object> eventHMap = MapUtils.newHashMap();
            eventHMap.put("transrrn", transRrn);
            eventHMap.put("lastUpdateTime", transactionLog.getTransStartTimestamp());
            eventHMap.put("eventrrn", event.getInstanceRrn());
            eventHMap.put("entityavailablebefore", event.getValidAvailability());
            eventHMap.put("entityavailableafter", event.getTargetAvailability());
            entityManager.insertEntityEventHistoty(eventHMap);

            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("event", event);
            map.put("user", resist.getModifyBy());
            map.put("comments", comments);
            map.put("entityrrn", resist.getInstanceRrn());
            for (ResistStatus status : resistStatuss) {
                updateResistStatus(status, transactionLog);
                resistStatusDao.insertResistStatusH(eventRrn, status, transactionLog);

            }
            transactionLogManager.markTransactionLog(transactionLog);
            return transRrn;
        }

        public void checkResistEnableLogEvent(long eventRrn, long resistRrn) {
            Entity entity = new Entity();
            Event myevent = new Event();
            Resist resist = new Resist();
            resist.setInstanceRrn(resistRrn);
            myevent.setInstanceRrn(eventRrn);
            resist = resistManager.getResistByResistRrn(resistRrn);

            Assert.isFalse(resist == null, Errors.create().key(MessageIdList.ENTITY_MISSING).content("找不到entity").build());

            myevent.setEntityType(resist.getObjectType());

            myevent = eventManager.getEvent(myevent);

            Assert.isFalse(myevent == null, Errors.create().key(MessageIdList.EVENT_MISSING).content("找不到事件").build());

            if (resist.getInstanceId() == null) {
                resist.setInstanceId(StringUtils.EMPTY);
            }
            if (resist.getInstanceStatus() == null) {
                resist.setInstanceStatus("ACTIVE");
            }

            Assert.isFalse(resist.getInstanceStatus().equalsIgnoreCase(ObjectStatus.HOLD_KEY),
                           Errors.create().key(MessageIdList.ENTITY_STATUS_HOLD).content("实体{}状态是hold")
                                 .args(resist.getInstanceId()).build());

            Assert.isFalse(eventManager.getEventAndEntityStatus(new Long(myevent.getInstanceRrn()),
                                                                new Long(resist.getInstanceRrn())) == null,
                           Errors.create().key(MessageIdList.EQUIPMENT_STATUS_INVALID).content("设备{}当前状态无效")
                                 .args(resist.getInstanceId()).build());

        }

    }