<?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.order.dao.AppOrderDao">

    <select id="selectOrderList" resultType="com.sqx.modules.order.entity.TbOrder">
        select tor.*, gs.shop_name as shopName, gs.detailed_address as detailedAddress, gs.shop_lng as shopLng,
        gs.shop_lat as shopLat, gs.phone as shopPhone,
        gs.errand_money as errandMoney, gs.exempt_min_money as exemptMinMoney, gs.minimum_delivery as minimumDelivery,
        gs.distribution_distance as distributionDistance
        from tb_order tor left join goods_shop gs on tor.shop_id = gs.shop_id
        where tor.user_id = #{userId}
        <if test="orderType!=null">
            and tor.order_type = #{orderType}
        </if>
        <if test="shopId!=null">
            and tor.shop_id = #{shopId}
        </if>
        <if test="status!=null">
            <if test="status == 0">
                and tor.status = 0
            </if>
            <if test="status == 1">
                and tor.status = 1
            </if>
            <if test="status == 2">
                and tor.status = 2
            </if>
            <if test="status == 3">
                and tor.status in (7, 6, 3, 4, 5, 8)
            </if>
        </if>
    </select>

    <insert id="insertOrder" parameterType="com.sqx.modules.order.entity.TbOrder" useGeneratedKeys="true"
            keyProperty="orderId">
        insert into tb_order (coupon_id, user_id, user_name, phone, address, order_number, is_pay, pay_time,
                              order_code, order_type, pack_money, errand_money, status, delete_flag, parent_user_id,
                              shop_id, parent_id, create_time)
        values (#{couponId}, #{userId}, #{userName}, #{phone}, #{address}, #{orderNumber}, #{isPay}, #{payTime},
                #{orderCode}, #{orderType}, #{packMoney}, #{errandMoney}, #{status}, #{deleteFlag}, #{parentUserId},
                #{shopId}, #{parentId}, #{createTime})
    </insert>

    <select id="selectOrder" resultType="com.sqx.modules.order.entity.TbOrder">
        select tor.*, tu.avatar as avatar, gs.shop_name as shopName, gs.detailed_address as detailedAddress, gs.phone as
        shopPhone,
        ti.rider_user_id as riderUserId, tcu.money as couponMoney
        from tb_order tor
        left join tb_user tu on tor.user_id = tu.user_id
        left join goods_shop gs on tor.shop_id = gs.shop_id
        left join tb_indent ti on tor.order_id = ti.order_id
        left join tb_coupon_user tcu on tor.coupon_id = tcu.id
        where 1 = 1 and tor.is_pay = 1
        <if test="shopName!=null and shopName!=''">
            and gs.shop_name like concat('%',#{shopName},'%')
        </if>
        <if test="userName!=null and userName!=''">
            and tor.user_name like concat('%',#{userName},'%')
        </if>
        <if test="phone!=null and phone!=''">
            and tor.phone like concat('%',#{phone},'%')
        </if>
        <if test="orderNumber!=null and orderNumber!=''">
            and tor.order_number = #{orderNumber}
        </if>
        <if test="status!=null and status!=5">
            and tor.status = #{status}
        </if>
        <if test="status!=null and status==5">
            and tor.status in (5, 8)
        </if>
        <if test="status==null">
            and tor.status in (7, 6, 3, 4, 5, 8)
        </if>
        <if test="shopId!=null">
            and tor.shop_id = #{shopId}
        </if>
        <if test="orderType!=null">
            and tor.order_type = #{orderType}
        </if>
        order by tor.pay_time desc, tor.create_time desc
    </select>

    <select id="selectAllOrderAdmin" resultType="com.sqx.modules.order.entity.TbOrder">
        select tor.*, tu.avatar as avatar, gs.shop_name as shopName, gs.detailed_address as detailedAddress, gs.phone as
        shopPhone,
        tiu.user_name as riderNickName,tiu.phone as riderPhone,ti.indent_id as indentId,ti.is_rider as isRider,
        ti.rider_user_id as riderUserId, tcu.money as couponMoney,
        apr.discount_amount as activityDiscountAmount, ai.title activityTitle,tiu.rider_station_id as riderStationId,
        (select rs.station_name from rider_station rs where  rs.id =tiu.rider_station_id ) as stationName,
        (select st.shop_type_name from shop_type st where  st.id =gs.shop_type_id ) as shopTypeName
        from tb_order tor
        left join tb_user tu on tor.user_id = tu.user_id
        left join goods_shop gs on tor.shop_id = gs.shop_id
        left join tb_indent ti on tor.order_id = ti.order_id
        left join tb_user tiu on tiu.user_id = ti.rider_user_id
        left join tb_coupon_user tcu on tor.coupon_id = tcu.id
        left join activity_part_record apr on apr.order_id = tor.order_id
        left join activity ai on ai.id = apr.activity_id
        where 1 = 1
        <if test="query.riderPhone!=null and query.riderPhone!=''">
            and tiu.phone =#{query.riderPhone}
        </if>
        <if test="query.shopName!=null and query.shopName!=''">
            and gs.shop_name like concat('%',#{query.shopName},'%')
        </if>
        <if test="query.userName!=null and query.userName!=''">
            and tor.user_name like concat('%',#{query.userName},'%')
        </if>
        <if test="query.phone!=null and query.phone!=''">
            and tor.phone like concat('%',#{query.phone},'%')
        </if>
        <if test="query.orderNumber!=null and query.orderNumber!=''">
            and tor.order_number = #{query.orderNumber}
        </if>
        <if test="query.status!=null and query.status!=-1 and query.status!=1">
            and tor.status = #{query.status}
        </if>
        <if test="query.status!=null and query.status==1">
            and tor.status in (1,2)
        </if>
        <if test="query.shopId!=null">
            and tor.shop_id = #{query.shopId}
        </if>
        <if test="query.orderType!=null">
            and tor.order_type_extra = #{query.orderType}
        </if>
        <if test="query.indentStatus != null and query.indentStatus != ''">
            and ti.indent_state = #{query.indentStatus}
        </if>
        <if test="query.reservationFlag != null and query.reservationFlag !=''">
            and tor.reservation_flag = #{query.reservationFlag}
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and date_format(tor.create_time,'%Y-%m-%d') >= date_format(#{query.startTime},'%Y-%m-%d')
        </if>
        <if test="query.endTime!=null and query.endTime!='' ">
            and date_format(tor.create_time,'%Y-%m-%d') &lt;= date_format(#{query.endTime},'%Y-%m-%d')
        </if>
        <if test="query.payStartTime != null and query.payStartTime != ''">
            and date_format(tor.pay_time,'%Y-%m-%d') >= date_format(#{query.payStartTime},'%Y-%m-%d')
        </if>
        <if test="query.payEndTime != null and query.payEndTime != '' ">
            and date_format(tor.pay_time,'%Y-%m-%d') &lt;= date_format(#{query.payEndTime},'%Y-%m-%d')
        </if>
        <if test="query.riderStationId!=null and query.riderStationId!=''">
            and tiu.rider_station_id=#{query.riderStationId}
        </if>
        <if test="query.shopTypeId!=null and query.shopTypeId!=''">
            and gs.shop_type_id=#{query.shopTypeId}
        </if>
        order by tor.pay_time desc, tor.create_time desc
    </select>

    <select id="excelAllOrderAdmin" resultType="com.sqx.modules.order.entity.TbOrder">
        select tor.*, tu.avatar as avatar, gs.shop_name as shopName, gs.detailed_address as detailedAddress, gs.phone as
        shopPhone,
        tiu.nick_name as riderNickName,tiu.phone as riderPhone,ti.indent_id as indentId,ti.is_rider as isRider,
        ti.rider_user_id as riderUserId, tcu.money as couponMoney,
        apr.discount_amount as activityDiscountAmount, ai.title activityTitle,ogg.detail,ogg.sumPrice,
        (select rs.station_name from rider_station rs where  rs.id =tiu.rider_station_id ) as stationName
        from tb_order tor
        left join tb_user tu on tor.user_id = tu.user_id
        left join goods_shop gs on tor.shop_id = gs.shop_id
        left join tb_indent ti on tor.order_id = ti.order_id
        left join tb_user tiu on tiu.user_id = ti.rider_user_id
        left join tb_coupon_user tcu on tor.coupon_id = tcu.id
        left join activity_part_record apr on apr.order_id = tor.order_id
        left join activity ai on ai.id = apr.activity_id
        left join (  select @a:=0,order_id ,group_concat(@a:=@a+1,".商品名：",goods_name,",数量：",goods_num,",规格：",sku_message) detail,sum(goods_num*goods_price) sumPrice  from order_goods og group by order_id
        ) ogg on ogg.order_id =tor.order_id
        where 1 = 1
        <if test="riderPhone!=null and riderPhone!=''">
            and tiu.phone =#{riderPhone}
        </if>
        <if test="shopName!=null and shopName!=''">
            and gs.shop_name like concat('%',#{shopName},'%')
        </if>
        <if test="userName!=null and userName!=''">
            and tor.user_name like concat('%',#{userName},'%')
        </if>
        <if test="phone!=null and phone!=''">
            and tor.phone like concat('%',#{phone},'%')
        </if>
        <if test="orderNumber!=null and orderNumber!=''">
            and tor.order_number = #{orderNumber}
        </if>
        <if test="status!=null and status!=-1 and status!=1">
            and tor.status = #{status}
        </if>
        <if test="status!=null and status==1">
            and tor.status in (1,2)
        </if>
        <if test="shopId!=null">
            and tor.shop_id = #{shopId}
        </if>
        <if test="orderType!=null">
            and tor.order_type_extra = #{orderType}
        </if>
        <if test="startTime!=null and startTime!=''">
            and date_format(tor.create_time,'%Y-%m-%d') >= date_format(#{startTime},'%Y-%m-%d')
        </if>
        <if test="endTime!=null and endTime!='' ">
            and date_format(tor.create_time,'%Y-%m-%d') &lt;= date_format(#{endTime},'%Y-%m-%d')
        </if>
        <if test="payStartTime != null and payStartTime != ''">
            and date_format(tor.pay_time,'%Y-%m-%d') >= date_format(#{payStartTime},'%Y-%m-%d')
        </if>
        <if test="payEndTime != null and payEndTime != '' ">
            and date_format(tor.pay_time,'%Y-%m-%d') &lt;= date_format(#{payEndTime},'%Y-%m-%d')
        </if>
        <if test="riderStationId!=null and riderStationId!=''">
            and tiu.rider_station_id=#{riderStationId}
        </if>
        order by tor.pay_time desc, tor.create_time desc
    </select>

    <select id="excelAllOrderAdminCount" resultType="java.lang.Integer">
        select count(1) from tb_order tor
        left join tb_user tu on tor.user_id = tu.user_id
        left join goods_shop gs on tor.shop_id = gs.shop_id
        left join tb_indent ti on tor.order_id = ti.order_id
        left join tb_user tiu on tiu.user_id = ti.rider_user_id
        where 1 = 1
        <if test="riderPhone!=null and riderPhone!=''">
            and tiu.phone =#{riderPhone}
        </if>
        <if test="shopName!=null and shopName!=''">
            and gs.shop_name like concat('%',#{shopName},'%')
        </if>
        <if test="userName!=null and userName!=''">
            and tor.user_name like concat('%',#{userName},'%')
        </if>
        <if test="phone!=null and phone!=''">
            and tor.phone like concat('%',#{phone},'%')
        </if>
        <if test="orderNumber!=null and orderNumber!=''">
            and tor.order_number = #{orderNumber}
        </if>
        <if test="status!=null and status!=-1 and status!=1">
            and tor.status = #{status}
        </if>
        <if test="status!=null and status==1">
            and tor.status in (1,2)
        </if>
        <if test="shopId!=null">
            and tor.shop_id = #{shopId}
        </if>
        <if test="orderType!=null">
            and tor.order_type_extra = #{orderType}
        </if>
        <if test="startTime!=null and startTime!=''">
            and date_format(tor.create_time,'%Y-%m-%d') >= date_format(#{startTime},'%Y-%m-%d')
        </if>
        <if test="endTime!=null and endTime!='' ">
            and date_format(tor.create_time,'%Y-%m-%d') &lt;= date_format(#{endTime},'%Y-%m-%d')
        </if>
        <if test="payStartTime != null and payStartTime != ''">
            and date_format(tor.pay_time,'%Y-%m-%d') >= date_format(#{payStartTime},'%Y-%m-%d')
        </if>
        <if test="payEndTime != null and payEndTime != '' ">
            and date_format(tor.pay_time,'%Y-%m-%d') &lt;= date_format(#{payEndTime},'%Y-%m-%d')
        </if>
        <if test="riderStationId!=null and riderStationId!=''">
            and tiu.rider_station_id=#{riderStationId}
        </if>
        order by tor.pay_time desc, tor.create_time desc
    </select>

    <select id="selectOrderByAdmin" resultType="com.sqx.modules.order.entity.TbOrder">
        select tor.*, tu.avatar as avatar, gs.shop_name as shopName from sys_user_shop sus
        left join tb_order tor on sus.shop_id = tor.shop_id
        left join tb_user tu on tor.user_id = tu.user_id
        left join goods_shop gs on tor.shop_id = gs.shop_id
        where 1 = 1 and sus.user_id = #{userId}
        and tor.is_pay = 1
        <if test="userName!=null and userName!=''">
            and tor.user_name like concat('%',#{userName},'%')
        </if>
        <if test="phone!=null and phone!=''">
            and tor.phone like concat('%',#{phone},'%')
        </if>
        <if test="orderNumber!=null and orderNumber!=''">
            and tor.order_number = #{orderNumber}
        </if>
        <if test="status!=null">
            and tor.status = #{status}
        </if>
        <if test="shopId!=null">
            and tor.shop_id = #{shopId}
        </if>
        <if test="orderType!=null">
            and tor.order_type = #{orderType}
        </if>
        order by tor.pay_time desc, tor.create_time desc
    </select>

    <select id="selectCountOrderByUserId" resultType="int">
        select count(*) from tb_order where user_id = #{userId}
        <if test="dateType=='day'">
            and date_format(pay_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(pay_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(pay_time,'%Y')=date_format(#{date},'%Y')
        </if>
    </select>

    <select id="selectSunMoneyByUserId" resultType="java.math.BigDecimal">
        select ifnull(sum(pay_money), 0) from tb_order where user_id = #{userId}
        <if test="dateType=='day'">
            and date_format(pay_time,'%Y-%m-%d')=date_format(#{date},'%Y-%m-%d')
        </if>
        <if test="dateType=='month'">
            and date_format(pay_time,'%Y-%m')=date_format(#{date},'%Y-%m')
        </if>
        <if test="dateType=='year'">
            and date_format(pay_time,'%Y')=date_format(#{date},'%Y')
        </if>
    </select>

    <select id="selectOrderDetails" resultType="com.sqx.modules.order.entity.TbOrder">
        select tor.*,
               gs.shop_name        as shopName,
               gs.detailed_address as detailedAddress,
               gs.phone            as shopPhone,
               tu.nick_name        as riderNickName,
               tu.phone            as riderPhone
        from tb_order tor
                 left join goods_shop gs on tor.shop_id = gs.shop_id
                 left join tb_indent ti on tor.order_id = ti.order_id
                 left join tb_user tu on ti.rider_user_id = tu.user_id
        where tor.user_id = #{userId}
          and tor.status in (7, 6, 3, 4, 5, 8)
        order by pay_time desc
    </select>

    <select id="waitTakeFood" resultType="com.sqx.modules.order.entity.TbOrder">
        select tor.*, tu.avatar as avatar, ti.rider_user_id as riderUserId, gs.shop_name as shopName, gs.shop_cover as
        shopCover, gs.detailed_address as detailedAddress,tu1.phone as riderPhone,gs.phone as shopPhone,ti.indent_state as indentState,
        (select count(*) from tb_order where order_type = 1 and status in (6, 3) and pay_time &lt; tor.pay_time
        and shop_id = tor.shop_id) as countOrder
        from tb_order tor
        left join tb_user tu on tor.user_id = tu.user_id
        left join tb_indent ti on tor.order_id = ti.order_id
        left join goods_shop gs on tor.shop_id = gs.shop_id
        left join tb_user tu1 on ti.rider_user_id = tu1.user_id
        where tor.user_id = #{userId}
        and order_type = #{orderType}
        <if test="status!=null and status!=5">
            and tor.status = #{status}
        </if>
        <if test="status!=null and status==5">
            and tor.status in (5, 8)
        </if>
        <if test="status==null">
            and tor.status in (0, 7, 6, 3, 4, 5, 8)
        </if>
        order by tor.create_time desc
    </select>


    <select id="selectCountOrderByTime" resultType="int">
        select count(*)
        from tb_order
        where order_type = 1
          and status = 3
          and pay_time &lt; #{payTime}
          and shop_id = #{shopId}
    </select>

    <update id="deleteCouponByOrderId">
        update tb_order
        set coupon_id = null
        where order_id = #{orderId}
    </update>

    <select id="selectSumMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(pay_money), 0) from tb_order where is_pay=1 and status in (0,3,4,7,6)
        <if test="query.shopId!=null">
            and shop_id=#{query.shopId}
        </if>
        <if test="query.dateType=='day'">
            and date_format(pay_time,'%Y-%m-%d')=date_format(#{query.date},'%Y-%m-%d')
        </if>
        <if test="query.dateType=='month'">
            and date_format(pay_time,'%Y-%m')=date_format(#{query.date},'%Y-%m')
        </if>
        <if test="query.dateType=='year'">
            and date_format(pay_time,'%Y')=date_format(#{query.date},'%Y')
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and pay_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and pay_time <![CDATA[<=]]> #{query.endTime}
        </if>
    </select>

    <select id="selectCountOrder" resultType="int">
        select count(*) from tb_order where is_pay=1 and status in (0,3,4,7,6)
        <if test="query.shopId!=null">
            and shop_id=#{query.shopId}
        </if>
        <if test="query.dateType=='day'">
            and date_format(pay_time,'%Y-%m-%d')=date_format(#{query.date},'%Y-%m-%d')
        </if>
        <if test="query.dateType=='month'">
            and date_format(pay_time,'%Y-%m')=date_format(#{query.date},'%Y-%m')
        </if>
        <if test="query.dateType=='year'">
            and date_format(pay_time,'%Y')=date_format(#{query.date},'%Y')
        </if>
        <if test="query.startTime!=null and query.startTime!=''">
            and pay_time>=#{query.startTime}
        </if>
        <if test="query.endTime!=null and query.endTime!=''">
            and pay_time <![CDATA[<=]]> #{query.endTime}
        </if>
    </select>

    <update id="updateorderStatus">
        update tb_order
        set status = 4
        where update_time &lt; #{date}
          and status = 3
    </update>

    <select id="selectOrderByTimeList" resultType="com.sqx.modules.order.entity.TbOrder">
        select *
        from tb_order
        where update_time &lt; #{date}
          and status = 3
    </select>

    <select id="selectShoppingTrolley" resultType="com.sqx.modules.order.entity.TbOrder">
        select t.*, gs.shop_name as shopName
        from tb_order t
                 left join goods_shop gs on t.shop_id = gs.shop_id
        where t.status = 1
          and t.user_id = #{userId}
        order by t.add_goods_time desc
    </select>

    <select id="selectShoppingTrolleyByShopId" resultType="com.sqx.modules.order.entity.TbOrder">
        select t.*, gs.shop_name as shopName
        from tb_order t
                 left join goods_shop gs on t.shop_id = gs.shop_id
        where t.status = 1
          and t.user_id = #{userId}
          and t.shop_id = #{shopId}
        order by t.add_goods_time desc
    </select>

    <select id="selectByOrderId" resultType="com.sqx.modules.order.entity.TbOrder">
        select tor.*,
               ti.indent_id         as indentId,
               ti.indent_number     as indentNumber,
               ti.rider_user_id     as riderUserId,
               ti.indent_state     as indentState,
               tu.nick_name         as riderNickName,
               tu.avatar            as riderAvatar,
               tu.phone             as riderPhone,
               e.evaluate_message   as evaluateMessage,
               e.shop_reply_message as shopReplyMessage,
               e.score              as score,
               e.create_time        as eCreateTime,
               tus.avatar           as avatar,
               tus.nick_name        as userNickName
        from tb_order tor
                 left join tb_indent ti on tor.order_id = ti.order_id
                 left join tb_user tu on ti.rider_user_id = tu.user_id
                 left join tb_user tus on ti.user_id = tus.user_id
                 left join evaluate e on tor.order_number = e.order_number
        where tor.order_id = #{orderId}
    </select>

    <select id="selectBuyGoods" resultType="com.sqx.modules.order.entity.TbOrder">
        select tor.*,
               gs.shop_name             as shopName,
               gs.detailed_address      as detailedAddress,
               gs.shop_lng              as shopLng,
               gs.shop_lat              as shopLat,
               gs.phone                 as shopPhone,
               gs.errand_money          as errandMoney,
               gs.exempt_min_money      as exemptMinMoney,
               gs.minimum_delivery      as minimumDelivery,
               gs.distribution_distance as distributionDistance
        from tb_order tor
                 left join goods_shop gs on tor.shop_id = gs.shop_id
        where tor.order_id = #{orderId}
          and tor.user_id = #{userId}
    </select>

    <select id="selectOverTimeOrder" resultType="com.sqx.modules.order.entity.TbOrder">
        select *
        from tb_order
        where status = 7
          and pay_time &lt; #{overTime}
    </select>

    <select id="selectMakeOrdersList" resultType="com.sqx.modules.order.entity.TbOrder">
        select * from tb_order where status=6 and shop_receiving_time <![CDATA[ <= #{minusMinutes}]]></select>

    <select id="selectCurrentOrderSequenceByShopId" resultType="java.lang.Long">
        select
               order_id
        from
             tb_order
        where
              is_pay = 1
          and shop_id = #{shopId}
          and date(pay_time) = date(#{payTime})
        order by pay_time asc
    </select>

    <select id="countByShopIdAndActivityIdAndUserIdAndCurDate" resultType="java.lang.Integer">
        select
            count(1)
        from
             tb_order o
             left join activity_part_record apr on apr.order_id = o.order_id
        where
            o.shop_id = #{shopId}
            and o.user_id = #{userId}
            and date(o.create_time) = curdate()
            and apr.activity_id = #{activityId}
            and o.is_pay = 1
            <if test="orderId != null">
                and o.order_id != #{orderId}
            </if>
    </select>

    <select id="selectCurWaitReceivingOrderIds" resultType="java.lang.Long">
        select
               order_id
        from
             tb_order
        where
              reservation_flag = '1'
                and `status` = '7'
                and expect_delivery_time <![CDATA[ <= ]]> adddate(now(),interval 30 minute)
    </select>

    <select id="countUnFinishByShopId" resultType="java.lang.Integer">
        select count(order_id)
        from tb_order
        where shop_id = #{shopId} and status in (6, 3)
    </select>

    <select id="countCurDayPayByShopId" resultType="java.lang.Integer">
        select
            count(order_id)
        from
            tb_order
        where
            is_pay = 1
          and shop_id = #{shopId}
          and date(pay_time) = date(#{payTime})
    </select>

</mapper>