package com.alarm.service;

import com.alarm.entity.alarm;
import com.alarm.mapper.alarmMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;



@Service
public class alarmService {
    @Autowired
    private alarmMapper alarmMapper;

    public List<List<String>> query(){
        List<alarm> alarmList = alarmMapper.list();
        alarmList.forEach(alarm -> {
            if (alarm.getType().equals("1")){
                alarm.setType("Smoke and Fire");
            }else if(alarm.getType().equals("2")){
                alarm.setType("Abnormal Behavior");
            }else if(alarm.getType().equals("3")){
                alarm.setType("Vehicle Violation");
            }else if(alarm.getType().equals("4")){
                alarm.setType("Illegal Operation");
            }
        });
        return alarmList.stream().map(alarm -> {
            // 针对每个Alarm对象，转换为一个包含4个字符串的List
            String Time = formatTime(alarm.getTime()); // 格式化时间
            String Type = alarm.getType(); // 转换为中文类型

            return List.of(
                    Time, // 格式化后的时间字符串，如 "2025-9.1-7:12:28"
                    Type,   // 中文预警类型
                    "View",        // 固定操作按钮1
                    "Ignore"         // 固定操作按钮2
            );
        }).collect(Collectors.toList());
    }

    // 格式化时间：从 LocalDateTime 到目标格式 "yyyy-M.d-H:mm:ss"
    private String formatTime(LocalDateTime dateTime) {
        DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("yyyy-M-d H:mm:ss");
        return dateTime.format(targetFormatter);
    }

    public Integer countAll(){
        return alarmMapper.selectAll();
    }

    public Integer count(Integer type){
        return alarmMapper.select(type);
    }

    public Integer countTd(){return alarmMapper.selectTd();}
}
