TaskService.java 6.3 KB

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