ClassifyTestServiceImpl.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.ips.system.service.impl;
  2. import java.util.Date;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.concurrent.CompletableFuture;
  7. import com.fasterxml.jackson.core.JsonProcessingException;
  8. import com.fasterxml.jackson.core.type.TypeReference;
  9. import com.fasterxml.jackson.databind.ObjectMapper;
  10. import com.ips.common.utils.DateUtils;
  11. import com.ips.common.utils.StringUtils;
  12. import com.ips.system.domain.AlgorithmConfig;
  13. import com.ips.system.dto.AlgorithmParamsDto;
  14. import com.ips.system.service.IAlgorithmConfigService;
  15. import com.ips.system.utils.AlgorithmCaller;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import com.ips.system.mapper.ClassifyTestMapper;
  21. import com.ips.system.domain.ClassifyTest;
  22. import com.ips.system.service.IClassifyTestService;
  23. /**
  24. * 分类测试Service业务层处理
  25. *
  26. * @author Allen
  27. * @date 2025-05-21
  28. */
  29. @Service
  30. public class ClassifyTestServiceImpl implements IClassifyTestService
  31. {
  32. protected final Logger logger = LoggerFactory.getLogger(this.getClass());
  33. @Autowired
  34. private ClassifyTestMapper classifyTestMapper;
  35. @Autowired
  36. private IAlgorithmConfigService algorithmConfigService;
  37. /**
  38. * 查询分类测试
  39. *
  40. * @param id 分类测试主键
  41. * @return 分类测试
  42. */
  43. @Override
  44. public ClassifyTest selectClassifyTestById(Long id)
  45. {
  46. return classifyTestMapper.selectClassifyTestById(id);
  47. }
  48. /**
  49. * 查询分类测试列表
  50. *
  51. * @param classifyTest 分类测试
  52. * @return 分类测试
  53. */
  54. @Override
  55. public List<ClassifyTest> selectClassifyTestList(ClassifyTest classifyTest)
  56. {
  57. return classifyTestMapper.selectClassifyTestList(classifyTest);
  58. }
  59. /**
  60. * 新增分类测试
  61. *
  62. * @param classifyTest 分类测试
  63. * @return 结果
  64. */
  65. @Override
  66. public int insertClassifyTest(ClassifyTest classifyTest)
  67. {
  68. classifyTest.setCreateTime(DateUtils.getNowDate());
  69. return classifyTestMapper.insertClassifyTest(classifyTest);
  70. }
  71. /**
  72. * 修改分类测试
  73. *
  74. * @param classifyTest 分类测试
  75. * @return 结果
  76. */
  77. @Override
  78. public int updateClassifyTest(ClassifyTest classifyTest)
  79. {
  80. classifyTest.setUpdateTime(DateUtils.getNowDate());
  81. return classifyTestMapper.updateClassifyTest(classifyTest);
  82. }
  83. /**
  84. * 批量删除分类测试
  85. *
  86. * @param ids 需要删除的分类测试主键
  87. * @return 结果
  88. */
  89. @Override
  90. public int deleteClassifyTestByIds(Long[] ids)
  91. {
  92. return classifyTestMapper.deleteClassifyTestByIds(ids);
  93. }
  94. /**
  95. * 删除分类测试信息
  96. *
  97. * @param id 分类测试主键
  98. * @return 结果
  99. */
  100. @Override
  101. public int deleteClassifyTestById(Long id)
  102. {
  103. return classifyTestMapper.deleteClassifyTestById(id);
  104. }
  105. @Override
  106. public String run(Long id) throws JsonProcessingException {
  107. ClassifyTest classifyTest = classifyTestMapper.selectClassifyTestById(id);
  108. if(classifyTest == null || classifyTest.getAlgorithmId() == null){
  109. return "无法找到该任务,id:"+id;
  110. }
  111. Long algorithmId = classifyTest.getAlgorithmId();
  112. AlgorithmConfig algorithmConfig = algorithmConfigService.selectAlgorithmConfigById(algorithmId);
  113. classifyTest.setStartTime(new Date());
  114. classifyTest.setStatus("1");
  115. this.updateClassifyTest(classifyTest);
  116. CompletableFuture.runAsync(() -> {
  117. // 异步逻辑
  118. doRun(algorithmConfig, classifyTest);
  119. });
  120. return null;
  121. }
  122. private void doRun(AlgorithmConfig algorithmConfig, ClassifyTest classifyTest) {
  123. String algorithmPath = algorithmConfig.getAlgorithmPath();
  124. // 组装json
  125. String inputPath = classifyTest.getInputPath();
  126. String outputPath = classifyTest.getOutputPath();
  127. ObjectMapper objectMapper = new ObjectMapper();
  128. Map<String, Object> params = new HashMap<>(2);
  129. String errorMsg = "";
  130. try {
  131. if (StringUtils.isNotEmpty(classifyTest.getAlgorithmParams())) {
  132. params = objectMapper.readValue(
  133. classifyTest.getAlgorithmParams(),
  134. new TypeReference<Map<String, Object>>() {
  135. }
  136. );
  137. }
  138. params.put("model", classifyTest.getModelPath());
  139. params.put("mode_pkl", classifyTest.getModelPklPath());
  140. AlgorithmParamsDto algorithmParamsDto = new AlgorithmParamsDto(algorithmConfig.getAlgorithmName(), inputPath, outputPath, params);
  141. // 对象 → JSON 字符串
  142. String json = objectMapper.writeValueAsString(algorithmParamsDto);
  143. // 处理算法
  144. errorMsg = AlgorithmCaller.executeAlgorithm(algorithmConfig.getAlgorithmName(),algorithmPath, json);
  145. } catch (JsonProcessingException e) {
  146. logger.error("格式化失败", e);
  147. errorMsg = "格式化失败";
  148. }
  149. //处理结果
  150. if (StringUtils.isEmpty(errorMsg)) {
  151. classifyTest.setStatus("2");
  152. } else {
  153. classifyTest.setStatus("3");
  154. }
  155. classifyTest.setEndTime(new Date());
  156. this.updateClassifyTest(classifyTest);
  157. }
  158. @Override
  159. public ClassifyTest getResultDetails(Long id) {
  160. ClassifyTest classifyTest = this.selectClassifyTestById(id);
  161. String inputPath = classifyTest.getInputPath();
  162. // try {
  163. // List<FileInfoDTO> fileInfoDTOList = CommonUtils.getSortedFiles(inputPath, "dat");
  164. // extractedFeatures.setFolderInfoDTO(fileInfoDTOList);
  165. // } catch (IOException e) {
  166. // logger.error("读取文件夹错误", e);
  167. // return null;
  168. // }
  169. return classifyTest;
  170. }
  171. }