package com.shzg.project.worn.service.impl;

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

import com.shzg.common.utils.DateUtils;
import com.shzg.common.utils.DeptScopeUtils;
import com.shzg.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shzg.project.worn.mapper.MqttSensorEventMapper;
import com.shzg.project.worn.domain.MqttSensorEvent;
import com.shzg.project.worn.service.IMqttSensorEventService;

/**
 * 设备事件Service业务层处理
 * 
 * @author shzg
 * @date 2026-04-01
 */
@Service
public class MqttSensorEventServiceImpl implements IMqttSensorEventService
{
    @Autowired
    private MqttSensorEventMapper mqttSensorEventMapper;

    /**
     * 查询设备事件
     * 
     * @param id 设备事件主键
     * @return 设备事件
     */
    @Override
    public MqttSensorEvent selectMqttSensorEventById(Long id)
    {
        return mqttSensorEventMapper.selectMqttSensorEventById(id);
    }

    /**
     * 查询设备事件列表
     * 
     * @param mqttSensorEvent 设备事件
     * @return 设备事件
     */
    @Override
    public List<MqttSensorEvent> selectMqttSensorEventList(MqttSensorEvent mqttSensorEvent)
    {
        // ================= 数据权限控制 =================
        if (!SecurityUtils.isAdmin())
        {
            mqttSensorEvent.setDeptIds(DeptScopeUtils.getDeptScope());
        }

        return mqttSensorEventMapper.selectMqttSensorEventList(mqttSensorEvent);
    }

    /**
     * 新增设备事件
     * 
     * @param mqttSensorEvent 设备事件
     * @return 结果
     */
    @Override
    public int insertMqttSensorEvent(MqttSensorEvent mqttSensorEvent)
    {
        mqttSensorEvent.setCreateTime(DateUtils.getNowDate());
        return mqttSensorEventMapper.insertMqttSensorEvent(mqttSensorEvent);
    }

    /**
     * 修改设备事件
     * 
     * @param mqttSensorEvent 设备事件
     * @return 结果
     */
    @Override
    public int updateMqttSensorEvent(MqttSensorEvent mqttSensorEvent)
    {
        mqttSensorEvent.setUpdateTime(DateUtils.getNowDate());
        return mqttSensorEventMapper.updateMqttSensorEvent(mqttSensorEvent);
    }

    /**
     * 批量删除设备事件
     * 
     * @param ids 需要删除的设备事件主键
     * @return 结果
     */
    @Override
    public int deleteMqttSensorEventByIds(Long[] ids)
    {
        return mqttSensorEventMapper.deleteMqttSensorEventByIds(ids);
    }

    /**
     * 删除设备事件信息
     * 
     * @param id 设备事件主键
     * @return 结果
     */
    @Override
    public int deleteMqttSensorEventById(Long id)
    {
        return mqttSensorEventMapper.deleteMqttSensorEventById(id);
    }

    @Autowired
    private MqttSensorEventMapper eventMapper;

    @Override
    public Map<String, Object> getAlarmStat()
    {
        // 当前用户权限范围
        List<Long> deptIds = DeptScopeUtils.getDeptScope();

        Map<String, Object> stat = eventMapper.countAlarmStat(deptIds);

        Map<String, Object> result = new HashMap<>();

        result.put("today", stat == null ? 0 : stat.getOrDefault("today", 0));
        result.put("unhandled", stat == null ? 0 : stat.getOrDefault("unhandled", 0));

        return result;
    }

    @Override
    public List<Map<String, Object>> getAlarmTrend()
    {
        // 当前用户权限范围
        List<Long> deptIds = DeptScopeUtils.getDeptScope();

        return eventMapper.selectAlarmTrend(deptIds);
    }

    @Override
    public List<Map<String, Object>> getAlarmTypeStat()
    {
        // 当前用户权限范围
        List<Long> deptIds = DeptScopeUtils.getDeptScope();

        return eventMapper.selectAlarmTypeStat(deptIds);
    }
}
