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.enums.ObjType;
import com.ruoyi.common.core.utils.DateTimeUtil;
import com.ruoyi.ems.domain.AdmOpAlarm;
import com.ruoyi.ems.domain.Area;
import com.ruoyi.ems.domain.AreaBuildingZoning;
import com.ruoyi.ems.domain.EmsDevice;
import com.ruoyi.ems.domain.EmsFacs;
import com.ruoyi.ems.domain.PowerBox;
import com.ruoyi.ems.mapper.AdmOpAlarmMapper;
import com.ruoyi.ems.mapper.AreaBuildingZoningMapper;
import com.ruoyi.ems.mapper.AreaMapper;
import com.ruoyi.ems.mapper.EmsDeviceMapper;
import com.ruoyi.ems.mapper.EmsFacsMapper;
import com.ruoyi.ems.mapper.PowerBoxMapper;
import com.ruoyi.ems.service.IAdmOpAlarmService;

/**
 * 能源设施告警Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-26
 */
@Service
public class AdmOpAlarmServiceImpl implements IAdmOpAlarmService {
    @Autowired
    private AdmOpAlarmMapper admOpAlarmMapper;

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private AreaBuildingZoningMapper areaBuildingZoningMapper;

    @Autowired
    private EmsFacsMapper emsFacsMapper;

    @Autowired
    private EmsDeviceMapper emsDeviceMapper;

    @Autowired
    private PowerBoxMapper powerBoxMapper;

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

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

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

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

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

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

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

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

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

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

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

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

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

    private String getObjInfo(Integer objType, String objCode) {
        if (ObjType.AREA.getCode().equals(objType)) {
            Area area = areaMapper.selectAreaByCode(objCode);
            if (ObjectUtils.isEmpty(area)) {
                return StringUtils.EMPTY;
            }
            return area.getAreaName();
        }
        if (ObjType.ZONE.getCode().equals(objType)) {
            AreaBuildingZoning areaBuildingZoning = areaBuildingZoningMapper.selectAreaBuildingZoningByCode(objCode);
            if (ObjectUtils.isEmpty(areaBuildingZoning)) {
                return StringUtils.EMPTY;
            }
            return areaBuildingZoning.getZoningName();
        }
        if (ObjType.FACS.getCode().equals(objType)) {
            EmsFacs emsFacs = emsFacsMapper.selectEmsFacsByCode(objCode);
            if (ObjectUtils.isEmpty(emsFacs)) {
                return StringUtils.EMPTY;
            }
            return emsFacs.getFacsName();
        }
        if (ObjType.DEVICE.getCode().equals(objType)) {
            EmsDevice emsDevice = emsDeviceMapper.selectEmsDeviceByCode(objCode);
            if (ObjectUtils.isEmpty(emsDevice)) {
                return StringUtils.EMPTY;
            }
            return emsDevice.getDeviceName();
        }
        if (ObjType.POWER_BOX.getCode().equals(objType)) {
            PowerBox powerBox = powerBoxMapper.selectPowerBoxByCode(objCode);
            if (ObjectUtils.isEmpty(powerBox)) {
                return StringUtils.EMPTY;
            }
            return String.format("%s(%s)", powerBox.getLocation(), powerBox.getAreaName());
        }
        return StringUtils.EMPTY;
    }
}
