2026-11-27 15:12:04 +08:00
|
|
|
|
package com.ruoyi.testing.service;
|
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
|
import com.ruoyi.testing.mapper.TestingCalibrationMapper;
|
|
|
|
|
|
import com.ruoyi.testing.mapper.TestingMapper;
|
|
|
|
|
|
import com.ruoyi.testing.recognition.TestingCalibrationRecognition;
|
2026-12-01 15:48:53 +08:00
|
|
|
|
import com.ruoyi.testing.recognition.TestingAnalysisParameterRecognition;
|
2026-11-27 15:12:04 +08:00
|
|
|
|
import com.ruoyi.testing.security.TestingAccess;
|
|
|
|
|
|
import com.ruoyi.testing.storage.TestingImageStorage;
|
|
|
|
|
|
import static com.ruoyi.testing.support.TestingJson.*;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import org.springframework.transaction.PlatformTransactionManager;
|
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import org.springframework.transaction.support.TransactionTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
/** 标定证据事务入口;图像归属由数据库决定,模型响应不修改标定历史或表单。 */
|
|
|
|
|
|
@Service
|
|
|
|
|
|
public class TestingCalibrationService {
|
|
|
|
|
|
private final TestingCalibrationMapper images;
|
|
|
|
|
|
private final TestingMapper db;
|
|
|
|
|
|
private final TestingAccess access;
|
|
|
|
|
|
private final TestingImageStorage storage;
|
|
|
|
|
|
private final TestingCalibrationData rules;
|
|
|
|
|
|
private final TestingCalibrationRecognition recognition;
|
2026-12-01 15:48:53 +08:00
|
|
|
|
private final TestingAnalysisParameterRecognition analysisRecognition;
|
2026-11-27 15:12:04 +08:00
|
|
|
|
private final TransactionTemplate transaction;
|
|
|
|
|
|
|
2026-12-01 15:48:53 +08:00
|
|
|
|
/** 注入既有权限、私有存储和两类转录器;分析参数同样使用项目标定证据及短事务资格复查。 */
|
2026-11-27 15:12:04 +08:00
|
|
|
|
public TestingCalibrationService(TestingCalibrationMapper images,TestingMapper db,TestingAccess access,
|
2026-12-01 15:48:53 +08:00
|
|
|
|
TestingImageStorage storage,TestingCalibrationData rules,TestingCalibrationRecognition recognition,
|
|
|
|
|
|
TestingAnalysisParameterRecognition analysisRecognition,PlatformTransactionManager manager) {
|
2026-11-27 15:12:04 +08:00
|
|
|
|
this.images=images; this.db=db; this.access=access; this.storage=storage; this.rules=rules; this.recognition=recognition;
|
2026-12-01 15:48:53 +08:00
|
|
|
|
this.analysisRecognition=analysisRecognition;
|
2026-11-27 15:12:04 +08:00
|
|
|
|
transaction=new TransactionTemplate(manager);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 上传区域原图并返回不含对象键的元数据;同设备同区域相同摘要复用图片身份。
|
|
|
|
|
|
* @param project 已选测试项目,写权限和设备归属在事务内重新核对。
|
|
|
|
|
|
* @param robot 被标定机器人主键。
|
|
|
|
|
|
* @param part 固定区域代码,不能上传到任意目录。
|
|
|
|
|
|
* @param name 原始文件名,仅用于显示。
|
|
|
|
|
|
* @param bytes 原图,真实类型、尺寸和摘要由服务器检测。
|
|
|
|
|
|
* @return 供表单引用的图片 ID 及公开元数据;事务失败补偿本次新对象。
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Transactional public Map<String,Object> upload(long project,long robot,String part,String name,byte[] bytes) {
|
|
|
|
|
|
access.project(project,"write",true); requireRobot(project,robot);
|
|
|
|
|
|
if(part==null || !TestingCalibrationData.PARTS.contains(part)) throw bad("标定截图区域无效");
|
|
|
|
|
|
String filename=text(name,255,true,"截图文件名");
|
|
|
|
|
|
var info=storage.inspect(bytes);
|
|
|
|
|
|
Map<String,Object> old=images.duplicate(project,robot,part,info.hash());
|
|
|
|
|
|
if(old!=null) return publicImage(old);
|
|
|
|
|
|
String key=storage.saveCalibration(project,robot,bytes,info);
|
|
|
|
|
|
Map<String,Object> row=map("project_id",project,"robot_id",robot,"part_code",part,"name",filename,"media_type",info.mediaType(),
|
|
|
|
|
|
"object_key",key,"sha256",info.hash(),"size",bytes.length,"width",info.width(),"height",info.height(),"created_by",access.actor());
|
|
|
|
|
|
images.insertImage(row);
|
|
|
|
|
|
db.audit(map("project_id",project,"actor_id",access.actor(),"action","UPLOAD","target_type","CALIBRATION_IMAGE","target_id",id(row,"id"),"before_json",null,"after_json",write(publicImage(row)),"reason",""));
|
|
|
|
|
|
return publicImage(row);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 返回经项目读取资格校验的截图信息,不暴露 MinIO 对象键或模型原文。 */
|
|
|
|
|
|
public Map<String,Object> info(long imageId) { return publicImage(authorized(imageId,"read")); }
|
|
|
|
|
|
|
|
|
|
|
|
/** 下载前核对当前项目资格和对象完整性,原始证据不可通过公开 URL 绕过权限。 */
|
|
|
|
|
|
public Map<String,Object> image(long imageId) {
|
|
|
|
|
|
Map<String,Object> image=authorized(imageId,"read");
|
|
|
|
|
|
return map("name",image.get("name"),"mediaType",image.get("media_type"),"bytes",storage.readCalibration(image));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-12-01 15:48:53 +08:00
|
|
|
|
* 按图片的固定区域选择转录器;模型调用不持有数据库事务,结束后重查写资格,仅缓存候选。
|
2026-11-27 15:12:04 +08:00
|
|
|
|
* @param imageId 当前选中的截图 ID,区域和设备均从数据库读取。
|
|
|
|
|
|
* @return 本图的区域候选和身份,前端必须校验仍是当前图片后才可填入。
|
|
|
|
|
|
*/
|
|
|
|
|
|
public Map<String,Object> recognize(long imageId) {
|
|
|
|
|
|
Map<String,Object> image=authorized(imageId,"write"); String part=image.get("part_code").toString();
|
|
|
|
|
|
if(part.startsWith("TOOL_")) throw bad("工具标定截图只留存原图,无需识别");
|
|
|
|
|
|
byte[] bytes=storage.readCalibration(image);
|
2026-12-01 15:48:53 +08:00
|
|
|
|
String mode=rules.analysisMode(part);
|
|
|
|
|
|
// 分析参数复用独立的字段/模式转录规则,但图片始终受标定的项目、机器人和区域边界保护。
|
|
|
|
|
|
JsonNode candidate=image.get("recognition_json")!=null?read(image.get("recognition_json")):
|
|
|
|
|
|
mode==null?recognition.recognize(part,bytes,image.get("media_type").toString()):analysisRecognition.recognize(mode,bytes,image.get("media_type").toString());
|
|
|
|
|
|
if(mode!=null && !mode.equals(candidate.path("mode").asText())) throw bad("识别图片与当前分析参数区域不一致,请检查上传的截图");
|
2026-11-27 15:12:04 +08:00
|
|
|
|
return transaction.execute(status->{
|
|
|
|
|
|
access.project(id(image,"project_id"),"write",true);
|
|
|
|
|
|
images.cacheRecognition(imageId,write(candidate));
|
|
|
|
|
|
Map<String,Object> cached=images.image(imageId);
|
|
|
|
|
|
return map("imageId",imageId,"partCode",part,"data",read(cached.get("recognition_json")));
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 保存追加版本前同时核对结构、截图项目、机器人和区域,拒绝从其他标定拼接证据。
|
|
|
|
|
|
* @param project 当前持锁项目。
|
|
|
|
|
|
* @param robot 本条标定的设备。
|
2026-12-01 15:48:53 +08:00
|
|
|
|
* @param kind 在用标定类型,分析参数的两个模式分别核对图像归属。
|
2026-11-27 15:12:04 +08:00
|
|
|
|
* @param data 只含表单字段和图片 ID 的新结构。
|
|
|
|
|
|
*/
|
|
|
|
|
|
public void validate(long project,long robot,String kind,JsonNode data) {
|
|
|
|
|
|
rules.validate(kind,data);
|
|
|
|
|
|
for(String part:rules.parts(kind)) {
|
|
|
|
|
|
Map<String,Object> image=images.image(data.path("images").path(part).asLong());
|
|
|
|
|
|
if(image==null || id(image,"project_id")!=project || id(image,"robot_id")!=robot || !part.equals(image.get("part_code")))
|
|
|
|
|
|
throw bad("标定截图与当前项目、机器人或区域不匹配");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 原图身份只能通过数据库元数据追溯,调用方不能提交项目 ID 来替代真实归属。 */
|
|
|
|
|
|
private Map<String,Object> authorized(long imageId,String action) {
|
|
|
|
|
|
Map<String,Object> image=images.image(imageId); if(image==null) throw TestingAccess.denied();
|
|
|
|
|
|
access.project(id(image,"project_id"),action,false); requireRobot(id(image,"project_id"),id(image,"robot_id")); return image;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 设备必须属于已经鉴权的项目,禁止跨项目上传或引用截图。 */
|
|
|
|
|
|
private void requireRobot(long project,long robot) {
|
|
|
|
|
|
Map<String,Object> row=db.robot(robot);
|
|
|
|
|
|
if(row==null || id(row,"project_id")!=project) throw bad("机器人不属于当前测试项目");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 显式输出允许公开的元数据,避免新增数据库字段时意外泄露私有对象键或候选原文。 */
|
|
|
|
|
|
private Map<String,Object> publicImage(Map<String,Object> image) {
|
|
|
|
|
|
return map("id",image.get("id"),"projectId",image.get("project_id"),"robotId",image.get("robot_id"),"partCode",image.get("part_code"),
|
|
|
|
|
|
"name",image.get("name"),"mediaType",image.get("media_type"),"sha256",image.get("sha256"),"size",image.get("size"),
|
|
|
|
|
|
"width",image.get("width"),"height",image.get("height"),"createdAt",image.get("created_at"));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|