package com.ips.system.service.impl; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.ips.common.utils.DateUtils; import com.ips.common.utils.StringUtils; import com.ips.system.domain.AlgorithmConfig; import com.ips.system.dto.AlgorithmParamsDto; import com.ips.system.service.IAlgorithmConfigService; import com.ips.system.utils.AlgorithmCaller; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ips.system.mapper.ClassifyTestMapper; import com.ips.system.domain.ClassifyTest; import com.ips.system.service.IClassifyTestService; /** * 分类测试Service业务层处理 * * @author Allen * @date 2025-05-21 */ @Service public class ClassifyTestServiceImpl implements IClassifyTestService { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private ClassifyTestMapper classifyTestMapper; @Autowired private IAlgorithmConfigService algorithmConfigService; /** * 查询分类测试 * * @param id 分类测试主键 * @return 分类测试 */ @Override public ClassifyTest selectClassifyTestById(Long id) { return classifyTestMapper.selectClassifyTestById(id); } /** * 查询分类测试列表 * * @param classifyTest 分类测试 * @return 分类测试 */ @Override public List selectClassifyTestList(ClassifyTest classifyTest) { return classifyTestMapper.selectClassifyTestList(classifyTest); } /** * 新增分类测试 * * @param classifyTest 分类测试 * @return 结果 */ @Override public int insertClassifyTest(ClassifyTest classifyTest) { classifyTest.setCreateTime(DateUtils.getNowDate()); return classifyTestMapper.insertClassifyTest(classifyTest); } /** * 修改分类测试 * * @param classifyTest 分类测试 * @return 结果 */ @Override public int updateClassifyTest(ClassifyTest classifyTest) { classifyTest.setUpdateTime(DateUtils.getNowDate()); return classifyTestMapper.updateClassifyTest(classifyTest); } /** * 批量删除分类测试 * * @param ids 需要删除的分类测试主键 * @return 结果 */ @Override public int deleteClassifyTestByIds(Long[] ids) { return classifyTestMapper.deleteClassifyTestByIds(ids); } /** * 删除分类测试信息 * * @param id 分类测试主键 * @return 结果 */ @Override public int deleteClassifyTestById(Long id) { return classifyTestMapper.deleteClassifyTestById(id); } @Override public String run(Long id) throws JsonProcessingException { ClassifyTest classifyTest = classifyTestMapper.selectClassifyTestById(id); if(classifyTest == null || classifyTest.getAlgorithmId() == null){ return "无法找到该任务,id:"+id; } Long algorithmId = classifyTest.getAlgorithmId(); AlgorithmConfig algorithmConfig = algorithmConfigService.selectAlgorithmConfigById(algorithmId); classifyTest.setStartTime(new Date()); classifyTest.setStatus("1"); this.updateClassifyTest(classifyTest); CompletableFuture.runAsync(() -> { // 异步逻辑 doRun(algorithmConfig, classifyTest); }); return null; } private void doRun(AlgorithmConfig algorithmConfig, ClassifyTest classifyTest) { String algorithmPath = algorithmConfig.getAlgorithmPath(); // 组装json String inputPath = classifyTest.getInputPath(); String outputPath = classifyTest.getOutputPath(); ObjectMapper objectMapper = new ObjectMapper(); Map params = new HashMap<>(2); String errorMsg = ""; try { if (StringUtils.isNotEmpty(classifyTest.getAlgorithmParams())) { params = objectMapper.readValue( classifyTest.getAlgorithmParams(), new TypeReference>() { } ); } params.put("model", classifyTest.getModelPath()); params.put("mode_pkl", classifyTest.getModelPklPath()); AlgorithmParamsDto algorithmParamsDto = new AlgorithmParamsDto(algorithmConfig.getAlgorithmName(), inputPath, outputPath, params); // 对象 → JSON 字符串 String json = objectMapper.writeValueAsString(algorithmParamsDto); // 处理算法 errorMsg = AlgorithmCaller.executeAlgorithm(algorithmConfig.getAlgorithmName(),algorithmPath, json); } catch (JsonProcessingException e) { logger.error("格式化失败", e); errorMsg = "格式化失败"; } //处理结果 if (StringUtils.isEmpty(errorMsg)) { classifyTest.setStatus("2"); } else { classifyTest.setStatus("3"); } classifyTest.setEndTime(new Date()); this.updateClassifyTest(classifyTest); } @Override public ClassifyTest getResultDetails(Long id) { ClassifyTest classifyTest = this.selectClassifyTestById(id); String inputPath = classifyTest.getInputPath(); // try { // List fileInfoDTOList = CommonUtils.getSortedFiles(inputPath, "dat"); // extractedFeatures.setFolderInfoDTO(fileInfoDTOList); // } catch (IOException e) { // logger.error("读取文件夹错误", e); // return null; // } return classifyTest; } }