定时任务案例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 }

猜你喜欢

转载自blog.csdn.net/qq_36838191/article/details/80480317