package com.ips.system.service.impl; 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.domain.Distillation; import com.ips.system.dto.AlgorithmParamsDto; import com.ips.system.mapper.DistillationMapper; import com.ips.system.service.IAlgorithmConfigService; import com.ips.system.service.IDistillationService; 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 java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; /** * 知识蒸馏Service业务层处理 * * @author Allen * @date 2025-05-21 */ @Service public class DistillationServiceImpl implements IDistillationService { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private DistillationMapper distillationMapper; @Autowired private IAlgorithmConfigService algorithmConfigService; /** * 查询知识蒸馏 * * @param id 知识蒸馏主键 * @return 知识蒸馏 */ @Override public Distillation selectDistillationById(Long id) { return distillationMapper.selectDistillationById(id); } /** * 查询知识蒸馏列表 * * @param distillation 知识蒸馏 * @return 知识蒸馏 */ @Override public List selectDistillationList(Distillation distillation) { return distillationMapper.selectDistillationList(distillation); } /** * 新增知识蒸馏 * * @param distillation 知识蒸馏 * @return 结果 */ @Override public int insertDistillation(Distillation distillation) { distillation.setCreateTime(DateUtils.getNowDate()); return distillationMapper.insertDistillation(distillation); } /** * 修改知识蒸馏 * * @param distillation 知识蒸馏 * @return 结果 */ @Override public int updateDistillation(Distillation distillation) { distillation.setUpdateTime(DateUtils.getNowDate()); return distillationMapper.updateDistillation(distillation); } /** * 批量删除知识蒸馏 * * @param ids 需要删除的知识蒸馏主键 * @return 结果 */ @Override public int deleteDistillationByIds(Long[] ids) { return distillationMapper.deleteDistillationByIds(ids); } /** * 删除知识蒸馏信息 * * @param id 知识蒸馏主键 * @return 结果 */ @Override public int deleteDistillationById(Long id) { return distillationMapper.deleteDistillationById(id); } @Override public String run(Long id) throws JsonProcessingException { Distillation distillation = distillationMapper.selectDistillationById(id); if(distillation == null || distillation.getAlgorithmId() == null){ return "无法找到该任务,id:"+id; } Long algorithmId = distillation.getAlgorithmId(); AlgorithmConfig algorithmConfig = algorithmConfigService.selectAlgorithmConfigById(algorithmId); distillation.setStartTime(new Date()); distillation.setStatus("1"); this.updateDistillation(distillation); CompletableFuture.runAsync(() -> { // 异步逻辑 doRun(algorithmConfig, distillation); }); return null; } private void doRun(AlgorithmConfig algorithmConfig, Distillation distillation) { String algorithmPath = algorithmConfig.getAlgorithmPath(); // 组装json String inputPath = distillation.getInputPath(); String outputPath = distillation.getOutputPath(); ObjectMapper objectMapper = new ObjectMapper(); Map params = new HashMap<>(0); String errorMsg = ""; try { if (StringUtils.isNotEmpty(distillation.getAlgorithmParams())) { objectMapper.readValue( distillation.getAlgorithmParams(), new TypeReference>() { } ); } AlgorithmParamsDto algorithmParamsDto = new AlgorithmParamsDto(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)) { distillation.setStatus("2"); } else { distillation.setStatus("3"); } distillation.setEndTime(new Date()); this.updateDistillation(distillation); } @Override public Distillation getResultDetails(Long id) { Distillation distillation = this.selectDistillationById(id); String inputPath = distillation.getInputPath(); // try { // List fileInfoDTOList = CommonUtils.getSortedFiles(inputPath, "dat"); // extractedFeatures.setFolderInfoDTO(fileInfoDTOList); // } catch (IOException e) { // logger.error("读取文件夹错误", e); // return null; // } return distillation; } }