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

    <!-- 通用查询映射结果 -->
    <resultMap id="orderGoodsResultMap" type="org.springblade.modules.finance.entity.OrderGoods">
        <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_id" property="orderId"/>
        <result column="goods_id" property="goodsId"/>
        <result column="goods_num" property="goodsNum"/>
        <result column="goods_name" property="goodsName"/>
        <result column="goods_price" property="goodsPrice"/>
        <result column="goods_image" property="goodsImage"/>
        <result column="goods_no" property="goodsNo"/>
        <result column="shop_id" property="shopId"/>
        <result column="store_name" property="storeName"/>
        <result column="device_id" property="deviceId"/>
        <result column="merchant_id" property="merchantId"/>
        <result column="user_id" property="userId"/>
        <result column="cargo_way_id" property="cargoWayId"/>
        <result column="shipping_time" property="shippingTime"/>
    </resultMap>


    <select id="selectOrderGoodsPage" resultType="org.springblade.modules.finance.vo.OrderGoodsVO">
        SELECT
            og.*,
            o.order_no,
            o.order_state,
            o.is_aftersales,
            u.`name` nickname,
            o.payment_mode,
            o.payment_time,
            o.payment_state,
            o.payment_amount,
            o.total_amount,
            o.discount_amount,
            o.third_order_no
        FROM
            t_order o
            JOIN t_order_goods og ON o.id = og.order_id
            JOIN blade_user u on o.user_id = u.id
        WHERE
        o.is_delete = 0
        <if test="@cn.hutool.core.util.StrUtil@isNotBlank(p.orderNo)">
            and instr(o.order_no, #{p.orderNo}) > 0
        </if>
        <if test="@cn.hutool.core.util.StrUtil@isNotBlank(p.phone)">
            and instr(u.phone, #{p.phone}) > 0
        </if>
        <if test="p.orderState != null">
            and o.order_state = #{p.orderState}
        </if>
        <if test="p.goodsId != null">
            and og.goods_id = #{p.goodsId}
        </if>
        <if test="p.merchantId != null">
            and og.merchant_id = #{p.merchantId}
        </if>
        <if test="p.shopId != null">
            and og.shop_id = #{p.shopId}
        </if>
        <if test="p.startTime != null">
            and o.create_time <![CDATA[ >= ]]> #{p.startTime}
        </if>
        <if test="p.endTime != null">
            and o.create_time <![CDATA[ <= ]]> #{p.endTime}
        </if>
        order by o.create_time desc
    </select>

    <select id="selectUserOrder" resultType="org.springblade.modules.finance.vo.OrderGoodsVO">
        SELECT
            og.*,
            o.order_no,
            o.order_state,
            o.is_aftersales,
            o.total_amount,
            o.payment_amount,
            o.payment_mode
        FROM
            t_order o
            JOIN t_order_goods og ON o.id = og.order_id
        WHERE o.is_delete = 0 and o.user_id = #{p.userId}
        <if test="p.orderState != 0 and p.orderState != null">
            and o.order_state = #{p.orderState}
        </if>
        <if test="p.isAfterSales != null">
            and o.is_aftersales = #{p.isAfterSales}
        </if>
        ORDER BY o.create_time desc
    </select>

    <select id="getMerchantOrder" resultType="org.springblade.modules.finance.vo.MerchantPageOrderVO">
        SELECT
            og.*,
            o.image,
            o.order_state,
            o.is_aftersales
        FROM
            t_order o
                JOIN t_order_goods og ON o.id = og.order_id
        WHERE o.is_delete = 0 and o.user_id = #{param.userId}
    </select>

    <select id="getMerchantOrderNum" resultType="java.lang.String">
        SELECT
           count(o.id)
        FROM
            t_order o
                JOIN t_order_goods og ON o.id = og.order_id
        WHERE o.is_delete = 0 and o.user_id = #{merchantId}
    </select>

</mapper>
