소스 검색

定时任务-每2小时1次删除1个月之前的进出场记录

hbjzws 1 년 전
부모
커밋
650b6b9da6

+ 71 - 0
src/main/java/com/charging/chargingparking/scheduled/ParkingRecordTask.java

@@ -0,0 +1,71 @@
+package com.charging.chargingparking.scheduled;
+
+import cn.hutool.core.date.DateUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.charging.chargingparking.entity.ParkingMember;
+import com.charging.chargingparking.entity.ParkingRecord;
+import com.charging.chargingparking.service.ParkingMemberService;
+import com.charging.chargingparking.service.ParkingRecordService;
+import com.charging.chargingparking.service.ParkingWhiteService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Profile;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.Date;
+import java.util.List;
+
+/**
+ * @author 墨鱼_mo
+ * @date 2022/6/6 0006 下午 2:04
+ */
+@Component
+@Slf4j
+@Transactional
+@Profile({"dev"})
+//@Profile({"production"})
+public class ParkingRecordTask {
+
+    @Autowired
+    private ParkingRecordService parkingRecordService;
+
+    @Autowired
+    private ParkingMemberService parkingMemberService;
+
+    @Autowired
+    private ParkingWhiteService parkingWhiteService;
+
+    //每2小时1次删除1个月之前的进出场记录
+    @Scheduled(cron = "0 0 */2 * * ?")
+    // @Scheduled(fixedDelay = 1000)
+    public  void run() {
+        try{
+
+            QueryWrapper<ParkingRecord> parkingRecordQueryWrapper = new QueryWrapper<>();
+            parkingRecordQueryWrapper.lt("create_time",DateUtil.offsetDay(new Date(),-30));
+            parkingRecordQueryWrapper.eq("release_status","0");
+            List<ParkingRecord> parkingRecordList = parkingRecordService.list(parkingRecordQueryWrapper);
+            if (parkingRecordList.size()>0){
+                for (ParkingRecord parkingRecord : parkingRecordList){
+                    System.out.println("---"+parkingRecord.getCreateTime());
+                    parkingRecord.setReleaseStatus("1");
+                    parkingRecord.setDelFlag(true);
+                    parkingRecord.setUpdateBy("system");
+                    parkingRecord.setUpdateTime(new Date());
+                    parkingRecord.setRemark(parkingRecord.getRemark()+"系统清理离场");
+                    parkingRecordService.updateById(parkingRecord);
+                }
+            }
+
+
+        }catch (Exception ex){
+            log.error(ex.getMessage(),ex);
+        }
+
+
+
+
+    }
+}

+ 22 - 4
src/test/java/com/charging/chargingparking/ChargingParkingApplicationTests.java

@@ -106,11 +106,29 @@ class ChargingParkingApplicationTests {
     private EcCouponService ecCouponService;
     @Test
     void testService()  {
-        EcCoupon ecCoupon = new EcCoupon();
-        ecCoupon.setCarNum("1");
-        ecCoupon.setParkId("2");
 
-        ecCouponService.save(ecCoupon);
+        QueryWrapper<ParkingRecord> parkingRecordQueryWrapper = new QueryWrapper<>();
+        parkingRecordQueryWrapper.lt("create_time",DateUtil.offsetDay(new Date(),-30));
+        parkingRecordQueryWrapper.eq("release_status","0");
+        List<ParkingRecord> parkingRecordList = parkingRecordService.list(parkingRecordQueryWrapper);
+        if (parkingRecordList.size()>0){
+            for (ParkingRecord parkingRecord : parkingRecordList){
+                System.out.println("---"+parkingRecord.getCreateTime());
+                parkingRecord.setReleaseStatus("1");
+                parkingRecord.setDelFlag(true);
+                parkingRecord.setUpdateBy("system");
+                parkingRecord.setUpdateTime(new Date());
+                parkingRecord.setRemark(parkingRecord.getRemark()+"系统清理离场");
+                parkingRecordService.updateById(parkingRecord);
+            }
+        }
+
+
+//        EcCoupon ecCoupon = new EcCoupon();
+//        ecCoupon.setCarNum("1");
+//        ecCoupon.setParkId("2");
+//
+//        ecCouponService.save(ecCoupon);
 
 //        ParkingChannel parkingChannel = new ParkingChannel();
 //        parkingChannel.setDelFlag(false);