|
@@ -0,0 +1,142 @@
|
|
|
+package com.gis.task;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.gis.common.util.RedisUtil;
|
|
|
+import com.gis.domain.entity.ReportEntity;
|
|
|
+import com.gis.service.FodderService;
|
|
|
+import com.gis.service.ReportService;
|
|
|
+import com.gis.service.WorkService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.junit.Test;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2022/11/9 0009 16:42
|
|
|
+ * 定时器
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class VisitTask {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RedisUtil redisUtil;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ReportService reportService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ WorkService workService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ FodderService fodderService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 需要在启动类加入注解
|
|
|
+ * 每天24点检查未开始的活动
|
|
|
+ * 每天24点执行一次 @Scheduled(cron=" 0 0 0 * * ? ") @Scheduled(cron=" * 1 * * ? ")
|
|
|
+ *
|
|
|
+ * Scheduled(cron=" 10 0/5 * * * ? ") //每五分钟执行一次
|
|
|
+ * Scheduled(cron = "0 0 3 * * ?") 每天凌晨三点
|
|
|
+ */
|
|
|
+ @Scheduled(cron = "0 0 2 * * ?")
|
|
|
+ public void task() {
|
|
|
+
|
|
|
+ log.warn("每天02:00开始执行定时任务:redis访问量数据同步到数据库");
|
|
|
+ try {
|
|
|
+
|
|
|
+ // 当前时间的前一天
|
|
|
+ LocalDate yesterday = LocalDate.now().plusDays(-1);
|
|
|
+ // 更新容量到数据库
|
|
|
+ addVolume(yesterday);
|
|
|
+
|
|
|
+ log.warn("执行完成定时任务:redis访问量数据同步到数据库");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// private void visitUpdateToDb(){
|
|
|
+// // 当前时间的前一天
|
|
|
+// String yesterday = LocalDate.now().plusDays(-1).toString();
|
|
|
+//
|
|
|
+// List<ProjectEntity> list = projectService.list();
|
|
|
+// // String key = now + "_" + type + "_" + projectId + "_" + moduleType;
|
|
|
+// VisitEntity visitEntity = null;
|
|
|
+// // 每个项目
|
|
|
+// for (ProjectEntity entity : list) {
|
|
|
+// Long projectId = entity.getId();
|
|
|
+// String visitType = entity.getVisitType();
|
|
|
+// if (StrUtil.isBlank(visitType)){
|
|
|
+// log.error("项目visitType为空结束本次循环, 项目id : {}", projectId);
|
|
|
+// continue;
|
|
|
+// }
|
|
|
+// String[] split = visitType.split(",");
|
|
|
+// // 每个类型
|
|
|
+// for (String type : split) {
|
|
|
+// String visitKey = yesterday + "_visit_" + projectId + "_" + type;
|
|
|
+// visitEntity = new VisitEntity();
|
|
|
+// visitEntity.setToday(yesterday);
|
|
|
+// visitEntity.setModuleType(type);
|
|
|
+// visitEntity.setPcsVisit(getRedisValue(visitKey));
|
|
|
+// visitEntity.setProjectId(projectId);
|
|
|
+// // visitEntity.setPcsStar(pcsStar);
|
|
|
+//
|
|
|
+// visitService.save(visitEntity);
|
|
|
+//
|
|
|
+// // 删除过期key
|
|
|
+// redisUtil.delete(visitKey);
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存当天的素材容量
|
|
|
+ * @param today
|
|
|
+ */
|
|
|
+ private void addVolume(LocalDate today){
|
|
|
+
|
|
|
+ Integer volume = fodderService.getVolume("GB");
|
|
|
+ ReportEntity entity = new ReportEntity();
|
|
|
+ entity.setType("volume");
|
|
|
+ entity.setTotal(volume); // 容量是GB
|
|
|
+ entity.setDay(today);
|
|
|
+ reportService.save(entity);
|
|
|
+ log.info("保存容量完成, 当前容量:{} GB", volume);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addWork(LocalDate today){
|
|
|
+
|
|
|
+// workService.countByType();
|
|
|
+// ReportEntity entity = new ReportEntity();
|
|
|
+// entity.setType("work");
|
|
|
+// entity.setTotal(volume); // 容量是GB
|
|
|
+// entity.setDay(today);
|
|
|
+// reportService.save(entity);
|
|
|
+// log.info("保存容量完成, 当前容量:{} GB", volume);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private int getRedisValue(String key){
|
|
|
+ log.info("redis key: {}", key);
|
|
|
+ Integer pcsVisit = (Integer)redisUtil.get(key);
|
|
|
+ pcsVisit = pcsVisit == null ? 0 : pcsVisit;
|
|
|
+ log.info("{} 的数据:{}", key, pcsVisit);
|
|
|
+ return pcsVisit;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test(){
|
|
|
+ String a = "1";
|
|
|
+ String[] split = a.split(",");
|
|
|
+ System.out.println();
|
|
|
+ }
|
|
|
+}
|