<?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.erp.order.repository.OrderRepository">

    <resultMap id="BaseResultMap" type="com.erp.order.entity.Order">
        <id column="id" property="id"/>
        <result column="short_id" property="shortId"/>
        <result column="platform_order_sn" property="platformOrderSn"/>
        <result column="platform" property="platform"/>
        <result column="shop_id" property="shopId"/>
        <result column="shop_name" property="shopName"/>
        <result column="order_time" property="orderTime"/>
        <result column="buyer_nick" property="buyerNick"/>
        <result column="receiver_name" property="receiverName"/>
        <result column="receiver_phone" property="receiverPhone"/>
        <result column="receiver_address" property="receiverAddress"/>
        <result column="goods_amount" property="goodsAmount"/>
        <result column="discount_amount" property="discountAmount"/>
        <result column="freight" property="freight"/>
        <result column="total_amount" property="totalAmount"/>
        <result column="order_status" property="orderStatus"/>
        <result column="platform_status" property="platformStatus"/>
        <result column="audit_status" property="auditStatus"/>
        <result column="delivery_status" property="deliveryStatus"/>
        <result column="warehouse_id" property="warehouseId"/>
        <result column="warehouse_name" property="warehouseName"/>
        <result column="express_company" property="expressCompany"/>
        <result column="express_name" property="expressName"/>
        <result column="express_no" property="expressNo"/>
        <result column="delivery_time" property="deliveryTime"/>
        <result column="end_time" property="endTime"/>
        <result column="audit_comment" property="auditComment"/>
        <result column="remark" property="remark"/>
        <result column="version" property="version"/>
        <result column="created_at" property="createdAt"/>
        <result column="updated_at" property="updatedAt"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <select id="selectOrderPage" resultMap="BaseResultMap">
        SELECT * FROM orders
        WHERE deleted = 0
        <if test="platform != null and platform != ''">
            AND platform = #{platform}
        </if>
        <if test="shopId != null">
            AND shop_id = #{shopId}
        </if>
        <if test="orderStatus != null and orderStatus != ''">
            AND order_status = #{orderStatus}
        </if>
        <if test="auditStatus != null and auditStatus != ''">
            AND audit_status = #{auditStatus}
        </if>
        <if test="deliveryStatus != null and deliveryStatus != ''">
            AND delivery_status = #{deliveryStatus}
        </if>
        <if test="keyword != null and keyword != ''">
            AND (
                short_id LIKE CONCAT('%', #{keyword}, '%')
                OR platform_order_sn LIKE CONCAT('%', #{keyword}, '%')
                OR receiver_name LIKE CONCAT('%', #{keyword}, '%')
                OR receiver_phone LIKE CONCAT('%', #{keyword}, '%')
            )
        </if>
        <if test="startDate != null">
            AND order_time &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            AND order_time &lt;= #{endDate}
        </if>
        ORDER BY created_at DESC
    </select>

    <!-- 待审核订单分页查询 -->
    <select id="selectPendingAuditPage" resultMap="BaseResultMap">
        SELECT * FROM orders
        WHERE deleted = 0
        AND audit_status = 'pending'
        <if test="platform != null and platform != ''">
            AND platform = #{platform}
        </if>
        <if test="shopId != null">
            AND shop_id = #{shopId}
        </if>
        <if test="keyword != null and keyword != ''">
            AND (
                short_id LIKE CONCAT('%', #{keyword}, '%')
                OR platform_order_sn LIKE CONCAT('%', #{keyword}, '%')
                OR receiver_name LIKE CONCAT('%', #{keyword}, '%')
                OR receiver_phone LIKE CONCAT('%', #{keyword}, '%')
            )
        </if>
        ORDER BY created_at DESC
    </select>

    <!-- 待匹配订单分页查询：存在订单项但 erp_sku_id 为空的订单 -->
    <select id="selectPendingMatchPage" resultMap="BaseResultMap">
        SELECT DISTINCT o.* FROM orders o
        INNER JOIN order_items oi ON o.id = oi.order_id
        WHERE o.deleted = 0
        AND oi.erp_sku_id IS NULL
        <if test="platform != null and platform != ''">
            AND o.platform = #{platform}
        </if>
        <if test="shopId != null">
            AND o.shop_id = #{shopId}
        </if>
        <if test="keyword != null and keyword != ''">
            AND (
                o.short_id LIKE CONCAT('%', #{keyword}, '%')
                OR o.platform_order_sn LIKE CONCAT('%', #{keyword}, '%')
                OR o.receiver_name LIKE CONCAT('%', #{keyword}, '%')
                OR o.receiver_phone LIKE CONCAT('%', #{keyword}, '%')
            )
        </if>
        ORDER BY o.created_at DESC
    </select>

</mapper>
