package com.ruoyi.common.util;

import com.ruoyi.bgt.domain.vo.BgtAttendanceCountVO;

import java.time.*;
import java.util.*;

public class DataUtil {

	public static List<BgtAttendanceCountVO> fillMissingDates(List<BgtAttendanceCountVO> countVOS, LocalDate startDate, LocalDate endDate) {
		// 使用 HashSet 存储已有的日期，方便快速查找
		Set<LocalDate> existingDates = new HashSet<>();
		for (BgtAttendanceCountVO vo : countVOS) {
			existingDates.add(vo.getDate());
		}

		// 创建一个新的列表，用于存储补充后的结果
		List<BgtAttendanceCountVO> filledList = new ArrayList<>(countVOS);

		// 遍历日期范围，补充缺失的日期
		LocalDate currentDate = startDate;
		while (!currentDate.isAfter(endDate)) {
			if (!existingDates.contains(currentDate)) {
				// 如果当前日期不在已有日期集合中，创建一个新的 BgtAttendanceCountVO 对象并添加到结果列表中
				filledList.add(new BgtAttendanceCountVO(0,0,currentDate,0));
			}
			// 日期递增一天
			currentDate = currentDate.plusDays(1);
		}

		// 对结果列表按日期排序
		filledList.sort(Comparator.comparing(BgtAttendanceCountVO::getDate));

		return filledList;
	}


	public static List<BgtAttendanceCountVO> statisticsByMonth(List<BgtAttendanceCountVO> countVOS, LocalDate startDate, LocalDate endDate) {
		// 用于存储每个月的统计结果
		Map<YearMonth, BgtAttendanceCountVO> monthMap = new TreeMap<>();

		// 遍历所有的 BgtAttendanceCountVO 对象
		for (BgtAttendanceCountVO countVO : countVOS) {
			LocalDate date = countVO.getDate();
			YearMonth yearMonth = YearMonth.from(date);

			// 如果该月份已经在 map 中，则累加该月份的到岗人数和总人数
			if (monthMap.containsKey(yearMonth)) {
				BgtAttendanceCountVO existingVO = monthMap.get(yearMonth);
				existingVO.setReportToDutyNum(existingVO.getReportToDutyNum() + countVO.getReportToDutyNum());
				existingVO.setTotalNum(existingVO.getTotalNum()>countVO.getTotalNum()?existingVO.getTotalNum():countVO.getTotalNum());
				existingVO.setAbsenceDutyNum(existingVO.getAbsenceDutyNum() + countVO.getTotalNum()-countVO.getReportToDutyNum());
			} else {
				// 如果该月份不在 map 中，则创建一个新的 BgtAttendanceCountVO 对象
				BgtAttendanceCountVO newVO = new BgtAttendanceCountVO(countVO.getReportToDutyNum(), countVO.getTotalNum(), date.withDayOfMonth(1),0);
				monthMap.put(yearMonth, newVO);
			}
		}

		// 找出日期范围的起始月份和结束月份
		if (monthMap.isEmpty()) {
			return new ArrayList<>();
		}
		YearMonth startMonth = YearMonth.from(startDate);
		YearMonth endMonth = YearMonth.from(endDate);

		// 补足空白月份
		List<BgtAttendanceCountVO> result = new ArrayList<>();
		for (YearMonth currentMonth = startMonth; !currentMonth.isAfter(endMonth); currentMonth = currentMonth.plusMonths(1)) {
			if (monthMap.containsKey(currentMonth)) {
				result.add(monthMap.get(currentMonth));
			} else {
				// 如果该月份没有统计数据，则创建一个新的 BgtAttendanceCountVO 对象，将到岗人数和总人数都设置为 0
				BgtAttendanceCountVO emptyVO = new BgtAttendanceCountVO(0, 0, currentMonth.atDay(1),0);
				result.add(emptyVO);
			}
		}

		return result;
	}


	public static Integer getMinutes(LocalDateTime clockInTime,LocalTime beginWorkTime){
		LocalDateTime workDateTime = LocalDateTime.of(clockInTime.toLocalDate(), beginWorkTime);

		// 计算两个 LocalDateTime 对象之间的时间差
		Duration duration = Duration.between(workDateTime, clockInTime);

		// 将时间差转换为分钟数
		long minutes = duration.toMinutes();
		return (int) minutes;
	}
}
