<?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.sqx.modules.app.dao.UserDao">


    <select id="selectUserPage" resultType="com.sqx.modules.app.entity.UserEntity">
        select * from tb_user
        where 1=1
        <if test="userEntity.userName!=null and userEntity.userName!=''">
            and user_name like concat('%',#{userEntity.userName},'%')
        </if>
        <if test="userEntity.sex!=null and userEntity.sex!=0">
            and sex=#{userEntity.sex}
        </if>
        <if test="userEntity.phone!=null and userEntity.phone!=''">
            and phone like concat("%",#{userEntity.phone},"%")
        </if>
        <if test="userEntity.platform!=null and userEntity.platform!=''">
            and platform=#{userEntity.platform}
        </if>
        <if test="userEntity.sysPhone!=null and userEntity.sysPhone!=''">
            and sys_phone=#{userEntity.sysPhone}
        </if>
        <if test="userEntity.invitationCode!=null and userEntity.invitationCode!=''">
            and invitation_code=#{userEntity.invitationCode}
        </if>
        <if test="userEntity.inviterCode!=null and userEntity.inviterCode!=''">
            and inviter_code=#{userEntity.inviterCode}
        </if>
        <if test="userEntity.status!=null and userEntity.status!=0">
            and status=#{userEntity.status}
        </if>
        <if test="userEntity.isUserVip!=null">
            and is_user_vip = #{userEntity.isUserVip}
        </if>
        <if test="userEntity.isCompanyVip!=null">
            and is_company_vip = #{userEntity.isCompanyVip}
        </if>
        <if test="startTime!=null and startTime!='' and startTime!=null and startTime!=''">
            and date_format(create_time,'%Y-%m-%d') BETWEEN #{startTime} and #{endTime}
        </if>
        <if test="startTime!=null and startTime!='' and  (endTime==null or endTime=='')">
            and date_format(create_time,'%Y-%m-%d') &gt;= #{startTime}
        </if>
        <if test="endTime!=null and endTime!='' and (startTime==null or startTime=='')">
            and date_format(create_time,'%Y-%m-%d') &lt;= #{endTime}
        </if>
        <if test="userEntity.userType!=null">
            and user_type = #{userEntity.userType}
        </if>
        order by create_time desc
    </select>
    <select id="queryInviterCount" resultType="int">
        select count(*)
        from tb_user
        where inviter_code = #{inviterCode}
    </select>
    <select id="queryUserCount" resultType="int">
        select count(*)from tb_user where 1=1
        <if test="userType!=null">
            and user_type=#{userType}
        </if>
        <if test="type==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="type==2">
            and date_format(create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="type==3">
            and date_format(create_time,'%Y')=date_format(#{date},'%Y')
        </if>

    </select>
    <select id="queryPayMoney" resultType="Double">
        select sum(money) from pay_details where `state`=1
        <if test="type==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="type==2">
            and date_format(create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="type==3">
            and date_format(create_time,'%Y')=date_format(#{date},'%Y')
        </if>
    </select>

    <select id="queryCourseOrder" resultType="com.sqx.modules.app.response.CourseOrderResponse">
        select * from (
        select c.course_id as courseId,sum(o.pay_money) as 'coursemoney' ,count(*) as 'coursenum',any_value(c.title) as
        'coursename' from orders o , course c where o.course_id=c.course_id and o.status=1 and o.orders_type=1
        <if test="type==1">
            and date_format(o.create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="type==2">
            and date_format(o.create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="type==3">
            and date_format(o.create_time,'%Y')=date_format(#{date},'%Y')
        </if>
        group by c.course_id
        ) a
        order by a.coursenum desc
    </select>
    <select id="userMessage" resultType="int">
        select count(*) from tb_user t ,user_vip v where t.user_id=v.user_id and is_vip=1
        <if test="type==1">
            and date_format(t.create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="type==2">
            and date_format(t.create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="type==3">
            and date_format(t.create_time,'%Y')=date_format(#{date},'%Y')
        </if>
    </select>


    <select id="takingOrdersMessage" resultType="com.sqx.modules.app.response.TakingOrderResponse">
        SELECT
        SUM( o.order_number ) AS orderNumber,
        t.game_id as gameName,
        SUM( o.pay_money ) AS payMoney
        FROM
        orders o
        LEFT JOIN order_taking t ON o.order_taking_id = t.id
        WHERE
        o.state = 1
        <if test="type==1">
            and date_format("o.create_time",'%y-%M-%d')=date_format(#{date},'%y-%M-%d')
        </if>
        <if test="type==2">
            and date_format("o.create_time",'%y-%M-%d')=date_format(#{date},'%y-%M-%d')
        </if>
        <if test="type==3">
            and date_format("o.create_time",'%y-%M-%d')=date_format(#{date},'%y-%M-%d')
        </if>

        GROUP BY
        t.game_id
    </select>

    <select id="selectInviterCodeByUserIdLists" resultType="com.sqx.modules.app.entity.UserEntity">
        select u.user_id as userId,u.user_name as userName,u.avatar as avatar,u.create_time as createTime,
               (select ifnull(sum(d.money),0.00) from user_money_details d where d.user_id=#{userId} and d.user_level=#{userType}) as money
        from tb_user u
        where 1=1
        <if test="userType!=null and userType==1">
            and u.inviter_code=#{invitationCode}
        </if>
        <if test="userType!=null and userType==2">
            and u.inviter_code in (select invitation_code from tb_user where inviter_code=#{invitationCode})
        </if>
        order by u.create_time desc
    </select>

    <select id="exportExcelUser" resultType="com.sqx.modules.app.entity.UserEntity">
        select * from tb_user
        where 1=1
        <if test="userEntity.userName!=null and userEntity.userName!=''">
            and user_name like concat('%',#{userEntity.userName},'%')
        </if>
        <if test="userEntity.sex!=null and userEntity.sex!=0">
            and sex=#{userEntity.sex}
        </if>
        <if test="userEntity.phone!=null and userEntity.phone!=''">
            and phone like concat("%",#{userEntity.phone},"%")
        </if>

        <if test="userEntity.platform!=null and userEntity.platform!=''">
            and platform=#{userEntity.platform}
        </if>
        <if test="userEntity.sysPhone!=null and userEntity.sysPhone!=''">
            and sys_phone=#{userEntity.sysPhone}
        </if>
        <if test="userEntity.status!=null and userEntity.status!=0">
            and status=#{userEntity.status}
        </if>
        <if test="userEntity.isUserVip!=null">
            and is_user_vip = #{userEntity.isUserVip}
        </if>
        <if test="userEntity.isCompanyVip!=null">
            and is_company_vip = #{userEntity.isCompanyVip}
        </if>

        <if test="userEntity.userType!=null">
            and user_type = #{userEntity.userType}
        </if>
        <if test="startTime!=null and startTime!='' and startTime!=null and startTime!=''">
            and date_format(create_time,'%Y-%m-%d') BETWEEN #{startTime} and #{endTime}
        </if>
        <if test="startTime!=null and startTime!='' and  (endTime==null or endTime=='')">
            and date_format(create_time,'%Y-%m-%d') &gt;= #{startTime}
        </if>
        <if test="endTime!=null and endTime!='' and (startTime==null or startTime=='')">
            and date_format(create_time,'%Y-%m-%d') &lt;= #{endTime}
        </if>
        order by create_time desc

    </select>
    <select id="getNotHasCompanyUserList" resultType="com.sqx.modules.app.entity.UserEntity">
        select u.* from tb_user u left join company c on u.user_id = c.user_id where c.company_id is null
        <if test="userEntity.phone!=null and userEntity.phone!=''">
            and u.phone like concat("%",#{userEntity.phone},"%")
        </if>
        <if test="userEntity.userName!=null and userEntity.userName!=''">
            and u.user_name like concat("%",#{userEntity.userName},"%")
        </if>
        order by c.create_time desc
    </select>


</mapper>