1 package nd.sdp.idea.modules.schedule; 2 3 import nd.sdp.idea.modules.idea.entity.Idea; 4 import nd.sdp.idea.modules.idea.service.IdeaService; 5 import nd.sdp.idea.modules.reminder.entity.CyclePendingIdeaReminder; 6 import nd.sdp.idea.modules.reminder.service.CPIReminderService; 7 import org.joda.time.DateTime; 8 import org.joda.time.format.ISODateTimeFormat; 9 import org.slf4j.Logger; 10 import org.slf4j.LoggerFactory; 11 import org.springframework.scheduling.annotation.Scheduled; 12 import org.springframework.stereotype.Component; 13 14 import javax.annotation.Resource; 15 import java.util.Date; 16 import java.util.List; 17 18 /** 19 * 周期性待处理提醒任务 20 * 21 * @author yanguanyu(290536) 22 * @since 0.1 created on 2016/10/20. 23 */ 24 @Component 25 public class CyclePendingReminderTask { 26 27 private static final Logger logger = LoggerFactory.getLogger(CyclePendingReminderTask.class); 28 29 @Resource 30 private IdeaService ideaService; 31 @Resource 32 private CPIReminderService cpiReminderService; 33 34 private static final String DATE_FORMAT = "dd/MM/yyyy"; 35 36 //每周一0点0分0秒触发,处理上上周 37 @Scheduled(cron = "0 0 0 ? * MON ") 38 public void weeklyRemind() { 39 logger.info("CyclePendingReminderTask.weeklyRemind"); 40 logger.info("周期性待处理提醒任务开始"); 41 String now = DateTime.now().toString(DATE_FORMAT); 42 //往前推2周,上上周周一 43 String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 44 .minusWeeks(2).toString(DATE_FORMAT); 45 //上上周周日 46 String to = DateTime.parse(from, ISODateTimeFormat.dateElementParser()) 47 .plusWeeks(1).minusDays(1).toString(DATE_FORMAT); 48 //上上周周一0点时间戳 49 long fromTime = DateTime.parse(from, ISODateTimeFormat.dateElementParser()).getMillis(); 50 //上周周一0点时间戳 51 long toTime = DateTime.parse(to, ISODateTimeFormat.dateElementParser()).plus(1).getMillis(); 52 List<String> userIdList = ideaService.getUserIdList(); 53 for (String userId : userIdList) { 54 List<Idea> ideaList = ideaService.findIdeasByCreateAt(userId, fromTime, toTime); 55 //有创建想法才会有提醒 56 if (ideaList.size() > 0) { 57 CyclePendingIdeaReminder reminder = new CyclePendingIdeaReminder(); 58 reminder.setUserId(userId); 59 reminder.setFrom(from); 60 reminder.setTo(to); 61 reminder.setFinished(false); 62 cpiReminderService.save(reminder); 63 } 64 } 65 logger.info("周期性待处理提醒任务完成"); 66 } 67 68 public static void main(String[] args) { 69 Date date = DateTime.parse("2016-10-17", ISODateTimeFormat.dateElementParser()).minusWeeks(2).toDate(); 70 System.out.println(date); 71 date = DateTime.parse("2016-10-03", ISODateTimeFormat.dateElementParser()).plusWeeks(1).minusDays(1).toDate(); 72 System.out.println(date); 73 System.out.println(DateTime.now().toString(DATE_FORMAT)); 74 } 75 }