Browse Source

每天凌晨三点删除 3个月之前的数据

王鑫刚 6 months ago
parent
commit
585e1288f8

+ 1 - 3
.idea/compiler.xml

@@ -7,15 +7,13 @@
         <sourceOutputDir name="target/generated-sources/annotations" />
         <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
         <outputRelativeToContentRoot value="true" />
+        <module name="face-server" />
         <module name="fyzd-phone" />
         <module name="face-server (2)" />
         <module name="face-server (1)" />
         <module name="ldb-airport" />
       </profile>
     </annotationProcessing>
-    <bytecodeTargetLevel>
-      <module name="face-server" target="1.8" />
-    </bytecodeTargetLevel>
   </component>
   <component name="JavacSettings">
     <option name="ADDITIONAL_OPTIONS_OVERRIDE">

+ 10 - 0
src/main/java/com/yx/face/FaceServerApplication.java

@@ -3,7 +3,11 @@ package com.yx.face;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.openfeign.EnableFeignClients;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Primary;
+import org.springframework.core.task.TaskExecutor;
 import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import tk.mybatis.spring.annotation.MapperScan;
 
 import java.net.InetSocketAddress;
@@ -17,4 +21,10 @@ public class FaceServerApplication {
     public static void main(String[] args) {
         SpringApplication.run(FaceServerApplication.class, args);
     }
+    @Primary
+    @Bean
+    public TaskExecutor primaryTaskExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        return executor;
+    }
 }

+ 21 - 21
src/main/java/com/yx/face/boot/component/aop/ServiceLog.java

