123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- 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<ClassifyTest> 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<String, Object> params = new HashMap<>(2);
- String errorMsg = "";
- try {
- if (StringUtils.isNotEmpty(classifyTest.getAlgorithmParams())) {
- params = objectMapper.readValue(
- classifyTest.getAlgorithmParams(),
- new TypeReference<Map<String, Object>>() {
- }
- );
- }
- 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<FileInfoDTO> fileInfoDTOList = CommonUtils.getSortedFiles(inputPath, "dat");
- // extractedFeatures.setFolderInfoDTO(fileInfoDTOList);
- // } catch (IOException e) {
- // logger.error("读取文件夹错误", e);
- // return null;
- // }
- return classifyTest;
- }
- }
|