# AlarmManagerImpl.java

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

    import com.mycim.framework.oid.IDGenerators;
    import com.mycim.framework.oid.type.IDNames;
    import com.mycim.framework.oid.type.SequenceNames;
    import com.mycim.framework.utils.lang.collections.CollectionUtils;
    import com.mycim.server.alarm.dao.AlarmDao;
    import com.mycim.server.alarm.manager.AlarmCategoryManager;
    import com.mycim.server.alarm.manager.AlarmManager;
    import com.mycim.server.base.manager.NamedObjectManager;
    import com.mycim.server.base.manager.RelationManager;
    import com.mycim.server.ctx.manager.ContextValueManager;
    import com.mycim.server.security.manager.UserManager;
    import com.mycim.valueobject.alm.*;
    import com.mycim.valueobject.bas.NamedObject;
    import com.mycim.valueobject.security.User;
    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.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;

    /**
     * @author yanbing.chen
     * @date 2019/10/10
     * @since 1.8
     **/
    @Service
    @Transactional
    public class AlarmManagerImpl implements AlarmManager {

        @Autowired
        AlarmDao alarmDao;

        @Autowired
        NamedObjectManager namedObjectManager;

        @Autowired
        RelationManager relationManager;

        @Autowired
        ContextValueManager contextValueManager;

        @Autowired
        AlarmCategoryManager alarmCategoryManager;

        @Autowired
        UserManager userManager;

        @Override
        public EenMessage getEenMessage(EenMessage eenMessage) {
            NamedObject namedObject = namedObjectManager.getNamedObject(eenMessage);
            if (namedObject == null) {
                return null;
            }
            long instanceRrn = namedObject.getInstanceRrn();
            eenMessage.copyNamedObject(namedObject);
            return alarmDao.getEenMessage(eenMessage, instanceRrn);
        }

        @Override
        public boolean isAlarmID(String alarmType, String alarmid) {
            return alarmDao.isAlarmID(alarmType, alarmid);
        }

        @Override
        public Alarm getAlarm(Alarm alarm) {
            alarm = alarmDao.getAlarm(alarm);
            if ((alarm != null) && (alarm.getAlarmCategoryRrn() != null) && (alarm.getAlarmCategoryRrn() > 0)) {
                alarm.setAlarmCategoryId(namedObjectManager.getNamedObjectId(alarm.getAlarmCategoryRrn()));
                return alarm;
            } else {
                return null;
            }
        }

        @Override
        public void insertAlarm(Alarm alarm) {
            alarmDao.insertAlarm(alarm);
        }

        @Override
        public void updateAlarm(Alarm alarm) {
            alarmDao.updateAlarm(alarm);
        }

        @Override
        public void deleteAlarm(Alarm alarm) {
            alarmDao.deleteAlarm(alarm);
        }

        @Override
        public List<EmailMessage> getAlarmEmailMessages(String status, int pageSize) {
            return getAlarmEmailMessages(status, pageSize, null);
        }

        @Override
        public List<EmailMessage> getAlarmEmailMessages(String status, int pageSize, String type) {
            return alarmDao.getAlarmEmailMessages(status, pageSize, type);
        }

        @Override
        public List<String> getEmailAddressByDepartment(String toUserGroupRrns) {
            List<User> userEmailList = alarmDao.getUserInfo4AlarmByDepartment(toUserGroupRrns);
            if (CollectionUtils.isEmpty(userEmailList)) {
                return null;
            }
            return userEmailList.parallelStream().map(User::getEmailAddress).collect(Collectors.toList());
        }

        @Override
        public List<String> getUserInfo4AlarmByUserGroups(String toUserGroupRrns) {
            List<User> userEmailList = alarmDao.getEmailAddressByUserGroups(toUserGroupRrns);
            if (CollectionUtils.isEmpty(userEmailList)) {
                return null;
            }
            return userEmailList.parallelStream().map(User::getEmailAddress).collect(Collectors.toList());
        }

        @Override
        public List<String> getEmailAddressByUserRrn(Long UserRrn) {
            User user = userManager.getUser(UserRrn);
            List result = new ArrayList();
            result.add(user.getEmailAddress());
            return result;
        }

        @Override
        public void deleteEmailMessage(EmailMessage emailMessage) {
            alarmDao.deleteEmailMessage(emailMessage);
        }

        @Override
        public void markedSendMailError(EmailMessage emailMessage) {
            alarmDao.markedSendMailError(emailMessage);
        }

        @Override
        public void deleteOldEmailMessages() {
            alarmDao.deleteOldEmailMessages();
        }

        @Override
        public void insertAlarmEmailMessag(EmailMessage emailMessage) {
            alarmDao.insertAlarmEmailMessag(emailMessage);
        }

        @Override
        public long insertAlarmActionHistory(HashMap jobData) {
            long reportAlarmRrn = Long.parseLong(
                    IDGenerators.get(IDNames.SEQUENCE).generateId(SequenceNames.MODULE_SEQ_TRANSACTION_RRN));
            Alarm alarm = (Alarm) jobData.get("alarm");
            AlarmCategory alarmCategory = (AlarmCategory) jobData.get("alarmCategory");
            String alarmText = (String) jobData.get("alarmText");
            Long userRrn = (Long) jobData.get("userRrn");
            Long equipmentRrn = (Long) jobData.get("equipmentRrn");
            Long chamberRrn = (Long) jobData.get("chamberRrn");
            Long jobRrn = (Long) jobData.get("jobRrn");
            Long runRrn = (Long) jobData.get("runRrn");
            Timestamp triggeredTime = (Timestamp) jobData.get("curTime");
            alarm = this.getAlarm(alarm);

            TriggeredAlarm triggeredAlarm = new TriggeredAlarm();
            triggeredAlarm.setAlarm(alarm);
            triggeredAlarm.setAlarmText(alarmText);
            triggeredAlarm.setTriggeredBy(userRrn);
            triggeredAlarm.setEqptRrn(equipmentRrn);
            triggeredAlarm.setChamberRrn(chamberRrn);
            triggeredAlarm.setPriority(alarmCategory.getPriority());
            triggeredAlarm.setSeverity(alarmCategory.getSeverity());
            triggeredAlarm.setReportAlarmRrn(reportAlarmRrn);
            triggeredAlarm.setTriggeredTime(triggeredTime);
            triggeredAlarm.setJobRrn(jobRrn);
            triggeredAlarm.setRunRrn(runRrn);

            if ((triggeredAlarm.getPriority() != null) &&
                    triggeredAlarm.getPriority().equalsIgnoreCase(AlarmCategory.SEVERITY_TYPE_INFORMATION)) {
                triggeredAlarm.setClearedFlag(TriggeredAlarm.FLAG_TRUE_KEY);
                triggeredAlarm.setAcknowledgedFlag(TriggeredAlarm.FLAG_TRUE_KEY);
                triggeredAlarm.setClearedTime(triggeredTime);
                triggeredAlarm.setAcknowledgedTime(triggeredTime);
            }
            alarmDao.insertAlarmActionHistory(triggeredAlarm);
            return reportAlarmRrn;
        }

        @Override
        public Map<String, Object> getTriggerAlarmWithWorkFlowInfo(Long entityRrn, String alarmId) {
            Map<String, Object> map = alarmDao.getTriggerAlarmWithWorkFlowInfo(entityRrn, alarmId);
            Alarm alarm = (Alarm) map.get("alarm");
            AlarmCategory alarmCategory = new AlarmCategory();
            alarmCategory.setInstanceRrn(alarm.getAlarmCategoryRrn());
            alarmCategory = alarmCategoryManager.getAlarmCategory(alarmCategory);
            map.put("alarmCategory", alarmCategory);
            return map;
        }

    }