package com.mortals.xhx.daemon.applicationservice;

import com.mortals.framework.service.ICacheService;
import com.mortals.framework.springcloud.service.IApplicationStartedService;
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.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

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

import static com.mortals.xhx.common.key.RedisKey.KEY_ATTENDANCE_STAT_CACHE;

/***
 * 考勤汇总任务执行
 */
@Component
@Slf4j
public class AttendSummaryService implements IApplicationStartedService {

    protected Boolean stopped = false;

    @Autowired
    private ICacheService cacheService;
    @Autowired
    private AttendanceStatService attendanceStatService;

    @Override
    public void start() {
        Thread sendThread = new Thread(new Runnable() {
            @Override
            public void run() {
                int waitTime = 1000;
                while (!stopped) {
                    try {
                        AttendanceSummaryQuery query = cacheService.brpop(KEY_ATTENDANCE_STAT_CACHE, 60,AttendanceSummaryQuery.class);
                        if(!ObjectUtils.isEmpty(query)){
                            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);
                                    if(query.getStaffId()!=null){
                                        temp.setStaffId(query.getStaffId());
                                    }
                                    attendanceStatService.doAttendanceSummary(temp);
                                }
                            } else {
                                attendanceStatService.doAttendanceSummary(query);
                            }
                        }

                        try {
                            Thread.sleep(waitTime);
                        } catch (InterruptedException e2) {
                        }

                    } catch (Exception e) {
                       // log.error("异常", e);

                        try {
                            Thread.sleep(waitTime);
                        } catch (InterruptedException e2) {
                        }
                    }
                }
            }
        });
        sendThread.start();
    }

    @Override
    public void stop() {
        log.info("停止服务..");
        this.stopped = true;
    }

    @Override
    public int getOrder() {
        return 20;
    }

    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;
    }
}
