package com.cn.fdapfe.biz.service.impl;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import com.cn.fdapfe.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cn.fdapfe.biz.mapper.PerformanceEvaluationMapper;
import com.cn.fdapfe.biz.domain.PerformanceEvaluation;
import com.cn.fdapfe.biz.service.IPerformanceEvaluationService;

/**
 * 算法性能评估Service业务层处理
 * 
 * @author Allen
 * @date 2025-03-09
 */
@Service
public class PerformanceEvaluationServiceImpl implements IPerformanceEvaluationService 
{
    @Autowired
    private PerformanceEvaluationMapper performanceEvaluationMapper;

    /**
     * 查询算法性能评估
     * 
     * @param id 算法性能评估主键
     * @return 算法性能评估
     */
    @Override
    public PerformanceEvaluation selectPerformanceEvaluationById(Long id)
    {
        return performanceEvaluationMapper.selectPerformanceEvaluationById(id);
    }

    /**
     * 查询算法性能评估列表
     * 
     * @param performanceEvaluation 算法性能评估
     * @return 算法性能评估
     */
    @Override
    public List<PerformanceEvaluation> selectPerformanceEvaluationList(PerformanceEvaluation performanceEvaluation)
    {
        return performanceEvaluationMapper.selectPerformanceEvaluationList(performanceEvaluation);
    }

    /**
     * 新增算法性能评估
     * 
     * @param performanceEvaluation 算法性能评估
     * @return 结果
     */
    @Override
    public int insertPerformanceEvaluation(PerformanceEvaluation performanceEvaluation)
    {
        performanceEvaluation.setCreateTime(DateUtils.getNowDate());
        return performanceEvaluationMapper.insertPerformanceEvaluation(performanceEvaluation);
    }

    /**
     * 修改算法性能评估
     * 
     * @param performanceEvaluation 算法性能评估
     * @return 结果
     */
    @Override
    public int updatePerformanceEvaluation(PerformanceEvaluation performanceEvaluation)
    {
        performanceEvaluation.setUpdateTime(DateUtils.getNowDate());
        return performanceEvaluationMapper.updatePerformanceEvaluation(performanceEvaluation);
    }

    /**
     * 批量删除算法性能评估
     * 
     * @param ids 需要删除的算法性能评估主键
     * @return 结果
     */
    @Override
    public int deletePerformanceEvaluationByIds(Long[] ids)
    {
        return performanceEvaluationMapper.deletePerformanceEvaluationByIds(ids);
    }

    /**
     * 删除算法性能评估信息
     * 
     * @param id 算法性能评估主键
     * @return 结果
     */
    @Override
    public int deletePerformanceEvaluationById(Long id)
    {
        return performanceEvaluationMapper.deletePerformanceEvaluationById(id);
    }

    @Override
    public int run(Long id) {
        PerformanceEvaluation performanceEvaluation = this.selectPerformanceEvaluationById(id);
        if(performanceEvaluation == null){
            return 0;
        }
        Date currentDate = new Date();
        performanceEvaluation.setStartTime(currentDate);
        Instant instant = currentDate.toInstant();
        Instant newInstant = instant.plus(5, ChronoUnit.SECONDS);

        // 转换回 Date
        Date newDate = Date.from(newInstant);
        performanceEvaluation.setEndTime(newDate);
        performanceEvaluation.setResultFilePath("/profile/upload/2025/03/09/demo/文件结果.txt");
        performanceEvaluation.setResultImagePath("/profile/upload/2025/03/09/demo/图片结果.png");
        if("1".equals(performanceEvaluation.getType())){ //速率测试
            performanceEvaluation.setResultText("{\"平均速率\":\"97ms\",\"最大时长\":\"257ms\"，\"最小时长\":\"37ms\"}");
        } else if ("2".equals(performanceEvaluation.getType())){
            performanceEvaluation.setResultText("{\"准确率\":\"83.04%\",\"召回率\":\"85.69%\"}");
        }


        return this.updatePerformanceEvaluation(performanceEvaluation);
    }
}
