<?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="search!=null and search!=''">
            and (user_id=#{search} or phone = #{search} or user_name =#{search} )
        </if>
        <if test="sex!=null and sex!=0">
            and sex=#{sex}
        </if>
        <if test="platform!=null and platform!=''">
            and platform=#{platform}
        </if>
        <if test="sysPhone!=null and sysPhone!=''">
            and sys_phone=#{sysPhone}
        </if>
        <if test="type!=null and type!=''">
            <if test="type==1">
                and (open_id is not null or wx_open_id is not null)
            </if>
            <if test="type==2">
                and (rider_open_id is not null or rider_wx_open_id is not null)
            </if>
        </if>
        <if test="status!=null and status!=0">
            and status=#{status}
        </if>
        <if test="userName!=null and userName!=''">
            and user_name = #{userName}
        </if>
        <if test="nickName!=null and nickName!=''">
            and nick_name = #{nickName}
        </if>
        <if test="isVip!=null">
            and is_vip = #{isVip}
        </if>
        <if test="shopAdminFlag!=null and shopAdminFlag==1">
            and admin_user_id is not null
        </if>
        order by create_time desc
    </select>


    <select id="queryRider" resultType="com.sqx.modules.app.entity.queryRiderVo">
        select u.user_id,u.clientid,u.open_id,u.rider_open_id,u.user_name,u.phone,u.cash_deposit,u.check_certification,r.station_name
        from tb_user u
        left join rider_station r on r.id = u.rider_station_id
        where  u.user_id = #{userId}
    </select>

    <select id="queryRiders" resultType="com.sqx.modules.app.entity.queryRidersVo">
        select u.user_id,u.user_name,u.phone,u.avatar,r.station_name
        from tb_user u
        left join rider_station r on r.id = u.rider_station_id
        where u.rider_open_id is not null and u.on_line_flag = 1
        and u.rider_station_id = #{riderStationId} and u.user_id != #{userId}
        <if test="keyword != null and keyword != ''">
            and (u.phone like concat('%',#{keyword},'%') or u.user_name like concat('%',#{keyword},'%'))
        </if>
    </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>


    <insert id="insertUser" parameterType="com.sqx.modules.app.entity.UserEntity" useGeneratedKeys="true"
            keyProperty="userId">
        INSERT INTO tb_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != userName and '' != userName">
                user_name,
            </if>
            <if test="null != phone and '' != phone">
                phone,
            </if>
            <if test="null != avatar and '' != avatar">
                avatar,
            </if>
            <if test="null != sex and '' != sex">
                sex,
            </if>
            <if test="null != age and '' != age">
                age,
            </if>
            <if test="null != openId and '' != openId">
                open_id,
            </if>
            <if test="null != wxOpenId and '' != wxOpenId">
                wx_open_id,
            </if>
            <if test="null != password and '' != password">
                password,
            </if>
            <if test="null != createTime and '' != createTime">
                create_time,
            </if>
            <if test="null != updateTime and '' != updateTime">
                update_time,
            </if>
            <if test="null != appleId and '' != appleId">
                apple_id,
            </if>
            <if test="null != sysPhone and '' != sysPhone">
                sys_phone,
            </if>
            <if test="null != status and '' != status">
                status,
            </if>
            <if test="null != platform and '' != platform">
                platform,
            </if>
            <if test="null != jifen and '' != jifen">
                jifen,
            </if>
            <if test="null != invitationCode and '' != invitationCode">
                invitation_code,
            </if>
            <if test="null != inviterCode and '' != inviterCode">
                inviter_code,
            </if>
            <if test="null != clientid and '' != clientid">
                clientid,
            </if>
            <if test="null != zhiFuBaoName and '' != zhiFuBaoName">
                zhi_fu_bao_name,
            </if>
            <if test="null != zhiFuBao and '' != zhiFuBao">
                zhi_fu_bao,
            </if>
            <if test="null != isVip">
                is_vip,
            </if>
            <if test="null != userType">
                user_type,
            </if>
            <if test="null != nickName and '' != nickName">
                nick_name,
            </if>
            <if test="null != cashDeposit">
                cash_deposit,
            </if>
            <if test="null != balance">
                balance,
            </if>
            <if test="null != riderWxOpenId and '' != riderWxOpenId">
                rider_wx_open_id,
            </if>
            <if test="null != newUserFlag">
                new_user_flag,
            </if>
            <if test="null != newUserFlagWm">
                new_user_flag_wm
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != userName and '' != userName">
                #{userName},
            </if>
            <if test="null != phone and '' != phone">
                #{phone},
            </if>
            <if test="null != avatar and '' != avatar">
                #{avatar},
            </if>
            <if test="null != sex and '' != sex">
                #{sex},
            </if>
            <if test="null != age and '' != age">
                #{age},
            </if>
            <if test="null != openId and '' != openId">
                #{openId},
            </if>
            <if test="null != wxOpenId and '' != wxOpenId">
                #{wxOpenId},
            </if>
            <if test="null != password and '' != password">
                #{password},
            </if>
            <if test="null != createTime and '' != createTime">
                #{createTime},
            </if>
            <if test="null != updateTime and '' != updateTime">
                #{updateTime},
            </if>
            <if test="null != appleId and '' != appleId">
                #{appleId},
            </if>
            <if test="null != sysPhone and '' != sysPhone">
                #{sysPhone},
            </if>
            <if test="null != status and '' != status">
                #{status},
            </if>
            <if test="null != platform and '' != platform">
                #{platform},
            </if>
            <if test="null != jifen and '' != jifen">
                #{jifen},
            </if>
            <if test="null != invitationCode and '' != invitationCode">
                #{invitationCode},
            </if>
            <if test="null != inviterCode and '' != inviterCode">
                #{inviterCode},
            </if>
            <if test="null != clientid and '' != clientid">
                #{clientid},
            </if>
            <if test="null != zhiFuBaoName and '' != zhiFuBaoName">
                #{zhiFuBaoName},
            </if>
            <if test="null != zhiFuBao and '' != zhiFuBao">
                #{zhiFuBao},
            </if>
            <if test="null != isVip">
                #{isVip},
            </if>
            <if test="null != userType">
                #{userType},
            </if>
            <if test="null != nickName and '' != nickName">
                #{nickName},
            </if>
            <if test="null != cashDeposit">
                #{cashDeposit},
            </if>
            <if test="null != balance">
                #{balance},
            </if>
            <if test="null != riderWxOpenId and '' != riderWxOpenId">
                #{riderWxOpenId},
            </if>
            <if test="null != newUserFlag">
                #{newUserFlag},
            </if>
            <if test="null != newUserFlagWm">
                #{newUserFlagWm}
            </if>
        </trim>
    </insert>

    <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>

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


    <!--<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>-->

    <select id="selectAllUser" resultType="com.sqx.modules.app.entity.UserEntity">
        select tu.*, um.sex as umSex from tb_user tu left join user_message um on tu.user_id = um.user_id where 1 = 1
        <if test="phone!=null and phone!=''">
            and phone = #{phone}
        </if>
        <if test="userType!=null">
            and type = #{userType}
        </if>
        order by tu.create_time desc
    </select>

    <select id="findUserMessage" resultType="com.sqx.modules.app.entity.UserEntity">
        select tu.*, um.birthday as birthday, um.height as height, um.weight as weight, um.occupation as occupation, um.individual_resume as individualResume,
        um.picture_list as pictureList, um.sex as umSex
         from tb_user tu left join user_message um on tu.user_id = um.user_id where tu.user_id = #{userId}
    </select>

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

    <select id="selectUserById" resultType="com.sqx.modules.app.entity.UserEntity">
        select tu.*, um.money as money, ui.integral_num as integralNum
        from tb_user tu
        left join user_money um on tu.user_id = um.user_id
        left join user_integral ui on tu.user_id = ui.user_id
        where tu.user_id = #{userId}
    </select>

    <insert id="topUpMoney">
        insert into pay_details (classify, order_id, money, user_id, state, create_time, pay_time, type)
        values (#{classify}, #{orderId}, #{money}, #{userId}, #{state}, #{date}, #{date1}, #{type})
    </insert>

    <update id="certification">
        update tb_user set user_name = #{userName}, audit_time = #{format},  identity_card_number = #{identityCardNumber}, identity_card_front = #{identityCardFront},
        identity_card_verso = #{identityCardVerso}, self_identity_card = #{selfIdentityCard}, check_certification = "0", cash_deposit=#{cashDeposit}, check_number=#{checkNumber} where user_id = #{userId}
    </update>

    <insert id="userFeedback">
        insert into sys_feedback (feedback_type, user_id, user_email, feedback_score, feedback_message, feedback_time)
        values
        (#{feedbackType}, #{userId}, #{userEmail}, #{feedbackScore}, #{feedbackMessage}, #{format})
    </insert>

    <insert id="addRedPacket">
        insert into errand_user_red_packet (user_id, red_packet_type, minimum_amount, red_packet_amount, red_packet_title, expiration_time, delete_flag, create_time)
        values (#{userId}, #{redPacketType}, #{minimumAmount}, #{redPacketAmount}, #{redPacketTitle}, #{expirationTime}, "0", #{createTime})
    </insert>

    <update id="updateOldUser">
        update tb_user set new_user_flag = "2" where user_id = #{userId}
    </update>

    <select id="selectUserMessage" resultType="com.sqx.modules.app.entity.UserEntity">
        select tu.*, gs.status as shopStatus from tb_user tu left join sys_user_shop sus on tu.admin_user_id = sus.user_id
        left join goods_shop gs on sus.shop_id = gs.shop_id
        where tu.user_id = #{userId}
    </select>

    <select id="selectVipOverdue" resultType="com.sqx.modules.app.entity.UserEntity">
        select * from tb_user where is_vip = 1 and vip_expiration_time &lt; #{date}
    </select>

    <update id="updateUserClientIdIsNull">
        update tb_user set clientid='' where clientid=#{clientid}
    </update>

    <update id="updateUserRiderClientIdIsNull">
        update tb_user set rider_clientid='' where rider_clientid=#{clientid}
    </update>

    <update id="updateUserShopClientIdIsNull">
        update tb_user set shop_client_id='' where shop_client_id=#{clientid}
    </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.on_line_flag=1
                      ) a where distance&lt;=#{distance}
    </select>


</mapper>
