|
@@ -0,0 +1,122 @@
|
|
|
+package com.qunzhixinxi.hnqz.admin.config;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.date.DatePattern;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.SysDept;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.SysNotice;
|
|
|
+import com.qunzhixinxi.hnqz.admin.api.entity.SysUserRole;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.SysDeptService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.SysNoticeService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.SysUserRoleService;
|
|
|
+import com.qunzhixinxi.hnqz.admin.service.SysUserService;
|
|
|
+import com.qunzhixinxi.hnqz.common.core.constant.CommonConstants;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.LinkedList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 系统定时任务
|
|
|
+ *
|
|
|
+ * @author jimmy
|
|
|
+ * @date 2024-11-21 17:11
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Configuration
|
|
|
+@EnableScheduling
|
|
|
+@AllArgsConstructor
|
|
|
+public class ScheduleConfiguration {
|
|
|
+
|
|
|
+ private final SysDeptService deptService;
|
|
|
+ private final SysUserRoleService userRoleService;
|
|
|
+ private final SysNoticeService noticeService;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 每天凌晨1点中 企业是否过期
|
|
|
+ */
|
|
|
+ @Async
|
|
|
+ @Scheduled(cron = "0 0 1 * * *")
|
|
|
+ public void checkEntExpiryDaily() {
|
|
|
+ log.info("=================[checkEntExpiryDaily start]=================");
|
|
|
+
|
|
|
+ try {
|
|
|
+ List<SysDept> depts = deptService.list(Wrappers.<SysDept>lambdaQuery().eq(SysDept::getLevel, 4).eq(SysDept::getDelFlag, CommonConstants.STATUS_NORMAL));
|
|
|
+
|
|
|
+ if (CollUtil.isEmpty(depts)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 过滤当前日期到截止日期少于30天的
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+ List<SysDept> collect = depts.stream().filter(d ->
|
|
|
+ {
|
|
|
+ LocalDate expiryDate = d.getExpiryDate();
|
|
|
+ return expiryDate != null && (today.plusMonths(1).isAfter(expiryDate));
|
|
|
+ // return true;
|
|
|
+ }
|
|
|
+ ).collect(Collectors.toList());
|
|
|
+
|
|
|
+ if (CollUtil.isEmpty(collect)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 封装为消息,推送给区域管理员 roleId = 4
|
|
|
+
|
|
|
+ List<SysUserRole> userRoles = userRoleService.list(Wrappers.<SysUserRole>lambdaQuery().eq(SysUserRole::getRoleId, 4));
|
|
|
+
|
|
|
+ if (CollUtil.isEmpty(userRoles)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Integer> userIds = userRoles.stream().mapToInt(SysUserRole::getUserId).boxed().distinct().sorted().collect(Collectors.toList());
|
|
|
+
|
|
|
+
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DatePattern.CHINESE_DATE_PATTERN);
|
|
|
+ List<SysNotice> notices = new LinkedList<>();
|
|
|
+ for (Integer userId : userIds) {
|
|
|
+
|
|
|
+ for (SysDept dept : collect) {
|
|
|
+ SysNotice notice = new SysNotice();
|
|
|
+ notice.setTitle(String.format("【%s】即将到期", dept.getName()));
|
|
|
+ notice.setContent(String.format("【%s】服务时间将于【%s】到期,请关注!", dept.getName(), formatter.format(dept.getExpiryDate())));
|
|
|
+ notice.setUserId(userId);
|
|
|
+ notice.setReadFlag(false);
|
|
|
+ notice.setCreateBy("admin");
|
|
|
+ notice.setCreateTime(now);
|
|
|
+ notices.add(notice);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (CollUtil.isEmpty(notices)) {
|
|
|
+ log.info("<<<<<< 不存在即将到期的服务商 >>>>>>");
|
|
|
+ } else {
|
|
|
+ noticeService.saveBatch(notices);
|
|
|
+ log.info("<<<<<< 存在即将到期的服务商,计划发送通知{}条 >>>>>>", notices.size());
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ log.info("=================[checkEntExpiryDaily end ]=================");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ //
|
|
|
+ // @Async
|
|
|
+ // @Scheduled(cron = "0/4 * * * * *")
|
|
|
+ // public void checkEntExpiryDaily1() {
|
|
|
+ // log.info("checkEntExpiryDaily1");
|
|
|
+ //
|
|
|
+ // }
|
|
|
+}
|