<?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="org.springblade.modules.finance.mapper.OrderMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="orderResultMap" type="org.springblade.modules.finance.entity.Order">
        <result column="id" property="id"/>
        <result column="is_delete" property="isDelete"/>
        <result column="create_user_id" property="createUserId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user_id" property="updateUserId"/>
        <result column="update_time" property="updateTime"/>
        <result column="order_no" property="orderNo"/>
        <result column="order_type" property="orderType"/>
        <result column="quantity" property="quantity"/>
        <result column="order_state" property="orderState"/>
        <result column="total_amount" property="totalAmount"/>
        <result column="payment_amount" property="paymentAmount"/>
        <result column="payment_mode" property="paymentMode"/>
        <result column="payment_time" property="paymentTime"/>
        <result column="payment_state" property="paymentState"/>
        <result column="discount_amount" property="discountAmount"/>
        <result column="postage" property="postage"/>
        <result column="invoice_state" property="invoiceState"/>
        <result column="is_aftersales" property="isAftersales"/>
        <result column="send_time" property="sendTime"/>
        <result column="record_time" property="recordTime"/>
        <result column="logistics_state" property="logisticsState"/>
        <result column="consignee" property="consignee"/>
        <result column="consignee_phone_number" property="consigneePhoneNumber"/>
        <result column="is_cart" property="isCart"/>
        <result column="transaction_no" property="transactionNo"/>
    </resultMap>


    <select id="selectOrderPage" resultType="org.springblade.modules.finance.vo.OrderVO">
        SELECT
            o.*, u.`name` nickname
        FROM
            t_order o
        LEFT JOIN blade_user u ON u.id = o.user_id
        WHERE
            o.is_delete = 0
        <if test="@cn.hutool.core.util.StrUtil@isNotBlank(order.orderNo)">
            and instr(o.order_no, #{order.orderNo}) > 0
        </if>
        <if test="@cn.hutool.core.util.StrUtil@isNotBlank(order.nickname)">
            and instr(u.nickname, #{order.nickname}) > 0
        </if>
        <if test="order.orderType != null">
            and o.order_type = #{order.orderType}
        </if>
        <if test="order.orderState != null">
            and o.order_state = #{order.orderState}
        </if>
        <if test="order.source != null">
            and o.source = #{order.source}
        </if>
        <if test="order.startTime != null">
            and o.create_time <![CDATA[ >= ]]> #{order.startTime}
        </if>
        <if test="order.endTime != null">
            and o.create_time <![CDATA[ <= ]]> #{order.endTime}
        </if>
        order by o.create_time desc
    </select>
    <select id="getUserOrder" resultType="org.springblade.modules.api.response.UserOrderResponse">
        SELECT
            o.id orderId,
            o.order_no,
            o.order_state,
            o.create_time,
            g.groupon_start_time,
            g.groupon_end_time,
            g.take_start_time,
            g.take_end_time,
            g.main_title,
            g.sub_title,
            st.`name` selfTakeName,
            st.contact_person commander,
            st.contact_number phone,
            u.wx_qrcode qrCode,
            og.pickup_code,
            g.id grouponId,
            o.payment_amount paymentAmount,
            og.is_arrive,
            st.address,
            st.longitude,
            st.latitude
        FROM
            t_order o
        JOIN t_order_groupon og ON og.order_id = o.id
        JOIN t_groupon g ON g.id = og.groupon_id
        JOIN t_self_take st ON st.id = og.self_take_id
        LEFT JOIN blade_user u on u.phone = st.contact_number
        WHERE o.user_id = #{userId}
        <if test="orderState != 0">
            and o.order_state = #{orderState}
        </if>

        ORDER BY o.create_time desc
    </select>
    <select id="getUserOrderDetail" resultType="org.springblade.modules.api.response.UserOrderDetailResponse">
        SELECT
            o.id orderId,
            o.order_no,
            o.order_state,
            o.create_time,
            o.payment_mode,
            o.payment_amount,
            o.total_amount,
            o.payment_time,
            g.groupon_start_time,
            g.groupon_end_time,
            g.take_start_time,
            g.take_end_time,
            g.main_title,
            st.contact_person commander,
            st.contact_number phone,
            u.wx_qrcode qrCode,
            og.pickup_code,
            og.self_take_id selfTakeId,
            g.id grouponId,
            og.is_arrive,
            st.address,
            st.longitude,
            st.latitude
        FROM
            t_order o
            JOIN t_order_groupon og ON og.order_id = o.id
            JOIN t_groupon g ON g.id = og.groupon_id
            JOIN t_self_take st ON st.id = og.self_take_id
            LEFT JOIN blade_user u on u.phone = st.contact_number
        WHERE o.id = #{orderId}
    </select>
    <select id="getOrderByExpirationTime" resultType="org.springblade.modules.finance.entity.Order">
      SELECT
	    o.*
      FROM
        t_order o
        LEFT JOIN t_order_groupon og ON o.id = og.order_id
        LEFT JOIN t_groupon g ON og.groupon_id = g.id
      WHERE
	    o.order_state in (2) AND
        g.take_start_time <![CDATA[ < ]]> #{time}
        and og.is_arrive = 1
    </select>

    <select id="getUserAfterSalesOrder" resultType="org.springblade.modules.api.response.UserOrderResponse">
        SELECT
            o.id orderId,
            o.order_no,
            o.order_state,
            o.create_time,
            g.groupon_start_time,
            g.groupon_end_time,
            g.take_start_time,
            g.take_end_time,
            g.main_title,
            g.sub_title,
            st.`name` selfTakeName,
            st.contact_person commander,
            st.contact_number phone,
            u.wx_qrcode qrCode,
            og.pickup_code,
            g.id grouponId,
            o.payment_amount paymentAmount,
            og.is_arrive,
            st.address,
            st.longitude,
            st.latitude,
            aa.id afterSalesId,
            aa.type,
            aa.order_groupon_goods_id,
            aa.status afterSalesStatus
        FROM
            t_after_sales aa
            JOIN t_order o ON aa.order_id = o.id
            JOIN t_order_groupon og ON og.order_id = o.id
            JOIN t_groupon g ON g.id = og.groupon_id
            JOIN t_self_take st ON st.id = og.self_take_id
            LEFT JOIN blade_user u on u.phone = st.contact_number
        WHERE
            o.user_id = #{userId}
        ORDER BY aa.create_time desc
    </select>

</mapper>
