# EntityCounterManagerImpl.java

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

    import com.mycim.framework.jdbc.Page;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.base.manager.TransactionLogManager;
    import com.mycim.server.ems.manager.CheckListJobManager;
    import com.mycim.server.pms.dao.EntityCounterDAO;
    import com.mycim.server.pms.manager.CheckListManager;
    import com.mycim.server.pms.manager.EntityCounterManager;
    import com.mycim.valueobject.bas.TransactionLog;
    import com.mycim.valueobject.consts.TransactionNames;
    import com.mycim.valueobject.ems.Checklist;
    import com.mycim.valueobject.ems.ChecklistJob;
    import com.mycim.valueobject.ems.EntityCounter;
    import com.mycim.valueobject.ems.EntityCounterType;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.sql.Timestamp;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;

    /**
     * @author Johnson.Wang
     * @version 6.0.0
     * @date 2019/9/25
     **/

    @Service
    @Transactional
    public class EntityCounterManagerImpl implements EntityCounterManager {

        @Autowired
        private NamedObjectManager namedObjectManager;

        @Autowired
        private EntityCounterDAO entityCounterDAO;

        @Autowired
        private TransactionLogManager transactionLogManager;

        @Autowired
        private CheckListManager checkListManager;

        @Autowired
        private CheckListJobManager checkListJobManager;

        @Override
        public List<EntityCounter> getEntityCountersOfEntity(long entityRrn) {
            List<EntityCounter> entityCounters = entityCounterDAO.getEntityCountersOfEntity(entityRrn);

            if (CollectionUtils.isNotEmpty(entityCounters)) {
                for (EntityCounter entityCounter : entityCounters) {
                    if ((entityCounter.getChecklistRrn() != null) && (entityCounter.getChecklistRrn() > 0)) {
                        entityCounter.setChecklistId(namedObjectManager.getNamedObjectId(entityCounter.getChecklistRrn()));
                    }
                }
            }

            return entityCounters;
        }

        @Override
        public List<EntityCounterType> getEntityCounterTypesOfEntity(long entityRrn) {
            List<EntityCounterType> entityCounterTypeList = entityCounterDAO.getEntityCounterTypesOfEntity(entityRrn);

            if (CollectionUtils.isNotEmpty(entityCounterTypeList)) {

                for (EntityCounterType entityCounterType : entityCounterTypeList) {
                    entityCounterType.setEventId(namedObjectManager.getNamedObjectId(entityCounterType.getEventRrn()));
                    entityCounterType.setRecipeId(namedObjectManager.getNamedObjectId(entityCounterType.getRecipeRrn()));
                }
            }

            return entityCounterTypeList;
        }

        @Override
        public List<Map<String, Object>> getChecklistEntityCounterLists(long checklistRrn) {
            return entityCounterDAO.getChecklistEntityCounterLists(checklistRrn);
        }

        @Override
        public long insertEntityCounter(EntityCounter entityCounter) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(entityCounter.getTransPerformedBy(), TransactionNames.CREATE_KEY);
            long instanceRrn = entityCounterDAO.insertEntityCounter(entityCounter, transactionLog);
            transactionLogManager.markTransactionLog(transactionLog);
            return instanceRrn;
        }

        @Override
        public void updateEntityCounter(EntityCounter entityCounter) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(entityCounter.getTransPerformedBy(), TransactionNames.MODIFY_KEY);
            entityCounterDAO.updateEntityCounter(entityCounter, transactionLog);
            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public void deleteEntityCounter(EntityCounter entityCounter) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(entityCounter.getTransPerformedBy(), TransactionNames.DELETE_KEY);
            entityCounterDAO.deleteEntityCounter(entityCounter, transactionLog);
            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public EntityCounter getActualCounter(long entityRrn, String counterType) {
            return entityCounterDAO.getActualCounter(entityRrn, counterType);
        }

        @Override
        public void updateActualValueForReset(EntityCounter entityCounter, long entityRrn, String type, double count) {
            TransactionLog transactionLog = transactionLogManager
                    .startTransactionLog(entityCounter.getTransPerformedBy(), TransactionNames.PM_COUNT_CHANGE_KEY);
            entityCounterDAO.insertEntityCounterHistory(entityCounter, transactionLog.getTransRrn());
            entityCounterDAO.updateActualValue(entityRrn, type, count);
            transactionLogManager.markTransactionLog(transactionLog);
        }

        @Override
        public Page getEntityCountersByPageQuery(long entityRrn, Page page) {
            if (entityRrn == 0) {
                return entityCounterDAO.getEntityCountersAllForPage(page);
            } else {
                return entityCounterDAO.getEntityCountersAllForPageByEntityRrn(entityRrn, page);
            }
        }

        @Override
        public List<Map> updateEqptPMCounter(long eqptRrn, long recipeRrn, long eventRrn, long jobRrn) {
            List<Map> returnValues = new ArrayList<>();

            List<Map> valueList = entityCounterDAO.getEqptCounter(eqptRrn, recipeRrn, eventRrn, jobRrn);

            entityCounterDAO.updateEqptCounteValue(valueList);

            List<Map> warningList = entityCounterDAO.checkCounterValue(eqptRrn, "warning");

            List<Map> alarmList = entityCounterDAO.checkCounterValue(eqptRrn, "alarm");

            for (Map map : alarmList) {
                String alarmId = (String) map.get("alarmId");
                Long checkListRrn = (Long) map.get("checkListRrn");

                ChecklistJob job = new ChecklistJob();

                job.setEntityRrn(eqptRrn);
                job.setChecklistRrn(checkListRrn);
                job.setComments("");
                job.setStartTimestamp(new Timestamp(System.currentTimeMillis()));
                job.setChecklistJobState("STARTED");
                job.setCreatePerformedBy("SYSTEM");
                job.setAttri1("设备计数");

                job.setTransPerformedby("SYSTEM");
                job.setTransId("CREATE");

                Checklist checklist = new Checklist();
                checklist.setInstanceRrn(checkListRrn);
                checklist = checkListManager.getCheckList(checklist);

                if (checklist != null) {
                    job.setStartEventRrn(checklist.getStartEventRrn());
                    job.setEndEventRrn(checklist.getEndEventRrn());
                }
                checkListJobManager.insertChecklistJob(job);
            }

            returnValues.addAll(warningList);
            returnValues.addAll(alarmList);

            return returnValues;
        }

    }