<?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.PromotionLevelConfigMapper">

    <resultMap id="BaseResultMap" type="com.dora.entity.PromotionLevelConfig">
        <id column="id" property="id"/>
        <result column="level_name" property="levelName"/>
        <result column="min_paid_fans" property="minPaidFans"/>
        <result column="commission_rate" property="commissionRate"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, level_name, min_paid_fans, commission_rate, create_time, update_time, is_deleted
    </sql>

    <select id="selectAllOrderByLevel" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM promotion_level_config 
        WHERE is_deleted = 0
        ORDER BY min_paid_fans ASC
    </select>

    <select id="selectByLevel" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM promotion_level_config
        WHERE id = #{level} AND is_deleted = 0
    </select>

    <select id="selectByPaidFansCount" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM promotion_level_config
        WHERE min_paid_fans &lt;= #{paidFansCount} AND is_deleted = 0
        ORDER BY min_paid_fans DESC
        LIMIT 1
    </select>

    <select id="selectNextLevel" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM promotion_level_config
        WHERE min_paid_fans &gt; (
            SELECT min_paid_fans 
        FROM promotion_level_config 
            WHERE id = #{currentLevel} AND is_deleted = 0
        ) AND is_deleted = 0
        ORDER BY min_paid_fans ASC
        LIMIT 1
    </select>

</mapper> 