package com.ruoyi.ems.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.utils.DateTimeUtil;
import com.ruoyi.ems.domain.OpAlarm;
import com.ruoyi.ems.domain.Area;
import com.ruoyi.ems.domain.EmsDevice;
import com.ruoyi.ems.domain.EmsFacs;
import com.ruoyi.ems.enums.AlarmObjType;
import com.ruoyi.ems.mapper.OpAlarmMapper;
import com.ruoyi.ems.mapper.AreaMapper;
import com.ruoyi.ems.mapper.EmsDeviceMapper;
import com.ruoyi.ems.mapper.EmsFacsMapper;
import com.ruoyi.ems.service.IOpAlarmService;

/**
 * 能源设施告警Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-26
 */
@Service
public class OpAlarmServiceImpl implements IOpAlarmService {
    @Autowired
    private OpAlarmMapper opAlarmMapper;

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private EmsFacsMapper emsFacsMapper;

    @Autowired
    private EmsDeviceMapper emsDeviceMapper;

    /**
     * 查询能源设施告警
     *
     * @param id 能源设施告警主键
     * @return 能源设施告警
     */
    @Override
    public OpAlarm selectOpAlarmById(Long id) {
        return opAlarmMapper.selectAdmOpAlarmById(id);
    }

    /**
     * 查询能源设施告警列表
     *
     * @param admOpAlarm 能源设施告警
     * @return 能源设施告警
     */
    @Override
    public List<OpAlarm> selectOpAlarmList(OpAlarm admOpAlarm) {
        return opAlarmMapper.selectAdmOpAlarmList(admOpAlarm);
    }

    /**
     * 新增能源设施告警
     *
     * @param admOpAlarm 能源设施告警
     * @return 结果
     */
    @Override
    public int insertOpAlarm(OpAlarm admOpAlarm) {
        admOpAlarm.setObjName(getObjInfo(admOpAlarm.getObjType(), admOpAlarm.getObjCode()));
        return opAlarmMapper.insertAdmOpAlarm(admOpAlarm);
    }

    /**
     * 修改能源设施告警
     *
     * @param admOpAlarm 能源设施告警
     * @return 结果
     */
    @Override
    public int updateOpAlarm(OpAlarm admOpAlarm) {
        admOpAlarm.setObjName(getObjInfo(admOpAlarm.getObjType(), admOpAlarm.getObjCode()));
        return opAlarmMapper.updateAdmOpAlarm(admOpAlarm);
    }

    /**
     * 批量删除能源设施告警
     *
     * @param ids 需要删除的能源设施告警主键
     * @return 结果
     */
    @Override
    public int deleteOpAlarmByIds(Long[] ids) {
        return opAlarmMapper.deleteAdmOpAlarmByIds(ids);
    }

    /**
     * 删除能源设施告警信息
     *
     * @param id 能源设施告警主键
     * @return 结果
     */
    @Override
    public int deleteOpAlarmById(Long id) {
        return opAlarmMapper.deleteAdmOpAlarmById(id);
    }

    @Override
    public List<Map<String, Object>> qryAlarmTypeIndex(String areaCode, String startTime, String endTime) {
        return opAlarmMapper.qryAlarmTypeIndex(areaCode, startTime, endTime);
    }

    @Override
    public List<Map<String, Object>> qryAlarmTypeIndexByDate(OpAlarm params) {
        return opAlarmMapper.qryAlarmTypeIndexByDate(params);
    }

    @Override
    public List<Map<String, Object>> qryAlarmTypeIndexDay(String areaCode) {
        String dateTime = DateTimeUtil.currentDateTime(DateTimeUtil.DateFormatter.yyyy_MM_dd);
        return opAlarmMapper.qryAlarmTypeIndexDay(dateTime, areaCode);
    }

    @Override
    public List<Map<String, Object>> qryAlarmTypeIndexMonth(String areaCode) {
        String firstDayOfRecentMonth = DateTimeUtil.getFirstDayOfRecentMonth();
        return opAlarmMapper.qryAlarmTypeIndexMonth(firstDayOfRecentMonth, areaCode);
    }

    @Override
    public List<Map<String, Object>> qryAlarmTypeIndexYear(String areaCode) {
        String firstDayOfRecentYear = DateTimeUtil.getFirstDayOfRecentYear();
        return opAlarmMapper.qryAlarmTypeIndexYear(firstDayOfRecentYear, areaCode);
    }

    @Override
    public List<Map<String, Object>> qrySubSysIndexDay(String areaCode) {
        String dateTime = DateTimeUtil.currentDateTime(DateTimeUtil.DateFormatter.yyyy_MM_dd);
        return opAlarmMapper.qrySubSysIndexDay(dateTime, areaCode);
    }

    @Override
    public List<Map<String, Object>> qrySubSysIndexMonth(String areaCode) {
        String firstDayOfRecentMonth = DateTimeUtil.getFirstDayOfRecentMonth();
        return opAlarmMapper.qrySubSysIndexMonth(firstDayOfRecentMonth, areaCode);
    }

    @Override
    public List<Map<String, Object>> qrySubSysIndexYear(String areaCode) {
        String firstDayOfRecentYear = DateTimeUtil.getFirstDayOfRecentYear();
        return opAlarmMapper.qrySubSysIndexYear(firstDayOfRecentYear, areaCode);
    }

    @Override
    public Map<String, Object> cntHandledAlarmByDate(OpAlarm admOpAlarm) {
        return opAlarmMapper.cntHandledAlarmByDate(admOpAlarm);
    }

    private String getObjInfo(Integer objType, String objCode) {
        if (AlarmObjType.AREA.getCode().equals(objType)) {
            Area area = areaMapper.selectAreaByCode(objCode);
            return ObjectUtils.isEmpty(area) ? StringUtils.EMPTY : area.getAreaName();
        }
        if (AlarmObjType.FACS.getCode().equals(objType)) {
            EmsFacs emsFacs = emsFacsMapper.selectEmsFacsByCode(objCode);
            return ObjectUtils.isEmpty(emsFacs) ? StringUtils.EMPTY : emsFacs.getFacsName();
        }
        if (AlarmObjType.DEVICE.getCode().equals(objType)) {
            EmsDevice emsDevice = emsDeviceMapper.selectDeviceByCode(objCode);
            return ObjectUtils.isEmpty(emsDevice) ? StringUtils.EMPTY : emsDevice.getDeviceName();
        }

        return StringUtils.EMPTY;
    }
}
