Merge remote-tracking branch 'origin/master'

devhuozheluoji
郭向斌 2 years ago
commit 8348b79ba4

@ -3,6 +3,7 @@ package org.jeecg.modules.api.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
@ -34,6 +35,7 @@ import javax.annotation.Resource;
import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
@ -71,6 +73,7 @@ public class ApiController {
}
//1.获取请求参数
String taskName = requestBody.getString("taskName");
AssertUtils.notTrue(ocrIdentifyService.calculateTimeDifference(taskName)<=10,"[任务名称]-"+taskName+" 短时间内已存在,不可再次请求");
String requestId = requestBody.getString("requestId");//请求唯一标识
String scenes = requestBody.getString("scenes");//场景类型door=门头照片cases=病例bill=票据
String ruleId = requestBody.getString("ruleId");//规则标识
@ -133,7 +136,7 @@ public class ApiController {
}
Thread.sleep(1000L);
//3.请求python ocr识别异步执行
taskService.postSemantic(ocrIdentify,fileList);
//taskService.postSemantic(ocrIdentify,fileList);
//ocrIdentifyService.postSemantic(ocrIdentify, fileList);
return Result.OK("请求成功");
}

@ -153,6 +153,7 @@ public class OcrIdentifyController extends JeecgController<OcrIdentify, IOcrIden
return Result.error("当前环境不支持该功能,请访问测试环境");
}
AssertUtils.notEmpty(ocrIdentify.getTaskName(), "[任务名称]-不可为空");
AssertUtils.notTrue(ocrIdentifyService.calculateTimeDifference(ocrIdentify.getTaskName())<=10,"[任务名称]-"+ocrIdentify.getTaskName()+" 短时间内已存在,不可再次请求");
AssertUtils.notEmpty(ocrIdentify.getIdentifyUrl(), "[识别路径]不可为空");
AssertUtils.notEmpty(ocrIdentify.getRuleCheck(), "请选择[规则检查配置]");
AssertUtils.notEmpty(ocrIdentify.getPriority(), "请选择[优先级]");
@ -182,7 +183,7 @@ public class OcrIdentifyController extends JeecgController<OcrIdentify, IOcrIden
ocrIdentify.setStatus("0");
ocrIdentifyService.save(ocrIdentify);
//3.请求python ocr识别异步执行
taskService.postSemantic(ocrIdentify, fileList);
//taskService.postSemantic(ocrIdentify, fileList);
return Result.OK("添加成功!");
}
@ -427,4 +428,5 @@ public class OcrIdentifyController extends JeecgController<OcrIdentify, IOcrIden
}
return Result.OK("操作成功");
}
}

