<?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.errand.dao.UserInfoDao">

    <select id="findUserInfoById" resultType="com.sqx.modules.app.entity.UserEntity">
        select * from tb_user where user_id = #{userId}
    </select>

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

    <update id="updateUserInfoById">
        update tb_user set
            nick_name = #{nickName},
            avatar = #{avatar},
            phone = #{phone},
            cash_qr_code = #{cashQrCode}
            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>

    <select id="findIncomeday" resultType="java.math.BigDecimal">
        select ifnull(sum(rider_money),0) from tb_indent where rider_user_id = #{userId} and indent_state = "6"
        and date_format(finish_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
    </select>

    <select id="findPrepayMoneyday" resultType="java.math.BigDecimal">
        select ifnull(sum(prepay_money),0) from tb_indent where rider_user_id = #{userId} and indent_state = "6"
        and date_format(finish_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
    </select>

    <select id="findIncomemonth" resultType="java.math.BigDecimal">
        select ifnull(sum(rider_money),0) from tb_indent where rider_user_id = #{userId} and indent_state = "6"
        and date_format(finish_time,'%Y-%m')=date_format(#{date},'%Y-%m')
    </select>

    <select id="findPrepayMoneymonth" resultType="java.math.BigDecimal">
        select ifnull(sum(prepay_money),0) from tb_indent where rider_user_id = #{userId} and indent_state = "6"
        and date_format(finish_time,'%Y-%m')=date_format(#{date},'%Y-%m')
    </select>

    <select id="findIncomesum" resultType="java.math.BigDecimal">
        select ifnull(sum(rider_money),0) from tb_indent where rider_user_id = #{userId} and indent_state = "6"
    </select>

    <select id="findPrepayMoneysum" resultType="java.math.BigDecimal">
        select ifnull(sum(prepay_money),0) from tb_indent where rider_user_id = #{userId} and indent_state = "6"
    </select>

    <select id="findIndentCount" resultType="int">
        select count(*) from tb_indent where rider_user_id = #{userId} and indent_state = "6"
        and date_format(create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
    </select>

    <select id="findBalance" resultType="double">
        select balance from tb_user where user_id = #{userId}
    </select>

    <select id="findCashDeposit" resultType="double">
        select cash_deposit from tb_user where user_id = #{userId}
    </select>

    <select id="findMoneyDetails" resultType="com.sqx.modules.app.entity.UserMoneyDetails">
        select * from user_money_details where user_id = #{userId} and classify = #{classify} order by create_time desc
    </select>

    <select id="findEvaluate" resultType="com.sqx.modules.errand.entity.ErrandEvaluate">
        select e.*, t.nick_name as nickName, t.avatar as avatar
        from errand_evaluate e left join tb_user t on e.user_id = t.user_id where rider_user_id = #{riderUserId} and satisfaction_flag = #{satisfactionFlag}
        order by create_time desc
    </select>

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

    <update id="updateCoordinate">
        update tb_user set station_lng = #{lng}, station_lat = #{lat} where user_id = #{userId}
    </update>

    <update id="updateOnLine">
        update tb_user set on_line_flag = #{onLineFlag} where user_id = #{userId}
    </update>

    <select id="rankingList" resultType="com.sqx.modules.errand.entity.TbIndent">
        SELECT
        *,
        @rank_num := @rank_num + 1 AS rankNum
        FROM
        ( SELECT @rank_num := 0 ) r,
        ( SELECT ifnull( sum( rider_money ), 0 ) AS moneyOrder, i.rider_user_id, u.nick_name as nickName, u.avatar as avatar, i.delivery_address as address
        FROM sys_indent i left join tb_user u on i.rider_user_id = u.user_id
        WHERE i.rider_user_id IS NOT NULL
        <if test="address!=null and address!=''">
            and i.delivery_address like concat('%',#{address},'%')
        </if>
        <if test="dateType=='day'">
            and date_format(i.create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(i.create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(i.create_time,'%Y')=date_format(#{date},'%Y')
        </if>
        GROUP BY i.rider_user_id ) a
        ORDER BY moneyOrder DESC
    </select>

    <select id="selectCashDeposit" resultType="com.sqx.modules.app.entity.UserMoneyDetails">
        select * from user_money_details where user_id = #{userId} and classify = #{classify} order by create_time desc
    </select>

    <select id="TrainingCenter" resultType="com.sqx.modules.errand.entity.TrainingCenter">
        select * from training_center where training_id = #{id}
    </select>

    <select id="getAllRiderInfo" resultType="com.sqx.modules.app.entity.UserEntity">
        SELECT
            *
        FROM
            tb_user
        WHERE
            rider_open_id IS NOT NULL
          AND check_certification = '1';
    </select>

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

</mapper>