<?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.orders.dao.OrdersDao">

    <select id="selectOrdersList" resultType="com.sqx.modules.orders.entity.Orders">
        select o.*,u.nick_name as userName,u.phone,
               d.driver_name as driverName,ud.phone as driverNamePhone,
        (select count(*) from message_info m where m.user_id=#{driverUserId} and m.state=11 and m.platform=o.orders_id) as browCount,
        (st_distance(POINT(o.ship_address_longitude, o.ship_address_latitude),point(#{longitude}, #{latitude})) *111195) AS riderDistance
        from orders o
        left join tb_user u on u.user_id=o.user_id
        left join driver d on d.driver_id=o.driver_id
        left join tb_user ud on d.user_id=ud.user_id
        where 1=1
        <if test="appointmentType!=null">
            and o.appointment_type=#{appointmentType}
        </if>
        <if test="driverRouteIds!=null">
            and o.driver_route_id=#{driverRouteIds}
        </if>
        <if test="appointmentNum!=null and appointmentNum!=3">
            and o.appointment_num=#{appointmentNum}
        </if>
        <if test="appointmentNum!=null and appointmentNum==3">
            and o.appointment_num>=#{appointmentNum}
        </if>
        <if test="status!=null">
            and o.status=#{status}
        </if>
        <if test="shipProvince!=null and shipProvince!=''">
            and o.ship_province like concat('%',#{shipProvince},'%')
        </if>
        <if test="shipCity!=null and shipCity!=''">
            and o.ship_city like concat('%',#{shipCity},'%')
        </if>
        <if test="shipDistrict!=null and shipDistrict!=''">
            and o.ship_district like concat('%',#{shipDistrict},'%')
        </if>
        <if test="shipAddress!=null and shipAddress!=''">
            and o.ship_address like concat('%',#{shipAddress},'%')
        </if>
        <if test="deliveryProvince!=null and deliveryProvince!=''">
            and o.delivery_province like concat('%',#{deliveryProvince},'%')
        </if>
        <if test="deliveryCity!=null and deliveryCity!=''">
            and o.delivery_city like concat('%',#{deliveryCity},'%')
        </if>
        <if test="deliveryDistrict!=null and deliveryDistrict!=''">
            and o.delivery_district like concat('%',#{deliveryDistrict},'%')
        </if>
        <if test="deliveryAddress!=null and deliveryAddress!=''">
            and o.delivery_address like concat('%',#{deliveryAddress},'%')
        </if>
        <if test="userName!=null and userName!=''">
            and u.nick_name like concat('%',#{userName},'%')
        </if>
        <if test="phone!=null and phone!=''">
            and u.phone like concat('%',#{phone},'%')
        </if>
        <if test="shipUserName!=null and shipUserName!=''">
            and o.ship_user_name like concat('%',#{shipUserName},'%')
        </if>
        <if test="shipUserPhone!=null and shipUserPhone!=''">
            and o.ship_user_phone like concat('%',#{shipUserPhone},'%')
        </if>
        <if test="driverName!=null and driverName!=''">
            and d.driver_name like concat('%',#{driverName},'%')
        </if>
        <if test="driverPhone!=null and driverPhone!=''">
            and ud.phone like concat('%',#{driverPhone},'%')
        </if>
        <if test="orderType!=null and orderType==1">
            and o.ship_city=o.delivery_city
        </if>
        <if test="orderType!=null and orderType==2">
            and o.ship_city!=o.delivery_city
        </if>
        <if test="driverId!=null">
            and o.driver_id = #{driverId}
        </if>
        <if test="ordersNo!=null and ordersNo!=''">
            and o.orders_no like concat('%',#{ordersNo},'%')
        </if>
        <if test="isFixedStroke!=null and isFixedStroke==1">
            and o.is_fixed_stroke =#{isFixedStroke}
        </if>
        <if test="isFixedStroke!=null and isFixedStroke==2">
            and o.is_fixed_stroke is null
        </if>
        <if test="sort==null">
            order by  o.create_time desc
        </if>
        <if test="sort!=null and sort==1">
            order by  o.create_time desc
        </if>
        <if test="sort!=null and sort==2">
            order by  o.pay_money desc
        </if>
        <if test="sort!=null and sort==3">
            order by  riderDistance asc
        </if>
    </select>

    <select id="selectTeamMoney" resultType="Double">
        select
            <if test="type==1">
                ifnull(sum(one_user_money),0.00)+ifnull(sum(one_driver_user_money),0.00)
            </if>
            <if test="type==1">
                ifnull(sum(two_user_money),0.00)+ifnull(sum(two_driver_user_money),0.00)
            </if>
        from orders
        where status=5
        <if test="type==1">
            and (one_user_id=#{userId} or one_driver_user_id=#{userId})
        </if>
        <if test="type==1">
            and (two_user_id=#{userId} or two_driver_user_id=#{userId})
        </if>
    </select>

    <select id="selectMyOrdersCount" resultType="Integer">
        select count(*) from orders
        where user_id=#{userId}
        and status in (2,3,4,5)
        <if test="date!=null">
            and date_format(create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
    </select>

    <select id="selectTakeOrdersCount" resultType="Integer">
        select count(*) from orders o
        left join driver d on d.driver_id=o.driver_id
        where d.user_id=#{userId}
        and o.status in (2,3,4,5)
        <if test="date!=null">
            and date_format(o.create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
    </select>

    <select id="selectOrdersDriverCount" resultType="Integer">
        select count(*) from orders o
        left join driver d on d.driver_id=o.driver_id
        where d.user_id=#{userId}
        and o.status=5 and date_format(o.create_time,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d')
    </select>

    <select id="selectOrdersDriverMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(drive_money), 0) from orders o
        left join driver d on d.driver_id=o.driver_id
        where d.user_id=#{userId}
        and o.status=5
    </select>

    <select id="selectOrdersPingMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(ping_money), 0)
        from orders
        where status = 5
        <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>
    </select>

    <select id="selectOrdersCount" resultType="Integer">
        select count(*)
        from orders
        where status in (3,4,5)
        <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>
    </select>

    <select id="selectOrderByStatus" resultType="Integer">
        select count(*)
        from orders
        where 1=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="status!=null">
            and status=#{status}
        </if>
    </select>

    <select id="selectTeamUserList" resultType="Map">
        select * from (
        select u.user_id as userId,u.nick_name as userName,u.avatar,u.create_time as createTime,
        <if test="type==1">
            (select ifnull(sum(o.one_user_money),0.00)+ifnull(sum(o.one_driver_user_money),0.00) from orders o where
            (o.user_id = u.user_id or o.driver_user_id = u.user_id) and status=5 and (one_user_id=#{userId} or one_driver_user_id=#{userId}) )+
            (select ifnull(sum(money) from user_money_details s where s.relation_id=u.user_id where s.user_id=#{userId} and classify=8 and user_level=1))
            as money
        </if>
        <if test="type==2">
            (select ifnull(sum(o.two_user_money),0.00)+ifnull(sum(o.two_driver_user_money),0.00) from orders o where
            (o.user_id = u.user_id or o.driver_user_id = u.user_id) and status=5 and (two_user_id=#{userId} or two_driver_user_id=#{userId}) )+
            (select ifnull(sum(money) from user_money_details s where s.relation_id=u.user_id where s.user_id=#{userId} and classify=8 and user_level=2))
             as money
        </if>
        from tb_user u
        where
        <if test="type==1">
            inviter_code=#{invitationCode}
        </if>
        <if test="type==2">
            inviter_code in (select invitation_code from tb_user where inviter_code=#{invitationCode})
        </if>
        ) a order by money desc
    </select>

    <select id="selectOrdersMoneyCountByUserId" resultType="Double">
        select
        <if test="type==1">
            ifnull(sum(one_user_money),0.00)+ifnull(sum(one_driver_user_money),0.00)+
            (select ifnull(sum(money),0.00) from user_money_details s where s.user_id=#{userId} and classify=8 and user_level=1)
        </if>
        <if test="type==2">
            ifnull(sum(two_user_money),0.00)+ifnull(sum(two_driver_user_money),0.00)+
            (select ifnull(sum(money),0.00) from user_money_details s where s.user_id=#{userId} and classify=8 and user_level=2)
        </if>
        from orders
        where status=5
        <if test="type==1">
            and (one_user_id=#{userId} or one_driver_user_id=#{userId})
        </if>
        <if test="type==2">
            and (two_user_id=#{userId} or two_driver_user_id=#{userId})
        </if>
    </select>

    <select id="selectUserCountByInvitationCode" resultType="Integer">
        select count(*) from tb_user
        where
        <if test="type==1">
            inviter_code=#{invitationCode}
        </if>
        <if test="type==2">
            inviter_code in (select invitation_code from tb_user where inviter_code=#{invitationCode})
        </if>
    </select>

    <select id="sumOrdersPayMoneyByTime" resultType="java.math.BigDecimal">
        select ifnull(sum(pay_money),0) from orders where status in (2,3,4,5)
        <if test="dateType==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType==2">
            and date_format(create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType==3">
            and date_format(create_time,'%Y')=date_format(#{date},'%Y')
        </if>
    </select>

    <select id="sumOrdersDriveMoneyByTime" resultType="java.math.BigDecimal">
        select ifnull(sum(drive_money),0) from orders where status in (2,3,4,5)
        <if test="dateType==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType==2">
            and date_format(create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType==3">
            and date_format(create_time,'%Y')=date_format(#{date},'%Y')
        </if>
    </select>

    <select id="sumOrdersUserMoneyByTime" resultType="java.math.BigDecimal">
        select
            <if test="type==1">
                ifnull(sum(one_user_money),0)+ifnull(sum(one_driver_user_money),0.00)
            </if>
            <if test="type==2">
                ifnull(sum(two_user_money),0)+ifnull(sum(two_driver_user_money),0.00)
            </if>
             from orders where status in (2,3,4,5)
        <if test="dateType==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType==2">
            and date_format(create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType==3">
            and date_format(create_time,'%Y')=date_format(#{date},'%Y')
        </if>
    </select>

    <select id="sumOrdersPingMoneyByTime" resultType="java.math.BigDecimal">
        select ifnull(sum(drive_money),0) from orders where status in (2,3,4,5)
        <if test="dateType==1">
            and date_format(create_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType==2">
            and date_format(create_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType==3">
            and date_format(create_time,'%Y')=date_format(#{date},'%Y')
        </if>
    </select>

    <select id="selectOrdersByStartTime" resultType="com.sqx.modules.orders.entity.Orders">
        select * from orders where status in (1,2) and DATE_FORMAT(STR_TO_DATE(appointment_end_time, '%m月%d日 %H:%i'), '%m-%d %H:%i:%s')&lt;=date_format(now(),'%m-%d %h:%i:%s')
    </select>

    <select id="selectOrdersCountStatisticsByYear" resultType="Integer">
        select count(*) from orders
        where 1=1
        <if test="status!=null and status!=0">
            and status=#{status}
        </if>
        <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>
    </select>

    <select id="selectOrdersListByInvoice" resultType="com.sqx.modules.orders.entity.Orders">
        SELECT
        i.*
        FROM
        orders i
        WHERE
        (SELECT COUNT(*) FROM
        invoice v
        WHERE FIND_IN_SET(i.orders_no,v.indent_number)) = 0
        AND i.status = 5
        AND i.user_id = #{userId}
        <if test="ordersNo!=null and ordersNo!=''">
            and i.orders_no like concat("%",#{ordersNo},"%")
        </if>
        <if test="startTime!=null and startTime!='' and endTime !=null and endTime!=''">
            and i.create_time &gt; #{startTime} and i.create_time &lt; #{endTime}
        </if>
        order by i.create_time desc
    </select>



</mapper>