package com.ruoyi.gysl.regular;

import com.ruoyi.docking.entity.SmartDeclaration;
import com.ruoyi.docking.entity.SmartReminders;
import com.ruoyi.docking.service.*;
import com.ruoyi.gysl.entity.BasicInformation;
import com.ruoyi.gysl.service.BasicInformationService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.util.ArrayList;
import java.util.List;

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


    @Resource
    private ProjectProgressService projectProgressService;



    @Resource
    private SmartRemindersService smartRemindersService;

    @Resource
    private SmartDeclarationService smartDeclarationService;

    @Resource
    private BasicInformationService basicInformationService;


    @Resource
    private UserService userService;

    /**
     * 每个月固定发一次政务通知
     */
    @Scheduled(cron = "0 0 0 1 * ?")
    private void configureTasks() {
        userService.getAllZwUser(LocalDate.now().getYear() +
                "年" +
                LocalDate.now().getMonth().getValue() +
                "月" +
                "项目进展未填写", null, null, String.valueOf(LocalDate.now().getMonth().getValue()));
    }


    /**
     * 定时不定期提醒
     */
    @Scheduled(cron = "0 40 1 * * *")
    public void changeNdq() {
        //获取企业的不定时提醒
        SmartReminders qy = smartRemindersService.getById(1);
        //获取政务的不定时提醒
        SmartReminders zw = smartRemindersService.getById(2);
        List<SmartDeclaration> smdList = new ArrayList<>();
        //获取所有的项目
        List<BasicInformation> list = basicInformationService.list();
        List<BasicInformation> list1 = basicInformationService.list();
        //如果当前时间在该项目的结束时间前自定义的提前月数之前，就去掉
        list.removeIf(x ->
                LocalDate.now().isBefore(convertToLocalDate(x.getEndTime()).minusMonths(qy.getDaysAdvance()))
        );
        list.forEach(x -> {
            SmartDeclaration smartDeclaration = new SmartDeclaration();
            smartDeclaration.setSmartRemindersId(1L);
            smartDeclaration.setTyshxydm(x.getTyshxydm());
            smartDeclaration.setAlertTime(convertToLocalDate(x.getEndTime()).atTime(0,0,0));
            smdList.add(smartDeclaration);
        });
        list1.removeIf(x ->
                LocalDate.now().isBefore(convertToLocalDate(x.getEndTime()).minusMonths(zw.getDaysAdvance()))
        );
        //获取到所有的符合条件的政务用户
        list1.forEach(x -> userService.getAllZwUser(null, null, 2L,
                String.valueOf(convertToLocalDate(x.getEndTime()).atTime(0,0,0))));
        smartDeclarationService.saveBatch(smdList);
    }

    public static LocalDate convertToLocalDate(String dateStr) {
        try {
            // 定义格式化器，使用严格模式（拒绝无效月份，如 "2023-13"）
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM")
                    .withResolverStyle(ResolverStyle.STRICT);
            // 解析为 YearMonth 对象
            YearMonth yearMonth = YearMonth.parse(dateStr, formatter);
            // 指定默认日为每月第一天
            return yearMonth.atDay(1);
        } catch (DateTimeParseException e) {
            throw new IllegalArgumentException("无效的日期格式: " + dateStr, e);
        }
    }

    /**
     * 每天更新产业标签信息
     */
}
