<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dora.mapper.RevenueConfigMapper">

    <resultMap id="RevenueConfigResultMap" type="com.dora.entity.RevenueConfig">
        <id column="id" property="id"/>
        <result column="config_key" property="configKey"/>
        <result column="config_type" property="configType"/>
        <result column="target_count" property="targetCount"/>
        <result column="reward_amount" property="rewardAmount"/>
        <result column="level" property="level"/>
        <result column="level_name" property="levelName"/>
        <result column="min_fans" property="minFans"/>
        <result column="commission_rate" property="commissionRate"/>
        <result column="config_value" property="configValue"/>
        <result column="description" property="description"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <!-- 根据配置类型查询配置 -->
    <select id="selectByConfigType" resultMap="RevenueConfigResultMap">
        SELECT * FROM revenue_config 
        WHERE config_type = #{configType} AND is_deleted = 0
        ORDER BY level ASC, create_time DESC
    </select>

    <!-- 根据配置类型和等级查询配置 -->
    <select id="selectByConfigTypeAndLevel" resultMap="RevenueConfigResultMap">
        SELECT * FROM revenue_config 
        WHERE config_type = #{configType} AND level = #{level} AND is_deleted = 0
        LIMIT 1
    </select>

    <!-- 查询下一个等级的配置 -->
    <select id="selectNextLevelConfig" resultMap="RevenueConfigResultMap">
        SELECT * FROM revenue_config 
        WHERE config_type = #{configType} 
        AND level > #{currentLevel} 
        AND is_deleted = 0
        ORDER BY level ASC
        LIMIT 1
    </select>

    <!-- 根据配置键查询配置 -->
    <select id="selectByConfigKey" resultMap="RevenueConfigResultMap">
        SELECT * FROM revenue_config 
        WHERE config_key = #{configKey} AND is_deleted = 0
        LIMIT 1
    </select>

    <!-- 查询所有收益配置 -->
    <select id="selectAll" resultMap="RevenueConfigResultMap">
        SELECT * FROM revenue_config 
        WHERE is_deleted = 0
        ORDER BY config_type, level ASC
    </select>

    <!-- 插入收益配置 -->
    <insert id="insert" parameterType="com.dora.entity.RevenueConfig" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO revenue_config (
            config_key, config_type, target_count, reward_amount, level, level_name, 
            min_fans, commission_rate, config_value, description, create_time, update_time, is_deleted
        ) VALUES (
            #{configKey}, #{configType}, #{targetCount}, #{rewardAmount}, #{level}, #{levelName},
            #{minFans}, #{commissionRate}, #{configValue}, #{description}, #{createTime}, #{updateTime}, 0
        )
    </insert>

    <!-- 根据配置键更新配置 -->
    <update id="updateByConfigKey" parameterType="com.dora.entity.RevenueConfig">
        UPDATE revenue_config SET
        <trim suffixOverrides=",">
            <if test="configType != null">config_type = #{configType},</if>
            <if test="targetCount != null">target_count = #{targetCount},</if>
            <if test="rewardAmount != null">reward_amount = #{rewardAmount},</if>
            <if test="level != null">level = #{level},</if>
            <if test="levelName != null">level_name = #{levelName},</if>
            <if test="minFans != null">min_fans = #{minFans},</if>
            <if test="commissionRate != null">commission_rate = #{commissionRate},</if>
            <if test="configValue != null">config_value = #{configValue},</if>
            <if test="description != null">description = #{description},</if>
            <if test="isDeleted != null">is_deleted = #{isDeleted},</if>
            update_time = #{updateTime},
        </trim>
        WHERE config_key = #{configKey} AND is_deleted = 0
    </update>

</mapper>