<?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="cn.iocoder.yudao.module.tkdata.dal.mysql.bigbrother.BigBrotherMapper">


    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.tkdata.dal.dataobject.bigbrother.BigBrotherDO">
        <!--@mbg.generated-->
        <!--@Table server_big_brother-->
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="display_id" jdbcType="VARCHAR" property="displayId" />
        <result column="user_id_str" jdbcType="VARCHAR" property="userIdStr" />
        <result column="nickname" jdbcType="VARCHAR" property="nickname" />
        <result column="level" jdbcType="INTEGER" property="level" />
        <result column="hostcoins" jdbcType="INTEGER" property="hostcoins" />
        <result column="follower_count" jdbcType="INTEGER" property="followerCount" />
        <result column="following_count" jdbcType="INTEGER" property="followingCount" />
        <result column="region" jdbcType="VARCHAR" property="region" />
        <result column="historic_high_coins" jdbcType="INTEGER" property="historicHighCoins" />
        <result column="total_gift_coins" jdbcType="INTEGER" property="totalGiftCoins" />
        <result column="host_display_id" jdbcType="VARCHAR" property="hostDisplayId" />
        <result column="user_id" jdbcType="BIGINT" property="userId" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <result column="creator" jdbcType="BIGINT" property="creator" />
        <result column="updater" jdbcType="VARCHAR" property="updater" />
        <result column="deleted" jdbcType="BOOLEAN" property="deleted" />
        <result column="tenant_id" jdbcType="BIGINT" property="tenantId" />
        <result column="fans_level" jdbcType="INTEGER" property="fansLevel" />
    </resultMap>



    <select id="selectPageWithXML" resultType="cn.iocoder.yudao.module.tkdata.dal.dataobject.bigbrother.BigBrotherDO">
        select
        sbr.id, sbr.display_id, sbr.user_id_str, sbr.nickname, sbr.level, sbr.hostcoins,
        sbr.follower_count, sbr.following_count, sbr.region, sbr.historic_high_coins, sbr.total_gift_coins,
        sbr.host_display_id, sbr.create_time, sbr.is_assigned, sbr.fans_level
        from server_big_brother sbr

        <if test="dto.region != null and dto.region != ''">
            left join server_country_info ci ON sbr.region = ci.country_name
        </if>

        <where>
            and sbr.tenant_id = #{dto.tenantId}
            and sbr.deleted = 0

            <if test="dto.region != null and dto.region != ''">
                and ci.country_group_name = #{dto.region,jdbcType=VARCHAR}
            </if>

            <if test="dto.countryName != null and dto.countryName != ''">
                and sbr.region = #{dto.countryName}
            </if>

            <if test="dto.createTimeStart != null and dto.createTimeEnd != null">
                and sbr.create_time BETWEEN #{dto.createTimeStart} and #{dto.createTimeEnd}
            </if>

            <if test="dto.displayId != null and dto.displayId != ''">
                and sbr.display_id like concat(#{dto.displayId}, '%')
            </if>

            <if test="dto.historicHighCoinsMin != null">
                and sbr.historic_high_coins &gt;= #{dto.historicHighCoinsMin}
            </if>
            <if test="dto.historicHighCoinsMax != null">
                and sbr.historic_high_coins &lt;= #{dto.historicHighCoinsMax}
            </if>

            <if test="dto.totalGiftCoinsMin != null">
                and sbr.total_gift_coins &gt;= #{dto.totalGiftCoinsMin}
            </if>
            <if test="dto.totalGiftCoinsMax != null">
                and sbr.total_gift_coins &lt;= #{dto.totalGiftCoinsMax}
            </if>

            <if test="dto.levelMin != null">
                and sbr.level &gt;= #{dto.levelMin}
            </if>
            <if test="dto.levelMax != null">
                and sbr.level &lt;= #{dto.levelMax}
            </if>

            <if test="dto.fansLevelMin != null">
                and sbr.fans_level &gt;= #{dto.fansLevelMin}
            </if>
            <if test="dto.fansLevelMax != null">
                and sbr.fans_level &lt;= #{dto.fansLevelMax}
            </if>

            <if test="dto.isAssigned != null">
                and sbr.is_assigned = #{dto.isAssigned}
            </if>
            <if test="dto.hostDisplayId != null">
                and sbr.host_display_id = #{dto.hostDisplayId,jdbcType=VARCHAR}
            </if>
        </where>

        <choose>
            <when test="dto.sortName == 'createTime' and dto.sort != null">
                order by sbr.create_time ${dto.sort}
            </when>
            <when test="dto.sortName == 'level' and dto.sort != null">
                order by sbr.level ${dto.sort}
            </when>
            <when test="dto.sortName == 'hostsCoins' and dto.sort != null">
                order by sbr.hostcoins ${dto.sort}
            </when>
            <when test="dto.sortName == 'totalGiftCoins' and dto.sort != null">
                order by sbr.total_gift_coins ${dto.sort}
            </when>

            <otherwise>
                order by sbr.create_time desc, sbr.id desc
            </otherwise>
        </choose>
    </select>
</mapper>