1. 程式人生 > >定時任務案例1:每月生成創意精選提醒

定時任務案例1:每月生成創意精選提醒

package nd.sdp.idea.modules.schedule;
  2 
  3 import nd.sdp.idea.modules.idea.entity.Idea;
  4 import nd.sdp.idea.modules.idea.enums.OriginalityType;
  5 import nd.sdp.idea.modules.idea.service.IdeaService;
  6 import nd.sdp.idea.modules.reminder.entity.IdeaSelectionReminder;
  7 import nd.sdp.idea.modules.reminder.service.ISReminderService;
8 import org.joda.time.DateTime; 9 import org.joda.time.format.ISODateTimeFormat; 10 import org.slf4j.Logger; 11 import org.slf4j.LoggerFactory; 12 import org.springframework.scheduling.annotation.Scheduled; 13 import org.springframework.stereotype.Component; 14 15 import javax.annotation.Resource;
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 IdeaSelectionReminderTask { 26 27 private static final Logger logger = LoggerFactory.getLogger(IdeaSelectionReminderTask.class
); 28 29 @Resource 30 private IdeaService ideaService; 31 @Resource 32 private ISReminderService isReminderService; 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 weeklySelectionRemind() { 39 logger.info("IdeaSelectionReminderTask weeklySelectionRemind start."); 40 logger.info("周精選任務開始"); 41 DateTime nowTime = DateTime.now(); 42 String now = nowTime.toString(DATE_FORMAT); 43 String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 44 .minusWeeks(1).toString(DATE_FORMAT); 45 String to = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 46 .minusDays(1).toString(DATE_FORMAT); 47 doWeekly(from, to); 48 logger.info("周精選任務完成"); 49 logger.info("IdeaSelectionReminderTask weeklySelectionRemind finish."); 50 } 51 52 //可供介面外部觸發 53 public void doWeekly(String from, String to) { 54 long fromTime = DateTime.parse(from, ISODateTimeFormat.dateElementParser()).getMillis(); 55 DateTime toDateTime = DateTime.parse(to, ISODateTimeFormat.dateElementParser()); 56 long toTime = toDateTime.plusDays(1).getMillis();//下一天0點 57 int month = toDateTime.getMonthOfYear();//周所屬月份,按週日所在的月份來看 58 int quarter = (month - 1) / 3 + 1; 59 int year = toDateTime.getYear();//周所屬年份,按週日所在的年來看 60 List<String> userIdList = ideaService.getUserIdList(); 61 for (String userId : userIdList) { 62 List<Idea> ideaList = ideaService.findOriginalities( 63 userId, fromTime, toTime, null); 64 if (ideaList.size() > 0) { 65 IdeaSelectionReminder reminder = 66 buildReminder(userId, OriginalityType.week, from, to, fromTime, toTime, 67 year, quarter, month); 68 isReminderService.save(reminder); 69 } 70 } 71 } 72 73 //每月一號0點0分0秒觸發 74 //當中再判斷當前月份進行季度和年度的處理操作 75 @Scheduled(cron = "0 0 0 1 * ? ") 76 public void monthlySelectionRemind() { 77 logger.info("IdeaSelectionReminderTask monthlySelectionRemind start."); 78 DateTime nowTime = DateTime.now(); 79 int month = nowTime.getMonthOfYear(); 80 String now = nowTime.toString(DATE_FORMAT); 81 //年度處理: 1 82 if (month == 1) { 83 logger.info("年度精選任務開始"); 84 String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 85 .minusYears(1).toString(DATE_FORMAT); 86 String to = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 87 .minusDays(1).toString(DATE_FORMAT); 88 doMonthly(from, to, OriginalityType.year); 89 logger.info("年度精選任務完成"); 90 } 91 //季度處理: 3(4) 6(7) 9(10) 12(1) 92 if (month == 4 || month == 7 || month == 10 || month == 1) { 93 logger.info("季度精選任務開始"); 94 String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 95 .minusMonths(3).toString(DATE_FORMAT); 96 String to = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 97 .minusDays(1).toString(DATE_FORMAT); 98 doMonthly(from, to, OriginalityType.quarter); 99 logger.info("季度精選任務完成"); 100 } 101 //月份處理 102 logger.info("月精選任務開始"); 103 String from = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 104 .minusMonths(1).toString(DATE_FORMAT); 105 String to = DateTime.parse(now, ISODateTimeFormat.dateElementParser()) 106 .minusDays(1).toString(DATE_FORMAT); 107 doMonthly(from, to, OriginalityType.month); 108 logger.info("月精選任務完成"); 109 logger.info("IdeaSelectionReminderTask monthlySelectionRemind finish."); 110 } 111 112 public void doMonthly(String from, String to, OriginalityType type) { 113 DateTime fromDT = DateTime.parse(from, ISODateTimeFormat.dateElementParser()); 114 //修正開始時間為所在周的週一 115 fromDT = getMondayDateTime(fromDT); 116 DateTime toDT = DateTime.parse(to, ISODateTimeFormat.dateElementParser()); 117 int month = toDT.getMonthOfYear(); 118 int quarter = (month - 1) / 3 + 1; 119 int year = toDT.getYear(); 120 long fromTime = fromDT.getMillis(); 121 long toTime = toDT.getMillis(); 122 List<String> userIdList = ideaService.getUserIdList(); 123 //todo 這裡不管有沒有待選項都生成記錄,如果要求沒有不能提醒,則需要在篩選接口裡面做特殊處理,把沒有待選項的提醒標記為完成 124 for (String userId : userIdList) { 125 IdeaSelectionReminder reminder = 126 buildReminder(userId, type, from, to, fromTime, toTime, 127 year, quarter, month); 128 isReminderService.save(reminder); 129 } 130 } 131 132 133 private IdeaSelectionReminder buildReminder(String userId, OriginalityType type, String from, String to, 134 long fromTime, long toTime, int year, int quarter, int month) { 135 IdeaSelectionReminder reminder = new IdeaSelectionReminder(); 136 reminder.setUserId(userId); 137 reminder.setType(type); 138 reminder.setFrom(from); 139 reminder.setFromTime(fromTime); 140 reminder.setTo(to); 141 reminder.setToTime(toTime); 142 reminder.setYear(year); 143 reminder.setQuarter(quarter); 144 reminder.setMonth(month); 145 reminder.setFinished(false); 146 return reminder; 147 } 148 149 // 獲取date所在周的週一的時間 150 private static DateTime getMondayDateTime(DateTime date) { 151 int dayOfWeek = date.getDayOfWeek(); 152 return date.minusDays(dayOfWeek-1); 153 } 154 155 public static void main(String[] args) { 156 System.out.println(DateTime.now().getMonthOfYear()); 157 System.out.println(DateTime.parse("2017-01-01", ISODateTimeFormat.dateElementParser()).minusMonths(3)); 158 DateTime dt = DateTime.parse("2015-11-01", ISODateTimeFormat.dateElementParser()); 159 System.out.println(getMondayDateTime(dt)); 160 } 161 }