package com.common.mapper;

import com.common.entity.DeviceCollectTask;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;

@Mapper
public interface DeviceCollectTaskMapper {

    /**
     * 根据ID查询采集任务
     */
    @Select("SELECT * FROM device_collect_task WHERE id = #{id}")
    DeviceCollectTask selectById(Integer id);

    /**
     * 查询所有采集任务
     */
    @Select("SELECT * FROM device_collect_task")
    List<DeviceCollectTask> selectAll();

    /**
     * 根据设备ID查询采集任务
     */
    @Select("SELECT * FROM device_collect_task WHERE device_id = #{deviceId}")
    List<DeviceCollectTask> selectByDeviceId(Integer deviceId);

    /**
     * 根据方法类型查询采集任务
     */
    @Select("SELECT * FROM device_collect_task WHERE method = #{method}")
    List<DeviceCollectTask> selectByMethod(Integer method);

    /**
     * 根据任务名称模糊查询
     */
    @Select("SELECT * FROM device_collect_task WHERE task_name LIKE CONCAT('%', #{taskName}, '%')")
    List<DeviceCollectTask> selectByTaskNameLike(String taskName);

    /**
     * 查询成功的采集任务
     */
    @Select("SELECT * FROM device_collect_task WHERE last_success_time IS NOT NULL")
    List<DeviceCollectTask> selectSuccessTasks();

    /**
     * 查询失败的采集任务
     */
    @Select("SELECT * FROM device_collect_task WHERE last_failed_time IS NOT NULL")
    List<DeviceCollectTask> selectFailedTasks();

    /**
     * 多条件组合查询
     */
    List<DeviceCollectTask> selectByCondition(DeviceCollectTask condition);

    /**
     * 插入采集任务
     */
    @Insert("INSERT INTO device_collect_task (created_at, updated_at, device_id, method, task_name, " +
            "first_time, last_success_time, last_failed_time, detail_id, epm, epm_peak, " +
            "process_architecture, task_count, recent_discover_time, epm_upper_limit, " +
            "expire_time, remark, device_ip, organization_id) " +
            "VALUES (NOW(), NOW(), #{deviceId}, #{method}, #{taskName}, #{firstTime}, " +
            "#{lastSuccessTime}, #{lastFailedTime}, #{detailId}, #{epm}, #{epmPeak}, " +
            "#{processArchitecture}, #{taskCount}, #{recentDiscoverTime}, #{epmUpperLimit}, " +
            "#{expireTime}, #{remark}, #{deviceIp}, #{organizationId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(DeviceCollectTask task);

    /**
     * 更新采集任务
     */
    @Update("UPDATE device_collect_task SET " +
            "updated_at = NOW(), " +
            "device_id = #{deviceId}, " +
            "method = #{method}, " +
            "task_name = #{taskName}, " +
            "first_time = #{firstTime}, " +
            "last_success_time = #{lastSuccessTime}, " +
            "last_failed_time = #{lastFailedTime}, " +
            "detail_id = #{detailId}, " +
            "epm = #{epm}, " +
            "epm_peak = #{epmPeak}, " +
            "process_architecture = #{processArchitecture}, " +
            "task_count = #{taskCount}, " +
            "recent_discover_time = #{recentDiscoverTime}, " +
            "epm_upper_limit = #{epmUpperLimit}, " +
            "expire_time = #{expireTime}, " +
            "remark = #{remark}, " +
            "device_ip = #{deviceIp}, " +
            "organization_id = #{organizationId} " +
            "WHERE id = #{id}")
    int update(DeviceCollectTask task);

    /**
     * 删除采集任务
     */
    @Delete("DELETE FROM device_collect_task WHERE id = #{id}")
    int deleteById(Integer id);

    /**
     * 根据设备ID删除采集任务
     */
    @Delete("DELETE FROM device_collect_task WHERE device_id = #{deviceId}")
    int deleteByDeviceId(Integer deviceId);

    /**
     * 更新任务成功状态
     */
    @Update("UPDATE device_collect_task SET " +
            "last_success_time = NOW(), " +
            "updated_at = NOW(), " +
            "task_count = COALESCE(task_count, 0) + 1 " +
            "WHERE id = #{id}")
    int updateSuccessStatus(Integer id);

    /**
     * 更新任务失败状态
     */
    @Update("UPDATE device_collect_task SET " +
            "last_failed_time = NOW(), " +
            "updated_at = NOW() " +
            "WHERE id = #{id}")
    int updateFailedStatus(Integer id);

    /**
     * 更新EPM指标
     */
    @Update("UPDATE device_collect_task SET " +
            "epm = #{epm}, " +
            "epm_peak = GREATEST(COALESCE(epm_peak, 0), #{epm}), " +
            "updated_at = NOW() " +
            "WHERE id = #{id}")
    int updateEpm(@Param("id") Integer id, @Param("epm") Integer epm);

    /**
     * 统计设备采集任务数量
     */
    @Select("SELECT COUNT(*) FROM device_collect_task WHERE device_id = #{deviceId}")
    int countByDeviceId(Integer deviceId);

    /**
     * 获取设备的最新采集任务
     */
    @Select("SELECT * FROM device_collect_task WHERE device_id = #{deviceId} ORDER BY updated_at DESC LIMIT 1")
    DeviceCollectTask selectLatestByDeviceId(Integer deviceId);
}