|
@@ -0,0 +1,789 @@
|
|
|
|
|
+package com.jeeplus.assess.programAuditAi.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
+import com.jeeplus.assess.programAuditAi.client.WeKnoraAgentClient;
|
|
|
|
|
+import com.jeeplus.assess.programAuditAi.config.WeKnoraAiAuditProperties;
|
|
|
|
|
+import com.jeeplus.assess.programAuditAi.domain.ProgramAuditAiGeneration;
|
|
|
|
|
+import com.jeeplus.assess.programAuditAi.domain.ProgramAuditAiGenerationFile;
|
|
|
|
|
+import com.jeeplus.assess.programAuditAi.mapper.ProgramAuditAiGenerationFileMapper;
|
|
|
|
|
+import com.jeeplus.assess.programAuditAi.mapper.ProgramAuditAiGenerationMapper;
|
|
|
|
|
+import com.jeeplus.assess.programAuditAi.support.ProgramAuditAiException;
|
|
|
|
|
+import lombok.Data;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
|
+import java.io.InputStream;
|
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
|
+import java.net.HttpURLConnection;
|
|
|
|
|
+import java.net.URL;
|
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.HashSet;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+import java.util.concurrent.Executor;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 校审 AI 异步执行器。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>任务记录与附件快照在请求线程先落库,异步线程只做下载、上传、调用与回写,
|
|
|
|
|
+ * 避免异步线程里的 MyBatis 自动填充拿不到登录上下文。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+public class ProgramAuditAiAsyncService {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Set<String> IMAGE_EXTENSIONS = new HashSet<>(Arrays.asList(
|
|
|
|
|
+ "jpg", "jpeg", "png", "bmp", "gif", "tif", "tiff", "webp"
|
|
|
|
|
+ ));
|
|
|
|
|
+
|
|
|
|
|
+ /** 图片解析每批数量,固定 4 张。 */
|
|
|
|
|
+ private static final int IMAGE_BATCH_SIZE = 4;
|
|
|
|
|
+
|
|
|
|
|
+ private static final String IMAGE_PARSE_QUERY =
|
|
|
|
|
+ "请逐张解析本会话中的全部图片资料,只输出一个 JSON 对象,不要输出 Markdown 代码块、解释或其他文字。\n"
|
|
|
|
|
+ + "JSON 格式固定为:\n"
|
|
|
|
|
+ + "{\"images\":[{\"file_name\":\"图片文件名\",\"picture_content\":\"解析出的文字描述\"}]}\n"
|
|
|
|
|
+ + "要求:\n"
|
|
|
|
|
+ + "1. 必须为每一张上传的图片输出且只输出一条记录,不得合并、遗漏或重复;\n"
|
|
|
|
|
+ + "2. file_name 必须与上传时的原始文件名完全一致;\n"
|
|
|
|
|
+ + "3. picture_content 填写从图片识别到的内容,如:项目名称、金额、等信息;无法识别的写“无法识别”;\n"
|
|
|
|
|
+ + "4. 不得编造,只输出 JSON;所有字符串必须使用双引号。";
|
|
|
|
|
+
|
|
|
|
|
+ @Resource(name = "programAuditAiExecutor")
|
|
|
|
|
+ private Executor executor;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private WeKnoraAiAuditProperties properties;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private WeKnoraAgentClient weKnoraAgentClient;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ProgramAuditAiGenerationMapper generationMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ProgramAuditAiGenerationFileMapper fileMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${server.port:9240}")
|
|
|
|
|
+ private int serverPort;
|
|
|
|
|
+
|
|
|
|
|
+ public void execute(final String generationId) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ executor.execute(new Runnable() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void run() {
|
|
|
|
|
+ doGenerate(generationId);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("校审AI任务提交线程池失败 generationId={} err={}", generationId, e.getMessage());
|
|
|
|
|
+ markFailed(generationId, "TASK_REJECTED", "生成任务提交失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void doGenerate(String generationId) {
|
|
|
|
|
+ ProgramAuditAiGeneration generation = generationMapper.selectById(generationId);
|
|
|
|
|
+ if (generation == null) {
|
|
|
|
|
+ log.warn("校审AI任务不存在 generationId={}", generationId);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Date begin = generation.getStartedAt() == null ? new Date() : generation.getStartedAt();
|
|
|
|
|
+ String auditSessionId = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ List<ProgramAuditAiGenerationFile> files = loadFiles(generationId);
|
|
|
|
|
+ auditSessionId = weKnoraAgentClient.createSession("校审AI-" + generationId);
|
|
|
|
|
+ generation.setWeknoraSessionId(auditSessionId);
|
|
|
|
|
+ updateGeneration(generation);
|
|
|
|
|
+
|
|
|
|
|
+ List<String> auditAttachmentIds = new ArrayList<>();
|
|
|
|
|
+ List<ProgramAuditAiGenerationFile> uploadedFiles = new ArrayList<>();
|
|
|
|
|
+ List<String> uploadedAttachmentIds = new ArrayList<>();
|
|
|
|
|
+ List<ImageItem> imageItems = new ArrayList<>();
|
|
|
|
|
+ int uploadedCount = 0;
|
|
|
|
|
+
|
|
|
|
|
+ // 先把所有非图片附件提交给 WeKnora,再统一等待解析。
|
|
|
|
|
+ // 逐个“上传后等待”会把解析串行化,多文件时很容易超过前端轮询时限。
|
|
|
|
|
+ for (ProgramAuditAiGenerationFile file : files) {
|
|
|
|
|
+ byte[] bytes;
|
|
|
|
|
+ try {
|
|
|
|
|
+ bytes = download(file);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ markFileFailed(file, e.getMessage());
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isImage(file.getFileType())) {
|
|
|
|
|
+ imageItems.add(new ImageItem(file, bytes));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ try {
|
|
|
|
|
+ WeKnoraAgentClient.AttachmentResult result = weKnoraAgentClient.uploadAttachment(
|
|
|
|
|
+ auditSessionId, file.getFileName(), bytes, properties.getAgentId());
|
|
|
|
|
+ if (StringUtils.isNotBlank(result.getId())) {
|
|
|
|
|
+ markFileUploaded(file, result);
|
|
|
|
|
+ if (!"failed".equalsIgnoreCase(StringUtils.defaultString(result.getStatus()))) {
|
|
|
|
|
+ uploadedFiles.add(file);
|
|
|
|
|
+ uploadedAttachmentIds.add(result.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ markFileFailed(file, "WeKnora 未返回附件ID");
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ markFileFailed(file, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = 0; i < uploadedFiles.size(); i++) {
|
|
|
|
|
+ ProgramAuditAiGenerationFile file = uploadedFiles.get(i);
|
|
|
|
|
+ try {
|
|
|
|
|
+ WeKnoraAgentClient.AttachmentResult ready = weKnoraAgentClient.waitAttachmentReady(
|
|
|
|
|
+ auditSessionId, uploadedAttachmentIds.get(i));
|
|
|
|
|
+ if (StringUtils.isNotBlank(ready.getId())) {
|
|
|
|
|
+ auditAttachmentIds.add(ready.getId());
|
|
|
|
|
+ markFileUploaded(file, ready);
|
|
|
|
|
+ uploadedCount++;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ markFileFailed(file, "WeKnora 附件解析完成但未返回附件ID");
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ markFileFailed(file, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String imageTexts = "";
|
|
|
|
|
+ if (!imageItems.isEmpty()) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ imageTexts = parseImages(imageItems);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ String message = StringUtils.abbreviate(
|
|
|
|
|
+ StringUtils.defaultIfBlank(e.getMessage(), "图片解析失败"), 1000);
|
|
|
|
|
+ log.warn("校审AI图片解析失败,降级使用非图片附件继续生成 generationId={} err={}",
|
|
|
|
|
+ generationId, message);
|
|
|
|
|
+ // 图片未纳入本次校审时,标记对应图片失败;只要还有非图片附件,任务继续。
|
|
|
|
|
+ for (ImageItem item : imageItems) {
|
|
|
|
|
+ if (!"failed".equalsIgnoreCase(StringUtils.defaultString(item.file.getParseStatus()))) {
|
|
|
|
|
+ markFileFailed(item.file, "图片解析失败:" + message);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (uploadedCount == 0 && StringUtils.isBlank(imageTexts)) {
|
|
|
|
|
+ throw new ProgramAuditAiException("ATTACHMENT_PARSE_FAILED", "附件均未能成功上传或解析,请检查文件后重试");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 只把真正可用的文件列入校审附件清单,避免把解析失败的图片/文件误报为已读取。
|
|
|
|
|
+ List<ProgramAuditAiGenerationFile> queryFiles = new ArrayList<>();
|
|
|
|
|
+ for (ProgramAuditAiGenerationFile file : files) {
|
|
|
|
|
+ if (!"failed".equalsIgnoreCase(StringUtils.defaultString(file.getParseStatus()))) {
|
|
|
|
|
+ queryFiles.add(file);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ String query = buildQuery(generation, queryFiles, imageTexts);
|
|
|
|
|
+ String answer = weKnoraAgentClient.chat(auditSessionId, query, properties.getAgentId(), auditAttachmentIds);
|
|
|
|
|
+ ParsedResult parsed = parseResult(answer);
|
|
|
|
|
+
|
|
|
|
|
+ generation.setStatus(parsed.getOverallStatus());
|
|
|
|
|
+ generation.setDetailContent(parsed.getDetail().getContent());
|
|
|
|
|
+ generation.setReportContent(parsed.getReport().getContent());
|
|
|
|
|
+ generation.setRemarksContent(parsed.getRemarks().getContent());
|
|
|
|
|
+ generation.setWorkContent(parsed.getWork().getContent());
|
|
|
|
|
+ generation.setDetailStatus(parsed.getDetail().getStatus());
|
|
|
|
|
+ generation.setReportStatus(parsed.getReport().getStatus());
|
|
|
|
|
+ generation.setRemarksStatus(parsed.getRemarks().getStatus());
|
|
|
|
|
+ generation.setWorkStatus(parsed.getWork().getStatus());
|
|
|
|
|
+ generation.setResultJson(parsed.getRawJson());
|
|
|
|
|
+ generation.setErrorCode(null);
|
|
|
|
|
+ generation.setErrorMessage(null);
|
|
|
|
|
+ finish(generation, begin);
|
|
|
|
|
+ log.info("校审AI生成完成 generationId={} status={} duration={}ms",
|
|
|
|
|
+ generationId, generation.getStatus(), generation.getDurationMs());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ String code = e instanceof ProgramAuditAiException
|
|
|
|
|
+ ? ((ProgramAuditAiException) e).getCode() : "AI_GENERATION_FAILED";
|
|
|
|
|
+ String message = StringUtils.abbreviate(StringUtils.defaultIfBlank(e.getMessage(), "生成失败"), 1000);
|
|
|
|
|
+ log.error("校审AI生成失败 generationId={} code={} err={}", generationId, code, message);
|
|
|
|
|
+ markFailed(generation, code, message, begin);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<ProgramAuditAiGenerationFile> loadFiles(String generationId) {
|
|
|
|
|
+ QueryWrapper<ProgramAuditAiGenerationFile> query = new QueryWrapper<>();
|
|
|
|
|
+ query.eq("generation_id", generationId);
|
|
|
|
|
+ query.ne("parse_status", "skipped");
|
|
|
|
|
+ query.orderByAsc("sort_no");
|
|
|
|
|
+ return fileMapper.selectList(query);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private byte[] download(ProgramAuditAiGenerationFile file) {
|
|
|
|
|
+ String key = StringUtils.removeStart(StringUtils.trimToEmpty(file.getStoragePath()), "/");
|
|
|
|
|
+ if (StringUtils.isBlank(key)) {
|
|
|
|
|
+ throw new IllegalStateException("附件 OSS key 为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ byte[] bytes = downloadFromLocalOss(key, file.getFileName());
|
|
|
|
|
+ if (bytes == null || bytes.length == 0) {
|
|
|
|
|
+ throw new IllegalStateException("附件下载失败或为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ return bytes;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 本地 OSS 下载接口已随校审服务加载;异步线程没有登录上下文,Feign 公共模块服务也不在当前部署注册,
|
|
|
|
|
+ * 因此直接调用本机同端口的 OSS 下载端点,避免跨服务路由和鉴权缺失。
|
|
|
|
|
+ */
|
|
|
|
|
+ private byte[] downloadFromLocalOss(String key, String fileName) {
|
|
|
|
|
+ HttpURLConnection connection = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ URL url = new URL("http://127.0.0.1:" + serverPort + "/oss/file/downBytesByStream");
|
|
|
|
|
+ connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
+ connection.setConnectTimeout(5000);
|
|
|
|
|
+ connection.setReadTimeout(120000);
|
|
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
|
|
+ connection.setDoOutput(true);
|
|
|
|
|
+ connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
|
|
|
|
|
+ String body = "key=" + URLEncoder.encode(key, "UTF-8")
|
|
|
|
|
+ + "&fileName=" + URLEncoder.encode(fileName == null ? "" : fileName, "UTF-8");
|
|
|
|
|
+ try (OutputStream out = connection.getOutputStream()) {
|
|
|
|
|
+ out.write(body.getBytes("UTF-8"));
|
|
|
|
|
+ }
|
|
|
|
|
+ int status = connection.getResponseCode();
|
|
|
|
|
+ if (status != 200) {
|
|
|
|
|
+ throw new IllegalStateException("OSS 下载接口返回 HTTP " + status);
|
|
|
|
|
+ }
|
|
|
|
|
+ try (InputStream in = connection.getInputStream()) {
|
|
|
|
|
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
|
|
+ int len;
|
|
|
|
|
+ while ((len = in.read(buffer)) != -1) {
|
|
|
|
|
+ output.write(buffer, 0, len);
|
|
|
|
|
+ }
|
|
|
|
|
+ return output.toByteArray();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("校审AI附件下载异常 key={} err={}", key, e.getMessage());
|
|
|
|
|
+ return null;
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ if (connection != null) {
|
|
|
|
|
+ connection.disconnect();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String parseImages(List<ImageItem> imageItems) {
|
|
|
|
|
+ int batchSize = IMAGE_BATCH_SIZE;
|
|
|
|
|
+ List<ImageText> allParsed = new ArrayList<>();
|
|
|
|
|
+ int batchIndex = 0;
|
|
|
|
|
+ for (int start = 0; start < imageItems.size(); start += batchSize) {
|
|
|
|
|
+ int end = Math.min(start + batchSize, imageItems.size());
|
|
|
|
|
+ List<ImageItem> batch = new ArrayList<>(imageItems.subList(start, end));
|
|
|
|
|
+ batchIndex++;
|
|
|
|
|
+ try {
|
|
|
|
|
+ allParsed.addAll(parseImageBatch(batch, batchIndex));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ String message = StringUtils.abbreviate(
|
|
|
|
|
+ StringUtils.defaultIfBlank(e.getMessage(), "图片解析失败"), 1000);
|
|
|
|
|
+ log.warn("校审AI图片批次解析失败,降级继续 batch={} size={} err={}",
|
|
|
|
|
+ batchIndex, batch.size(), message);
|
|
|
|
|
+ // 只标记当前批次,其他批次和非图片附件继续处理。
|
|
|
|
|
+ for (ImageItem item : batch) {
|
|
|
|
|
+ if (!"failed".equalsIgnoreCase(StringUtils.defaultString(item.file.getParseStatus()))) {
|
|
|
|
|
+ markFileFailed(item.file, "图片解析失败:" + message);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (allParsed.isEmpty()) {
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+ return buildImageTextsJson(allParsed);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 单个图片批次:独立会话、独立解析、独立校验,避免多批图片互相污染上下文。
|
|
|
|
|
+ */
|
|
|
|
|
+ private List<ImageText> parseImageBatch(List<ImageItem> batch, int batchIndex) {
|
|
|
|
|
+ if (StringUtils.isBlank(properties.getImageAgentId())) {
|
|
|
|
|
+ throw new ProgramAuditAiException("AGENT_NOT_CONFIGURED", "图片解析 Agent 未配置");
|
|
|
|
|
+ }
|
|
|
|
|
+ String imageSessionId = weKnoraAgentClient.createSession(
|
|
|
|
|
+ "校审图片-" + batchIndex + "-" + System.currentTimeMillis());
|
|
|
|
|
+ List<ImageItem> uploadedCandidates = new ArrayList<>();
|
|
|
|
|
+ List<String> candidateAttachmentIds = new ArrayList<>();
|
|
|
|
|
+ for (ImageItem item : batch) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ WeKnoraAgentClient.AttachmentResult result = weKnoraAgentClient.uploadAttachment(
|
|
|
|
|
+ imageSessionId, item.file.getFileName(), item.bytes, properties.getImageAgentId());
|
|
|
|
|
+ if (StringUtils.isNotBlank(result.getId())) {
|
|
|
|
|
+ uploadedCandidates.add(item);
|
|
|
|
|
+ candidateAttachmentIds.add(result.getId());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ markFileFailed(item.file, "WeKnora 未返回图片附件ID");
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ markFileFailed(item.file, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 同一批先全部上传,再统一等待解析,避免逐张串行等待。
|
|
|
|
|
+ List<ImageItem> uploadedItems = new ArrayList<>();
|
|
|
|
|
+ List<String> imageAttachmentIds = new ArrayList<>();
|
|
|
|
|
+ for (int i = 0; i < uploadedCandidates.size(); i++) {
|
|
|
|
|
+ ImageItem item = uploadedCandidates.get(i);
|
|
|
|
|
+ try {
|
|
|
|
|
+ WeKnoraAgentClient.AttachmentResult ready = weKnoraAgentClient.waitAttachmentReady(
|
|
|
|
|
+ imageSessionId, candidateAttachmentIds.get(i));
|
|
|
|
|
+ if (StringUtils.isNotBlank(ready.getId())) {
|
|
|
|
|
+ imageAttachmentIds.add(ready.getId());
|
|
|
|
|
+ markFileUploaded(item.file, ready);
|
|
|
|
|
+ uploadedItems.add(item);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ markFileFailed(item.file, "WeKnora 图片解析完成但未返回附件ID");
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ markFileFailed(item.file, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (imageAttachmentIds.isEmpty()) {
|
|
|
|
|
+ return new ArrayList<>();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String answer = weKnoraAgentClient.chat(imageSessionId, IMAGE_PARSE_QUERY,
|
|
|
|
|
+ properties.getImageAgentId(), imageAttachmentIds);
|
|
|
|
|
+ List<ImageText> parsedImages = parseImageAnswer(answer, uploadedItems);
|
|
|
|
|
+ if (parsedImages.isEmpty()) {
|
|
|
|
|
+ throw new ProgramAuditAiException("IMAGE_JSON_INVALID",
|
|
|
|
|
+ "图片解析结果没有有效的 file_name/picture_content");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Set<String> parsedFileIds = new HashSet<>();
|
|
|
|
|
+ for (ImageText imageText : parsedImages) {
|
|
|
|
|
+ markImageParsed(imageText.file, imageText.pictureContent);
|
|
|
|
|
+ parsedFileIds.add(imageText.file.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+ // Agent 少返回的图片没有可用解析文字,按失败处理,避免状态显示为 ready 但无内容。
|
|
|
|
|
+ for (ImageItem item : uploadedItems) {
|
|
|
|
|
+ if (!parsedFileIds.contains(item.file.getId())) {
|
|
|
|
|
+ markFileFailed(item.file, "图片解析结果缺少该图片的 picture_content");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return parsedImages;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 解析图片 Agent 返回的 JSON,并按 file_name 与本次上传的图片逐条匹配。
|
|
|
|
|
+ */
|
|
|
|
|
+ private List<ImageText> parseImageAnswer(String answer, List<ImageItem> expected) {
|
|
|
|
|
+ JSONArray imageArray = extractImageArray(answer);
|
|
|
|
|
+ List<ImageText> results = new ArrayList<>();
|
|
|
|
|
+ Set<Integer> matchedIndexes = new HashSet<>();
|
|
|
|
|
+ for (int i = 0; i < imageArray.size(); i++) {
|
|
|
|
|
+ Object value = imageArray.get(i);
|
|
|
|
|
+ if (!(value instanceof JSONObject)) {
|
|
|
|
|
+ log.warn("校审AI图片解析结果第{}项不是JSON对象,已忽略", i + 1);
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ JSONObject imageObject = (JSONObject) value;
|
|
|
|
|
+ String fileName = StringUtils.trimToEmpty(imageObject.getString("file_name"));
|
|
|
|
|
+ String pictureContent = StringUtils.trimToEmpty(imageObject.getString("picture_content"));
|
|
|
|
|
+ if (StringUtils.isBlank(fileName) || StringUtils.isBlank(pictureContent)) {
|
|
|
|
|
+ log.warn("校审AI图片解析结果第{}项缺少 file_name 或 picture_content,已忽略", i + 1);
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ int matchedIndex = findImageIndex(expected, matchedIndexes, fileName);
|
|
|
|
|
+ if (matchedIndex < 0) {
|
|
|
|
|
+ log.warn("校审AI图片解析结果中的 file_name 未匹配到上传图片: {}", fileName);
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ matchedIndexes.add(matchedIndex);
|
|
|
|
|
+ ImageItem imageItem = expected.get(matchedIndex);
|
|
|
|
|
+ ImageText imageText = new ImageText();
|
|
|
|
|
+ imageText.file = imageItem.file;
|
|
|
|
|
+ imageText.fileName = imageItem.file.getFileName();
|
|
|
|
|
+ imageText.pictureContent = limitPictureContent(pictureContent);
|
|
|
|
|
+ results.add(imageText);
|
|
|
|
|
+ }
|
|
|
|
|
+ return results;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 从 Agent 回答中提取图片解析 JSON。兼容根对象 {"images":[...]} 和根数组 [...].
|
|
|
|
|
+ */
|
|
|
|
|
+ private JSONArray extractImageArray(String answer) {
|
|
|
|
|
+ String text = stripCodeFence(answer);
|
|
|
|
|
+ int start = text.indexOf('{');
|
|
|
|
|
+ int arrayStart = text.indexOf('[');
|
|
|
|
|
+ if (arrayStart >= 0 && (start < 0 || arrayStart < start)) {
|
|
|
|
|
+ start = arrayStart;
|
|
|
|
|
+ }
|
|
|
|
|
+ int end = text.lastIndexOf('}');
|
|
|
|
|
+ int arrayEnd = text.lastIndexOf(']');
|
|
|
|
|
+ if (arrayEnd >= 0 && arrayEnd > end) {
|
|
|
|
|
+ end = arrayEnd;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (start < 0 || end <= start) {
|
|
|
|
|
+ throw new ProgramAuditAiException("IMAGE_JSON_INVALID",
|
|
|
|
|
+ "图片解析 Agent 返回内容不是有效 JSON");
|
|
|
|
|
+ }
|
|
|
|
|
+ Object parsed;
|
|
|
|
|
+ try {
|
|
|
|
|
+ parsed = JSON.parse(text.substring(start, end + 1));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new ProgramAuditAiException("IMAGE_JSON_INVALID",
|
|
|
|
|
+ "图片解析 Agent 返回 JSON 解析失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (parsed instanceof JSONArray) {
|
|
|
|
|
+ return (JSONArray) parsed;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (parsed instanceof JSONObject) {
|
|
|
|
|
+ JSONObject root = (JSONObject) parsed;
|
|
|
|
|
+ Object images = root.get("images");
|
|
|
|
|
+ if (images instanceof JSONArray) {
|
|
|
|
|
+ return (JSONArray) images;
|
|
|
|
|
+ }
|
|
|
|
|
+ Object imageTexts = root.get("image_texts");
|
|
|
|
|
+ if (imageTexts instanceof JSONArray) {
|
|
|
|
|
+ return (JSONArray) imageTexts;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ throw new ProgramAuditAiException("IMAGE_JSON_INVALID",
|
|
|
|
|
+ "图片解析 Agent 返回 JSON 缺少 images 数组");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private int findImageIndex(List<ImageItem> expected, Set<Integer> matchedIndexes, String fileName) {
|
|
|
|
|
+ String target = normalizeFileName(fileName);
|
|
|
|
|
+ int baseNameMatch = -1;
|
|
|
|
|
+ for (int i = 0; i < expected.size(); i++) {
|
|
|
|
|
+ if (matchedIndexes.contains(i)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ String current = normalizeFileName(expected.get(i).file.getFileName());
|
|
|
|
|
+ if (current.equals(target)) {
|
|
|
|
|
+ return i;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (baseNameMatch < 0 && stripExtension(current).equals(stripExtension(target))) {
|
|
|
|
|
+ baseNameMatch = i;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return baseNameMatch;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String normalizeFileName(String fileName) {
|
|
|
|
|
+ String value = StringUtils.trimToEmpty(fileName).replace('\\', '/');
|
|
|
|
|
+ int slash = value.lastIndexOf('/');
|
|
|
|
|
+ if (slash >= 0) {
|
|
|
|
|
+ value = value.substring(slash + 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ return StringUtils.lowerCase(value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String stripExtension(String fileName) {
|
|
|
|
|
+ int dot = fileName.lastIndexOf('.');
|
|
|
|
|
+ return dot > 0 ? fileName.substring(0, dot) : fileName;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String limitPictureContent(String content) {
|
|
|
|
|
+ int max = properties.getImageTextMaxChars() == null ? 4000
|
|
|
|
|
+ : Math.max(200, properties.getImageTextMaxChars());
|
|
|
|
|
+ return truncateContent(content, max, "(图片解析文字超长,已截断)");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String truncateContent(String content, int max, String suffix) {
|
|
|
|
|
+ if (StringUtils.length(content) <= max) {
|
|
|
|
|
+ return content;
|
|
|
|
|
+ }
|
|
|
|
|
+ String safeSuffix = StringUtils.defaultString(suffix);
|
|
|
|
|
+ int keep = Math.max(1, max - safeSuffix.length());
|
|
|
|
|
+ return StringUtils.left(content, keep) + safeSuffix;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成传给校审 Agent 的 image_texts JSON。数据库保留每张图片的 picture_content,
|
|
|
|
|
+ * 这里只对本次 query 的总长度做预算,避免总文本过长。
|
|
|
|
|
+ */
|
|
|
|
|
+ private String buildImageTextsJson(List<ImageText> images) {
|
|
|
|
|
+ int totalLimit = properties.getImageTextTotalMaxChars() == null ? 30000
|
|
|
|
|
+ : Math.max(1000, properties.getImageTextTotalMaxChars());
|
|
|
|
|
+ int total = 0;
|
|
|
|
|
+ for (ImageText image : images) {
|
|
|
|
|
+ total += StringUtils.length(image.pictureContent);
|
|
|
|
|
+ }
|
|
|
|
|
+ JSONArray imageArray = new JSONArray();
|
|
|
|
|
+ int remaining = totalLimit;
|
|
|
|
|
+ for (int i = 0; i < images.size(); i++) {
|
|
|
|
|
+ ImageText image = images.get(i);
|
|
|
|
|
+ String content = image.pictureContent;
|
|
|
|
|
+ if (total > totalLimit) {
|
|
|
|
|
+ int remainingImages = images.size() - i;
|
|
|
|
|
+ int maxForThis = Math.max(200, remaining / Math.max(1, remainingImages));
|
|
|
|
|
+ content = truncateContent(content, maxForThis, "(因图片文字总量限制,已截断)");
|
|
|
|
|
+ remaining -= StringUtils.length(content);
|
|
|
|
|
+ }
|
|
|
|
|
+ JSONObject item = new JSONObject();
|
|
|
|
|
+ item.put("file_name", image.fileName);
|
|
|
|
|
+ item.put("picture_content", content);
|
|
|
|
|
+ imageArray.add(item);
|
|
|
|
|
+ }
|
|
|
|
|
+ JSONObject root = new JSONObject();
|
|
|
|
|
+ root.put("image_texts", imageArray);
|
|
|
|
|
+ return root.toJSONString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void markImageParsed(ProgramAuditAiGenerationFile file, String pictureContent) {
|
|
|
|
|
+ file.setPictureContent(pictureContent);
|
|
|
|
|
+ file.setParseStatus("ready");
|
|
|
|
|
+ file.setParseError(null);
|
|
|
|
|
+ file.setUpdateById(file.getCreateById());
|
|
|
|
|
+ file.setUpdateTime(new Date());
|
|
|
|
|
+ fileMapper.updateById(file);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String buildQuery(ProgramAuditAiGeneration generation, List<ProgramAuditAiGenerationFile> files, String imageTexts) {
|
|
|
|
|
+ StringBuilder query = new StringBuilder();
|
|
|
|
|
+ query.append("项目ID:").append(generation.getProgramId()).append('\n');
|
|
|
|
|
+ query.append("校审级别:").append(generation.getAuditLevel()).append('\n');
|
|
|
|
|
+ if (StringUtils.isNotBlank(generation.getAuditId())) {
|
|
|
|
|
+ query.append("校审记录ID:").append(generation.getAuditId()).append('\n');
|
|
|
|
|
+ }
|
|
|
|
|
+ query.append("本次附件清单:\n");
|
|
|
|
|
+ for (ProgramAuditAiGenerationFile file : files) {
|
|
|
|
|
+ query.append("- ").append(file.getFileName()).append("(").append(StringUtils.upperCase(file.getFileType())).append(")\n");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isNotBlank(imageTexts)) {
|
|
|
|
|
+ query.append("\n图片结构化解析文字(image_texts):\n").append(imageTexts).append('\n');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ query.append("\n图片结构化解析文字(image_texts):无\n");
|
|
|
|
|
+ }
|
|
|
|
|
+ query.append("\n请读取本会话全部附件,严格按系统提示词输出四段校审意见。");
|
|
|
|
|
+ query.append("只输出一个 JSON 对象,字段为 detail、report、remarks、work;");
|
|
|
|
|
+ query.append("每段包含 status、content、issues、citations;status 不是 generated 时,把未生成的原因直接写进 content,不要输出 reason、coverage_mode、coverage_note。\n");
|
|
|
|
|
+ query.append("只出草稿,不审批;不要输出 JSON 之外的任何文字。");
|
|
|
|
|
+ return query.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private ParsedResult parseResult(String answer) {
|
|
|
|
|
+ String text = stripCodeFence(answer);
|
|
|
|
|
+ int start = text.indexOf('{');
|
|
|
|
|
+ int end = text.lastIndexOf('}');
|
|
|
|
|
+ if (start < 0 || end <= start) {
|
|
|
|
|
+ throw new ProgramAuditAiException("JSON_INVALID", "校审 Agent 返回内容不是有效 JSON");
|
|
|
|
|
+ }
|
|
|
|
|
+ String json = text.substring(start, end + 1);
|
|
|
|
|
+ JSONObject root;
|
|
|
|
|
+ try {
|
|
|
|
|
+ root = JSON.parseObject(json);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new ProgramAuditAiException("JSON_INVALID", "校审 Agent 返回 JSON 解析失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ ParsedResult result = new ParsedResult();
|
|
|
|
|
+ result.setRawJson(json);
|
|
|
|
|
+ result.setDetail(readSegment(root, "detail"));
|
|
|
|
|
+ result.setReport(readSegment(root, "report"));
|
|
|
|
|
+ result.setRemarks(readSegment(root, "remarks"));
|
|
|
|
|
+ result.setWork(readSegment(root, "work"));
|
|
|
|
|
+ result.setOverallStatus(overallStatus(result));
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Segment readSegment(JSONObject root, String key) {
|
|
|
|
|
+ Object value = root.get(key);
|
|
|
|
|
+ Segment segment = new Segment();
|
|
|
|
|
+ segment.setKey(key);
|
|
|
|
|
+ String label = segmentLabel(key);
|
|
|
|
|
+ if (!(value instanceof JSONObject)) {
|
|
|
|
|
+ segment.setStatus("failed");
|
|
|
|
|
+ segment.setContent(label + "未生成校审意见:Agent 未返回该段。");
|
|
|
|
|
+ return segment;
|
|
|
|
|
+ }
|
|
|
|
|
+ JSONObject obj = (JSONObject) value;
|
|
|
|
|
+ String status = StringUtils.lowerCase(StringUtils.trimToEmpty(obj.getString("status")));
|
|
|
|
|
+ if (!"generated".equals(status) && !"skipped".equals(status) && !"failed".equals(status)) {
|
|
|
|
|
+ status = StringUtils.isBlank(obj.getString("content")) ? "failed" : "generated";
|
|
|
|
|
+ }
|
|
|
|
|
+ String content = StringUtils.trimToEmpty(obj.getString("content"));
|
|
|
|
|
+ if ("generated".equals(status) && StringUtils.isBlank(content)) {
|
|
|
|
|
+ status = "failed";
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!"generated".equals(status) && StringUtils.isBlank(content)) {
|
|
|
|
|
+ content = "failed".equals(status)
|
|
|
|
|
+ ? label + "生成失败,未生成校审意见:AI 未返回具体错误原因。"
|
|
|
|
|
+ : label + "缺少可用资料,未生成校审意见:AI 未返回具体缺失资料说明。";
|
|
|
|
|
+ }
|
|
|
|
|
+ segment.setStatus(status);
|
|
|
|
|
+ segment.setContent(content);
|
|
|
|
|
+ return segment;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String segmentLabel(String key) {
|
|
|
|
|
+ if ("detail".equals(key)) {
|
|
|
|
|
+ return "明细表";
|
|
|
|
|
+ }
|
|
|
|
|
+ if ("report".equals(key)) {
|
|
|
|
|
+ return "评估报告";
|
|
|
|
|
+ }
|
|
|
|
|
+ if ("remarks".equals(key)) {
|
|
|
|
|
+ return "技术说明";
|
|
|
|
|
+ }
|
|
|
|
|
+ if ("work".equals(key)) {
|
|
|
|
|
+ return "工作底稿";
|
|
|
|
|
+ }
|
|
|
|
|
+ return key;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String overallStatus(ParsedResult result) {
|
|
|
|
|
+ Segment[] segments = new Segment[]{result.getDetail(), result.getReport(), result.getRemarks(), result.getWork()};
|
|
|
|
|
+ boolean allGenerated = true;
|
|
|
|
|
+ boolean anyGenerated = false;
|
|
|
|
|
+ for (Segment segment : segments) {
|
|
|
|
|
+ if ("generated".equals(segment.getStatus())) {
|
|
|
|
|
+ anyGenerated = true;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ allGenerated = false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!anyGenerated) {
|
|
|
|
|
+ return "failed";
|
|
|
|
|
+ }
|
|
|
|
|
+ return allGenerated ? "success" : "partial";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String stripCodeFence(String text) {
|
|
|
|
|
+ String value = StringUtils.trimToEmpty(text);
|
|
|
|
|
+ if (value.startsWith("```")) {
|
|
|
|
|
+ int firstLine = value.indexOf('\n');
|
|
|
|
|
+ if (firstLine >= 0) {
|
|
|
|
|
+ value = value.substring(firstLine + 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (value.endsWith("```")) {
|
|
|
|
|
+ value = value.substring(0, value.length() - 3);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return StringUtils.trimToEmpty(value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void markFileUploaded(ProgramAuditAiGenerationFile file, WeKnoraAgentClient.AttachmentResult result) {
|
|
|
|
|
+ file.setWeknoraAttachmentId(result.getId());
|
|
|
|
|
+ if ("failed".equalsIgnoreCase(result.getStatus())) {
|
|
|
|
|
+ file.setParseStatus("failed");
|
|
|
|
|
+ file.setParseError("WeKnora 附件解析失败");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ file.setParseStatus(StringUtils.defaultIfBlank(result.getStatus(), "uploaded"));
|
|
|
|
|
+ file.setParseError(null);
|
|
|
|
|
+ }
|
|
|
|
|
+ file.setUpdateById(file.getCreateById());
|
|
|
|
|
+ file.setUpdateTime(new Date());
|
|
|
|
|
+ fileMapper.updateById(file);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void markFileFailed(ProgramAuditAiGenerationFile file, String error) {
|
|
|
|
|
+ file.setParseStatus("failed");
|
|
|
|
|
+ file.setParseError(StringUtils.abbreviate(StringUtils.defaultString(error), 1000));
|
|
|
|
|
+ file.setUpdateById(file.getCreateById());
|
|
|
|
|
+ file.setUpdateTime(new Date());
|
|
|
|
|
+ fileMapper.updateById(file);
|
|
|
|
|
+ log.warn("校审AI附件处理失败 file={} err={}", file.getFileName(), file.getParseError());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void markFailed(String generationId, String code, String message) {
|
|
|
|
|
+ ProgramAuditAiGeneration generation = generationMapper.selectById(generationId);
|
|
|
|
|
+ if (generation == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ markFailed(generation, code, message, generation.getStartedAt() == null ? new Date() : generation.getStartedAt());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void markFailed(ProgramAuditAiGeneration generation, String code, String message, Date begin) {
|
|
|
|
|
+ String error = StringUtils.abbreviate(StringUtils.defaultIfBlank(message, "生成失败"), 1000);
|
|
|
|
|
+ generation.setStatus("failed");
|
|
|
|
|
+ generation.setErrorCode(code);
|
|
|
|
|
+ generation.setErrorMessage(error);
|
|
|
|
|
+ generation.setDetailStatus("failed");
|
|
|
|
|
+ generation.setReportStatus("failed");
|
|
|
|
|
+ generation.setRemarksStatus("failed");
|
|
|
|
|
+ generation.setWorkStatus("failed");
|
|
|
|
|
+ generation.setDetailContent(failedContent("明细表", error));
|
|
|
|
|
+ generation.setReportContent(failedContent("评估报告", error));
|
|
|
|
|
+ generation.setRemarksContent(failedContent("技术说明", error));
|
|
|
|
|
+ generation.setWorkContent(failedContent("工作底稿", error));
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject result = new JSONObject();
|
|
|
|
|
+ putFailedSegment(result, "detail", "明细表", error);
|
|
|
|
|
+ putFailedSegment(result, "report", "评估报告", error);
|
|
|
|
|
+ putFailedSegment(result, "remarks", "技术说明", error);
|
|
|
|
|
+ putFailedSegment(result, "work", "工作底稿", error);
|
|
|
|
|
+ generation.setResultJson(result.toJSONString());
|
|
|
|
|
+ finish(generation, begin);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String failedContent(String label, String error) {
|
|
|
|
|
+ return label + "未生成校审意见:AI 生成失败,原因:" + error;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void putFailedSegment(JSONObject result, String key, String label, String error) {
|
|
|
|
|
+ JSONObject segment = new JSONObject();
|
|
|
|
|
+ segment.put("status", "failed");
|
|
|
|
|
+ segment.put("content", failedContent(label, error));
|
|
|
|
|
+ result.put(key, segment);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void finish(ProgramAuditAiGeneration generation, Date begin) {
|
|
|
|
|
+ Date end = new Date();
|
|
|
|
|
+ generation.setFinishedAt(end);
|
|
|
|
|
+ generation.setDurationMs(end.getTime() - begin.getTime());
|
|
|
|
|
+ generation.setUpdateById(generation.getOperatorId());
|
|
|
|
|
+ generation.setUpdateTime(end);
|
|
|
|
|
+ generationMapper.updateById(generation);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void updateGeneration(ProgramAuditAiGeneration generation) {
|
|
|
|
|
+ generation.setUpdateById(generation.getOperatorId());
|
|
|
|
|
+ generation.setUpdateTime(new Date());
|
|
|
|
|
+ generationMapper.updateById(generation);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean isImage(String fileType) {
|
|
|
|
|
+ return IMAGE_EXTENSIONS.contains(StringUtils.lowerCase(StringUtils.trimToEmpty(fileType)));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Data
|
|
|
|
|
+ private static class ImageItem {
|
|
|
|
|
+ private ProgramAuditAiGenerationFile file;
|
|
|
|
|
+ private byte[] bytes;
|
|
|
|
|
+
|
|
|
|
|
+ ImageItem(ProgramAuditAiGenerationFile file, byte[] bytes) {
|
|
|
|
|
+ this.file = file;
|
|
|
|
|
+ this.bytes = bytes;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Data
|
|
|
|
|
+ private static class ImageText {
|
|
|
|
|
+ private ProgramAuditAiGenerationFile file;
|
|
|
|
|
+ private String fileName;
|
|
|
|
|
+ private String pictureContent;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Data
|
|
|
|
|
+ public static class Segment {
|
|
|
|
|
+ private String key;
|
|
|
|
|
+ private String status;
|
|
|
|
|
+ private String content;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Data
|
|
|
|
|
+ public static class ParsedResult {
|
|
|
|
|
+ private String rawJson;
|
|
|
|
|
+ private Segment detail;
|
|
|
|
|
+ private Segment report;
|
|
|
|
|
+ private Segment remarks;
|
|
|
|
|
+ private Segment work;
|
|
|
|
|
+ private String overallStatus;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|