package com.mortals.xhx.common.utils;

import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.module.attendance.model.vo.AttendanceSummaryQuery;
import com.mortals.xhx.module.attendance.service.AttendanceStatService;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 考勤核查后续处理线程
 *
 * @author:
 * @date: 2023/7/17 14:42
 */
@RequiredArgsConstructor
@Slf4j
public class AttendSummaryThread implements Runnable {
    @NonNull
    private AttendanceStatService attendanceStatService;
    @NonNull
    private AttendanceSummaryQuery query;

    @Override
    public void run() {

//        AttendanceSummaryQuery query = new AttendanceSummaryQuery();
//        query.setStaffId(entity.getStaffId());
//        query.setSummaryTime(DateUtils.getStrDate(entity.getErrorDateTime()));
        if (StringUtils.isNotEmpty(query.getSummaryTimeStart()) && StringUtils.isNotEmpty(query.getSummaryTimeEnd())) {
            List<String> dateList = getDatesBetween(query.getSummaryTimeStart(), query.getSummaryTimeEnd());
            for (String day : dateList) {
                AttendanceSummaryQuery temp = new AttendanceSummaryQuery(day);
                attendanceStatService.doAttendanceSummary(temp);
            }
        } else {
            attendanceStatService.doAttendanceSummary(query);
        }

    }

    private List<String> getDatesBetween(String startDateStr, String endDateStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startDate = LocalDate.parse(startDateStr, formatter);
        LocalDate endDate = LocalDate.parse(endDateStr, formatter);
        List<String> dates = new ArrayList<>();
        //这里是判断开始日期是否在结束日期之后或者=结束日期
        while (startDate.isBefore(endDate) || startDate.isEqual(endDate)) {
            String add = startDate.format(formatter);
            dates.add(add);
            startDate = startDate.plusDays(1);
        }
        return dates;
    }

}
