Merge branch '0.8.X' of https://gitee.com/hdka/klp-oa into 0.8.X
This commit is contained in:
@@ -43,98 +43,4 @@ public class WmsImageRecognitionController extends BaseController {
|
|||||||
return R.ok(iImageRecognitionService.recognizeImage(bo));
|
return R.ok(iImageRecognitionService.recognizeImage(bo));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 识别BOM内容
|
|
||||||
*/
|
|
||||||
@PostMapping("/recognizeBom")
|
|
||||||
public R<ImageRecognitionVo> recognizeBom(@RequestBody @Validated(AddGroup.class) ImageRecognitionBo bo) {
|
|
||||||
bo.setRecognitionType("bom");
|
|
||||||
return R.ok(iImageRecognitionService.recognizeBom(bo));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 识别文字内容
|
|
||||||
*/
|
|
||||||
@PostMapping("/recognizeText")
|
|
||||||
public R<ImageRecognitionVo> recognizeText(@RequestBody @Validated(AddGroup.class) ImageRecognitionBo bo) {
|
|
||||||
bo.setRecognitionType("text");
|
|
||||||
return R.ok(iImageRecognitionService.recognizeText(bo));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 批量识别图片
|
|
||||||
*/
|
|
||||||
@PostMapping("/recognizeBatch")
|
|
||||||
public R<List<ImageRecognitionVo>> recognizeBatch(@RequestBody @Validated(AddGroup.class) List<ImageRecognitionBo> boList) {
|
|
||||||
return R.ok(iImageRecognitionService.recognizeImages(boList));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试AI连接
|
|
||||||
*/
|
|
||||||
@PostMapping("/testConnection")
|
|
||||||
public R<Map<String, Object>> testConnection() {
|
|
||||||
return R.ok(iImageRecognitionService.testAiConnection());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取识别配置
|
|
||||||
*/
|
|
||||||
@GetMapping("/config")
|
|
||||||
public R<Map<String, Object>> getConfig() {
|
|
||||||
return R.ok(iImageRecognitionService.getRecognitionConfig());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 更新识别配置
|
|
||||||
*/
|
|
||||||
@PostMapping("/config")
|
|
||||||
public R<Void> updateConfig(@RequestBody Map<String, Object> config) {
|
|
||||||
iImageRecognitionService.updateRecognitionConfig(config);
|
|
||||||
return R.ok();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取识别历史
|
|
||||||
*/
|
|
||||||
@PostMapping("/history")
|
|
||||||
public R<Map<String, Object>> getHistory(@RequestBody Map<String, Object> pageQuery) {
|
|
||||||
return R.ok(iImageRecognitionService.getRecognitionHistory(pageQuery));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 简单识别接口(兼容原有格式)
|
|
||||||
*/
|
|
||||||
@PostMapping("/recognizeTextSimple")
|
|
||||||
public R<ImageRecognitionVo> recognizeTextSimple(@RequestBody Map<String, String> request) {
|
|
||||||
String imgUrl = request.get("imgUrl");
|
|
||||||
if (imgUrl == null || imgUrl.trim().isEmpty()) {
|
|
||||||
return R.fail("图片URL不能为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageRecognitionBo bo = new ImageRecognitionBo();
|
|
||||||
bo.setImageUrl(imgUrl);
|
|
||||||
bo.setRecognitionType("text");
|
|
||||||
|
|
||||||
ImageRecognitionVo result = iImageRecognitionService.recognizeText(bo);
|
|
||||||
return R.ok(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 识别BOM接口(兼容原有格式)
|
|
||||||
*/
|
|
||||||
@PostMapping("/recognizeBomSimple")
|
|
||||||
public R<ImageRecognitionVo> recognizeBomSimple(@RequestBody Map<String, String> request) {
|
|
||||||
String imgUrl = request.get("imgUrl");
|
|
||||||
if (imgUrl == null || imgUrl.trim().isEmpty()) {
|
|
||||||
return R.fail("图片URL不能为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
ImageRecognitionBo bo = new ImageRecognitionBo();
|
|
||||||
bo.setImageUrl(imgUrl);
|
|
||||||
bo.setRecognitionType("bom");
|
|
||||||
|
|
||||||
ImageRecognitionVo result = iImageRecognitionService.recognizeBom(bo);
|
|
||||||
return R.ok(result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public class ImageRecognitionBo extends BaseEntity {
|
|||||||
/**
|
/**
|
||||||
* 是否启用多轮投票
|
* 是否启用多轮投票
|
||||||
*/
|
*/
|
||||||
private Boolean enableVoting = true;
|
private Boolean enableVoting = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 投票轮数
|
* 投票轮数
|
||||||
|
|||||||
19
klp-wms/src/main/java/com/klp/domain/vo/AttributeVo.java
Normal file
19
klp-wms/src/main/java/com/klp/domain/vo/AttributeVo.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package com.klp.domain.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 属性信息
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class AttributeVo {
|
||||||
|
/**
|
||||||
|
* 属性名称
|
||||||
|
*/
|
||||||
|
private String attrKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 属性值
|
||||||
|
*/
|
||||||
|
private String attrValue;
|
||||||
|
}
|
||||||
39
klp-wms/src/main/java/com/klp/domain/vo/BomItemVo.java
Normal file
39
klp-wms/src/main/java/com/klp/domain/vo/BomItemVo.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package com.klp.domain.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BOM项目信息
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class BomItemVo {
|
||||||
|
/**
|
||||||
|
* 原材料ID
|
||||||
|
*/
|
||||||
|
private String rawMaterialId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 原材料名称
|
||||||
|
*/
|
||||||
|
private String rawMaterialName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数量
|
||||||
|
*/
|
||||||
|
private Double quantity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单位
|
||||||
|
*/
|
||||||
|
private String unit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规格
|
||||||
|
*/
|
||||||
|
private String specification;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 备注
|
||||||
|
*/
|
||||||
|
private String remark;
|
||||||
|
}
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
package com.klp.domain.vo;
|
package com.klp.domain.vo;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -19,11 +17,6 @@ public class ImageRecognitionVo implements Serializable {
|
|||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
/**
|
|
||||||
* 识别ID
|
|
||||||
*/
|
|
||||||
private Long recognitionId;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 图片URL
|
* 图片URL
|
||||||
*/
|
*/
|
||||||
@@ -54,11 +47,6 @@ public class ImageRecognitionVo implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private List<AttributeVo> attributes;
|
private List<AttributeVo> attributes;
|
||||||
|
|
||||||
/**
|
|
||||||
* 识别置信度
|
|
||||||
*/
|
|
||||||
private Double confidence;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 识别状态:success-成功,failed-失败,processing-处理中
|
* 识别状态:success-成功,failed-失败,processing-处理中
|
||||||
*/
|
*/
|
||||||
@@ -74,61 +62,6 @@ public class ImageRecognitionVo implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private Long processingTime;
|
private Long processingTime;
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建时间
|
|
||||||
*/
|
|
||||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
|
||||||
private Date createTime;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* BOM项目信息
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public static class BomItemVo {
|
|
||||||
/**
|
|
||||||
* 原材料ID
|
|
||||||
*/
|
|
||||||
private String rawMaterialId;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 原材料名称
|
|
||||||
*/
|
|
||||||
private String rawMaterialName;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 数量
|
|
||||||
*/
|
|
||||||
private Double quantity;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 单位
|
|
||||||
*/
|
|
||||||
private String unit;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 规格
|
|
||||||
*/
|
|
||||||
private String specification;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 备注
|
|
||||||
*/
|
|
||||||
private String remark;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 属性信息
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public static class AttributeVo {
|
|
||||||
/**
|
|
||||||
* 属性名称
|
|
||||||
*/
|
|
||||||
private String attrKey;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 属性值
|
|
||||||
*/
|
|
||||||
private String attrValue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -22,56 +22,5 @@ public interface IImageRecognitionService {
|
|||||||
*/
|
*/
|
||||||
ImageRecognitionVo recognizeImage(ImageRecognitionBo bo);
|
ImageRecognitionVo recognizeImage(ImageRecognitionBo bo);
|
||||||
|
|
||||||
/**
|
|
||||||
* 批量识别图片
|
|
||||||
*
|
|
||||||
* @param boList 识别请求参数列表
|
|
||||||
* @return 识别结果列表
|
|
||||||
*/
|
|
||||||
List<ImageRecognitionVo> recognizeImages(List<ImageRecognitionBo> boList);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 识别BOM内容
|
|
||||||
*
|
|
||||||
* @param bo 识别请求参数
|
|
||||||
* @return BOM识别结果
|
|
||||||
*/
|
|
||||||
ImageRecognitionVo recognizeBom(ImageRecognitionBo bo);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 识别文字内容
|
|
||||||
*
|
|
||||||
* @param bo 识别请求参数
|
|
||||||
* @return 文字识别结果
|
|
||||||
*/
|
|
||||||
ImageRecognitionVo recognizeText(ImageRecognitionBo bo);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试AI连接
|
|
||||||
*
|
|
||||||
* @return 连接测试结果
|
|
||||||
*/
|
|
||||||
Map<String, Object> testAiConnection();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取识别配置
|
|
||||||
*
|
|
||||||
* @return 配置信息
|
|
||||||
*/
|
|
||||||
Map<String, Object> getRecognitionConfig();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 更新识别配置
|
|
||||||
*
|
|
||||||
* @param config 配置信息
|
|
||||||
*/
|
|
||||||
void updateRecognitionConfig(Map<String, Object> config);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取识别历史
|
|
||||||
*
|
|
||||||
* @param pageQuery 分页查询参数
|
|
||||||
* @return 识别历史列表
|
|
||||||
*/
|
|
||||||
Map<String, Object> getRecognitionHistory(Map<String, Object> pageQuery);
|
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.klp.config.ImageRecognitionConfig;
|
import com.klp.config.ImageRecognitionConfig;
|
||||||
import com.klp.domain.bo.ImageRecognitionBo;
|
import com.klp.domain.bo.ImageRecognitionBo;
|
||||||
|
import com.klp.domain.vo.AttributeVo;
|
||||||
import com.klp.domain.vo.ImageRecognitionVo;
|
import com.klp.domain.vo.ImageRecognitionVo;
|
||||||
import com.klp.service.IImageRecognitionService;
|
import com.klp.service.IImageRecognitionService;
|
||||||
import com.klp.utils.ImageProcessingUtils;
|
import com.klp.utils.ImageProcessingUtils;
|
||||||
@@ -78,33 +79,7 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<ImageRecognitionVo> recognizeImages(List<ImageRecognitionBo> boList) {
|
|
||||||
List<CompletableFuture<ImageRecognitionVo>> futures = new ArrayList<>();
|
|
||||||
|
|
||||||
for (ImageRecognitionBo bo : boList) {
|
|
||||||
CompletableFuture<ImageRecognitionVo> future = CompletableFuture.supplyAsync(() ->
|
|
||||||
recognizeImage(bo), executorService);
|
|
||||||
futures.add(future);
|
|
||||||
}
|
|
||||||
|
|
||||||
List<ImageRecognitionVo> results = new ArrayList<>();
|
|
||||||
for (CompletableFuture<ImageRecognitionVo> future : futures) {
|
|
||||||
try {
|
|
||||||
results.add(future.get());
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("批量识别失败", e);
|
|
||||||
ImageRecognitionVo errorResult = new ImageRecognitionVo();
|
|
||||||
errorResult.setStatus("failed");
|
|
||||||
errorResult.setErrorMessage(e.getMessage());
|
|
||||||
results.add(errorResult);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ImageRecognitionVo recognizeBom(ImageRecognitionBo bo) {
|
public ImageRecognitionVo recognizeBom(ImageRecognitionBo bo) {
|
||||||
String prompt = buildBomPrompt(bo);
|
String prompt = buildBomPrompt(bo);
|
||||||
String aiResponse = callAiApi(bo.getImageUrl(), prompt, bo.getEnableVoting(), bo.getVotingRounds());
|
String aiResponse = callAiApi(bo.getImageUrl(), prompt, bo.getEnableVoting(), bo.getVotingRounds());
|
||||||
@@ -117,7 +92,7 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService {
|
|||||||
// 解析识别结果
|
// 解析识别结果
|
||||||
try {
|
try {
|
||||||
// 直接解析属性数组
|
// 直接解析属性数组
|
||||||
List<ImageRecognitionVo.AttributeVo> attributes = parseAttributesResponse(aiResponse);
|
List<AttributeVo> attributes = parseAttributesResponse(aiResponse);
|
||||||
result.setAttributes(attributes);
|
result.setAttributes(attributes);
|
||||||
|
|
||||||
// 构建结构化结果
|
// 构建结构化结果
|
||||||
@@ -138,7 +113,6 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ImageRecognitionVo recognizeText(ImageRecognitionBo bo) {
|
public ImageRecognitionVo recognizeText(ImageRecognitionBo bo) {
|
||||||
String prompt = buildTextPrompt(bo);
|
String prompt = buildTextPrompt(bo);
|
||||||
String aiResponse = callAiApi(bo.getImageUrl(), prompt, bo.getEnableVoting(), bo.getVotingRounds());
|
String aiResponse = callAiApi(bo.getImageUrl(), prompt, bo.getEnableVoting(), bo.getVotingRounds());
|
||||||
@@ -151,66 +125,6 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, Object> testAiConnection() {
|
|
||||||
Map<String, Object> result = new HashMap<>();
|
|
||||||
try {
|
|
||||||
// 构建测试请求
|
|
||||||
Map<String, Object> requestBody = new HashMap<>();
|
|
||||||
requestBody.put("model", config.getModelName());
|
|
||||||
|
|
||||||
Map<String, String> message = new HashMap<>();
|
|
||||||
message.put("role", "user");
|
|
||||||
message.put("content", "你好");
|
|
||||||
requestBody.put("messages", Arrays.asList(message));
|
|
||||||
requestBody.put("max_tokens", 10);
|
|
||||||
|
|
||||||
HttpHeaders headers = new HttpHeaders();
|
|
||||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
||||||
headers.setBearerAuth(config.getApiKey());
|
|
||||||
|
|
||||||
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);
|
|
||||||
ResponseEntity<Map> response = restTemplate.postForEntity(
|
|
||||||
config.getApiUrl(), entity, Map.class);
|
|
||||||
|
|
||||||
result.put("success", true);
|
|
||||||
result.put("message", "AI连接测试成功");
|
|
||||||
result.put("response", response.getBody());
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("AI连接测试失败", e);
|
|
||||||
result.put("success", false);
|
|
||||||
result.put("message", "AI连接测试失败: " + e.getMessage());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, Object> getRecognitionConfig() {
|
|
||||||
Map<String, Object> configMap = new HashMap<>();
|
|
||||||
configMap.put("apiUrl", config.getApiUrl());
|
|
||||||
configMap.put("modelName", config.getModelName());
|
|
||||||
configMap.put("apiKey", config.getApiKey().substring(0, 10) + "...");
|
|
||||||
configMap.put("maxRetries", config.getMaxRetries());
|
|
||||||
configMap.put("temperature", config.getTemperature());
|
|
||||||
configMap.put("maxTokens", config.getMaxTokens());
|
|
||||||
configMap.put("maxImageDimension", config.getMaxImageDimension());
|
|
||||||
configMap.put("imageQuality", config.getImageQuality());
|
|
||||||
return configMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void updateRecognitionConfig(Map<String, Object> config) {
|
|
||||||
log.info("更新识别配置: {}", config);
|
|
||||||
// 这里可以实现配置更新逻辑
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, Object> getRecognitionHistory(Map<String, Object> pageQuery) {
|
|
||||||
Map<String, Object> result = new HashMap<>();
|
|
||||||
result.put("rows", new ArrayList<>());
|
|
||||||
result.put("total", 0L);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用识别方法
|
* 通用识别方法
|
||||||
@@ -426,15 +340,15 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService {
|
|||||||
/**
|
/**
|
||||||
* 解析属性数组响应
|
* 解析属性数组响应
|
||||||
*/
|
*/
|
||||||
private List<ImageRecognitionVo.AttributeVo> parseAttributesResponse(String response) {
|
private List<AttributeVo> parseAttributesResponse(String response) {
|
||||||
try {
|
try {
|
||||||
// 尝试直接解析JSON数组
|
// 尝试直接解析JSON数组
|
||||||
List<Map<String, Object>> attrList = objectMapper.readValue(response,
|
List<Map<String, Object>> attrList = objectMapper.readValue(response,
|
||||||
objectMapper.getTypeFactory().constructCollectionType(List.class, Map.class));
|
objectMapper.getTypeFactory().constructCollectionType(List.class, Map.class));
|
||||||
|
|
||||||
List<ImageRecognitionVo.AttributeVo> attributes = new ArrayList<>();
|
List<AttributeVo> attributes = new ArrayList<>();
|
||||||
for (Map<String, Object> attr : attrList) {
|
for (Map<String, Object> attr : attrList) {
|
||||||
ImageRecognitionVo.AttributeVo attribute = new ImageRecognitionVo.AttributeVo();
|
AttributeVo attribute = new AttributeVo();
|
||||||
attribute.setAttrKey((String) attr.get("attrKey"));
|
attribute.setAttrKey((String) attr.get("attrKey"));
|
||||||
attribute.setAttrValue((String) attr.get("attrValue"));
|
attribute.setAttrValue((String) attr.get("attrValue"));
|
||||||
attributes.add(attribute);
|
attributes.add(attribute);
|
||||||
@@ -450,9 +364,9 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService {
|
|||||||
List<Map<String, Object>> attrList = objectMapper.readValue(matcher.group(),
|
List<Map<String, Object>> attrList = objectMapper.readValue(matcher.group(),
|
||||||
objectMapper.getTypeFactory().constructCollectionType(List.class, Map.class));
|
objectMapper.getTypeFactory().constructCollectionType(List.class, Map.class));
|
||||||
|
|
||||||
List<ImageRecognitionVo.AttributeVo> attributes = new ArrayList<>();
|
List<AttributeVo> attributes = new ArrayList<>();
|
||||||
for (Map<String, Object> attr : attrList) {
|
for (Map<String, Object> attr : attrList) {
|
||||||
ImageRecognitionVo.AttributeVo attribute = new ImageRecognitionVo.AttributeVo();
|
AttributeVo attribute = new AttributeVo();
|
||||||
attribute.setAttrKey((String) attr.get("attrKey"));
|
attribute.setAttrKey((String) attr.get("attrKey"));
|
||||||
attribute.setAttrValue((String) attr.get("attrValue"));
|
attribute.setAttrValue((String) attr.get("attrValue"));
|
||||||
attributes.add(attribute);
|
attributes.add(attribute);
|
||||||
@@ -469,132 +383,5 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 解析BOM响应(保留用于兼容性)
|
|
||||||
*/
|
|
||||||
private Map<String, Object> parseBomResponse(String response) {
|
|
||||||
try {
|
|
||||||
// 尝试直接解析JSON
|
|
||||||
return objectMapper.readValue(response, Map.class);
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
// 如果直接解析失败,尝试提取JSON部分
|
|
||||||
Pattern jsonPattern = Pattern.compile("\\{[\\s\\S]*\\}");
|
|
||||||
Matcher matcher = jsonPattern.matcher(response);
|
|
||||||
if (matcher.find()) {
|
|
||||||
try {
|
|
||||||
return objectMapper.readValue(matcher.group(), Map.class);
|
|
||||||
} catch (JsonProcessingException ex) {
|
|
||||||
log.warn("无法解析BOM响应为JSON: {}", response);
|
|
||||||
Map<String, Object> fallback = new HashMap<>();
|
|
||||||
fallback.put("rawText", response);
|
|
||||||
return fallback;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Map<String, Object> fallback = new HashMap<>();
|
|
||||||
fallback.put("rawText", response);
|
|
||||||
return fallback;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 提取BOM项目列表
|
|
||||||
*/
|
|
||||||
private List<ImageRecognitionVo.BomItemVo> extractBomItems(Map<String, Object> structuredResult) {
|
|
||||||
List<ImageRecognitionVo.BomItemVo> bomItems = new ArrayList<>();
|
|
||||||
|
|
||||||
try {
|
|
||||||
List<Map<String, Object>> items = (List<Map<String, Object>>) structuredResult.get("bomItems");
|
|
||||||
if (items != null) {
|
|
||||||
for (Map<String, Object> item : items) {
|
|
||||||
ImageRecognitionVo.BomItemVo bomItem = new ImageRecognitionVo.BomItemVo();
|
|
||||||
bomItem.setRawMaterialId((String) item.get("rawMaterialId"));
|
|
||||||
bomItem.setRawMaterialName((String) item.get("rawMaterialName"));
|
|
||||||
|
|
||||||
Object quantity = item.get("quantity");
|
|
||||||
if (quantity instanceof Number) {
|
|
||||||
bomItem.setQuantity(((Number) quantity).doubleValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
bomItem.setUnit((String) item.get("unit"));
|
|
||||||
bomItem.setSpecification((String) item.get("specification"));
|
|
||||||
bomItem.setRemark((String) item.get("remark"));
|
|
||||||
|
|
||||||
bomItems.add(bomItem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn("提取BOM项目失败: {}", e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
return bomItems;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 提取属性列表
|
|
||||||
*/
|
|
||||||
private List<ImageRecognitionVo.AttributeVo> extractAttributes(Map<String, Object> structuredResult) {
|
|
||||||
List<ImageRecognitionVo.AttributeVo> attributes = new ArrayList<>();
|
|
||||||
|
|
||||||
try {
|
|
||||||
List<Map<String, Object>> attrList = (List<Map<String, Object>>) structuredResult.get("attributes");
|
|
||||||
if (attrList != null) {
|
|
||||||
for (Map<String, Object> attr : attrList) {
|
|
||||||
ImageRecognitionVo.AttributeVo attribute = new ImageRecognitionVo.AttributeVo();
|
|
||||||
attribute.setAttrKey((String) attr.get("attrKey"));
|
|
||||||
attribute.setAttrValue((String) attr.get("attrValue"));
|
|
||||||
attributes.add(attribute);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果没有attributes字段,尝试从其他字段生成属性
|
|
||||||
if (attributes.isEmpty()) {
|
|
||||||
attributes = generateAttributesFromResult(structuredResult);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn("提取属性失败: {}", e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
return attributes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 从识别结果生成属性列表
|
|
||||||
*/
|
|
||||||
private List<ImageRecognitionVo.AttributeVo> generateAttributesFromResult(Map<String, Object> structuredResult) {
|
|
||||||
List<ImageRecognitionVo.AttributeVo> attributes = new ArrayList<>();
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 提取summary作为内容总结
|
|
||||||
String summary = (String) structuredResult.get("summary");
|
|
||||||
if (summary != null && !summary.isEmpty()) {
|
|
||||||
ImageRecognitionVo.AttributeVo summaryAttr = new ImageRecognitionVo.AttributeVo();
|
|
||||||
summaryAttr.setAttrKey("内容总结");
|
|
||||||
summaryAttr.setAttrValue(summary);
|
|
||||||
attributes.add(summaryAttr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 提取totalItems
|
|
||||||
Object totalItems = structuredResult.get("totalItems");
|
|
||||||
if (totalItems != null) {
|
|
||||||
ImageRecognitionVo.AttributeVo totalAttr = new ImageRecognitionVo.AttributeVo();
|
|
||||||
totalAttr.setAttrKey("总项目数");
|
|
||||||
totalAttr.setAttrValue(String.valueOf(totalItems));
|
|
||||||
attributes.add(totalAttr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 提取bomItems数量
|
|
||||||
List<Map<String, Object>> bomItems = (List<Map<String, Object>>) structuredResult.get("bomItems");
|
|
||||||
if (bomItems != null && !bomItems.isEmpty()) {
|
|
||||||
ImageRecognitionVo.AttributeVo bomCountAttr = new ImageRecognitionVo.AttributeVo();
|
|
||||||
bomCountAttr.setAttrKey("BOM项目数");
|
|
||||||
bomCountAttr.setAttrValue(String.valueOf(bomItems.size()));
|
|
||||||
attributes.add(bomCountAttr);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn("生成属性失败: {}", e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
return attributes;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user