<?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.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
        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
        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 = #{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>
        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
        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
        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 = #{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>
        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,
        (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
        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 (7, 6, 3, 4, 5, 8)
        </if>
        order by pay_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="shopId!=null">
            and shop_id=#{shopId}
        </if>
        <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="selectCountOrder" resultType="int">
        select count(*) from tb_order where is_pay=1 and status in (0,3,4,7,6)
        <if test="shopId!=null">
            and shop_id=#{shopId}
        </if>
        <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>

    <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,
               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 now() >= DATE_ADD(shop_receiving_time, INTERVAL #{minute} MINUTE )
    </select>

</mapper>