@Component
@Lazy(false)
public class EnterpriseNotification implements SchedulingConfigurer {
//每月5号 0 0 0 5 * ?
private final static String DEFAULT_CRON = "0 0 0 5 * ?";
@Resource
private EnterpriseUserService enterpriseUserService;
@Resource
private NoticeService noticeService;
@Resource
private NoticeUserService noticeUserService;
@Resource
private ConfigService configService;
//@Scheduled(cron = "${enterprise.push}")
private void noticePush() {
Config configContent = configService.getConfigContent("enterprise.nameList", Config.Type.All);
String[] enterpriseIds = null;
if (configContent != null) {
enterpriseIds = configContent.getValue().split(",");
}
Date now = new Date();
String generate = UUIDUtils.generate();
Notice notice = new Notice();
notice.setId(generate);
notice.setIsDeleted(false);
notice.setCreateDate(now);
notice.setEditDate(now);
Config title = configService.getConfigContent("enterprise.title", Config.Type.All);
notice.setTitle(title == null ? "......" : title.getValue());
Config content = configService.getConfigContent("enterprise.content", Config.Type.All);
notice.setContent(content == null ? "......" : content.getValue());
notice.setType(Notice.Type.guide);
notice.setNoticeDate(now);
notice.setCount(1);
noticeService.saveNotice(notice);
if (enterpriseIds != null) {
for (String enterpriseId : enterpriseIds) {
List<EnterpriseUser> enterpriseUsers = enterpriseUserService.findByEnterprise(enterpriseId);
for (EnterpriseUser eu : enterpriseUsers) {
NoticeUser noticeUser = new NoticeUser();
noticeUser.setIsDeleted(false);
noticeUser.setCreateDate(now);
noticeUser.setEditDate(now);
noticeUser.setCreator("sys_job");
noticeUser.setEditor("sys_job");
noticeUser.setEnterprise(eu.getEnterprise());
noticeUser.setNoticeId(generate);
noticeUser.setUserId(eu.getId());
noticeUser.setStatus("1");
noticeUser.setId(UUIDUtils.generate());
noticeUser.setNoticeUserType(NoticeUser.NoticeUserType.guide);
noticeUserService.saveNoticeUser(noticeUser);
}
}
}
}
private String getCron() {
Config configContent = configService.getConfigContent("enterprise.push", Config.Type.All);
if (configContent == null || StringUtils.isBlank(configContent.getValue())) {
return DEFAULT_CRON;
}
return configContent.getValue();
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(() -> noticePush(), triggerContext -> {
String cron = getCron();
CronTrigger trigger = new CronTrigger(cron);
Date next = trigger.nextExecutionTime(triggerContext);
return next;
});
}
}
|