package com.common.mapper;

import com.Modules.etl.handler.ArrayIntegerTypeHandler;
import com.Modules.etl.handler.ArrayStringTypeHandler;
import com.common.entity.DeviceInterlockingCmd;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface DeviceInterlockingCmdMapper {

    /**
     * 根据ID查询
     */
    @Select("SELECT * FROM device_interlocking_cmd WHERE id = #{id}")
    @Results({
            @Result(column = "device_interlocking_id", property = "deviceInterlockingId",
                    typeHandler = ArrayIntegerTypeHandler.class),
            @Result(column = "device_interlocking_ip", property = "deviceInterlockingIp",
                    typeHandler = ArrayStringTypeHandler.class),
            @Result(column = "ban_ips", property = "banIps",
                    typeHandler = ArrayStringTypeHandler.class)
    })
    DeviceInterlockingCmd selectById(Long id);

    /**
     * 查询待执行的指令
     * @param probeId 探针ID
     * @return 待执行指令列表
     */
    @Select("SELECT * FROM device_interlocking_cmd WHERE probe_id = #{probeId} AND cmd_status = '0' ORDER BY create_time ASC")
    @Results({
            @Result(column = "device_interlocking_id", property = "deviceInterlockingId",
                    typeHandler = ArrayIntegerTypeHandler.class),
            @Result(column = "device_interlocking_ip", property = "deviceInterlockingIp",
                    typeHandler = ArrayStringTypeHandler.class),
            @Result(column = "ban_ips", property = "banIps",
                    typeHandler = ArrayStringTypeHandler.class)
    })
    List<DeviceInterlockingCmd> selectPendingCommands(@Param("probeId") Long probeId);

    /**
     * 根据探针ID和状态查询
     */
    @Select("SELECT * FROM device_interlocking_cmd WHERE probe_id = #{probeId} AND cmd_status = #{cmdStatus}")
    @Results({
            @Result(column = "device_interlocking_id", property = "deviceInterlockingId",
                    typeHandler = ArrayIntegerTypeHandler.class),
            @Result(column = "device_interlocking_ip", property = "deviceInterlockingIp",
                    typeHandler = ArrayStringTypeHandler.class),
            @Result(column = "ban_ips", property = "banIps",
                    typeHandler = ArrayStringTypeHandler.class)
    })
    List<DeviceInterlockingCmd> selectByProbeIdAndStatus(@Param("probeId") Long probeId, @Param("cmdStatus") String cmdStatus);

    /**
     * 插入指令
     */
    @Insert("INSERT INTO device_interlocking_cmd (probe_id, probe_ip, device_interlocking_id, device_interlocking_ip, " +
            "ban_ips, ban_method, ban_type, cmd_status, ban_duration, create_time, update_time, " +
            "tenant_id, create_dept, create_by, remark, ban_operation_type) " +
            "VALUES (#{probeId}, #{probeIp}, " +
            "#{deviceInterlockingId, typeHandler=com.Modules.etl.handler.ArrayIntegerTypeHandler}, " +
            "#{deviceInterlockingIp, typeHandler=com.Modules.etl.handler.ArrayStringTypeHandler}, " +
            "#{banIps, typeHandler=com.Modules.etl.handler.ArrayStringTypeHandler}, " +
            "#{banMethod}, #{banType}, #{cmdStatus}, #{banDuration}, SYSDATE, SYSDATE, " +
            "#{tenantId}, #{createDept}, #{createBy}, #{remark}, #{banOperationType})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(DeviceInterlockingCmd cmd);

    /**
     * 更新指令状态
     * @param id 指令ID
     * @param cmdStatus 新状态
     * @return 影响行数
     */
    @Update("UPDATE device_interlocking_cmd SET cmd_status = #{cmdStatus}, update_time = SYSDATE WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("cmdStatus") String cmdStatus);

    /**
     * 更新指令状态为执行中
     */
    @Update("UPDATE device_interlocking_cmd SET cmd_status = '2', update_time = SYSDATE WHERE id = #{id}")
    int updateStatusToExecuting(@Param("id") Long id);

    /**
     * 更新指令状态为执行完成
     */
    @Update("UPDATE device_interlocking_cmd SET cmd_status = '1', update_time = SYSDATE WHERE id = #{id}")
    int updateStatusToCompleted(@Param("id") Long id);

    /**
     * 更新指令状态为执行失败
     */
    @Update("UPDATE device_interlocking_cmd SET cmd_status = '3', update_time = SYSDATE WHERE id = #{id}")
    int updateStatusToFailed(@Param("id") Long id);

    /**
     * 根据多个ID查询
     */
    @Select("<script>" +
            "SELECT * FROM device_interlocking_cmd WHERE id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    @Results({
            @Result(column = "device_interlocking_id", property = "deviceInterlockingId",
                    typeHandler = ArrayIntegerTypeHandler.class),
            @Result(column = "device_interlocking_ip", property = "deviceInterlockingIp",
                    typeHandler = ArrayStringTypeHandler.class),
            @Result(column = "ban_ips", property = "banIps",
                    typeHandler = ArrayStringTypeHandler.class)
    })
    List<DeviceInterlockingCmd> selectByIds(@Param("ids") List<Long> ids);

    /**
     * 查询所有指令
     */
    @Select("SELECT * FROM device_interlocking_cmd ORDER BY create_time DESC")
    @Results({
            @Result(column = "device_interlocking_id", property = "deviceInterlockingId",
                    typeHandler = ArrayIntegerTypeHandler.class),
            @Result(column = "device_interlocking_ip", property = "deviceInterlockingIp",
                    typeHandler = ArrayStringTypeHandler.class),
            @Result(column = "ban_ips", property = "banIps",
                    typeHandler = ArrayStringTypeHandler.class)
    })
    List<DeviceInterlockingCmd> selectAll();
}
