package com.jpsoft.shinestar.scheduled;

import com.jpsoft.shinestar.modules.base.entity.CompanyInfo;
import com.jpsoft.shinestar.modules.base.service.CompanyInfoService;
import com.jpsoft.shinestar.modules.base.service.WorkOverPersonService;
import com.jpsoft.shinestar.modules.base.service.WorkShiftCompanyService;
import com.jpsoft.shinestar.modules.business.service.WorkScheduleAttendanceService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;

@Component
@Slf4j
@Profile(value = {"production","test","dev"})
public class WorkScheduleTask {
    @Autowired
    private WorkScheduleAttendanceService workScheduleAttendanceService;

    @Autowired
    private WorkShiftCompanyService workShiftCompanyService;

    @Autowired
    private WorkOverPersonService workOverPersonService;

    @Autowired
    private CompanyInfoService companyInfoService;

    /**
     * 每天5,18点统计昨天的考勤情况
     */
    @Scheduled(cron = "0 0 5,18 * * ?")
    public void run() {
        //查询出关联了班次考勤的单位

        //List<String> companyList = workShiftCompanyService.findDistinctCompanyId();
        List<CompanyInfo> companyList = companyInfoService.list();

        DateTime prevTime = DateTime.now().minusDays(1).withTimeAtStartOfDay();
        String prevDay = prevTime.toString("yyyy-MM-dd");

        //更新加班结束时间
        int affectCount = workOverPersonService.updateFinishTime(prevDay);
        log.warn("更新{}加班结束时间共{}条", prevDay, affectCount);

        for (CompanyInfo companyInfo : companyList) {
            //查询前天考勤情况
            workScheduleAttendanceService.batchUpdate(companyInfo.getId(),null,null,null,
                    prevTime.toDate(),prevTime.plusDays(1).toDate(),false);
        }
    }
}
