package com.dcs.hnyz.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dcs.common.utils.DateUtils;
import com.dcs.hnyz.domain.Flow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dcs.hnyz.mapper.AlarmEventMapper;
import com.dcs.hnyz.domain.AlarmEvent;
import com.dcs.hnyz.service.IAlarmEventService;

/**
 * 告警中心管理Service业务层处理
 *
 * @author hmy
 * @date 2025-04-30
 */
@Service
public class AlarmEventServiceImpl implements IAlarmEventService {
    @Autowired
    private AlarmEventMapper alarmEventMapper;

    /**
     * 查询告警中心管理
     *
     * @param alarmId 告警中心管理主键
     * @return 告警中心管理
     */
    @Override
    public AlarmEvent selectAlarmEventByAlarmId(Long alarmId) {
        return alarmEventMapper.selectById(alarmId);
    }

    /**
     * 查询告警中心管理列表
     *
     * @param alarmEvent 告警中心管理
     * @return 告警中心管理
     */
    @Override
    public List<AlarmEvent> selectAlarmEventList(AlarmEvent alarmEvent) {
        return alarmEventMapper.selectAlarmEventList(alarmEvent);
    }

    /**
     * 新增告警中心管理
     *
     * @param alarmEvent 告警中心管理
     * @return 结果
     */
    @Override
    public int insertAlarmEvent(AlarmEvent alarmEvent) {
        return alarmEventMapper.insert(alarmEvent);
    }


    /**
     * 修改告警中心管理
     *
     * @param alarmEvent 告警中心管理
     * @return 结果
     */
    @Override
    public int updateAlarmEvent(AlarmEvent alarmEvent) {
//        alarmEvent.setUpdateTime(DateUtils.getNowDate());
        return alarmEventMapper.updateById(alarmEvent);
    }

    /**
     * 批量删除告警中心管理
     *
     * @param alarmIds 需要删除的告警中心管理主键
     * @return 结果
     */
    @Override
    public int deleteAlarmEventByAlarmIds(Long[] alarmIds) {
        return alarmEventMapper.deleteBatchIds(Arrays.asList(alarmIds));
    }

    /**
     * 删除告警中心管理信息
     *
     * @param alarmId 告警中心管理主键
     * @return 结果
     */
    @Override
    public int deleteAlarmEventByAlarmId(Long alarmId) {
        return alarmEventMapper.deleteById(alarmId);
    }

    @Override
    public Map<String, Object> getAlarmQuantity() {
        Map<String, Object> map = new HashMap<>();
        LocalDate today = LocalDate.now();
        LocalDateTime startOfDay = today.atStartOfDay();
        LocalDateTime yesterdayStart = today.minusDays(1).atStartOfDay();
        //查询今日告警数量
        LambdaQueryWrapper<AlarmEvent> w = new LambdaQueryWrapper<>();
        w.ge(AlarmEvent::getCreateTime, startOfDay);
        int alarmNum_today = Math.toIntExact(alarmEventMapper.selectCount(w));
        map.put("value", alarmNum_today);
        //昨日告警数量
        LambdaQueryWrapper<AlarmEvent> w1 = new LambdaQueryWrapper<>();
        w1.lt(AlarmEvent::getCreateTime, startOfDay);
        w1.ge(AlarmEvent::getCreateTime, yesterdayStart);
        int alarmNum_yesterday = Math.toIntExact(alarmEventMapper.selectCount(w1));
        //今日流程数量变化
        map.put("trend", alarmNum_today - alarmNum_yesterday);
        return map;
    }

    @Override
    public List<AlarmEvent> getLatestAlarm(Integer alarmCount) {
        if (alarmCount == null) {
            alarmCount = 5;
        }
        LambdaQueryWrapper<AlarmEvent> w = new LambdaQueryWrapper<>();
        w.orderByDesc(AlarmEvent::getCreateTime);
        return alarmEventMapper.selectList(w.last("LIMIT " + alarmCount));
    }

    @Override
    public List<Map<String, Object>> getAlarmEventWeeklyCount() {
        LocalDateTime sevenDaysAgo = LocalDateTime.now().minusDays(7);
        QueryWrapper<AlarmEvent> wrapper = new QueryWrapper<>();
        wrapper.select("alarm_type, COUNT(*) AS alarmCount")
                .ge("occur_time", sevenDaysAgo)
                .groupBy("alarm_type");
        return alarmEventMapper.selectMaps(wrapper);
    }
}
