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 java.util.Map;

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.FdAlgorithmMapper;
import com.cn.fdapfe.biz.domain.FdAlgorithm;
import com.cn.fdapfe.biz.service.IFdAlgorithmService;

import javax.annotation.Resource;

/**
 * 故障诊断算法功能验证Service业务层处理
 * 
 * @author Allen
 * @date 2025-03-07
 */
@Service
public class FdAlgorithmServiceImpl implements IFdAlgorithmService 
{
    @Resource
    private FdAlgorithmMapper fdAlgorithmMapper;

    /**
     * 查询故障诊断算法功能验证
     * 
     * @param id 故障诊断算法功能验证主键
     * @return 故障诊断算法功能验证
     */
    @Override
    public FdAlgorithm selectFdAlgorithmById(Long id)
    {
        return fdAlgorithmMapper.selectFdAlgorithmById(id);
    }

    /**
     * 查询故障诊断算法功能验证列表
     * 
     * @param fdAlgorithm 故障诊断算法功能验证
     * @return 故障诊断算法功能验证
     */
    @Override
    public List<FdAlgorithm> selectFdAlgorithmList(FdAlgorithm fdAlgorithm)
    {
        return fdAlgorithmMapper.selectFdAlgorithmList(fdAlgorithm);
    }

    /**
     * 新增故障诊断算法功能验证
     * 
     * @param fdAlgorithm 故障诊断算法功能验证
     * @return 结果
     */
    @Override
    public int insertFdAlgorithm(FdAlgorithm fdAlgorithm)
    {
        fdAlgorithm.setCreateTime(DateUtils.getNowDate());
        return fdAlgorithmMapper.insertFdAlgorithm(fdAlgorithm);
    }

    /**
     * 修改故障诊断算法功能验证
     * 
     * @param fdAlgorithm 故障诊断算法功能验证
     * @return 结果
     */
    @Override
    public int updateFdAlgorithm(FdAlgorithm fdAlgorithm)
    {
        fdAlgorithm.setUpdateTime(DateUtils.getNowDate());
        return fdAlgorithmMapper.updateFdAlgorithm(fdAlgorithm);
    }

    /**
     * 批量删除故障诊断算法功能验证
     * 
     * @param ids 需要删除的故障诊断算法功能验证主键
     * @return 结果
     */
    @Override
    public int deleteFdAlgorithmByIds(Long[] ids)
    {
        return fdAlgorithmMapper.deleteFdAlgorithmByIds(ids);
    }

    /**
     * 删除故障诊断算法功能验证信息
     * 
     * @param id 故障诊断算法功能验证主键
     * @return 结果
     */
    @Override
    public int deleteFdAlgorithmById(Long id)
    {
        return fdAlgorithmMapper.deleteFdAlgorithmById(id);
    }

    @Override
    public int handleVerify(Long id) {
        FdAlgorithm fdAlgorithm = this.selectFdAlgorithmById(id);
        if(null == fdAlgorithm){
            return 0;
        }
        Date currentDate = new Date();
        fdAlgorithm.setStartTime(currentDate);
        Instant instant = currentDate.toInstant();
        Instant newInstant = instant.plus(5, ChronoUnit.SECONDS);

        // 转换回 Date
        Date newDate = Date.from(newInstant);
        fdAlgorithm.setEndTime(newDate);
        fdAlgorithm.setTestLabels(fdAlgorithm.getTruthLabels());
        return this.updateFdAlgorithm(fdAlgorithm);
    }

    @Override
    public List<Map<String, Object>> getOptions() {
        return fdAlgorithmMapper.getOptions();
    }
}
