|
|
|
|
@ -1,15 +1,18 @@
|
|
|
|
|
package com.biutag.supervision; |
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
|
|
|
|
import com.biutag.supervision.constants.AppConstants; |
|
|
|
|
import com.biutag.supervision.pojo.entity.SupDepart; |
|
|
|
|
import com.biutag.supervision.service.SupDepartService; |
|
|
|
|
import com.github.tobato.fastdfs.service.FastFileStorageClient; |
|
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.boot.test.context.SpringBootTest; |
|
|
|
|
import org.springframework.scheduling.TaskScheduler; |
|
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; |
|
|
|
|
import org.springframework.scheduling.config.ScheduledTaskRegistrar; |
|
|
|
|
import org.springframework.scheduling.support.CronTrigger; |
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
@SpringBootTest |
|
|
|
|
class SupervisionApplicationTests { |
|
|
|
|
@ -23,18 +26,36 @@ class SupervisionApplicationTests {
|
|
|
|
|
@Autowired |
|
|
|
|
private TaskScheduler taskScheduler; |
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
SupDepartService departService; |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
void contextLoads() { |
|
|
|
|
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); |
|
|
|
|
scheduler.setPoolSize(5); |
|
|
|
|
scheduler.setThreadNamePrefix("scheduled-task-"); |
|
|
|
|
scheduler.initialize(); |
|
|
|
|
Runnable runnable = () -> { |
|
|
|
|
System.out.println("Executing task at " + LocalDateTime.now()); |
|
|
|
|
}; |
|
|
|
|
taskRegistrar.setTaskScheduler(scheduler); |
|
|
|
|
taskRegistrar.addTriggerTask(runnable, new CronTrigger("0 2 * * * ?")); |
|
|
|
|
List<SupDepart> departs = departService.list(); |
|
|
|
|
departs.forEach(item -> { |
|
|
|
|
String pathTrace = getPathTrace(item); |
|
|
|
|
if (!pathTrace.equals(item.getPathTrace())) { |
|
|
|
|
departService.update(new LambdaUpdateWrapper<SupDepart>().eq(SupDepart::getId, item.getId()).set(SupDepart::getPathTrace, pathTrace)); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public String getPathTrace(SupDepart depart) { |
|
|
|
|
List<String> pathTrace = new ArrayList<>(); |
|
|
|
|
buildPathTraceRecursive(depart, pathTrace); |
|
|
|
|
return "/" + String.join("/", pathTrace) + "/"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void buildPathTraceRecursive(SupDepart depart, List<String> pathTrace) { |
|
|
|
|
pathTrace.add(0, depart.getId()); |
|
|
|
|
if (AppConstants.ROOT_DEPART_ID.equals(depart.getId())) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (AppConstants.ROOT_DEPART_ID.equals(depart.getPid())) { |
|
|
|
|
pathTrace.add(0, AppConstants.ROOT_DEPART_ID); |
|
|
|
|
} else { |
|
|
|
|
buildPathTraceRecursive(departService.getById(depart.getPid()), pathTrace); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|