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