package com.usky.alarm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.alarm.domain.BaseAlarm;
import com.usky.alarm.domain.BaseAlarmStatistic;
import com.usky.alarm.mapper.BaseAlarmStatisticMapper;
import com.usky.alarm.service.BaseAlarmService;
import com.usky.alarm.service.BaseAlarmStatisticService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 告警统计表 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-11-24
 */
@Service
public class BaseAlarmStatisticServiceImpl extends AbstractCrudService<BaseAlarmStatisticMapper, BaseAlarmStatistic> implements BaseAlarmStatisticService {
    @Autowired
    private BaseAlarmService baseAlarmService;

    @Transactional
    @Override
    public void baseAlarmStatisticTask(Integer type) {
        LocalDateTime now = LocalDateTime.now();
        LocalDate currentDate = LocalDate.now();
        LocalDate previousDate = currentDate.minusDays(1);
        BaseAlarmStatistic baseAlarmStatistic = new BaseAlarmStatistic();
        LocalDateTime startDateTime = LocalDateTime.of(previousDate, LocalTime.MIN);
        LocalDateTime endDateTime = LocalDateTime.of(previousDate, LocalTime.MAX);
        LocalDateTime startOfPreviousMonth = YearMonth.from(now).minusMonths(1).atDay(1).atTime(0, 0, 0);
        LocalDateTime endOfPreviousMonth = YearMonth.from(now).minusMonths(1).atEndOfMonth().atTime(23, 59, 59);
        QueryWrapper<BaseAlarm> queryWrapper = Wrappers.query();
        if (type.equals(1)){
            queryWrapper.select("COUNT(alarm_grade=1 or null) as alarmCount","COUNT(alarm_grade!=1 or" +
                    " null) as hiddenCount")
                    .between("alarm_time",startDateTime,endDateTime);
            baseAlarmStatistic.setStatisticDimen(1);
            baseAlarmStatistic.setStatisticTime(startDateTime);
        }else {
            queryWrapper.select("COUNT(alarm_grade=1 or null) as alarmCount","COUNT(alarm_grade!=1 or" +
                    " null) as hiddenCount")
                    .between("alarm_time",startOfPreviousMonth,endOfPreviousMonth);
            baseAlarmStatistic.setStatisticDimen(2);
            baseAlarmStatistic.setStatisticTime(startOfPreviousMonth);
        }
        List<Map<String,Object>> alarmStatisticList = baseAlarmService.listMaps(queryWrapper);
        baseAlarmStatistic.setAlarmNum(Integer.valueOf(alarmStatisticList.get(0).get("alarmCount").toString()));
        baseAlarmStatistic.setPitfallNum(Integer.valueOf(alarmStatisticList.get(0).get("hiddenCount").toString()));
        baseAlarmStatistic.setCreateTime(now);
        this.save(baseAlarmStatistic);
    }

    @Override
    public List<Map<String, Object>> alarmStatistic(Integer deptId,Integer dateType) {
        LocalDateTime currentDate = LocalDateTime.now();
        LocalDateTime firstDayOfMonth = LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
        LocalDateTime halfYearAgo = currentDate.minus(6, ChronoUnit.MONTHS);
        LocalDateTime firstDayOfHalfYearAgo = halfYearAgo.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0);
        LocalDateTime oneYearAgo = currentDate.minusYears(1);
        LocalDateTime zeroTime = oneYearAgo.withHour(0).withMinute(0).withSecond(0);
        QueryWrapper<BaseAlarmStatistic> queryWrapper = Wrappers.query();
        if (dateType.equals(1)){
            queryWrapper.select("alarm_num as alarmNum","pitfall_num as pitfallNum","DATE_FORMAT(statistic_time, '%m-%d') AS " +
                    "statisticTime")
                    .between("statistic_time",firstDayOfMonth,currentDate)
                    .eq("statistic_dimen",1);
        }else if (dateType.equals(2)){
            queryWrapper.select("alarm_num as alarmNum","pitfall_num as pitfallNum","DATE_FORMAT(statistic_time, " +
                    "'%Y-%m') AS " +
                    "statisticTime")
                    .between("statistic_time",firstDayOfHalfYearAgo,currentDate)
                    .eq("statistic_dimen",2);
        }else {
            queryWrapper.select("alarm_num as alarmNum","pitfall_num as pitfallNum","DATE_FORMAT(statistic_time, " +
                    "'%Y-%m') AS " +
                    "statisticTime")
                    .between("statistic_time",zeroTime,currentDate)
                    .eq("statistic_dimen",2);
        }
        List<Map<String, Object>> list = this.listMaps(queryWrapper);
        return list;
    }
}
