From 8489dfb8d4dffeb031304aa0317fba9a9648e3c0 Mon Sep 17 00:00:00 2001 From: maojian <550076202@qq.com> Date: Fri, 9 Jan 2026 18:37:09 +0800 Subject: [PATCH] =?UTF-8?q?bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/bw/ocr/entity/AppResultDoc.java | 2 +- .../src/main/java/com/bw/ocr/entity/Constants.java | 1 + .../bw/ocr/service/impl/OcrTaskServiceImpl.java | 5 +- .../com/bw/opai/app/controller/AppController.java | 19 +- .../java/com/bw/opai/app/entity/AppResultDoc.java | 2 +- .../java/com/bw/opai/app/service/AppService.java | 14 +- .../bw/opai/app/service/impl/AppServiceImpl.java | 221 +++++++++++---------- 7 files changed, 156 insertions(+), 108 deletions(-) diff --git a/ocr-service/src/main/java/com/bw/ocr/entity/AppResultDoc.java b/ocr-service/src/main/java/com/bw/ocr/entity/AppResultDoc.java index 7ae3fe0..40712b1 100644 --- a/ocr-service/src/main/java/com/bw/ocr/entity/AppResultDoc.java +++ b/ocr-service/src/main/java/com/bw/ocr/entity/AppResultDoc.java @@ -22,7 +22,7 @@ public class AppResultDoc implements Serializable { private String taskId; /** 应用id */ - private String appId; + private Integer appId; /** 状态 0 进行中,1成功,2失败 */ private Integer status; diff --git a/ocr-service/src/main/java/com/bw/ocr/entity/Constants.java b/ocr-service/src/main/java/com/bw/ocr/entity/Constants.java index 7059076..07b3fab 100644 --- a/ocr-service/src/main/java/com/bw/ocr/entity/Constants.java +++ b/ocr-service/src/main/java/com/bw/ocr/entity/Constants.java @@ -17,6 +17,7 @@ public class Constants { /************************应用参数*************************************/ public static final String CODE = "code"; + public static final String ID = "id"; public static final String MESSAGE = "message"; /******************************api使用*******************************/ public static final String CONTENT = "content"; diff --git a/ocr-service/src/main/java/com/bw/ocr/service/impl/OcrTaskServiceImpl.java b/ocr-service/src/main/java/com/bw/ocr/service/impl/OcrTaskServiceImpl.java index bde3f35..056b4e7 100644 --- a/ocr-service/src/main/java/com/bw/ocr/service/impl/OcrTaskServiceImpl.java +++ b/ocr-service/src/main/java/com/bw/ocr/service/impl/OcrTaskServiceImpl.java @@ -102,6 +102,7 @@ public class OcrTaskServiceImpl implements OcrTaskService { //失败直接发送结果 AppResultDoc entity = new AppResultDoc(); entity.setTaskId((String)task.get(Constants.TASKID)); + entity.setAppId((Integer)task.get(Constants.ID)); long now = System.currentTimeMillis(); entity.setCreateTime(now); Map result = new HashMap(16); @@ -157,9 +158,9 @@ public class OcrTaskServiceImpl implements OcrTaskService { //成功 发送结果 AppResultDoc entity = new AppResultDoc(); entity.setTaskId((String)task.get(Constants.TASKID)); + entity.setAppId((Integer)task.get(Constants.ID)); long now = System.currentTimeMillis(); entity.setCreateTime(now); - entity.setTaskId((String)task.get(Constants.TASKID)); Map result = new HashMap(16); result.put(Constants.CONTENT, parseContent); result.put(Constants.WROD_COUNT, parseContent.length()); @@ -174,6 +175,7 @@ public class OcrTaskServiceImpl implements OcrTaskService { //发送失败结果 AppResultDoc entity = new AppResultDoc(); entity.setTaskId((String)task.get(Constants.TASKID)); + entity.setAppId((Integer)task.get(Constants.ID)); long now = System.currentTimeMillis(); entity.setCreateTime(now); Map result = new HashMap(16); @@ -192,6 +194,7 @@ public class OcrTaskServiceImpl implements OcrTaskService { //发送失败结果 AppResultDoc entity = new AppResultDoc(); entity.setTaskId((String)task.get(Constants.TASKID)); + entity.setAppId((Integer)task.get(Constants.ID)); long now = System.currentTimeMillis(); entity.setCreateTime(now); Map result = new HashMap(16); diff --git a/opai-api/src/main/java/com/bw/opai/app/controller/AppController.java b/opai-api/src/main/java/com/bw/opai/app/controller/AppController.java index 39446ac..7eacb7d 100644 --- a/opai-api/src/main/java/com/bw/opai/app/controller/AppController.java +++ b/opai-api/src/main/java/com/bw/opai/app/controller/AppController.java @@ -4,6 +4,7 @@ import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -28,7 +29,7 @@ import lombok.extern.slf4j.Slf4j; @Slf4j @RefreshScope @RequestMapping("/apps") - +@CrossOrigin public class AppController { @Autowired @@ -77,9 +78,11 @@ public class AppController { public Res getMyTasks( @RequestParam(value = "page", defaultValue = "1", required = false) Integer page, @RequestParam(value = "size", defaultValue = "10", required = false) Integer size, - @RequestParam Boolean isData) { + @RequestParam Boolean isData, + @RequestParam Integer appId + ) { - return appService.getMyTasks(page, size,isData); + return appService.getMyTasks(page, size,isData,appId); } /** @@ -91,4 +94,14 @@ public class AppController { public Res appDataSave(@RequestBody AppResultDoc appData) { return appService.dataSave(appData); } + + /** + * 获取任务数据 + * @param id + * @return + */ + @GetMapping("/task/data/{id}") + public Res getAppResById(@PathVariable("id") String id){ + return appService.getAppResById(id); + } } diff --git a/opai-api/src/main/java/com/bw/opai/app/entity/AppResultDoc.java b/opai-api/src/main/java/com/bw/opai/app/entity/AppResultDoc.java index 5ddd238..cf8f8bf 100644 --- a/opai-api/src/main/java/com/bw/opai/app/entity/AppResultDoc.java +++ b/opai-api/src/main/java/com/bw/opai/app/entity/AppResultDoc.java @@ -21,7 +21,7 @@ public class AppResultDoc implements Serializable { private String taskId; /** 应用id */ - private String appId; + private Integer appId; /** 状态 0 进行中,1成功,2失败 */ private Integer status; diff --git a/opai-api/src/main/java/com/bw/opai/app/service/AppService.java b/opai-api/src/main/java/com/bw/opai/app/service/AppService.java index ed35f17..5316416 100644 --- a/opai-api/src/main/java/com/bw/opai/app/service/AppService.java +++ b/opai-api/src/main/java/com/bw/opai/app/service/AppService.java @@ -41,7 +41,19 @@ public interface AppService { * @param size * @return */ - public Res getMyTasks(Integer page, Integer size,Boolean isData); + public Res getMyTasks(Integer page, Integer size,Boolean isData,Integer appId); + /** + * 数据结果保存 + * @param appData + * @return + */ public Res dataSave(AppResultDoc appData); + + /** + * 查询任务数据 + * @param id + * @return + */ + public Res getAppResById(String id); } diff --git a/opai-api/src/main/java/com/bw/opai/app/service/impl/AppServiceImpl.java b/opai-api/src/main/java/com/bw/opai/app/service/impl/AppServiceImpl.java index 56d2fe1..1699da9 100644 --- a/opai-api/src/main/java/com/bw/opai/app/service/impl/AppServiceImpl.java +++ b/opai-api/src/main/java/com/bw/opai/app/service/impl/AppServiceImpl.java @@ -153,7 +153,7 @@ public class AppServiceImpl implements AppService { @Override - public Res getMyTasks(Integer page, Integer size,Boolean isData) { + public Res getMyTasks(Integer page, Integer size,Boolean isData,Integer appId) { if (page == null || page < 1) { page = 1; @@ -173,6 +173,7 @@ public class AppServiceImpl implements AppService { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(Task::getDel, 0) + .eq(Task::getAppId,appId) .eq(userId != null, Task::getUserId, userId) .orderByDesc(Task::getCreateTime); @@ -182,106 +183,8 @@ public class AppServiceImpl implements AppService { List tasks = result.getRecords(); for (Task task : tasks) { String taskId = task.getId(); - CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - credentialsProvider.setCredentials( - AuthScope.ANY, - new UsernamePasswordCredentials(esUser, esPassword) - ); - - CloseableHttpClient httpClient = null; - try { - if (esUser != null && !esUser.trim().equals("")) { - httpClient = HttpClients.custom() - .setDefaultCredentialsProvider(credentialsProvider) - .build(); - } else { - httpClient = HttpClients.custom().build(); - } - - // ================== 构建查询 DSL ================== - - Map query = new HashMap(); - // must 条件 - List> mustList = new ArrayList>(); - - Map termDel = new HashMap(); - termDel.put("del", 0); - Map termDelWrap = new HashMap(); - termDelWrap.put("term", termDel); - mustList.add(termDelWrap); - //任务id查询条件 - Map termTaskId = new HashMap(); - termTaskId.put("taskId", taskId); - Map termTaskIdWrap = new HashMap(); - termTaskIdWrap.put("term", termTaskId); - mustList.add(termTaskIdWrap); - - Map bool = new HashMap(); - bool.put("must", mustList); - - Map queryBody = new HashMap(); - queryBody.put("bool", bool); - - query.put("query", queryBody); - - // sort - List> sortList = new ArrayList>(); - Map order = new HashMap(); - order.put("order", "desc"); - Map sortField = new HashMap(); - sortField.put("createTime", order); - sortList.add(sortField); - query.put("sort", sortList); - log.info("任务数据查询条件:{}",JSONObject.toJSONString(query)); - - // ================== 发起 HTTP 请求 ================== - StringBuffer host = new StringBuffer(); - host.append(esUrl) - .append("/") - .append(indexName) - .append("/_search"); - - HttpPost httpPost = new HttpPost(host.toString()); - httpPost.setHeader("Content-Type", "application/json"); - - StringEntity entity = new StringEntity( - JSONObject.toJSONString(query), - ContentType.APPLICATION_JSON - ); - httpPost.setEntity(entity); - - HttpResponse response = httpClient.execute(httpPost); - int statusCode = response.getStatusLine().getStatusCode(); - String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); - - if (statusCode != 200) { - log.error("ES 查询失败 status={}, body={}", statusCode, responseBody); - continue; - } - - // ================== 解析返回 ================== - JSONObject json = JSONObject.parseObject(responseBody); - JSONObject hits = json.getJSONObject("hits"); - - List list = new ArrayList(); - JSONArray hitList = hits.getJSONArray("hits"); - for (int i = 0; i < hitList.size(); i++) { - JSONObject source = hitList.getJSONObject(i).getJSONObject("_source"); - AppResultDoc item = source.toJavaObject(AppResultDoc.class); - list.add(item); - } - task.setData(list); - - } catch (Exception e) { - log.error("查询用户任务失败 userId={}", userId, e); - continue; - } finally { - if (httpClient != null) { - try { - httpClient.close(); - } catch (Exception ignored) {} - } - } + List list = getAppData(taskId); + task.setData(list); } } log.info("任务查询成功"); @@ -292,6 +195,114 @@ public class AppServiceImpl implements AppService { } } + + /** + * 根据任务id查询是数据 + * @param taskId + * @return + */ + private List getAppData(String taskId) { + CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials( + AuthScope.ANY, + new UsernamePasswordCredentials(esUser, esPassword) + ); + List list = new ArrayList(); + CloseableHttpClient httpClient = null; + try { + if (esUser != null && !esUser.trim().equals("")) { + httpClient = HttpClients.custom() + .setDefaultCredentialsProvider(credentialsProvider) + .build(); + } else { + httpClient = HttpClients.custom().build(); + } + + // ================== 构建查询 DSL ================== + + Map query = new HashMap(); + // must 条件 + List> mustList = new ArrayList>(); + + Map termDel = new HashMap(); + termDel.put("del", 0); + Map termDelWrap = new HashMap(); + termDelWrap.put("term", termDel); + mustList.add(termDelWrap); + //任务id查询条件 + Map termTaskId = new HashMap(); + termTaskId.put("taskId", taskId); + Map termTaskIdWrap = new HashMap(); + termTaskIdWrap.put("term", termTaskId); + mustList.add(termTaskIdWrap); + + Map bool = new HashMap(); + bool.put("must", mustList); + + Map queryBody = new HashMap(); + queryBody.put("bool", bool); + + query.put("query", queryBody); + + // sort + List> sortList = new ArrayList>(); + Map order = new HashMap(); + order.put("order", "desc"); + Map sortField = new HashMap(); + sortField.put("createTime", order); + sortList.add(sortField); + query.put("sort", sortList); + log.info("任务数据查询条件:{}",JSONObject.toJSONString(query)); + + // ================== 发起 HTTP 请求 ================== + StringBuffer host = new StringBuffer(); + host.append(esUrl) + .append("/") + .append(indexName) + .append("/_search"); + + HttpPost httpPost = new HttpPost(host.toString()); + httpPost.setHeader("Content-Type", "application/json"); + + StringEntity entity = new StringEntity( + JSONObject.toJSONString(query), + ContentType.APPLICATION_JSON + ); + httpPost.setEntity(entity); + + HttpResponse response = httpClient.execute(httpPost); + int statusCode = response.getStatusLine().getStatusCode(); + String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); + + if (statusCode != 200) { + log.error("ES 查询失败 taskId={},status={}, body={}", taskId,statusCode, responseBody); + return list; + } + + // ================== 解析返回 ================== + JSONObject json = JSONObject.parseObject(responseBody); + JSONObject hits = json.getJSONObject("hits"); + + + JSONArray hitList = hits.getJSONArray("hits"); + for (int i = 0; i < hitList.size(); i++) { + JSONObject source = hitList.getJSONObject(i).getJSONObject("_source"); + AppResultDoc item = source.toJavaObject(AppResultDoc.class); + list.add(item); + } + return list; + + } catch (Exception e) { + log.error("查询任务数据失败 taskId={}", taskId, e); + return list; + } finally { + if (httpClient != null) { + try { + httpClient.close(); + } catch (Exception ignored) {} + } + } + } @Override public Res dataSave(AppResultDoc appData) { String docId = UUID.randomUUID().toString().replace("-", Constants.EMPTY); @@ -302,6 +313,7 @@ public class AppServiceImpl implements AppService { log.error("启动应用记录写入异常, appData={}", appData); Task task = new Task(); task.setId(appData.getTaskId()); + task.setAppId(appData.getAppId()); task.setStatus(appData.getStatus()); task.setFinishTime(LocalDateTime.now()); task.setStatus(2); @@ -312,6 +324,7 @@ public class AppServiceImpl implements AppService { log.info("任务状态更新"); Task task = new Task(); task.setId(appData.getTaskId()); + task.setAppId(appData.getAppId()); task.setStatus(appData.getStatus()); task.setFinishTime(LocalDateTime.now()); task.setStatus(appData.getStatus()); @@ -384,5 +397,11 @@ public class AppServiceImpl implements AppService { } catch (Exception ignored){} } } + @Override + public Res getAppResById(String id) { + // TODO Auto-generated method stub + List list = getAppData(id); + return Res.ok(list); + } }