<?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.TbIndentDao">

    <select id="findNewIndent" resultType="com.sqx.modules.errand.entity.TbIndent">
        select * from (
        select ti.*,
        (st_distance (point (ti.shop_lng,ti.shop_lat),point(#{riderLng},#{riderLat}) ) *111195) AS distancess,
        (st_distance (point (ti.user_lng,ti.user_lat),point(#{riderLng},#{riderLat}) ) *111195) AS distancessd
        from tb_indent ti
        where ti.indent_state = 2
        ) a
        where 1=1
        <if test="distances!=null and distances!=''">
            and ((distancess is not null and distancess &lt;= #{distances}) or (distancess is null and distancessd &lt;=
            #{distances}))
        </if>
        <if test="indentType!=null and indentType!=''">
            and indent_type = #{indentType}
        </if>
        order by create_time desc
    </select>

    <select id="findNewIndentByDistances" resultType="com.sqx.modules.errand.entity.TbIndent">
        select * from (
        select ti.*,
        (st_distance (point (ti.shop_lng,ti.shop_lat),point(#{queryDTO.riderLng},#{queryDTO.riderLat}) ) * 111195) AS distancess,
        (st_distance (point (ti.user_lng,ti.user_lat),point(#{queryDTO.riderLng},#{queryDTO.riderLat}) ) * 111195) AS distancessd
        from tb_indent ti
        where ti.indent_state = 2
        ) a
        <where>
            <if test="queryDTO.distances!= null">
                and ((distancess is not null and distancess &lt;= #{queryDTO.distances}) or (distancess is null and distancessd &lt;= #{queryDTO.distances}))
            </if>
            <if test="queryDTO.indentType != null and queryDTO.indentType != ''">
                and indent_type = #{queryDTO.indentType}
            </if>
            <if test="queryDTO.buyType != null and queryDTO.buyType != ''">
                and buy_type = #{queryDTO.buyType}
            </if>
            <if test="queryDTO.shopId != null">
                and shop_id = #{queryDTO.shopId}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="findNewIndentByDistancesGroupByShop" resultType="com.sqx.modules.errand.vo.ShopWaitAcceptOrderVO">
        select shop_id, shop_name, count(indent_id) as countOfWaitAccept from (
            select
                shop_id, shop_name, indent_id,
                (st_distance(point(shop_lng,shop_lat), point(#{queryDTO.riderLng}, #{queryDTO.riderLat})) * 111195) AS distancess,
                (st_distance(point(user_lng,user_lat), point(#{queryDTO.riderLng}, #{queryDTO.riderLat})) * 111195) AS distancessd
            from
                tb_indent
            where indent_state = 2
        ) a
        <where>
            <if test="queryDTO.distances!= null">
                and ((distancess is not null and distancess &lt;= #{queryDTO.distances}) or (distancess is null and distancessd &lt;= #{queryDTO.distances}))
            </if>
            <if test="queryDTO.indentType != null and queryDTO.indentType != ''">
                and indent_type = #{queryDTO.indentType}
            </if>
            <if test="queryDTO.buyType != null and queryDTO.buyType != ''">
                and buy_type = #{queryDTO.buyType}
            </if>
            <if test="queryDTO.shopId != null">
                and shop_id = #{queryDTO.shopId}
            </if>
        </where>
        group by
            shop_id, shop_name
        order by
            distancess asc
    </select>

    <select id="findNewIndentcity" resultType="com.sqx.modules.errand.entity.TbIndent">
        select * from (
        select ti.*,
        (st_distance (point (ti.shop_lng,ti.shop_lat),point(#{riderLng},#{riderLat}) ) *111195) AS distancess,
        (st_distance (point (ti.user_lng,ti.user_lat),point(#{riderLng},#{riderLat}) ) *111195) AS distancessd
        from tb_indent ti
        where ti.indent_state = 2
        ) a
        where
        user_city like concat('%',#{address},'%')
        <if test="indentType!=null and indentType!=''">
            and indent_type = #{indentType}
        </if>
        order by distancess asc
    </select>

    <select id="findNewIndentByCity" resultType="com.sqx.modules.errand.entity.TbIndent">
        select * from (
        select ti.*,
        (st_distance (point (ti.shop_lng,ti.shop_lat),point(#{queryDTO.riderLng},#{queryDTO.riderLat}) ) * 111195) AS distancess,
        (st_distance (point (ti.user_lng,ti.user_lat),point(#{queryDTO.riderLng},#{queryDTO.riderLat}) ) * 111195) AS distancessd
        from tb_indent ti
        where ti.indent_state = 2
        ) a
        where
        user_city like concat('%',#{queryDTO.city},'%')
        <if test="queryDTO.indentType!=null and queryDTO.indentType!=''">
            and indent_type = #{queryDTO.indentType}
        </if>
        <if test="queryDTO.buyType != null and queryDTO.buyType != ''">
            and buy_type = #{queryDTO.buyType}
        </if>
        <if test="queryDTO.shopId != null">
            and shop_id = #{queryDTO.shopId}
        </if>
        order by distancess asc
    </select>

    <select id="findNewIndentByCityGroupByShop" resultType="com.sqx.modules.errand.vo.ShopWaitAcceptOrderVO">
        select
            shop_id, shop_name, count(indent_id) as countOfWaitAccept,
            (st_distance_sphere (point (shop_lng,shop_lat),point(115.82770500,28.70783200))) AS distancess
        from
            tb_indent
        where
            indent_state = 2
            and shop_city like concat('%',#{queryDTO.city},'%')
        <if test="queryDTO.indentType!=null and queryDTO.indentType!=''">
            and indent_type = #{queryDTO.indentType}
        </if>
        <if test="queryDTO.buyType != null and queryDTO.buyType != ''">
            and buy_type = #{queryDTO.buyType}
        </if>
        <if test="queryDTO.shopId != null">
            and shop_id = #{queryDTO.shopId}
        </if>
        group by
            shop_id, shop_name, shop_lng, shop_lat
        order by
            distancess asc
    </select>

    <select id="findIndentByNumber" resultType="com.sqx.modules.errand.entity.TbIndent">
        select *
        from tb_indent
        where indent_number = #{indentNumber}
    </select>

    <select id="findIndentByOrderId" resultType="com.sqx.modules.errand.entity.TbIndent">
        select *
        from tb_indent
        where order_id = #{orderId}
    </select>

    <select id="selectIndentByRiderUserCount" resultType="Integer">
        select count(*)
        from tb_indent
        where rider_user_id=#{riderUserId} and indent_state in (3,4)
    </select>

    <update id="indentReceiv">
        update tb_indent
        set indent_state   = #{state},
            rider_user_id  = #{userId},
            receiving_time = #{format}
        where indent_number = #{indentNumber}
    </update>

    <update id="indentReceiving">
        update tb_indent
        set indent_state   = 3,
            rider_user_id  = #{userId},
            receiving_time = #{format}
        where indent_number = #{indentNumber}
    </update>

    <update id="orderIndentReceiv">
        update tb_indent
        set indent_state   = #{state},
            rider_user_id  = #{userId},
            receiving_time = #{format}
        where order_id = #{orderId}
    </update>

    <update id="orderIndentReceiving">
        update tb_indent
        set indent_state   = 4,
            rider_user_id  = #{userId},
            receiving_time = #{format}
        where order_id = #{orderId}
    </update>

    <select id="findRiderIndent" resultType="com.sqx.modules.errand.entity.TbIndent">
        select * from tb_indent where rider_user_id = #{userId}
        <if test="indentState!=''">
            <if test="indentState==0">
                and indent_state = 0
            </if>
            <if test="indentState==2">
                and indent_state = 2
            </if>
            <if test="indentState==3">
                and indent_state = 3
            </if>
            <if test="indentState==4">
                and indent_state = 4
            </if>
            <if test="indentState==5">
                and indent_state = 5
            </if>
            <if test="indentState==6">
                and indent_state = 6
            </if>
            <if test="indentState==8 or indentState==9 or indentState==10">
                and (indent_state = 8 or indent_state = 9 or indent_state = 10)
            </if>
        </if>
        order by receiving_time desc
    </select>

    <select id="indentRiderMessage" resultType="com.sqx.modules.errand.entity.TbIndent">
        select ti.*,
               (st_distance(point(ti.shop_lng, ti.shop_lat), point(#{riderLng}, #{riderLat})) * 111195) AS distancess,
               (st_distance(point(ti.user_lng, ti.user_lat), point(#{riderLng}, #{riderLat})) * 111195) AS distancessd,
               u.nick_name                                                                              as nickName,
               u.phone                                                                                  as phone,
               u.avatar                                                                                 as avatar
        from tb_indent ti
                 left join tb_user u on ti.user_id = u.user_id
        where indent_number = #{indentNumber}
    </select>

    <select id="indentMessageByOrderId" resultType="com.sqx.modules.errand.entity.TbIndent">
        select ti.*,
               (st_distance(point(ti.shop_lng, ti.shop_lat), point(#{riderLng}, #{riderLat})) * 111195) AS distancess,
               (st_distance(point(ti.user_lng, ti.user_lat), point(#{riderLng}, #{riderLat})) * 111195) AS distancessd,
               u.nick_name                                                                              as nickName,
               u.phone                                                                                  as phone,
               u.avatar                                                                                 as avatar
        from tb_indent ti
                 left join tb_user u on ti.user_id = u.user_id
        where order_id = #{orderId}
    </select>

    <update id="riderCancleIndent">
        update tb_indent
        set indent_state   = 2,
            receiving_time = null,
            rider_user_id  = null
        where rider_user_id = #{riderUserId}
          and indent_number = #{indentNumber}
    </update>

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

    <update id="riderDelivery">
        update tb_indent
        set indent_state = 6,
            finish_time  = #{date}
        where indent_id = #{indentId}
    </update>

    <select id="selectIndentByIndentNumber" resultType="com.sqx.modules.errand.entity.TbIndent">
        select ti.*, t.nick_name as nickName, t.avatar
        from tb_indent ti
                 LEFT JOIN tb_user t on ti.user_id = t.user_id
        where indent_number = #{indentNumber}
    </select>


    <update id="updateRiderBalance">
        update tb_user
        set balance = #{balance}
        where user_id = #{userId}
    </update>

    <update id="userDelivery">
        update tb_indent
        set indent_state = 6,
            finish_time  = #{date}
        where user_id = #{userId}
          and indent_number = #{indentNumber}
    </update>

    <select id="findallIndent" resultType="com.sqx.modules.errand.entity.TbIndent">
        select i.*,
        ru.nick_name as riderNickName, ru.phone as riderPhone
        from tb_indent i
        LEFT JOIN tb_user ru on i.rider_user_id = ru.user_id
        where 1 = 1
        <if test="riderPhone!=null and riderPhone!=''">
            and ru.phone = #{riderPhone}
        </if>
        <if test="userId!=null">
            and i.user_id = #{userId}
        </if>
        <if test="riderUserId!=null">
            and i.rider_user_id = #{riderUserId}
        </if>
        <if test="shopId!=null">
            and i.shop_id = #{shopId}
        </if>
        <if test="indentType!=null">
            and i.indent_type=#{indentType}
        </if>
        <if test="indentType==null">
            and i.indent_type in (1, 2, 3, 4)
        </if>
        <if test="ordersNo!=null and ordersNo!=''">
            and i.indent_number=#{ordersNo}
        </if>
        <if test="status!=null">
            and i.indent_state=#{status}
        </if>
        order by i.create_time desc
    </select>

    <update id="updateDefault">
        update errand_address
        set address_default = "0"
        where user_id = #{userId}
    </update>

    <insert id="addUserAddress" useGeneratedKeys="true" keyProperty="addressId"
            parameterType="com.sqx.modules.errand.entity.ErrandAddress">
        insert into errand_address (user_id, user_name, user_phone, address, address_detail, address_longitude,
                                    address_latitude, create_time, delete_flag, address_default)
        values (#{userId}, #{userName}, #{userPhone}, #{address}, #{addressDetail}, #{addressLongitude},
                #{addressLatitude}, #{createTime}, "0", #{addressDefault})
    </insert>

    <update id="updateAddress">
        update errand_address set
        update_time = #{updatetime},
        <if test="userName!=null and userName!=''">
            user_name= #{userName},
        </if>
        <if test="userPhone!=null and userPhone!=''">
            user_phone = #{userPhone},
        </if>
        <if test="address!=null and address!=''">
            address = #{address},
        </if>
        <if test="addressDetail!=null and addressDetail!=''">
            address_detail = #{addressDetail},
        </if>
        <if test="lng!=null and lng!=''">
            address_longitude = #{lng},
        </if>
        <if test="lat!=null and lat!=''">
            address_latitude = #{lat},
        </if>
        <if test="addressDefault!=null and addressDefault!=''">
            address_default = #{addressDefault}
        </if>
        where user_id = #{userId} and address_id = #{addressId}
    </update>

    <select id="findUserAddress" resultType="com.sqx.modules.errand.entity.ErrandAddress">
        select *
        from errand_address
        where user_id = #{userId}
          and delete_flag = "0"
    </select>

    <select id="findAddressById" resultType="com.sqx.modules.errand.entity.ErrandAddress">
        select *
        from errand_address
        where user_id = #{userId}
          and address_id = #{addressId}
    </select>

    <update id="addDefaultAddress">
        update errand_address
        set address_default = "1"
        where user_id = #{userId}
          and address_id = #{addressId}
    </update>

    <update id="delUserAddress">
        update errand_address
        set delete_flag = "1"
        where user_id = #{userId}
          and address_id = #{addressId}
    </update>

    <select id="findRed" resultType="com.sqx.modules.errand.entity.ErrandRedPacket">
        select *
        from errand_user_red_packet
        where red_packet_id = #{redPacketId}
    </select>

    <update id="delRedPacket">
        update errand_user_red_packet
        set delete_flag = "1"
        where red_packet_id = #{redPacketId}
    </update>

    <select id="findAllIndent" resultType="com.sqx.modules.errand.entity.TbIndent">
        select i.*, t.nick_name as nickName, t.avatar as avatar , r.red_packet_amount as redPacketAmount,
        ru.nick_name as riderNickName, ru.phone as riderPhone,
        from tb_indent i
        LEFT JOIN tb_user t on i.user_id = t.user_id
        LEFT JOIN tb_user ru on i.rider_user_id = ru.user_id
        LEFT JOIN errand_user_red_packet r on i.red_packet_id = r.red_packet_id where indent_type = #{indentType}
        <if test="phone!=null and phone!=''">
            and user_phone = #{phone}
        </if>
        <if test="riderPhone!=null and riderPhone!=''">
            and ru.phone = #{riderPhone}
        </if>
        <if test="indentState==0">
            and indent_state in (0, 1, 2, 3 ,4 ,5 ,6 ,8 ,10)
        </if>
        <if test="indentState==1">
            and indent_state = 0
        </if>
        <if test="indentState==2">
            and indent_state = 2
        </if>
        <if test="indentState==3">
            and indent_state in (3, 4, 5)
        </if>
        <if test="indentState==4">
            and indent_state = 6
        </if>
        <if test="indentState==5">
            and indent_state in (1, 8, 10)
        </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>
        order by i.create_time desc
    </select>

    <select id="findAllIndents" resultType="com.sqx.modules.errand.entity.TbIndent">
        select i.*, t.nick_name as nickName, t.avatar as avatar ,
        ru.nick_name as riderNickName, ru.phone as riderPhone,
        r.red_packet_amount as redPacketAmount from tb_indent i
        LEFT JOIN tb_user t on i.user_id = t.user_id
        LEFT JOIN tb_user ru on i.rider_user_id = ru.user_id
        LEFT JOIN errand_user_red_packet r on i.red_packet_id = r.red_packet_id where indent_type = #{indentType}
        <if test="phone!=null and phone!=''">
            and user_phone = #{phone}
        </if>
        <if test="riderPhone!=null and riderPhone!=''">
            and ru.phone = #{riderPhone}
        </if>
        <if test="indentState==0">
            and indent_state in (0, 1, 2, 3 ,4 ,5 ,6 ,8 ,10)
        </if>
        <if test="indentState!=0">
            and indent_state = #{indentState}
        </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>
        order by i.create_time desc
    </select>

    <select id="IndentCenter" resultType="com.sqx.modules.errand.entity.TbIndent">
        select i.*, u.nick_name as nickName, u.phone as phone from tb_indent i left join tb_user u on u.user_id =
        i.rider_user_id where 1 = 1
        <if test="indentType!=null and indentType!=''">
            and indent_type = #{indentType}
        </if>
        <if test="indentNumber!=null and indentNumber!=''">
            and indent_number = #{indentNumber}
        </if>
        <if test="indentState!=null and indentState!=''">
            and indent_state = #{indentState}
        </if>
        <if test="indentState==null or indentState==''">
            and indent_state in (3,4,5,6)
        </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>
        <if test="phone!=null and phone!=''">
            and u.phone like concat('%',#{phone},'%')
        </if>
        order by i.create_time desc
    </select>

    <update id="updateIndentState">
        update tb_indent
        set indent_state = #{indentState}
        where indent_number = #{indentNumber}
    </update>

    <update id="adminSoldOutIndent">
        update tb_indent
        set indent_state = 10
        where indent_number = #{indentNumber}
    </update>

    <select id="billMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(indent_money),0) from tb_indent where indent_state in (2, 3, 4, 6)
        <if test="query.dateType=='day'">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{query.date},'%Y-%m-%d')
        </if>
        <if test="query.dateType=='month'">
            and date_format(create_time,'%Y-%m')=date_format(#{query.date},'%Y-%m')
        </if>
        <if test="query.dateType=='year'">
            and date_format(create_time,'%Y')=date_format(#{query.date},'%Y')
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and create_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and create_time <![CDATA[<=]]> #{query.endTime}
        </if>
    </select>

    <select id="receivingMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(indent_money),0) from tb_indent where indent_state in (3, 4, 6)
        <if test="query.dateType=='day'">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{query.date},'%Y-%m-%d')
        </if>
        <if test="query.dateType=='month'">
            and date_format(create_time,'%Y-%m')=date_format(#{query.date},'%Y-%m')
        </if>
        <if test="query.dateType=='year'">
            and date_format(create_time,'%Y')=date_format(#{query.date},'%Y')
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and create_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and create_time <![CDATA[<=]]> #{query.endTime}
        </if>
    </select>

    <select id="billCount" resultType="int">
        select count(*) from tb_indent
        where indent_state in (2, 3, 4, 6)
        <if test="query.dateType=='day'">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{query.date},'%Y-%m-%d')
        </if>
        <if test="query.dateType=='month'">
            and date_format(create_time,'%Y-%m')=date_format(#{query.date},'%Y-%m')
        </if>
        <if test="query.dateType=='year'">
            and date_format(create_time,'%Y')=date_format(#{query.date},'%Y')
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and create_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and create_time <![CDATA[<=]]> #{query.endTime}
        </if>
    </select>

    <select id="reveivingCount" resultType="int">
        select count(*) from tb_indent where indent_state in (3, 4, 6)
        <if test="query.dateType=='day'">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{query.date},'%Y-%m-%d')
        </if>
        <if test="query.dateType=='month'">
            and date_format(create_time,'%Y-%m')=date_format(#{query.date},'%Y-%m')
        </if>
        <if test="query.dateType=='year'">
            and date_format(create_time,'%Y')=date_format(#{query.date},'%Y')
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and create_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and create_time <![CDATA[<=]]> #{query.endTime}
        </if>
    </select>

    <select id="taskAnalysisMoney" resultType="com.sqx.modules.errand.entity.TbIndent">
        select s.indent_type,t.nick_name as nickName,u.nick_name as riderNickName, s.create_time, s.platform_money,
        s.user_id, s.rider_user_id from tb_indent s
        left join tb_user t on s.user_id = t.user_id
        left join tb_user u on s.rider_user_id = u.user_id
        where indent_state = "6"
        <if test="indentState!=null and indentState!=''">
            and s.indent_state = #{status}
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and s.create_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and s.create_time <![CDATA[<=]]> #{query.endTime}
        </if>
        order by s.create_time desc
    </select>

    <select id="findAllCount" resultType="int">
        select count(*)
        from tb_user
        where status = "1"
    </select>

    <select id="findDayAddUser" resultType="int">
        select count(*)
        from tb_user
        where status = "1"
          and create_time &gt; #{date}
    </select>

    <select id="findMonthAddUser" resultType="int">
        select count(*)
        from tb_user
        where status = "1"
          and create_time &gt; #{date}
    </select>

    <select id="findYearAddUser" resultType="int">
        select count(*)
        from tb_user
        where status = "1"
          and create_time &gt; #{date}
    </select>

    <select id="findDayIncome" resultType="java.math.BigDecimal">
        select ifnull(sum(platform_money), 0)
        from tb_indent
        where indent_state = "6"
          and create_time &gt; #{date}
    </select>

    <select id="findMonthIncome" resultType="java.math.BigDecimal">
        select ifnull(sum(platform_money), 0)
        from tb_indent
        where indent_state = "6"
          and create_time &gt; #{date}
    </select>

    <select id="findYearIncome" resultType="java.math.BigDecimal">
        select ifnull(sum(platform_money), 0)
        from tb_indent
        where indent_state = "6"
          and create_time &gt; #{date}
    </select>

    <select id="findIndentCount" resultType="int">
        select count(*)
        from tb_indent
        where indent_state = "6"
    </select>

    <select id="selectOrderByStatus" resultType="int">
        select count(*) from tb_indent where 1 = 1
        <if test="status!=null">
            and indent_state = #{status}
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and create_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and create_time <![CDATA[<=]]> #{query.endTime}
        </if>
    </select>

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

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

    <select id="findAdminIndentCount" resultType="int">
        select count(*) from tb_indent where rider_user_id = #{userId} and indent_state = "6"
        <if test="dateType=='day'">
            and date_format(finish_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(finish_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(finish_time,'%Y')=date_format(#{date},'%Y')
        </if>
    </select>

    <select id="selectIndentById" resultType="com.sqx.modules.errand.entity.TbIndent">
        select * from tb_indent where rider_user_id = #{userId} and indent_state = "6"
        <if test="dateType=='day'">
            and date_format(finish_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(finish_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(finish_time,'%Y')=date_format(#{date},'%Y')
        </if>
        order by create_time desc
    </select>

    <select id="findUserIndent" resultType="com.sqx.modules.errand.entity.TbIndent">
        select t.*,(select count(*) from errand_evaluate e where e.user_id=#{userId} and t.indent_number=e.indent_number) as evaluateMessage
        from tb_indent t  where t.user_id = #{userId} and t.indent_type != 5
        <if test="indentState==1">
            and t.indent_state in ("0", "2", "3", "4", "6", "8", "9", "10")
        </if>
        <if test="indentState==2">
            and t.indent_state = "0"
        </if>
        <if test="indentState==3">
            and t.indent_state = "2"
        </if>
        <if test="indentState==4">
            and t.indent_state = "3"
        </if>
        <if test="indentState==5">
            and t.indent_state = "4"
        </if>
        <if test="indentState==6">
            and t.indent_state = "6"
        </if>
        <if test="indentState==7">
            and t.indent_state in ("8", "9", "10")
        </if>
        order by t.create_time desc
    </select>

    <select id="indentMessage" resultType="com.sqx.modules.errand.entity.TbIndent">
        select i.*,
               r.red_packet_amount as redPacketAmount,
               u.nick_name         as riderNickName,
               u.phone             as phone,
               u.avatar            as avatar
        from tb_indent i
                 left join tb_user u on i.rider_user_id = u.user_id
                 left join errand_user_red_packet r on i.red_packet_id = r.red_packet_id
        where indent_number = #{indentNumber}
    </select>

    <select id="find5KmRider" resultType="com.sqx.modules.app.entity.UserEntity">
        SELECT *
        FROM tb_user
        WHERE (st_distance(point(station_lng, station_lat), point(#{lng}, #{lat})) * 111195) &lt; 50000
          and user_type = "2"
          and on_line_flag = "1"
    </select>

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

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

    <select id="findAllRedPacket" resultType="com.sqx.modules.errand.entity.ErrandRedPacket">
        select *
        from errand_user_red_packet
        where user_id = #{userId}
          and delete_flag = "0"
          and expiration_time &gt; #{expirationTime}
    </select>

    <!--获取可用红包-->
    <select id="findRedPacket" resultType="com.sqx.modules.errand.entity.ErrandRedPacket">
        select *
        from errand_user_red_packet
        where user_id = #{userId}
          and (red_packet_type = "0" or red_packet_type = #{indentType})
          and minimum_amount &lt;= #{indentBasicsMoney}
          and expiration_time &gt; #{expirationTime}
          and delete_flag = "0"
        order by create_time desc
    </select>

    <update id="userCancleIndent">
        update tb_indent
        set indent_state       = "8",
            cancel_indent_time = #{cancelIndentTime}
        where user_id = #{userId}
          and indent_number = #{indentNumber}
    </update>

    <update id="updateRedPaclet">
        update errand_user_red_packet
        set delete_flag = "0"
        where red_packet_id = #{redPacketId}
    </update>

    <select id="selectIndentList" resultType="com.sqx.modules.errand.entity.TbIndent">
        select *
        from tb_indent
        where rider_user_id = #{userId}
          and indent_state in (3, 4)
    </select>

    <select id="selectSendOrder" resultType="com.sqx.modules.errand.entity.TbIndent">
        select *
        from tb_indent
        where indent_state = 2
          and indent_type in (1, 2, 3, 5)
          and create_time &lt; #{date}
    </select>

    <select id="selectRiderByDictance" resultType="Long">
        select user_id
        from (
                 SELECT t.user_id,
                 (select count(*) from tb_indent i
                 where i.rider_user_id=t.user_id and i.indent_state in (3,4)) as ordersCount ,
                        (st_distance(point(t.station_lat, t.station_lng), point(#{userLat}, #{userLng})) *
                         111195) AS distance
                 FROM tb_user t
                 WHERE t.on_line_flag = 1
                   and t.cash_deposit &gt;= #{cashDeposit}
                   and user_id not in (select rider_user_id from tb_indent where indent_state in (3, 4))
             ) a
        where distance &lt; #{distance} and ordersCount &lt; #{maxNum}
        order by distance asc limit 1
    </select>

    <!--
        现在根据订单地址，查出了距离该位置最近的骑手
        但是还要再加一个条件：要查出距离该订单最近的，没有进行中的订单的骑手
        判断骑手是否有订单在进行中：indent_state in (3, 4)
        select * from (
SELECT
	t.user_id,
	ti.indent_id,
	(st_distance (point (t.station_lat, t.station_lng),point (34.16351, 108.65161)) * 111195) AS distance
FROM
	tb_user t
LEFT JOIN tb_indent ti ON t.user_id = ti.rider_user_id
WHERE
 ti.indent_state NOT IN (3, 4) and t.on_line_flag = 1
) a where distance<100000 order by distance asc limit 1

====================================

select tu.user_id, (st_distance (point (station_lat, station_lng), point(#{userLat},#{userLng}) ) *111195) as distance
        from tb_user tu where ((st_distance (point (station_lat, station_lng), point(#{userLat},#{userLng}) ) *111195)) &lt; #{distance}
        order by distance asc limit 1
    -->

</mapper>
