|
|
|
@ -4,16 +4,18 @@ import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.jeecg.module.custom.ocr.api.entity.TaskCompletionRequest;
|
|
|
|
|
import org.jeecg.module.custom.ocr.common.entity.RequestData;
|
|
|
|
|
import org.jeecg.module.custom.ocr.common.entity.ResultData;
|
|
|
|
|
import org.jeecg.module.custom.ocr.config.ApiConfig;
|
|
|
|
|
import org.jeecg.module.custom.ocr.dataDao.DuplicateTaskMapper;
|
|
|
|
|
import org.jeecg.module.custom.ocr.dataDao.OcrPictureInfoMapper;
|
|
|
|
|
import org.jeecg.module.custom.ocr.dataDao.PictureCompareMapper;
|
|
|
|
|
import org.jeecg.module.custom.ocr.dataobject.DuplicateTask;
|
|
|
|
|
import org.jeecg.module.custom.ocr.dataobject.OcrPicture;
|
|
|
|
|
import org.jeecg.module.custom.ocr.dataobject.PictureCompare;
|
|
|
|
|
import org.jeecg.module.custom.ocr.dataDao.TaskMapper;
|
|
|
|
|
import org.jeecg.module.custom.ocr.dataobject.*;
|
|
|
|
|
import org.jeecg.module.custom.ocr.service.OcrPictureInfoService;
|
|
|
|
|
import org.jeecg.module.custom.ocr.service.OcrPictureService;
|
|
|
|
|
import org.jeecg.module.custom.ocr.utils.ApiHelper;
|
|
|
|
|
import org.jeecg.module.custom.ocr.utils.ImageClassUtil;
|
|
|
|
@ -23,6 +25,7 @@ import org.jeecg.module.custom.ocr.utils.httputil.HttpParamers;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
@ -46,9 +49,15 @@ public class DuplicateTaskRunner {
|
|
|
|
|
@Resource
|
|
|
|
|
private DuplicateTaskMapper duplicateTaskMapper;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private TaskMapper taskMapper;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private OcrPictureService ocrPictureService;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private OcrPictureInfoMapper ocrPictureInfoMapper;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private PictureCompareMapper pictureCompareMapper;
|
|
|
|
|
|
|
|
|
@ -62,6 +71,8 @@ public class DuplicateTaskRunner {
|
|
|
|
|
private String classifyBaseUrl;
|
|
|
|
|
|
|
|
|
|
private ScheduledExecutorService executor;
|
|
|
|
|
@Autowired
|
|
|
|
|
private OcrPictureInfoService ocrPictureInfoService;
|
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
|
public void init() {
|
|
|
|
@ -76,52 +87,65 @@ public class DuplicateTaskRunner {
|
|
|
|
|
private void processDuplicateTasks() {
|
|
|
|
|
try {
|
|
|
|
|
// 查询所有未完成的 DuplicateTask
|
|
|
|
|
List<DuplicateTask> tasks = duplicateTaskMapper.getAllUnCompletedTask();
|
|
|
|
|
if (tasks != null && !tasks.isEmpty()) {
|
|
|
|
|
for (DuplicateTask task : tasks) {
|
|
|
|
|
List<DuplicateTask> duplicateTasks = duplicateTaskMapper.getAllUnCompletedTask();
|
|
|
|
|
if (duplicateTasks != null && !duplicateTasks.isEmpty()) {
|
|
|
|
|
for (DuplicateTask duplicateTask : duplicateTasks) {
|
|
|
|
|
// 解析 queryConfig 为 QueryWrapper
|
|
|
|
|
QueryWrapper<OcrPicture> queryWrapper = toQueryWrapper(task);
|
|
|
|
|
// 调用 OcrPictureService.listPage 查询图片
|
|
|
|
|
List<OcrPicture> pictures = ocrPictureService.listPage(queryWrapper);
|
|
|
|
|
|
|
|
|
|
// 如果图片数量大于 1,进行两两对比
|
|
|
|
|
double maxSimilarity = 0.0;
|
|
|
|
|
if (pictures != null && pictures.size() > 1) {
|
|
|
|
|
for (int i = 0; i < pictures.size(); i++) {
|
|
|
|
|
for (int j = i + 1; j < pictures.size(); j++) {
|
|
|
|
|
OcrPicture first = pictures.get(i);
|
|
|
|
|
OcrPicture second = pictures.get(j);
|
|
|
|
|
|
|
|
|
|
// 计算相似度
|
|
|
|
|
String similarityScore = ImageClassUtil.getSimilarity(first.getImageUrl(), second.getImageUrl(), classifyBaseUrl);
|
|
|
|
|
double similarityValue = Double.parseDouble(similarityScore);
|
|
|
|
|
maxSimilarity = Math.max(maxSimilarity, similarityValue);
|
|
|
|
|
|
|
|
|
|
// 保存对比结果到 PictureCompare
|
|
|
|
|
PictureCompare compare = new PictureCompare();
|
|
|
|
|
compare.setFirstImgNo(first.getId());
|
|
|
|
|
compare.setFirstImgUrl(first.getImageUrl());
|
|
|
|
|
compare.setFirstLocalImgUrl(first.getLocalImageUrl());
|
|
|
|
|
compare.setFirstImgHash(first.getImgHash());
|
|
|
|
|
compare.setSecondImgNo(second.getId());
|
|
|
|
|
compare.setSecondImgUrl(second.getImageUrl());
|
|
|
|
|
compare.setSecondLocalImgUrl(second.getLocalImageUrl());
|
|
|
|
|
compare.setSecondImgHash(second.getImgHash());
|
|
|
|
|
compare.setSimilarityScore(similarityScore);
|
|
|
|
|
pictureCompareMapper.save(compare);
|
|
|
|
|
// QueryWrapper<OcrPicture> queryWrapper = toQueryWrapper(duplicateTask);
|
|
|
|
|
// // 调用 OcrPictureService.listPage 查询图片
|
|
|
|
|
// List<OcrPicture> pictures = ocrPictureService.listPage(queryWrapper);
|
|
|
|
|
|
|
|
|
|
List<Task> tasks = taskMapper.selectByAccountNoAndQueryConfig(duplicateTask.getAccountNo(), duplicateTask.getQueryConfig());
|
|
|
|
|
|
|
|
|
|
boolean result = true;
|
|
|
|
|
if (!CollectionUtils.isEmpty(tasks)) {
|
|
|
|
|
List<Long> taskIds = tasks.stream().map(Task::getId).collect(Collectors.toList());
|
|
|
|
|
List<OcrPictureInfo> pictures = ocrPictureInfoMapper.selectByTaskIds(taskIds);
|
|
|
|
|
// 如果图片数量大于 1,进行两两对比
|
|
|
|
|
double maxSimilarity = 0.0;
|
|
|
|
|
if (pictures != null && pictures.size() > 1) {
|
|
|
|
|
for (int i = 0; i < pictures.size(); i++) {
|
|
|
|
|
for (int j = i + 1; j < pictures.size(); j++) {
|
|
|
|
|
OcrPictureInfo first = pictures.get(i);
|
|
|
|
|
OcrPictureInfo second = pictures.get(j);
|
|
|
|
|
|
|
|
|
|
// 计算相似度
|
|
|
|
|
String similarityScore = ImageClassUtil.getSimilarity(first.getImageUrl(), second.getImageUrl(), classifyBaseUrl);
|
|
|
|
|
double similarityValue = Double.parseDouble(similarityScore);
|
|
|
|
|
maxSimilarity = Math.max(maxSimilarity, similarityValue);
|
|
|
|
|
|
|
|
|
|
// 保存对比结果到 PictureCompare
|
|
|
|
|
PictureCompare compare = new PictureCompare();
|
|
|
|
|
compare.setFirstImgNo(first.getId());
|
|
|
|
|
compare.setFirstImgUrl(first.getImageUrl());
|
|
|
|
|
compare.setFirstLocalImgUrl(first.getLocalImageUrl());
|
|
|
|
|
compare.setFirstImgHash(first.getImgHash());
|
|
|
|
|
compare.setSecondImgNo(second.getId());
|
|
|
|
|
compare.setSecondImgUrl(second.getImageUrl());
|
|
|
|
|
compare.setSecondLocalImgUrl(second.getLocalImageUrl());
|
|
|
|
|
compare.setSecondImgHash(second.getImgHash());
|
|
|
|
|
compare.setSimilarityScore(similarityScore);
|
|
|
|
|
pictureCompareMapper.save(compare);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新 DuplicateTask 状态和最大相似度
|
|
|
|
|
duplicateTaskMapper.updateCompletedAndMaxSimilarity(duplicateTask.getId(), 1, String.valueOf(maxSimilarity));
|
|
|
|
|
} else {
|
|
|
|
|
// 更新 DuplicateTask 状态和最大相似度
|
|
|
|
|
duplicateTaskMapper.updateCompletedAndMaxSimilarity(duplicateTask.getId(), 1, "0");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新 DuplicateTask 状态和最大相似度
|
|
|
|
|
duplicateTaskMapper.updateCompletedAndMaxSimilarity(task.getId(), true, String.valueOf(maxSimilarity));
|
|
|
|
|
String[] taskNos = duplicateTask.getTaskNos().split(",");
|
|
|
|
|
for (String taskNo : taskNos) {
|
|
|
|
|
notifyTaskCompletion(taskNo, duplicateTask.getTenantNo(), duplicateTask.getAccountNo(), maxSimilarity, pictures);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 更新 DuplicateTask 状态和最大相似度
|
|
|
|
|
duplicateTaskMapper.updateCompletedAndMaxSimilarity(task.getId(), true, "0");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String[] taskNos = task.getTaskNos().split(",");
|
|
|
|
|
for (String taskNo : taskNos) {
|
|
|
|
|
notifyTaskCompletion(taskNo, task.getTenantNo(), task.getAccountNo(), maxSimilarity, pictures);
|
|
|
|
|
String[] taskNos = duplicateTask.getTaskNos().split(",");
|
|
|
|
|
for (String taskNo : taskNos) {
|
|
|
|
|
notifyTaskCompletion(taskNo, duplicateTask.getTenantNo(), duplicateTask.getAccountNo(), 0, Lists.newArrayList());
|
|
|
|
|
}
|
|
|
|
|
duplicateTaskMapper.updateCompletedAndMaxSimilarity(duplicateTask.getId(), 1, "0");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -259,7 +283,7 @@ public class DuplicateTaskRunner {
|
|
|
|
|
return queryWrapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void notifyTaskCompletion(String taskNo, Long tenantNo, Long accountNo, double maxSimilarity, List<OcrPicture> pictures) {
|
|
|
|
|
private boolean notifyTaskCompletion(String taskNo, Long tenantNo, Long accountNo, double maxSimilarity, List<OcrPictureInfo> pictures) {
|
|
|
|
|
try {
|
|
|
|
|
// 构造任务完成数据
|
|
|
|
|
TaskCompletionRequest.TaskCompletionData data = new TaskCompletionRequest.TaskCompletionData();
|
|
|
|
@ -281,13 +305,13 @@ public class DuplicateTaskRunner {
|
|
|
|
|
repeat.setImgNo(p.getId());
|
|
|
|
|
repeat.setImgUrl(p.getImageUrl());
|
|
|
|
|
repeat.setDynamicFields(new HashMap<>()); // 动态字段待补充
|
|
|
|
|
repeat.setHisPictureRepeatList(Arrays.asList());
|
|
|
|
|
return repeat;
|
|
|
|
|
})
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
data.setPictureRepeatList(pictureRepeatList);
|
|
|
|
|
|
|
|
|
|
// 其他列表暂设为空(需补充逻辑)
|
|
|
|
|
data.setHisPictureRepeatList(Arrays.asList());
|
|
|
|
|
data.setFalseImgList(Arrays.asList());
|
|
|
|
|
data.setBriefRepeatTaskList(Arrays.asList());
|
|
|
|
|
data.setApproveDetailList(Arrays.asList());
|
|
|
|
@ -302,10 +326,14 @@ public class DuplicateTaskRunner {
|
|
|
|
|
apiConfig.getAccessCode(),
|
|
|
|
|
jsonData
|
|
|
|
|
);
|
|
|
|
|
// RequestData requestData = ApiHelper.buildResponse(
|
|
|
|
|
// apiConfig.getAccessCode(),
|
|
|
|
|
// jsonData
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// 组装请求
|
|
|
|
|
// TODO: 需提供实际路径
|
|
|
|
|
String url = apiConfig.getInterfaceDomain() + "/api/task/complete";
|
|
|
|
|
String url = apiConfig.getInterfaceDomain();
|
|
|
|
|
String requestBodyJson = JSONObject.toJSONString(requestData);
|
|
|
|
|
HttpParamers httpParamers = new HttpParamers(HttpMethod.POST);
|
|
|
|
|
httpParamers.setJsonParamer(requestBodyJson);
|
|
|
|
@ -326,11 +354,14 @@ public class DuplicateTaskRunner {
|
|
|
|
|
// 处理响应
|
|
|
|
|
if (resultData.getStatus() == 100) {
|
|
|
|
|
log.info("Task {} completion notified successfully", taskNo);
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
log.error("Failed to notify task completion, taskNo={}, response={}", taskNo, responseJsonStr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Error notifying task completion, taskNo={}", taskNo, e);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|