package com.dora.mapper;

import com.dora.entity.ToolConfig;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 工具配置Mapper
 */
@Mapper
public interface ToolConfigMapper {

    /**
     * 根据ID查询
     */
    @Select("SELECT * FROM tool_config WHERE id = #{id} AND is_deleted = 0")
    ToolConfig selectById(@Param("id") Long id);

    /**
     * 根据工具编码查询
     */
    @Select("SELECT * FROM tool_config WHERE tool_code = #{toolCode} AND is_deleted = 0")
    ToolConfig selectByToolCode(@Param("toolCode") String toolCode);

    /**
     * 查询所有启用的工具
     */
    @Select("SELECT * FROM tool_config WHERE is_enabled = 1 AND is_deleted = 0 ORDER BY sort_order ASC, id ASC")
    List<ToolConfig> selectAllEnabled();

    /**
     * 按分类查询启用的工具
     */
    @Select("SELECT * FROM tool_config WHERE category = #{category} AND is_enabled = 1 AND is_deleted = 0 ORDER BY sort_order ASC")
    List<ToolConfig> selectByCategory(@Param("category") String category);

    /**
     * 查询所有工具（管理端）
     */
    @Select("SELECT * FROM tool_config WHERE is_deleted = 0 ORDER BY category ASC, sort_order ASC, id DESC")
    List<ToolConfig> selectAll();

    /**
     * 插入工具配置
     */
    @Insert("INSERT INTO tool_config (tool_code, tool_name, category, description, api_endpoint, request_method, " +
            "points_cost, is_enabled, sort_order, create_time, update_time, is_deleted) " +
            "VALUES (#{toolCode}, #{toolName}, #{category}, #{description}, #{apiEndpoint}, #{requestMethod}, " +
            "#{pointsCost}, #{isEnabled}, #{sortOrder}, NOW(), NOW(), 0)")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(ToolConfig toolConfig);

    /**
     * 更新工具配置
     */
    @Update("UPDATE tool_config SET tool_code = #{toolCode}, tool_name = #{toolName}, category = #{category}, " +
            "description = #{description}, api_endpoint = #{apiEndpoint}, request_method = #{requestMethod}, " +
            "points_cost = #{pointsCost}, is_enabled = #{isEnabled}, sort_order = #{sortOrder}, update_time = NOW() " +
            "WHERE id = #{id} AND is_deleted = 0")
    int updateById(ToolConfig toolConfig);

    /**
     * 更新积分消耗
     */
    @Update("UPDATE tool_config SET points_cost = #{pointsCost}, update_time = NOW() WHERE id = #{id} AND is_deleted = 0")
    int updatePointsCost(@Param("id") Long id, @Param("pointsCost") Integer pointsCost);

    /**
     * 更新启用状态
     */
    @Update("UPDATE tool_config SET is_enabled = #{isEnabled}, update_time = NOW() WHERE id = #{id} AND is_deleted = 0")
    int updateStatus(@Param("id") Long id, @Param("isEnabled") Integer isEnabled);

    /**
     * 逻辑删除
     */
    @Update("UPDATE tool_config SET is_deleted = 1, update_time = NOW() WHERE id = #{id}")
    int deleteById(@Param("id") Long id);

    /**
     * 检查工具编码是否存在
     */
    @Select("SELECT COUNT(*) FROM tool_config WHERE tool_code = #{toolCode} AND is_deleted = 0 AND id != #{excludeId}")
    int countByToolCode(@Param("toolCode") String toolCode, @Param("excludeId") Long excludeId);

    /**
     * 统计工具数量
     */
    @Select("SELECT COUNT(*) FROM tool_config WHERE is_deleted = 0")
    Long countAll();

    /**
     * 统计启用的工具数量
     */
    @Select("SELECT COUNT(*) FROM tool_config WHERE is_enabled = 1 AND is_deleted = 0")
    Long countEnabled();
}
