package com.ruoyi.gysl.regular;

import com.ruoyi.docking.entity.Project;
import com.ruoyi.docking.entity.ProjectProgress;
import com.ruoyi.docking.service.ProjectProgressService;
import com.ruoyi.docking.service.ProjectService;
import com.ruoyi.gysl.entity.Notice;
import com.ruoyi.gysl.service.NoticeService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 每月固定时间定时任务
 * @author du
 * @since 2025/3/23 16:35
 */
@Configuration
@EnableScheduling
public class NoticeTiming {

    @Resource
    private NoticeService noticeService;

    @Resource
    private ProjectProgressService projectProgressService;

    @Resource
    private ProjectService projectService;

    /**
     * 每个月固定发一次政务通知
     */
    @Scheduled(cron = "0 0 0 1 * ?")
    private void configureTasks() {
        Notice notice = new Notice();
        notice.setType(1);
        notice.setContent(LocalDate.now().getYear() +
                "年"+
                LocalDate.now().getMonth().getValue()+
                "月"+
                "项目进展未填写");
        noticeService.save(notice);
    }

    /**
     * 每个月1号的2点进行上个月的项目月度信息查询并进行新增
     */
    @Scheduled(cron = "0 0 2 1 * ?")  // 每月1号 2:00:00 执行
    private void changeProgress() {
        List<ProjectProgress> pList = projectProgressService.djList();
        //修改该progressId为空
        pList.forEach(x-> x.setProgressId(null));
        //批量新增
        projectProgressService.saveBatch(pList);

        List<Project> pjList = projectService.djList();
        projectService.saveBatch(pjList);
    }
}
