避免重复创建

This commit is contained in:
2025-04-07 09:46:16 +08:00
parent 389c28300f
commit 84287df87b
10 changed files with 128 additions and 221 deletions

View File

@ -3,6 +3,7 @@ package com.ycwl.basic.task;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.ycwl.basic.constant.StorageConstant;
import com.ycwl.basic.facebody.adapter.IFaceBodyAdapter;
import com.ycwl.basic.mapper.FaceMapper;
import com.ycwl.basic.mapper.FaceSampleMapper;
import com.ycwl.basic.mapper.ScenicMapper;
@ -10,6 +11,7 @@ import com.ycwl.basic.mapper.SourceMapper;
import com.ycwl.basic.mapper.VideoMapper;
import com.ycwl.basic.model.pc.face.req.FaceReqQuery;
import com.ycwl.basic.model.pc.face.resp.FaceRespVO;
import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity;
import com.ycwl.basic.model.pc.faceSample.req.FaceSampleReqQuery;
import com.ycwl.basic.model.pc.faceSample.resp.FaceSampleRespVO;
import com.ycwl.basic.model.pc.scenic.entity.ScenicConfigEntity;
@ -20,6 +22,7 @@ import com.ycwl.basic.model.pc.source.resp.SourceRespVO;
import com.ycwl.basic.model.pc.video.req.VideoReqQuery;
import com.ycwl.basic.model.pc.video.resp.VideoRespVO;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.service.pc.ScenicService;
import com.ycwl.basic.service.task.TaskFaceService;
import com.ycwl.basic.storage.StorageFactory;
import com.ycwl.basic.storage.adapters.IStorageAdapter;
@ -37,6 +40,7 @@ import java.util.Map;
import java.util.Objects;
import static com.ycwl.basic.constant.FaceConstant.USER_FACE_DB_NAME;
import static com.ycwl.basic.service.task.impl.TaskFaceServiceImpl.generateEntityId;
@Component
@EnableScheduling
@ -46,8 +50,6 @@ public class FaceCleaner {
@Autowired
private ScenicMapper scenicMapper;
@Autowired
private TaskFaceService faceService;
@Autowired
private FaceSampleMapper faceSampleMapper;
@Autowired
private SourceMapper sourceMapper;
@ -57,39 +59,66 @@ public class FaceCleaner {
private ScenicRepository scenicRepository;
@Autowired
private FaceMapper faceMapper;
@Autowired
private ScenicService scenicService;
@Scheduled(cron = "0 0 4 * * ?")
@Scheduled(cron = "0 0 1 * * ?")
public void clean(){
ScenicReqQuery scenicQuery = new ScenicReqQuery();
List<ScenicRespVO> scenicList = scenicMapper.list(scenicQuery);
scenicList.forEach(scenic -> {
log.info("当前景区{},开始删除人脸样本", scenic.getName());
faceService.batchDeleteExpiredFace(scenic.getId());
log.info("当前景区{},开始删除人脸样本", scenic.getId());
IFaceBodyAdapter adapter = scenicService.getScenicFaceBodyAdapter(scenic.getId());
ScenicConfigEntity scenicConfig = scenicRepository.getScenicConfig(scenic.getId());
Integer sampleStoreDay = scenicConfig.getSampleStoreDay();
if (sampleStoreDay == null) {
log.info("当前景区{}人脸样本保存天数未设置默认7天", scenic.getId());
sampleStoreDay = 7;
}
Date sampleEndDate = DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -sampleStoreDay);
List<FaceSampleEntity> faceSampleList = faceSampleMapper.listEntityBeforeDate(scenic.getId(), sampleEndDate);
if (faceSampleList.isEmpty()) {
log.info("当前景区{},人脸样本为空", scenic.getId());
return;
}
faceSampleList.forEach(faceSample -> {
boolean success = adapter.deleteFace(String.valueOf(scenic.getId()), generateEntityId(faceSample));
if (success) {
log.info("当前景区{}人脸样本ID{},删除成功", scenic.getId(), faceSample.getId());
faceSampleMapper.deleteById(faceSample.getId());
} else {
log.info("当前景区{}人脸样本ID{},删除失败", scenic.getId(), faceSample.getId());
}
});
});
}
@Scheduled(cron = "0 0 3 * * ?")
@Scheduled(cron = "0 45 2 * * ?")
public void deleteExpireFace() {
ScenicReqQuery scenicQuery = new ScenicReqQuery();
List<ScenicRespVO> scenicList = scenicMapper.list(scenicQuery);
scenicList.parallelStream().forEach(scenic -> {
log.info("当前景区{},开始删除用户人脸", scenic.getId());
ScenicConfigEntity scenicConfig = scenicRepository.getScenicConfig(scenic.getId());
Integer sampleStoreDay = scenicConfig.getSampleStoreDay();
if (sampleStoreDay == null) {
IFaceBodyAdapter adapter = scenicService.getScenicFaceBodyAdapter(scenic.getId());
Integer faceStoreDay = scenicConfig.getFaceStoreDay();
if (faceStoreDay == null) {
log.info("当前景区{}人脸样本保存天数未设置默认3天", scenic.getName());
sampleStoreDay = 3;
faceStoreDay = 3;
}
FaceReqQuery req = new FaceReqQuery();
req.setScenicId(scenic.getId());
req.setUpdateEndTime(DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -sampleStoreDay));
req.setUpdateEndTime(DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -faceStoreDay));
List<FaceRespVO> list = faceMapper.list(req);
list.forEach(face -> {
boolean result = faceService.deleteFaceSample(face.getScenicId(), USER_FACE_DB_NAME+face.getScenicId(), face.getId().toString());
boolean result = adapter.deleteFace(USER_FACE_DB_NAME+face.getScenicId(), face.getId().toString());
if (result) {
log.info("当前景区{}人脸样本ID{},删除成功", scenic.getId(), face.getId());
faceMapper.deleteById(face.getId());
} else {
log.info("当前景区{}人脸样本ID{},删除失败", scenic.getId(), face.getId());
}
});
log.info("当前景区{},删除人脸样本{}个", scenic.getName(), list.size());
});
}
@ -174,7 +203,7 @@ public class FaceCleaner {
});
}
@Scheduled(cron = "0 0 5 * * ?")
@Scheduled(cron = "0 0 1 * * ?")
public void clearOss(){
cleanFaceSampleOss();
cleanSourceOss();
@ -196,9 +225,7 @@ public class FaceCleaner {
log.info("开始清理源视频素材文件");
List<SourceRespVO> list = sourceMapper.list(new SourceReqQuery());
scenicMapper.list(new ScenicReqQuery()).forEach(scenic -> {
ScenicConfigEntity scenicConfig = scenicRepository.getScenicConfig(scenic.getId());
IStorageAdapter adapter = StorageFactory.get(scenicConfig.getStoreType());
adapter.loadConfig(JSONObject.parseObject(scenicConfig.getStoreConfigJson(), Map.class));
IStorageAdapter adapter = scenicService.getScenicStorageAdapter(scenic.getId());
log.info("开始清理视频文件");
List<StorageFileObject> fileObjectList = adapter.listDir(StorageConstant.VIDEO_PIECE_PATH);
fileObjectList.parallelStream().forEach(fileObject -> {
@ -225,9 +252,7 @@ public class FaceCleaner {
log.info("开始清理视频文件");
List<VideoRespVO> list = videoMapper.list(new VideoReqQuery());
scenicMapper.list(new ScenicReqQuery()).forEach(scenic -> {
ScenicConfigEntity scenicConfig = scenicRepository.getScenicConfig(scenic.getId());
IStorageAdapter adapter = StorageFactory.get(scenicConfig.getStoreType());
adapter.loadConfig(JSONObject.parseObject(scenicConfig.getStoreConfigJson(), Map.class));
IStorageAdapter adapter = scenicService.getScenicStorageAdapter(scenic.getId());
log.info("开始清理视频文件");
List<StorageFileObject> fileObjectList = adapter.listDir(StorageConstant.VLOG_PATH);
fileObjectList.parallelStream().forEach(fileObject -> {