ScheduledTask.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. package com.qxgmat.task;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.github.pagehelper.Page;
  5. import com.nuliji.tools.Tools;
  6. import com.nuliji.tools.Transform;
  7. import com.nuliji.tools.third.OauthData;
  8. import com.qxgmat.data.constants.enums.ServiceKey;
  9. import com.qxgmat.data.constants.enums.SettingKey;
  10. import com.qxgmat.data.constants.enums.module.CourseModule;
  11. import com.qxgmat.data.constants.enums.module.ProductType;
  12. import com.qxgmat.data.constants.enums.status.MessageStatus;
  13. import com.qxgmat.data.dao.entity.*;
  14. import com.qxgmat.data.relation.entity.UserPrepareRelation;
  15. import com.qxgmat.help.WechatHelp;
  16. import com.qxgmat.service.UserPaperService;
  17. import com.qxgmat.service.UserServiceService;
  18. import com.qxgmat.service.UsersService;
  19. import com.qxgmat.service.extend.CourseExtendService;
  20. import com.qxgmat.service.extend.MessageExtendService;
  21. import com.qxgmat.service.extend.PreviewService;
  22. import com.qxgmat.service.inline.*;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.scheduling.annotation.Scheduled;
  27. import org.springframework.stereotype.Component;
  28. import javax.tools.Tool;
  29. import java.text.DateFormat;
  30. import java.text.ParseException;
  31. import java.text.SimpleDateFormat;
  32. import java.util.*;
  33. /**
  34. * Created by gaojie on 2017/11/20.
  35. */
  36. @Component
  37. public class ScheduledTask {
  38. private static final Logger logger = LoggerFactory.getLogger(ScheduledTask.class);
  39. @Autowired
  40. private AsyncTask asyncTask;
  41. @Autowired
  42. private SettingService settingService;
  43. @Autowired
  44. private UsersService usersService;
  45. @Autowired
  46. private WechatHelp wechatHelp;
  47. @Autowired
  48. private MessageTemplateService messageTemplateService;
  49. @Autowired
  50. private MessageExtendService messageExtendService;
  51. @Autowired
  52. private CourseService courseService;
  53. @Autowired
  54. private CourseNoService courseNoService;
  55. @Autowired
  56. private PreviewService previewService;
  57. @Autowired
  58. private PreviewAssignService previewAssignService;
  59. @Autowired
  60. private UserPaperService userPaperService;
  61. @Autowired
  62. private UserCourseAppointmentService userCourseAppointmentService;
  63. @Autowired
  64. private UserOrderRecordService userOrderRecordService;
  65. @Autowired
  66. private CourseExtendService courseExtendService;
  67. @Autowired
  68. private UserCourseProgressService userCourseProgressService;
  69. @Autowired
  70. private UserCourseRecordService userCourseRecordService;
  71. @Autowired
  72. private UserServiceService userServiceService;
  73. /**
  74. * cron表达式:* * * * * *(共6位,使用空格隔开,具体如下)
  75. * cron表达式:*(秒0-59) *(分钟0-59) *(小时0-23) *(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT)
  76. * 注意: 30 * * * * * 表示每分钟的第30秒执行,而(*斜杠30)表示每30秒执行
  77. *
  78. * */
  79. // 每小时刷新快过期用户的accessToken
  80. @Scheduled(cron="0 0 * * * *")
  81. public void refreshWechatToken(){
  82. logger.info("Start refresh Wechat token");
  83. int page = 1;
  84. int size = 100;
  85. Date date = new Date();
  86. Page<User> userList;
  87. do{
  88. userList = usersService.listByWechatExpire(page, size, date);
  89. for(User user : userList){
  90. OauthData data = wechatHelp.refreshNative(user.getWechatRefreshToken());
  91. usersService.edit(User.builder()
  92. .id(user.getId())
  93. .wechatAccessToken(data.getAccessToken())
  94. .wechatRefreshToken(data.getRefreshToken())
  95. .wechatExpireTime(data.getExpiresTime())
  96. .build()
  97. );
  98. }
  99. }while(userList.size() >= size);
  100. }
  101. // 每小时刷新备考统计信息
  102. @Scheduled(cron="0 0 * * * *")
  103. public void refreshPrepare(){
  104. logger.info("Start refresh Prepare");
  105. JSONObject value = new JSONObject();
  106. // 分别统计不同信息
  107. List<UserPrepareRelation> relations = null;
  108. // 身份
  109. relations = usersService.statPrepareStatus();
  110. JSONArray status = new JSONArray();
  111. for(UserPrepareRelation relation : relations){
  112. JSONObject one = new JSONObject();
  113. one.put("key", relation.getS());
  114. one.put("value", relation.getNumber());
  115. status.add(one);
  116. }
  117. value.put("status", status);
  118. // 考分
  119. relations = usersService.statPrepareGoal();
  120. JSONArray goal = new JSONArray();
  121. for(UserPrepareRelation relation : relations){
  122. JSONObject one = new JSONObject();
  123. one.put("key", relation.getI());
  124. one.put("value", relation.getNumber());
  125. goal.add(one);
  126. }
  127. value.put("goal", goal);
  128. // 考试时间
  129. relations = usersService.statPrepareExaminationTime();
  130. JSONArray examinationTime = new JSONArray();
  131. for(UserPrepareRelation relation : relations){
  132. JSONObject one = new JSONObject();
  133. one.put("key", relation.getS());
  134. one.put("value", relation.getNumber());
  135. examinationTime.add(one);
  136. }
  137. value.put("examinationTime", examinationTime);
  138. // 出分时间
  139. relations = usersService.statPrepareScoreTime();
  140. JSONArray scoreTime = new JSONArray();
  141. for(UserPrepareRelation relation : relations){
  142. JSONObject one = new JSONObject();
  143. one.put("key", relation.getS());
  144. one.put("value", relation.getNumber());
  145. scoreTime.add(one);
  146. }
  147. value.put("scoreTime", scoreTime);
  148. Setting setting = Setting.builder().value(value).build();
  149. settingService.editByKey(SettingKey.PREPARE_INFO, setting);
  150. }
  151. // 每小时判断发送消息
  152. @Scheduled(cron="0 0 * * * *")
  153. public void autoSendMessage(){
  154. logger.info("Start auto Send message");
  155. List<MessageTemplate> list = messageTemplateService.listCustomerExpire();
  156. for(MessageTemplate message : list){
  157. Map<String, String> params = new HashMap<>();
  158. messageTemplateService.edit(MessageTemplate.builder()
  159. .id(message.getId())
  160. .sendStatus(MessageStatus.SENDING.index)
  161. .build());
  162. int number = 0;
  163. int page = 1;
  164. int size = 20;
  165. List<User> userList;
  166. do{
  167. userList = usersService.listAdmin(page, size, null, null, null, null, null, null, null, null);
  168. for(User user : userList){
  169. messageExtendService.sendCustom(user, message, params);
  170. number += 1;
  171. }
  172. }while(userList.size()>= size);
  173. messageTemplateService.edit(MessageTemplate.builder()
  174. .id(message.getId())
  175. .sendNumber(number)
  176. .sendStatus(MessageStatus.SENDED.index)
  177. .build());
  178. }
  179. }
  180. // 每天判断是否自动组卷
  181. @Scheduled(cron="0 1 0 * * *")
  182. public void autoExercisePaper() throws ParseException {
  183. Setting setting = settingService.getByKey(SettingKey.EXERCISE_PAPER_AUTO);
  184. JSONObject value = setting.getValue();
  185. String dateString = value.getString("date");
  186. if (dateString == null) {
  187. logger.info("auto Exercise Paper stop, date is empty");
  188. return;
  189. }
  190. Date date = DateFormat.getDateTimeInstance().parse(dateString);
  191. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  192. String param = sdf.format(date);
  193. String now = sdf.format(new Date());
  194. if(param.equals(now)){
  195. // 当天
  196. logger.info("auto Exercise Paper start");
  197. asyncTask.autoExercisePaperError();
  198. logger.info("auto Exercise Paper finish");
  199. }
  200. }
  201. // 每小时判断发送预习作业消息:发送预习作业到期通知:6小时,去除夜间12-7点
  202. @Scheduled(cron="0 0 * * * *")
  203. public void autoSendPreviewMessage(){
  204. // 只考虑assign设定到期时间的作业
  205. Date now = new Date();
  206. Date startTime = Tools.addHour(now, 5);
  207. Date endTime = Tools.addHour(now, 6);
  208. int hour = Tools.hour(endTime);
  209. if (hour == 0){
  210. endTime = Tools.addHour(endTime, 7);
  211. }else if(hour >0 && hour <= 7){
  212. startTime = Tools.addHour(startTime, 7);
  213. endTime = Tools.addHour(endTime, 7);
  214. }else if(hour > 7 && hour <= 14){
  215. // 8-14已经提早播报,暂停
  216. return;
  217. }
  218. logger.info("Start auto Send Preview message");
  219. List<PreviewAssign> previewAssignList = previewAssignService.listAppointmentExpire(startTime.toString(), endTime.toString());
  220. for(PreviewAssign previewAssign : previewAssignList){
  221. Course course = courseService.get(previewAssign.getCourseId());
  222. if (previewAssign.getCourseAppointment() > 0){
  223. // 1v1,查找对应预约用户
  224. UserCourseAppointment appointment = userCourseAppointmentService.get(previewAssign.getCourseAppointment());
  225. User user = usersService.get(appointment.getUserId());
  226. // 替换课程题目
  227. course.setTitle(appointment.getTitle());
  228. messageExtendService.sendPreviewNotice(user, course, previewAssign);
  229. }
  230. // if (previewAssign.getCourseTime() > 0){
  231. // previewService.replaceTitle(previewAssign);
  232. // // 小班课,查找时间段用户
  233. // List<UserOrderRecord> userOrderRecordList = userOrderRecordService.listByOnline(previewAssign.getCourseId(), previewAssign.getCourseTime());
  234. // for(UserOrderRecord record : userOrderRecordList){
  235. // User user = usersService.get(record.getUserId());
  236. // messageExtendService.sendPreviewNotice(user, course, previewAssign);
  237. // }
  238. // }
  239. }
  240. logger.info("End auto Send Preview message");
  241. }
  242. // 课程到期:判断延期奖励,听课频率<=2天/作业100%->10天,90%以上7天
  243. // @Scheduled(cron="0 0 * * * *")
  244. public void awardCourseExpire(){
  245. // 下一小时内到期的课程
  246. Date startTime = new Date();
  247. Date endTime = Tools.addHour(startTime, 1);
  248. List<UserOrderRecord> recordList = userOrderRecordService.allCourseUseExpire(startTime.toString(), endTime.toString());
  249. for(UserOrderRecord record : recordList){
  250. Course course = courseService.get(record.getId());
  251. if (CourseModule.ValueOf(course.getCourseModule()) != CourseModule.VIDEO){
  252. continue;
  253. }
  254. List<CourseNo> courseNoList = courseNoService.allCourse(course.getId());
  255. Collection courseNoIds = Transform.getIds(courseNoList, CourseNo.class, "id");
  256. // 听课频率
  257. List<UserCourseProgress> progressList = userCourseProgressService.listCourse(record.getId(), course.getId());
  258. List<UserCourseRecord> records = userCourseRecordService.allWithRecord(record.getId());
  259. Integer currentNo = courseExtendService.computeCourseNoCurrent(courseNoList, progressList);
  260. Integer days = courseExtendService.computeCourseDay(record);
  261. if (days/currentNo > 2){
  262. continue;
  263. }
  264. // 获取所有作业进度
  265. List<PreviewAssign> previewAssignList = previewAssignService.listByCourseNos(course.getId(), courseNoIds);
  266. Collection assignIds = Transform.getIds(previewAssignList, PreviewAssign.class, "id");
  267. List<UserPaper> userPaperList = userPaperService.listWithCourse(record.getUserId(), assignIds, record.getId());
  268. // Collection paperIds = Transform.getIds(userPaperList, UserPaper.class, "id");
  269. // List<UserReport> userReportList = userReportService.listByPaper(paperIds);
  270. int finish = 0;
  271. for(UserPaper userPaper: userPaperList){
  272. if (userPaper.getTimes() > 0){
  273. finish += 1;
  274. }
  275. }
  276. int percent = finish * 100 / assignIds.size();
  277. if (percent <90){
  278. continue;
  279. }
  280. int day = 7;
  281. if (percent == 100){
  282. day = 10;
  283. }
  284. courseExtendService.awardCourse(record.getUserId(), record.getId(), day);
  285. }
  286. }
  287. // 每天判断是否恢复停课: 最多30就需要恢复停课
  288. @Scheduled(cron="0 1 0 * * *")
  289. public void restoreSuspendCourse() {
  290. Date endTime = Tools.today();
  291. Date startTime = Tools.addDate(endTime, 30);
  292. List<UserOrderRecord> recordList = userOrderRecordService.allSuspendExpire(startTime.toString(), endTime.toString());
  293. for(UserOrderRecord record : recordList){
  294. courseExtendService.restoreCourse(record.getUserId(), record.getId());
  295. }
  296. }
  297. // 每天判断VIP,使用有效期还剩10天
  298. @Scheduled(cron="0 1 0 * * *")
  299. public void vipUseExpire() {
  300. Date endTime = Tools.today();
  301. Date startTime = Tools.addDate(endTime, 10);
  302. List<UserService> serviceList = userServiceService.allExpire(ServiceKey.VIP, startTime.toString(), endTime.toString());
  303. Collection userIds = Transform.getIds(serviceList, UserOrderRecord.class, "userId");
  304. List<User> userList = usersService.select(userIds);
  305. Map userMap = Transform.getMap(userList, User.class, "id");
  306. for(UserService service : serviceList){
  307. User user = (User) userMap.get(service.getUserId());
  308. messageExtendService.sendUseExpire(user, UserOrderRecord.builder()
  309. .userId(user.getId())
  310. .productType(ProductType.SERVICE.key)
  311. .service(ServiceKey.VIP.key)
  312. .useEndTime(service.getExpireTime())
  313. .build(), 10);
  314. }
  315. }
  316. // 每天判断模考一直没开通、距离开通有效期还剩30天
  317. @Scheduled(cron="0 1 0 * * *")
  318. public void catOpenExpire() {
  319. Date endTime = Tools.today();
  320. Date startTime = Tools.addDate(endTime, 30);
  321. List<UserOrderRecord> recordList = userOrderRecordService.allServiceOpenExpire(ServiceKey.QX_CAT, startTime.toString(), endTime.toString());
  322. Collection userIds = Transform.getIds(recordList, UserOrderRecord.class, "userId");
  323. List<User> userList = usersService.select(userIds);
  324. Map userMap = Transform.getMap(userList, User.class, "id");
  325. for(UserOrderRecord record : recordList){
  326. User user = (User) userMap.get(record.getUserId());
  327. messageExtendService.sendOpenExpire(user, record, 30);
  328. }
  329. }
  330. // 每天判断模考已开通,使用有效期还剩10天
  331. @Scheduled(cron="0 1 0 * * *")
  332. public void catUseExpire() {
  333. Date endTime = Tools.today();
  334. Date startTime = Tools.addDate(endTime, 10);
  335. List<UserOrderRecord> recordList = userOrderRecordService.allServiceUseExpire(ServiceKey.QX_CAT, startTime.toString(), endTime.toString());
  336. Collection userIds = Transform.getIds(recordList, UserOrderRecord.class, "userId");
  337. List<User> userList = usersService.select(userIds);
  338. Map userMap = Transform.getMap(userList, User.class, "id");
  339. for(UserOrderRecord record : recordList){
  340. User user = (User) userMap.get(record.getUserId());
  341. messageExtendService.sendUseExpire(user, record, 10);
  342. }
  343. }
  344. // 每天判断机经一直没开通、距离开通有效期还剩30天。
  345. @Scheduled(cron="0 1 0 * * *")
  346. public void textbookOpenExpire() {
  347. Date endTime = Tools.today();
  348. Date startTime = Tools.addDate(endTime, 30);
  349. List<UserOrderRecord> recordList = userOrderRecordService.allServiceOpenExpire(ServiceKey.TEXTBOOK, startTime.toString(), endTime.toString());
  350. Collection userIds = Transform.getIds(recordList, UserOrderRecord.class, "userId");
  351. List<User> userList = usersService.select(userIds);
  352. Map userMap = Transform.getMap(userList, User.class, "id");
  353. for(UserOrderRecord record : recordList){
  354. User user = (User) userMap.get(record.getUserId());
  355. messageExtendService.sendOpenExpire(user, record, 30);
  356. }
  357. }
  358. // 每天判断机经已开通,使用有效期还剩10天
  359. @Scheduled(cron="0 1 0 * * *")
  360. public void textbookUseExpire() {
  361. Date endTime = Tools.today();
  362. Date startTime = Tools.addDate(endTime, 30);
  363. List<UserOrderRecord> recordList = userOrderRecordService.allServiceUseExpire(ServiceKey.TEXTBOOK, startTime.toString(), endTime.toString());
  364. Collection userIds = Transform.getIds(recordList, UserOrderRecord.class, "userId");
  365. List<User> userList = usersService.select(userIds);
  366. Map userMap = Transform.getMap(userList, User.class, "id");
  367. for(UserOrderRecord record : recordList){
  368. User user = (User) userMap.get(record.getUserId());
  369. messageExtendService.sendUseExpire(user, record, 10);
  370. }
  371. }
  372. // 每天判断课程一直没开通、距离开通有效期还剩30天。
  373. @Scheduled(cron="0 1 0 * * *")
  374. public void courseOpenExpire() {
  375. Date endTime = Tools.today();
  376. Date startTime = Tools.addDate(endTime, 30);
  377. List<UserOrderRecord> recordList = userOrderRecordService.allCourseOpenExpire(startTime.toString(), endTime.toString());
  378. Collection userIds = Transform.getIds(recordList, UserOrderRecord.class, "userId");
  379. List<User> userList = usersService.select(userIds);
  380. Map userMap = Transform.getMap(userList, User.class, "id");
  381. for(UserOrderRecord record : recordList){
  382. User user = (User) userMap.get(record.getUserId());
  383. messageExtendService.sendOpenExpire(user, record, 30);
  384. }
  385. }
  386. // 每天判断课程已开通,使用有效期还剩10天
  387. @Scheduled(cron="0 1 0 * * *")
  388. public void courseUseExpire() {
  389. Date endTime = Tools.today();
  390. Date startTime = Tools.addDate(endTime, 10);
  391. List<UserOrderRecord> recordList = userOrderRecordService.allCourseUseExpire(startTime.toString(), endTime.toString());
  392. Collection userIds = Transform.getIds(recordList, UserOrderRecord.class, "userId");
  393. List<User> userList = usersService.select(userIds);
  394. Map userMap = Transform.getMap(userList, User.class, "id");
  395. for(UserOrderRecord record : recordList){
  396. User user = (User) userMap.get(record.getUserId());
  397. messageExtendService.sendUseExpire(user, record, 10);
  398. }
  399. }
  400. }