<?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.newhosts.NewHostsMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->


    <select id="selectPageWithXML" resultType="cn.iocoder.yudao.module.tkdata.dal.dataobject.newhosts.NewHostsDO">
        select ns.id,hosts_id, hosts_level, hosts_coins, Invitation_type, fans, fllowernum,
        yesterday_coins,ns.create_time, country, online_fans,hosts_kind ,is_assigned,uid ,ns.update_time,ns.operation_status,ns.ai_operation,ns.country_eng
        from server_new_hosts ns left join
        server_country_info ci ON ns.country = ci.country_name
        WHERE tenant_id =#{req.tenantId,jdbcType=BIGINT}
        and deleted = 0
        <!--  主播国家筛选    -->
        <if test="req.region!= '' and req.region != null">
            and ci.country_group_name =#{req.region,jdbcType=VARCHAR}
        </if>
        <if test="req.country != '' and req.country !=null ">
            and ns.country = #{req.country}
        </if>
        <if test="req.userId != null "  >
            and ns.user_id =#{req.userId,jdbcType=BIGINT}
        </if>
        <!--  按照入库时间筛选主播    -->
        <if test="req.createTime != null">
            and DATE(ns.create_time) =#{req.createTime}
        </if>
        <!--  主播 Id 模糊搜索    -->
        <if test="req.hostsId != '' and req.hostsId != null  ">
            and ns.hosts_id like concat(#{req.hostsId,jdbcType=VARCHAR},'%')
        </if>
        <!--  直播类型模糊搜索    -->
        <if test="req.hostsKind != null and req.hostsKind != ''">
            and ns.hosts_kind like concat("%",#{req.hostsKind,jdbcType=VARCHAR},"%")
        </if>
        <!--  是否分配筛选    -->
        <if test="req.isAssigned != null ">
            and ns.is_assigned =#{req.isAssigned,jdbcType=INTEGER}
        </if>
        <!--  主播等级筛选    -->
        <if test="req.hostsLevel != null and req.hostsLevel.size() > 0">
            AND ns.hosts_level IN
            <foreach collection="req.hostsLevel" item="level" open="(" separator="," close=")">
                #{level, jdbcType=VARCHAR}
            </foreach>
        </if>
        <if test="req.aiOperation != null ">
            and ns.ai_operation = #{req.aiOperation,jdbcType=TINYINT}
        </if>
        <!-- 今日主播金币筛选   -->
        <if test="req.hostsCoinsMin != null  and req.hostsCoinsMax == null  ">
            and ns.hosts_coins >=#{req.hostsCoinsMin,jdbcType=INTEGER}
        </if>
        <if test="req.hostsCoinsMax != null  and req.hostsCoinsMin == null ">
            and ns.hosts_coins &lt;=#{req.hostsCoinsMax,jdbcType=INTEGER}
        </if>
        <if test="req.hostsCoinsMin != null and req.hostsCoinsMax != null " >
            and ns.hosts_coins between #{req.hostsCoinsMin,jdbcType=INTEGER} and #{req.hostsCoinsMax,jdbcType=INTEGER}
        </if>
        <!--  昨日主播金币筛选  -->
        <if test="req.yesterdayCoinsMin != null  and req.yesterdayCoinsMax == null ">
            and ns.yesterday_coins >=#{req.yesterdayCoinsMin,jdbcType=INTEGER}
        </if>
        <if test="req.yesterdayCoinsMax != null  and req.yesterdayCoinsMin == null ">
            and ns.yesterday_coins &lt;=#{req.yesterdayCoinsMax,jdbcType=INTEGER}
        </if>
        <if test="req.yesterdayCoinsMin != null and req.yesterdayCoinsMax != null " >
            and ns.yesterday_coins between #{req.yesterdayCoinsMin,jdbcType=INTEGER} and #{req.yesterdayCoinsMax,jdbcType=INTEGER}
        </if>
        <!--    主播粉丝数筛选-->
        <if test="req.fansMin != null  and req.fansMax == null ">
            and ns.fans >=#{req.fansMin,jdbcType=INTEGER}
        </if>
        <if test="req.fansMax != null  and req.fansMin == null  ">
            and ns.fans &lt;=#{req.fansMax,jdbcType=INTEGER}
        </if>
        <if test="req.fansMin != null and req.fansMax != null " >
            and ns.fans between #{req.fansMin,jdbcType=INTEGER} and #{req.fansMax,jdbcType=INTEGER}
        </if>
        <!--    主播关注筛选-->
        <if test="req.fllowernumMin != null  and req.fllowernumMax == null">
            and ns.fllowernum >=#{req.fllowernumMin,jdbcType=INTEGER}
        </if>
        <if test="req.fllowernumMax != null  and req.fllowernumMin == null  ">
            and ns.fllowernum &lt;=#{req.fllowernumMax,jdbcType=INTEGER}
        </if>
        <if test="req.fllowernumMin != null and req.fllowernumMax != null " >
            and ns.fllowernum between #{req.fllowernumMin,jdbcType=INTEGER} and #{req.fllowernumMax,jdbcType=INTEGER}
        </if>
        <!--    主播在线人数筛选-->
        <if test="req.onlineFansMin != null  and req.onlineFansMax == null  ">
            and ns.online_fans >=#{req.onlineFansMin,jdbcType=INTEGER}
        </if>
        <if test="req.onlineFansMax != null  and req.onlineFansMin == null ">
            and ns.online_fans &lt;= #{req.onlineFansMax,jdbcType=INTEGER}
        </if>
        <if test="req.onlineFansMin != null and req.onlineFansMax != null " >
            and ns.online_fans between #{req.onlineFansMin,jdbcType=INTEGER} and #{req.onlineFansMax,jdbcType=INTEGER}
        </if>
        <!--  邀请类筛选 -->
        <if test="req.invitationType != null">
            and ns.Invitation_type =#{req.invitationType,jdbcType=INTEGER}
        </if>
        <!--   判断主播是否有直播场次     -->
        <if test="req.hasLiveRecoder != null">
            <choose>
                <when test="req.hasLiveRecoder == 0">
                    AND EXISTS (
                    SELECT 1
                    FROM server_live_host_detail ld
                    WHERE ld.deleted = 0
                    AND ld.tenant_id = ns.tenant_id
                    AND ld.hosts_id = ns.hosts_id
                    )
                </when>
                <when test="req.hasLiveRecoder == 1">
                    AND NOT EXISTS (
                    SELECT 1
                    FROM server_live_host_detail ld
                    WHERE ld.deleted = 0
                    AND ld.tenant_id = ns.tenant_id
                    AND ld.hosts_id = ns.hosts_id
                    )
                </when>
            </choose>
        </if>
        <!-- 排序类型 -->
        group by
        ns.hosts_id
        order by
        <choose>
            <!-- 传空和默认的情况下按照时间降序排序 -->
            <when test="req.sortName == null or req.sortName == ''">
                ns.create_time desc,ns.id desc
            </when>
            <!-- sortName 有值的情况下排序 -->
            <when test="req.sortName != null and req.sort != null">
                <choose>
                    <when test="req.sortName == 'createTime'">
                        ns.create_time ${req.sort}
                    </when>
                    <when test="req.sortName == 'yesterdayCoins'">
                        ns.yesterday_coins ${req.sort}
                    </when>
                    <when test="req.sortName == 'hostsCoins'">
                        ns.hosts_coins ${req.sort}
                    </when>
                    <when test="req.sortName == 'fans'">
                        ns.fans ${req.sort}
                    </when>
                    <when test="req.sortName == 'fllowernum'">
                        ns.fllowernum ${req.sort}
                    </when>
                    <when test="req.sortName == 'onlineFans'">
                        ns.online_fans ${req.sort}
                    </when>
                    <when test="req.sortName == 'updateTime'">
                        ns.update_time ${req.sort}
                    </when>
                    <otherwise>
                        ns.create_time desc
                    </otherwise>
                </choose>
            </when>
            <otherwise>
                ns.create_time desc
            </otherwise>
        </choose>
    </select>
</mapper>