@ -0,0 +1,155 @@
package org.jeecg.modules.ocr.init;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.common.constant.OcrConstant;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.AssertUtils;
import org.jeecg.common.util.RestUtil;
import org.jeecg.modules.ocr.dto.OcrIdentifyDTO;
import org.jeecg.modules.ocr.entity.OcrIdentify;
import org.jeecg.modules.ocr.service.IOcrIdentifyDetailService;
import org.jeecg.modules.ocr.service.IOcrIdentifyService;
import org.jeecg.modules.ocr.service.impl.TaskService;
import org.jeecg.modules.ocr.utils.FileOUtils;
import org.jeecg.modules.system.entity.SysLog;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.system.service.ISysLogService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @Description
* @Author ZhouWenTao
* @Date 2023/9/20 17:53
*/
@Configuration
@Slf4j
public class HandleTaskInit implements ApplicationRunner {
@Resource
IOcrIdentifyService ocrIdentifyService;
@Resource
IOcrIdentifyDetailService ocrIdentifyDetailService;
@Resource
TaskService taskService;
@Resource
private ISysDictService sysDictService;
@Resource
private ISysLogService sysLogService;
@Value("${system.project.enableHandleTask}")
private boolean enableHandleTask;
@Override
public void run(ApplicationArguments args) throws Exception {
if(enableHandleTask){
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (true) {
try {
Thread.sleep(10000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("---------检测是否有任务...");
//获取任务
List<OcrIdentify> list= ocrIdentifyService.getSemanticTaskList();
if (CollectionUtils.isEmpty(list)) {
continue;
}
//获取相对路径
List<DictModel> ocr_relative_path = sysDictService.queryDictItemsByCode("ocr_relative_path");
String relativePath = null;
if (ocr_relative_path != null && ocr_relative_path.size() > 0) {
for (DictModel dictModel : ocr_relative_path) {
if (dictModel.getValue().equals("0")) {
relativePath = dictModel.getText();
}
}
}
//获取识别的图片
List<String> fileList=null;
//临时变量
List<String> fileUrlList;
String image;//图片
//执行获取到的任务
identifyFor: for (OcrIdentify ocrIdentify : list) {
image=null;
fileList=new ArrayList<>();
//识别的图片路径
String imageUrl = ocrIdentify.getIdentifyUrl();
//判断是不是网络图片
Boolean onlineFile = FileOUtils.isOnlineFile(ocrIdentify.getIdentifyUrl());
//把过去执行过的明细给删掉
ocrIdentifyDetailService.deleteByOcrIdentifyId(ocrIdentify.getId());
//更新开始时间
ocrIdentifyService.updateMasterTaskStartTime(ocrIdentify.getId());
//最终要请求ocr识别的图片对象
JSONObject requestBody = new JSONObject();
if (onlineFile) {
//如果是网络图片,则将图片下载
image = FileOUtils.downLoadFromUrl(imageUrl, FileOUtils.getFileName(imageUrl), OcrConstant.FILE_DOWNLOAD_URL_PREFIX);
fileList.add(image);
}else{
//路径下识别到的图片集合
fileUrlList = FileOUtils.fileLists(relativePath, ocrIdentify.getIdentifyUrl());
AssertUtils.notNull(fileUrlList, "图片地址不存在");
for (String fileUrl : fileUrlList) {
//判断附件是否是 图片格式
if (fileUrl.lastIndexOf(".png") != -1 || fileUrl.lastIndexOf(".jpg") != -1 || fileUrl.lastIndexOf(".jpeg") != -1) {
fileList.add(fileUrl);
}
}
}
//最终要识别哪些图片
int i=0;
for (String img : fileList) {
i++;
requestBody.put("task_id", ocrIdentify.getId()+"_"+i);
requestBody.put("img_path", img);
log.info("----------------------请求参数");
log.info(requestBody.toJSONString());
try {
log.info("----------------------请求参数");
log.info(requestBody.toJSONString());
JSONObject semanticResponseJson = RestUtil.post(OcrConstant.api_test2_identify_url, requestBody);
semanticResponseJson.put("identifyId", ocrIdentify.getId());
log.info("ocr识别返回数据:");
log.info(semanticResponseJson.toJSONString());
ocrIdentifyService.getSemanticInfo(semanticResponseJson);
}catch (Exception e){
log.error("识别图片失败:");
SysLog sysLog=new SysLog();
sysLog.setLogType(2);
sysLog.setLogContent(img+"_识别图片失败:"+e.getMessage());
sysLog.setOperateType(2);
sysLogService.save(sysLog);
log.error(e.getMessage());
log.error("正在重试...");
continue identifyFor;
}
}
//更改任务状态
ocrIdentifyService.updateOcrIdentifyStatus(ocrIdentify.getId(), "1");
}
}
}
}
}

@ -48,8 +48,9 @@ public class HandleTransInit implements ApplicationRunner {
@Override
@Async
@Deprecated
public void run(ApplicationArguments args) throws UnknownHostException {
if (enableHandleTask) {
if (false) {
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {

@ -18,4 +18,6 @@ public interface IOcrIdentifyDetailService extends IService<OcrIdentifyDetail> {
* @return
*/
List<OcrIdentifyDetail> listByIdentifyId(String identifyId);
boolean deleteByOcrIdentifyId(String id);
}

@ -49,6 +49,12 @@ public interface IOcrIdentifyService extends IService<OcrIdentify> {
*/
Result pushTask(JSONObject jsonObject);
/**
*
*/
void updateOcrIdentifyStartTime(String id);
@Deprecated
void updateMasterTaskStartTime(String id);
/**
@ -62,4 +68,17 @@ public interface IOcrIdentifyService extends IService<OcrIdentify> {
* @param simulateChecksVO
*/
JSONObject simulateChecks(SimulateChecksVO simulateChecksVO);
/**
*
* @return
*/
List<OcrIdentify> getSemanticTaskList();
/**
*
* @return
*/
long calculateTimeDifference(String taskName);
}

@ -23,4 +23,11 @@ public class OcrIdentifyDetailServiceImpl extends ServiceImpl<OcrIdentifyDetailM
List<OcrIdentifyDetail> list = super.list(new LambdaQueryWrapper<OcrIdentifyDetail>().eq(OcrIdentifyDetail::getIdentifyId, identifyId));
return list;
}
@Override
public boolean deleteByOcrIdentifyId(String identifyId) {
LambdaQueryWrapper<OcrIdentifyDetail> queryWrapper=new LambdaQueryWrapper<>();
queryWrapper.eq(OcrIdentifyDetail::getIdentifyId,identifyId);
return this.remove(queryWrapper);
}
}

@ -3,6 +3,7 @@ package org.jeecg.modules.ocr.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
@ -10,6 +11,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.OcrConstant;
import org.jeecg.common.util.AssertUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.RestUtil;
import org.jeecg.modules.ocr.dto.OcrIdentifyDTO;
@ -87,13 +89,8 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
log.debug("打印 ocr 结果:" + responseBody.toString());
String identifyId = responseBody.getString("identifyId");//任务id
String imgPath = responseBody.getString("img_path");//图片路径
String imgName = null;//图片名称
if (StringUtils.isNotBlank(imgPath)) {
imgName = imgPath.substring(imgPath.lastIndexOf("/") + 1, imgPath.length());
} else {
log.error("异常,识别图片地址为空");
return;
}
AssertUtils.notEmpty(imgPath,"异常,识别图片地址为空");
String imgName = imgPath.substring(imgPath.lastIndexOf("/") + 1, imgPath.length());//图片名称
String message = responseBody.getString("message");//描述
String taskStatus = responseBody.getString("taskStatus");//任务是否完成
Double ocrTime = responseBody.getDouble("ocr_time");//OCR识别时长
@ -355,7 +352,6 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
@Async
public void updateOcrIdentifyStatus(String id, String status) {
//4.更新主任务状态
OcrIdentifyDTO ocrIdentifyDTO = this.findById(id);
LambdaUpdateWrapper<OcrIdentify> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(OcrIdentify::getId, id);
List<OcrIdentifyDetail> identifyDetailList = ocrIdentifyDetailService.listByIdentifyId(id);
@ -420,7 +416,6 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
public void callbackWly(String ocrIdentifyId) {
OcrIdentifyCallbackLog ocrIdentifyCallbackLog = new OcrIdentifyCallbackLog();
ocrIdentifyCallbackLog.setIdentifyId(ocrIdentifyId);
Date date = new Date();
ocrIdentifyCallbackLog.setCreateTime(date);
LambdaUpdateWrapper<OcrIdentify> updateWrapper = new LambdaUpdateWrapper<OcrIdentify>();
@ -431,7 +426,7 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
List<OcrIdentifyDetail> identifyDetailList = ocrIdentifyDetailService.listByIdentifyId(ocrIdentifyId);
ocrIdentifyCallbackLog.setStartTime(date);
boolean b = CallBackWlyUtils.callbackWly(ocrIdentify, identifyDetailList);
ocrIdentifyCallbackLog.setStatus(b?1:0);
ocrIdentifyCallbackLog.setStatus(b?1:0);//0-失败1-成功
ocrIdentifyCallbackLog.setEndTime(new Date());
ocrIdentifyCallbackLogService.save(ocrIdentifyCallbackLog);
if (b) {
@ -661,6 +656,15 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
return Result.OK("已追加到任务");
}
@Override
public void updateOcrIdentifyStartTime(String id) {
LambdaUpdateWrapper<OcrIdentify> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(OcrIdentify::getStartTime, new Date());
updateWrapper.set(OcrIdentify::getEndTime,null);
updateWrapper.eq(OcrIdentify::getId, id);
super.update(updateWrapper);
}
@Override
public void updateMasterTaskStartTime(String id) {
LambdaUpdateWrapper<OcrIdentify> updateWrapper = new LambdaUpdateWrapper<>();
@ -899,7 +903,7 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
} else if (StringUtils.isBlank(inputText)) {
//没有输入值.
fieldRightMap.put(field, false);
ocrResultAdd(ocrResultDTOList, value.getFieldName(), field, inputText, text, probability, imgPath, "没有输入值不做匹配", false, ruleInfo);
ocrResultAdd(ocrResultDTOList, value.getFieldName(), field, inputText, text, probability, imgPath, "没有输入值", false, ruleInfo);
} else if ("0".equals(ruleInfo)) {
//不必校验,有识别到就行,通过
fieldRightMap.put(field, true);
@ -1026,6 +1030,15 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
return responseBody;
}
@Override
public List<OcrIdentify> getSemanticTaskList() {
LambdaQueryWrapper<OcrIdentify> queryWrapper=new LambdaQueryWrapper<>();
queryWrapper.in(OcrIdentify::getStatus,0,2);
queryWrapper.orderByDesc(OcrIdentify::getPriority);//优先 加急 1>0 不加急
queryWrapper.ne(OcrIdentify::getTaskSource,"模拟实验");//不获取模拟实验室的数据
return this.list(queryWrapper);
}
// 自定义的脱敏方法,对字段进行半脱敏
private String desensitizeText(String text) {
StringBuilder sb = new StringBuilder(text);
@ -1208,4 +1221,17 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
return taskList;
}
@Override
public long calculateTimeDifference(String taskName) {
QueryWrapper<OcrIdentify> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("taskName", taskName).orderByDesc("createTime").last("LIMIT 1");
OcrIdentify lastIdentify = this.getOne(queryWrapper);
if (lastIdentify != null) {
Date lastCreateTime = lastIdentify.getCreateTime();
Date currentTime = new Date();
return (currentTime.getTime() - lastCreateTime.getTime()) / 1000;
}
return 11; // 如果没有找到数据,返回默认值
}
}

Loading…
Cancel
Save