@@ -44,29 +44,29 @@ public class ServiceLog {
      */
     @Around("log()")
     public Object aroundLog(ProceedingJoinPoint point) throws Throwable {
-        long start = System.currentTimeMillis();
-        Signature signature = point.getSignature();
-        MyAnnotation annotation = getAnnotation(point);
-        if(annotation == null){
-            log.info("【请求日志级别】:{}【请求类名】:{}【请求方法名】:{}",logPrint,signature.getDeclaringTypeName(),signature.getName());
-            try {
-                String req = JSON.toJSONString(point.getArgs());
-                log.info("【参数】:{}",this.logPrint(req));//如果传的是流就会报错
-            }catch (Exception e){
-                log.info("打印参数报错");
-            }
-        }
+//        long start = System.currentTimeMillis();
+//        Signature signature = point.getSignature();
+//        MyAnnotation annotation = getAnnotation(point);
+//        if(annotation == null){
+//            log.info("【请求日志级别】:{}【请求类名】:{}【请求方法名】:{}",logPrint,signature.getDeclaringTypeName(),signature.getName());
+//            try {
+//                String req = JSON.toJSONString(point.getArgs());
+//                log.info("【参数】:{}",this.logPrint(req));//如果传的是流就会报错
+//            }catch (Exception e){
+//                log.info("打印参数报错");
+//            }
+//        }
         //运行
         Object result = point.proceed();
-        if(annotation == null){
-            try {
-                String resp = JSON.toJSONString(result);
-                log.info("【返回值】:{}",this.logPrint(resp));//如果返回的是流就会报错
-            }catch (Exception e){
-                log.info("打印返回值报错");
-            }
-            log.info("【耗时】:{}",System.currentTimeMillis() - start);
-        }
+//        if(annotation == null){
+//            try {
+//                String resp = JSON.toJSONString(result);
+//                log.info("【返回值】:{}",this.logPrint(resp));//如果返回的是流就会报错
+//            }catch (Exception e){
+//                log.info("打印返回值报错");
+//            }
+//            log.info("【耗时】:{}",System.currentTimeMillis() - start);
+//        }
 
         return result;
     }

+ 4 - 0
src/main/java/com/yx/face/dao/FaceLogDao.java

@@ -1,5 +1,6 @@
 package com.yx.face.dao;
 
+import cn.hutool.core.date.DateTime;
 import com.yx.face.boot.component.tk.TKMapper;
 import com.yx.face.model.entity.FaceLog;
 import com.yx.face.model.excel.FaceLogVoExcel;
@@ -35,5 +36,8 @@ public interface FaceLogDao extends TKMapper<FaceLog> {
     List<FaceLogVO> getYellow(@Param("adminId")Integer adminId,@Param("faceTime")String faceTime,@Param("jkm")String jkm);
     /*2022-02-27对face_log查询就行优化 单表查询*/
     List<FaceLogVO> getFaceLogsNew(Map<String, Object> map);
+    Integer selectMaxIdByFaceTime(@Param("faceTime")DateTime faceTime);
+
+    List<Integer> selectFaceLogIdList(@Param("maxFaceLogId")Integer maxFaceLogId);
 }
 

+ 74 - 0
src/main/java/com/yx/face/service/job/ClearDataJob.java

@@ -0,0 +1,74 @@
+package com.yx.face.service.job;
+
+
+
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.StrUtil;
+import com.yx.face.boot.uitls.RedisUtil;
+import com.yx.face.dao.FaceLogDao;
+import com.yx.face.dao.FaceLogFromDao;
+import com.yx.face.dao.ShortTermFaceLogDao;
+import com.yx.face.model.entity.FaceLogFrom;
+import com.yx.face.model.entity.ScanCodeBoxCode;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+import tk.mybatis.mapper.entity.Example;
+import tk.mybatis.mapper.weekend.WeekendSqls;
+
+import javax.annotation.PostConstruct;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+
+@Configuration      //1.主要用于标记配置类,兼备Component的效果。
+@EnableScheduling   // 2.开启定时任务
+@EnableAsync
+@Slf4j
+@RequiredArgsConstructor(onConstructor = @__(@Autowired))
+public class ClearDataJob {
+
+    private final FaceLogFromDao faceLogFromDao;
+    private final FaceLogDao faceLogDao;
+    private final ShortTermFaceLogDao shortTermFaceLogDao;
+    private final ThreadPoolTaskExecutor executor;
+    private final RedisUtil redisUtil;
+
+
+    @Async
+    @Scheduled(cron = "0 0 3 * * ? ", zone = "Asia/Shanghai")
+    public void zeroTask() {
+        executor.execute(()->{
+            log.info("-----------开始清除记录-----------");
+            //1.查询最大的id
+            DateTime dateTime = DateUtil.offsetMonth(DateUtil.date(), -3);
+            Integer maxFaceLogId = faceLogDao.selectMaxIdByFaceTime(dateTime);
+
+            while (true){
+                List<Integer> faceLogIdList = faceLogDao.selectFaceLogIdList(maxFaceLogId);
+                if(CollUtil.isEmpty(faceLogIdList)){
+                    break;
+                }
+                String faceLogIdListString = StrUtil.join(",", faceLogIdList);
+                int faceLogDaoNum = faceLogDao.deleteByIds(faceLogIdListString);
+                int faceLogFromDaoNum = faceLogFromDao.deleteByExample(Example.builder(FaceLogFrom.class)
+                        .where(WeekendSqls.<FaceLogFrom>custom()
+                                .andIn(FaceLogFrom::getFaceLogId, faceLogIdList)
+                        ).build()
+                );
+                int shortTermFaceLogDaoNum = shortTermFaceLogDao.deleteByIds(faceLogIdListString);
+                log.info("faceLogDaoNum:{},faceLogFromDaoNum:{},shortTermFaceLogDaoNum:{}",faceLogDaoNum,faceLogFromDaoNum,shortTermFaceLogDaoNum);
+            }
+            log.info("-----------结束清除记录-----------");
+        });
+    }
+}

+ 9 - 0
src/main/resources/mapper/FaceLogDao.xml

@@ -436,5 +436,14 @@
         </where>
         ORDER BY log.id DESC
     </select>
+    <select id="selectMaxIdByFaceTime" resultType="java.lang.Integer">
+        select max(id) from face_log where
+        <![CDATA[ face_time < #{faceTime}]]>
+
+    </select>
+    <select id="selectFaceLogIdList" resultType="java.lang.Integer">
+        select id from face_log where   <![CDATA[ id <= #{maxFaceLogId}]]>
+        limit 10
+    </select>
 </mapper>