Browse Source

fix: 每天刷新企业信用记录

zhangying 7 months ago
parent
commit
a12996cbbe

+ 6 - 0
pom.xml

@@ -169,6 +169,12 @@
             <artifactId>weixin-java-mp</artifactId>
             <version>3.7.0</version>
         </dependency>
+
+        <!-- Quartz定时任务 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-quartz</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

+ 25 - 0
src/main/java/com/hz/employmentsite/config/QuartzConfig.java

@@ -0,0 +1,25 @@
+package com.hz.employmentsite.config;
+
+import com.hz.employmentsite.util.CronUtil;
+import org.quartz.*;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class QuartzConfig {
+    private static final String CREDIT_RECORD_TASK_IDENTITY = "Credit_Record_Quartz";
+
+    @Bean
+    public JobDetail quartzDetail() {
+        return JobBuilder.newJob(CronUtil.class).withIdentity(CREDIT_RECORD_TASK_IDENTITY).storeDurably().build();
+    }
+
+    @Bean
+    public Trigger quartzTrigger() {
+        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.dailyAtHourAndMinute(0, 5); // 每天0点5分执行
+        return TriggerBuilder.newTrigger().forJob(quartzDetail())
+                .withIdentity(CREDIT_RECORD_TASK_IDENTITY)
+                .withSchedule(scheduleBuilder)
+                .build();
+    }
+}

+ 2 - 0
src/main/java/com/hz/employmentsite/mapper/cquery/CreditRecordCQuery.java

@@ -4,6 +4,7 @@ import com.hz.employmentsite.model.PcCreditRecord;
 import com.hz.employmentsite.vo.companyService.CreditRecordVo;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.Date;
 import java.util.List;
 
 public interface CreditRecordCQuery {
@@ -11,4 +12,5 @@ public interface CreditRecordCQuery {
 
     PcCreditRecord getById(@Param("creditRecordID") String creditRecordID);
 
+    int updateEffective(@Param("day") Date day);
 }

+ 5 - 0
src/main/java/com/hz/employmentsite/services/impl/companyService/CreditRecordServiceImpl.java

@@ -63,4 +63,9 @@ public class CreditRecordServiceImpl implements CreditRecordService {
         criteria.andCreditRecordIDIn(ids);
         return pcCreditRecordMapper.deleteByExample(example);
     }
+
+    @Override
+    public int updateEffective() {
+        return creditRecordCQuery.updateEffective(new Date());
+    }
 }

+ 2 - 0
src/main/java/com/hz/employmentsite/services/service/companyService/CreditRecordService.java

@@ -13,4 +13,6 @@ public interface CreditRecordService {
     Integer save(PcCreditRecord data, String userId);
 
     int delete(List<String> ids);
+
+    int updateEffective();
 }

+ 20 - 0
src/main/java/com/hz/employmentsite/util/CronUtil.java

@@ -0,0 +1,20 @@
+package com.hz.employmentsite.util;
+
+import com.hz.employmentsite.services.service.companyService.CreditRecordService;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.quartz.QuartzJobBean;
+import org.springframework.stereotype.Component;
+
+@Component
+public class CronUtil extends QuartzJobBean {
+    @Autowired
+    private CreditRecordService creditRecordService;
+
+    @Override
+    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
+        // 修改已到期的企业信用记录
+        creditRecordService.updateEffective();
+    }
+}

+ 8 - 0
src/main/resources/mapping/cquery/CreditRecordCQuery.xml

@@ -23,4 +23,12 @@
             and creditRecordID = #{creditRecordID}
         </if>
     </select>
+
+    <update id="updateEffective">
+        UPDATE `pc_credit_record`
+        SET IsEffective = 1
+        WHERE
+            DATE( ExpirationTime ) <![CDATA[ < ]]> DATE(#{day})
+            AND IsEffective = 0
+    </update>
 </mapper>