定时任务
This commit is contained in:
parent
ac64c21e39
commit
ccfc0e2925
|
|
@ -0,0 +1,31 @@
|
|||
package com.bonus.ahsbs.base.dao;
|
||||
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* packageName com.bonus.ahsbs.base.dao
|
||||
*
|
||||
* @author lsun
|
||||
* @version 1.0.0
|
||||
* @className TrainingDao (此处以class为例)
|
||||
* @date 2025/8/8
|
||||
* @description 培评预警
|
||||
*/
|
||||
@Repository(value = "TrainingDao")
|
||||
public interface TrainingDao {
|
||||
|
||||
/**
|
||||
* 获取某天培训人数
|
||||
* @param startTime endTime
|
||||
* @return
|
||||
*/
|
||||
int getAttNum(@Param("startTime") String startTime, @Param("endTime") String endTime);
|
||||
|
||||
/**
|
||||
* 添加培训人数
|
||||
* @param date
|
||||
* @param attNum
|
||||
*/
|
||||
void add(String date, int attNum);
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
package com.bonus.ahsbs.base.task;
|
||||
|
||||
import com.bonus.ahsbs.base.dao.TrainingDao;
|
||||
import com.bonus.ahsbs.base.entity.BlackListBean;
|
||||
import com.bonus.ahsbs.core.util.DateTimeHelper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
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.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* packageName com.bonus.ahsbs.base.task
|
||||
*
|
||||
* @author lsun
|
||||
* @version 1.0.0
|
||||
* @className TrainingTask (此处以class为例)
|
||||
* @date 2025/8/8
|
||||
* @description 培评预警
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@EnableScheduling
|
||||
@EnableAsync
|
||||
public class TrainingTask {
|
||||
|
||||
@Resource(name = "TrainingDao")
|
||||
private TrainingDao dao;
|
||||
|
||||
@Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行
|
||||
// @Scheduled(cron = "0 */3 * * * ?") // 每3秒执行一次
|
||||
public void add() {
|
||||
log.info("培评预警定时任务开始执行");
|
||||
try {
|
||||
String date = DateTimeHelper.getPervDate(1); //获取前一天
|
||||
|
||||
// 使用正确的日期格式解析
|
||||
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
LocalDate localDate = LocalDate.parse(date, dateFormatter);
|
||||
|
||||
// 获取当天开始时间:00:00:00
|
||||
LocalDateTime startOfDay = localDate.atStartOfDay();
|
||||
// 获取当天结束时间:23:59:59
|
||||
LocalDateTime endOfDay = localDate.atTime(23, 59, 59);
|
||||
|
||||
// 使用完整的日期时间格式进行格式化
|
||||
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
String startTime = startOfDay.format(dateTimeFormatter);
|
||||
String endTime = endOfDay.format(dateTimeFormatter);
|
||||
|
||||
int attNum = dao.getAttNum(startTime,endTime); //获取前天出勤人数
|
||||
|
||||
dao.add(date,attNum);
|
||||
} catch (Exception e) {
|
||||
log.error(e.toString(), e);
|
||||
}
|
||||
log.info("培评预警定时任务结束执行");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.bonus.ahsbs.base.dao.TrainingDao">
|
||||
<insert id="add">
|
||||
INSERT INTO `xbg_training`(`date`, `att_num`)
|
||||
VALUES (#{ date}, #{attNum})
|
||||
</insert>
|
||||
|
||||
<select id="getAttNum" resultType="java.lang.Integer">
|
||||
SELECT
|
||||
count( 1 )
|
||||
FROM
|
||||
( SELECT id, id_card AS idNumber FROM xbg_user_attendance
|
||||
WHERE data_type IN ( '1', '2', '3' ) AND check_time BETWEEN #{startTime} AND #{endTime}
|
||||
GROUP BY id_card
|
||||
) a
|
||||
</select>
|
||||
</mapper>
|
||||
Loading…
Reference in New Issue