<?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 u.*,ifnull(v.is_vip,2) as isVip,v.end_time as endTime
        from tb_user u
        left join user_vip v on v.user_id=u.user_id
        <where>
            <if test="userEntity.search!=null and userEntity.search!=''">
                and (u.user_id=#{userEntity.search} or u.phone = #{userEntity.search} or u.user_name =#{userEntity.search} )
            </if>
            <if test="userEntity.nickName!=null and userEntity.nickName!=''">
                and u.nick_name like concat('%',#{userEntity.nickName},'%')
            </if>
            <if test="userEntity.phone!=null and userEntity.phone!=''">
                and u.phone like concat('%',#{userEntity.phone},'%')
            </if>
            <if test="userEntity.userType!=null and userEntity.userType==1">
                and u.user_type=#{userEntity.userType}
            </if>
            <if test="userEntity.userType!=null and userEntity.userType==2">
                and u.user_type=#{userEntity.userType} and u.user_id in (select user_id from driver where status=2)
            </if>
            <if test="userEntity.sex!=null and userEntity.sex!=0">
                and u.sex=#{userEntity.sex}
            </if>
            <if test="userEntity.invitationCode!=null and userEntity.invitationCode!=''">
                and u.invitation_code like concat("%",#{userEntity.invitationCode},"%")
            </if>
            <if test="userEntity.inviterCode!=null and userEntity.inviterCode!=''">
                and u.inviter_code like concat("%",#{userEntity.inviterCode},"%")
            </if>
            <if test="userEntity.platform!=null and userEntity.platform!=''">
                and u.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 u.status=#{userEntity.status}
            </if>
            <if test="userEntity.isVip!=null and userEntity.isVip!=0">
                and v.is_vip=1
            </if>
        </where>

        order by u.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="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,
        g.game_name,
        SUM( o.pay_money ) AS payMoney
        FROM
        ( orders o LEFT JOIN order_taking t ON o.order_taking_id = t.id )
        LEFT JOIN game g ON g.id = t.game_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
        g.game_name
    </select>


    <select id="selectUserList" resultType="com.sqx.modules.app.entity.UserEntity">
        select * from tb_user
        where 1=1
        <if test="userName!=null and userName!''">
            and user_name =#{userName}
        </if>
        order by create_time desc
    </select>


    <update id="updateCashDeposit">
        update tb_user
        set cash_deposit = cash_deposit + #{cashDeposit}
        where user_id = #{userId}
    </update>

    <update id="updateMoney">
        update tb_user set
        <if test="type==1">
            balance = balance+#{money}
        </if>
        <if test="type==2">
            balance = balance-#{money}
        </if>
        where user_id = #{userId}
    </update>

    <update id="deleteCertification">
        update tb_user
        set cash_deposit=0.00,
            user_name=null,
            identity_card_front=null,
            identity_card_number=null,
            identity_card_verso=null,
            check_certification=null,
            check_certification_message=null,
            audit_time=null
        where user_id = #{userId}
    </update>
    <update id="updateRateByCity">
        update tb_user
        set rate = #{riderRate}
        where city = #{city}
    </update>
    <select id="selectRiderUserByOnLineFlag" resultType="com.sqx.modules.app.entity.UserEntity">
        select *
        from (select (st_distance(point(r.lng, r.lat), point(#{lng}, #{lat})) * 111195) as distance, t.*
              from rider_location r
                       left join tb_user t on t.user_id = r.user_id
              where t.user_type = 2
                and t.on_line_flag = 1
             ) a
        where distance &lt;= #{distance}
    </select>
    <select id="getNearByRider" resultType="com.sqx.modules.app.entity.UserEntity">
        SELECT *
        FROM (SELECT u.*,
                     (st_distance(POINT(r.lng, r.lat), POINT(#{lng}, #{lat})) * 111195) AS distance,
                     (select count(0)
                      from sys_indent s
                      where s.rider_user_id = u.user_id
                        and s.indent_state in (0, 1, 2))                                as indentCount
              FROM tb_user u,
                   rider_location r
              WHERE u.user_id = r.user_id
                and u.on_line_flag = 1
                and u.status = 1) AS t1
        where t1.distance &lt;= #{range} * 1000

    </select>

    <select id="selectUserCount" resultType="int">
        select count(*)
        from tb_user
        where status = 1
        <if test="flag!=null and flag==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{time},'%Y-%m-%d')
        </if>
        <if test="flag!=null and flag==2">
            and date_format(create_time,'%Y-%m')=date_format(#{time},'%Y-%m')
        </if>
        <if test="flag!=null and flag==3">
            and date_format(create_time,'%Y')=date_format(#{time},'%Y')
        </if>
        <if test="userType!=null">
            and user_type=#{userType}
        </if>
        <if test="phone!=null">
            and phone is not null
        </if>
    </select>

    <!--<select id="selectInviterList" resultType="com.sqx.modules.app.entity.UserEntity">
        SELECT
        *
        FROM
        tb_user
        WHERE
        inviter_code = ( SELECT invitation_code FROM tb_user WHERE user_id = #{userId} )
    </select>-->

    <select id="selectInviterList" resultType="Map">
        SELECT
        nick_name,create_time,first_order,avatar
        FROM
        tb_user
        WHERE
        inviter_code = ( SELECT invitation_code FROM tb_user WHERE user_id = #{userId} )
        order by create_time desc
    </select>

</mapper>