TaskService.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.fdkankan.ucenter.task;
  2. import cn.hutool.json.JSONObject;
  3. import cn.hutool.json.JSONUtil;
  4. import com.fdkankan.common.util.DateUtil;
  5. import com.fdkankan.common.util.FileUtils;
  6. import com.fdkankan.redis.util.RedisUtil;
  7. import com.fdkankan.ucenter.common.RedisKeyUtil;
  8. import com.fdkankan.ucenter.common.StatisticsUtil;
  9. import com.fdkankan.ucenter.common.constants.NacosProperty;
  10. import com.fdkankan.ucenter.common.utils.AuthLicenseUtil;
  11. import com.fdkankan.ucenter.constant.QrCodeFilePath;
  12. import com.fdkankan.ucenter.service.*;
  13. import com.fdkankan.ucenter.util.DateUserUtil;
  14. import com.fdkankan.ucenter.vo.response.AuthLicenseEntityVo;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.scheduling.annotation.Scheduled;
  19. import org.springframework.stereotype.Component;
  20. import java.io.File;
  21. import java.util.Date;
  22. import java.util.Map;
  23. import java.util.Objects;
  24. @Component
  25. public class TaskService {
  26. public static final Logger log = LoggerFactory.getLogger("timeLogger");
  27. @Autowired
  28. RedisUtil redisUtil;
  29. @Autowired
  30. private ISceneProService sceneProService;
  31. @Autowired
  32. private ISceneCleanService sceneCleanService;
  33. @Scheduled(cron = "${task.cron.del_qrCode:0 */10 * * * ?}")
  34. public void delLoginQrCode(){
  35. if("local".equals(NacosProperty.uploadType)){
  36. this.updateSceneViewCount();
  37. return;
  38. }
  39. try {
  40. long startTime = new Date().getTime();
  41. String qrCodePath = QrCodeFilePath.LOGIN_QR_CODE_PATH;
  42. String aliQrCodePath = QrCodeFilePath.ALI_QRCODE_FOLDER;
  43. String wxCodePath = QrCodeFilePath.WEIXIN_QRCODE_FOLDER;
  44. File file = new File(qrCodePath);
  45. File file2 = new File(wxCodePath);
  46. File file3 = new File(aliQrCodePath);
  47. File[] files = file.listFiles();
  48. File[] files1 = file2.listFiles();
  49. File[] files2 = file3.listFiles();
  50. delFile(files);
  51. delFile(files1);
  52. delFile(files2);
  53. log.info("delLoginQrCode:结束定时清理本地登录二维码:耗时{}秒",(new Date().getTime() - startTime)/1000 );
  54. this.updateSceneViewCount();
  55. }catch (Exception e){
  56. log.error("delLoginQrCode错误",e);
  57. }
  58. }
  59. private void updateSceneViewCount() {
  60. String redisKey = RedisKeyUtil.V4_SCENE_VISIT_CNT;
  61. Map<String,String> hmget = redisUtil.hmget(redisKey);
  62. for (String key : hmget.keySet()) {
  63. log.info("定时更新场景viewCount--num:{},viewCount:{}",key,hmget.get(key));
  64. sceneProService.updateDbViewCount(key,hmget.get(key));
  65. }
  66. }
  67. private void delFile(File[] files){
  68. if(Objects.isNull(files) || files.length == 0){
  69. return;
  70. }
  71. for (File f : files) {
  72. if(files == null && files.length >0){
  73. return;
  74. }
  75. if(f==null || !f.getName().contains(".")){
  76. continue;
  77. }
  78. String name = f.getName().substring(0,f.getName().lastIndexOf("."));
  79. if(!redisUtil.hasKey(RedisKeyUtil.QRCODE + name)){
  80. log.info("删除文件:{}",f.getPath());
  81. FileUtils.delFile(f.getPath());
  82. }
  83. }
  84. }
  85. @Autowired
  86. IUserIncrementService userIncrementService;
  87. @Scheduled(cron = "${task.cron.increment_sendMsg:0 0 12 * * ?}")
  88. public void job11() {
  89. if("local".equals(NacosProperty.uploadType)){
  90. return;
  91. }
  92. log.info("每天12:00开始执行定时任务:短信提醒增值权益准备到期");
  93. try {
  94. userIncrementService.incrementExpireSendSms();
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. log.error(e.getMessage());
  98. }
  99. }
  100. @Scheduled(cron = "${task.cron.update_order_status:0 0 0 * * ?}")
  101. public void checkIncrementExpire() {
  102. if("local".equals(NacosProperty.uploadType)){
  103. return;
  104. }
  105. log.info("每天00:00开始执行定时任务:增值权益到期检查");
  106. try {
  107. userIncrementService.incrementExpire();
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. log.error(e.getMessage());
  111. }
  112. }
  113. @Autowired
  114. IOrderService orderService;
  115. @Scheduled(cron = "${task.cron.update_order_status:0 0 1 * * ?}")
  116. public void updateOrderStatus() {
  117. if("local".equals(NacosProperty.uploadType)){
  118. return;
  119. }
  120. log.info("每天01:00开始执行定时任务:更新收货状态");
  121. try {
  122. //更新收货状态,发货后15天,默认用户已经收到货物
  123. orderService.autoUpdateOrderStatus();
  124. } catch (Exception e) {
  125. e.printStackTrace();
  126. log.error(e.getMessage());
  127. }
  128. }
  129. @Autowired
  130. ISceneStatisticsService sceneStatisticsService;
  131. @Scheduled(cron = "${task.cron.add_baidu_data:0 0 2 * * ?}")
  132. public void addBaiduData() {
  133. if("local".equals(NacosProperty.uploadType)){
  134. return;
  135. }
  136. log.info("每天02:00开始执行定时任务:获取百度统计的数据入库");
  137. try {
  138. String dateStr = DateUtil.date2String(DateUtil.daysCalculate(new Date(), -1), DateUtil.YYYYMMDD_DATA_FORMAT);
  139. Map<String, Map<String, Double>> map = StatisticsUtil.findStatisticsData(dateStr);
  140. sceneStatisticsService.addData(map, dateStr);
  141. } catch (Exception e) {
  142. e.printStackTrace();
  143. log.error(e.getMessage());
  144. }
  145. }
  146. /**
  147. * 清理场景nas资源
  148. * 执行时间:每天凌晨
  149. * 删除条件:场景计算时间大于6个月且未删除过资源
  150. */
  151. @Scheduled(cron = "${task.cron.scene_clean_resource:0 0 0/2 * * ?}")
  152. public void sceneCleanResource() {
  153. log.info("每天凌晨12:00开始执行定时任务:清除场景计算目录");
  154. try {
  155. sceneCleanService.sceneCleanResource();
  156. } catch (Exception e) {
  157. log.error("清除失败", e);
  158. log.error(e.getMessage());
  159. }
  160. log.info("清除场景计算目录任务执行完成");
  161. }
  162. }