<?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.aftersale.repository.AfterSaleMapper">

    <resultMap id="BaseResultMap" type="com.erp.aftersale.entity.AfterSale">
        <id column="id" property="id"/>
        <result column="short_id" property="shortId"/>
        <result column="order_id" property="orderId"/>
        <result column="order_short_id" property="orderShortId"/>
        <result column="platform_order_sn" property="platformOrderSn"/>
        <result column="shop_id" property="shopId"/>
        <result column="shop_name" property="shopName"/>
        <result column="customer_id" property="customerId"/>
        <result column="customer_name" property="customerName"/>
        <result column="customer_phone" property="customerPhone"/>
        <result column="type" property="type"/>
        <result column="reason" property="reason"/>
        <result column="reason_detail" property="reasonDetail"/>
        <result column="description" property="description"/>
        <result column="apply_amount" property="applyAmount"/>
        <result column="approve_amount" property="approveAmount"/>
        <result column="freight_refund" property="freightRefund"/>
        <result column="actual_refund" property="actualRefund"/>
        <result column="status" property="status"/>
        <result column="audit_status" property="auditStatus"/>
        <result column="previous_status" property="previousStatus"/>
        <result column="audit_comment" property="auditComment"/>
        <result column="auditor_id" property="auditorId"/>
        <result column="auditor_name" property="auditorName"/>
        <result column="audit_time" property="auditTime"/>
        <result column="refund_time" property="refundTime"/>
        <result column="return_tracking_no" property="returnTrackingNo"/>
        <result column="return_express_company" property="returnExpressCompany"/>
        <result column="receive_time" property="receiveTime"/>
        <result column="receive_comment" property="receiveComment"/>
        <result column="complete_time" property="completeTime"/>
        <result column="close_time" property="closeTime"/>
        <result column="close_reason" property="closeReason"/>
        <result column="images" property="images"/>
        <result column="exchange_order_id" property="exchangeOrderId"/>
        <result column="repair_order_id" property="repairOrderId"/>
        <result column="finance_refund_id" property="financeRefundId"/>
        <result column="version" property="version"/>
        <result column="created_at" property="createdAt"/>
        <result column="updated_at" property="updatedAt"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <resultMap id="ItemResultMap" type="com.erp.aftersale.entity.AfterSaleItem">
        <id column="id" property="id"/>
        <result column="after_sale_id" property="afterSaleId"/>
        <result column="order_item_id" property="orderItemId"/>
        <result column="product_id" property="productId"/>
        <result column="product_name" property="productName"/>
        <result column="sku_id" property="skuId"/>
        <result column="sku_code" property="skuCode"/>
        <result column="sku_specs" property="skuSpecs"/>
        <result column="image_url" property="imageUrl"/>
        <result column="unit_price" property="unitPrice"/>
        <result column="quantity" property="quantity"/>
        <result column="apply_quantity" property="applyQuantity"/>
        <result column="approve_quantity" property="approveQuantity"/>
        <result column="return_quantity" property="returnQuantity"/>
        <result column="item_amount" property="itemAmount"/>
        <result column="created_at" property="createdAt"/>
        <result column="updated_at" property="updatedAt"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <resultMap id="LogResultMap" type="com.erp.aftersale.entity.AfterSaleOperationLog">
        <id column="id" property="id"/>
        <result column="after_sale_id" property="afterSaleId"/>
        <result column="operator_id" property="operatorId"/>
        <result column="operator_name" property="operatorName"/>
        <result column="operator_type" property="operatorType"/>
        <result column="action" property="action"/>
        <result column="from_status" property="fromStatus"/>
        <result column="to_status" property="toStatus"/>
        <result column="content" property="content"/>
        <result column="ip" property="ip"/>
        <result column="created_at" property="createdAt"/>
    </resultMap>

    <!-- ============ AfterSale 查询 ============ -->
    <select id="selectPage" resultMap="BaseResultMap">
        SELECT * FROM after_sales WHERE deleted = 0
        <if test="shortId != null and shortId != ''">
            AND short_id LIKE CONCAT('%', #{shortId}, '%')
        </if>
        <if test="orderId != null">
            AND order_id = #{orderId}
        </if>
        <if test="orderShortId != null and orderShortId != ''">
            AND order_short_id LIKE CONCAT('%', #{orderShortId}, '%')
        </if>
        <if test="platformOrderSn != null and platformOrderSn != ''">
            AND platform_order_sn = #{platformOrderSn}
        </if>
        <if test="type != null and type != ''">
            AND type = #{type}
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        <if test="auditStatus != null and auditStatus != ''">
            AND audit_status = #{auditStatus}
        </if>
        <if test="customerId != null">
            AND customer_id = #{customerId}
        </if>
        <if test="shopId != null">
            AND shop_id = #{shopId}
        </if>
        <if test="auditorId != null">
            AND auditor_id = #{auditorId}
        </if>
        <if test="startTime != null and startTime != ''">
            AND created_at &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND created_at &lt;= #{endTime}
        </if>
        ORDER BY created_at DESC
    </select>

    <select id="selectById" resultMap="BaseResultMap">
        SELECT * FROM after_sales WHERE id = #{id} AND deleted = 0
    </select>

    <select id="selectByShortId" resultMap="BaseResultMap">
        SELECT * FROM after_sales WHERE short_id = #{shortId} AND deleted = 0
    </select>

    <select id="selectItemsByAfterSaleId" resultMap="ItemResultMap">
        SELECT * FROM after_sale_items WHERE after_sale_id = #{afterSaleId} AND deleted = 0
    </select>

    <select id="selectLogsByAfterSaleId" resultMap="LogResultMap">
        SELECT * FROM after_sale_logs WHERE after_sale_id = #{afterSaleId} ORDER BY created_at DESC
    </select>

    <!-- ============ 统计查询 ============ -->
    <select id="countByStatus" resultType="java.lang.Long">
        SELECT COUNT(*) FROM after_sales WHERE deleted = 0 AND status = #{status}
    </select>

    <select id="sumActualRefund" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(actual_refund), 0) FROM after_sales WHERE deleted = 0 AND actual_refund IS NOT NULL
    </select>

    <select id="sumApplyAmount" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(apply_amount), 0) FROM after_sales WHERE deleted = 0
    </select>

    <select id="countGroupByType" resultType="java.util.Map">
        SELECT type, COUNT(*) as count FROM after_sales WHERE deleted = 0 GROUP BY type
    </select>

    <select id="countGroupByStatus" resultType="java.util.Map">
        SELECT status, COUNT(*) as count FROM after_sales WHERE deleted = 0 GROUP BY status
    </select>

    <!-- ============ 更新操作 ============ -->
    <update id="updateStatus">
        UPDATE after_sales
        SET status = #{status},
            audit_comment = #{auditComment},
            auditor_id = #{auditorId},
            auditor_name = #{auditorName},
            audit_time = NOW()
        WHERE id = #{id}
    </update>

</mapper>
