package com.fs.hisStore.mapper;

import com.fs.hisStore.param.FsMyStoreOrderQueryParam;
import com.fs.hisStore.param.MergedAfterSalesQueryParam;
import com.fs.hisStore.vo.FsMergedOrderListQueryVO;
import com.fs.hisStore.vo.MergedAfterSalesVO;
import com.fs.live.param.MergedOrderQueryParam;
import com.fs.live.vo.MergedOrderVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 合并订单Mapper接口
 *
 * @author fs
 * @date 2025-01-XX
 */
public interface MergedOrderMapper
{
    /**
     * 查询合并的订单列表（销售订单+商城订单+直播订单）
     *
     * @param param 查询参数
     * @return 合并后的订单列表
     */
    List<MergedOrderVO> selectMergedOrderList(@Param("maps") MergedOrderQueryParam param);

    /**
     * 查询销售订单列表（只查询销售订单，order_type = 1）
     *
     * @param param 查询参数
     * @return 销售订单列表
     */
    List<MergedOrderVO> selectSalesOrderList(@Param("maps") MergedOrderQueryParam param);

    /**
     * 查询商城订单列表（只查询商城订单，order_type = 2）
     *
     * @param param 查询参数
     * @return 商城订单列表
     */
    List<MergedOrderVO> selectStoreOrderList(@Param("maps") MergedOrderQueryParam param);

    /**
     * 查询直播订单列表（只查询直播订单，order_type = 3）
     *
     * @param param 查询参数
     * @return 直播订单列表
     */
    List<MergedOrderVO> selectLiveOrderList(@Param("maps") MergedOrderQueryParam param);

    /**
     * 从视图查询合并订单列表（全部订单，使用视图优化）
     *
     * @param param 查询参数
     * @return 合并后的订单列表
     */
    List<MergedOrderVO> selectMergedOrderListFromView(@Param("maps") MergedOrderQueryParam param);

    /**
     * 统计合并订单总数
     *
     * @param param 查询参数
     * @return 订单总数
     */
    long countMergedOrderList(@Param("maps") MergedOrderQueryParam param);

    /**
     * 查询合并的售后列表（商城售后+直播售后）
     *
     * @param param 查询参数
     * @return 合并后的售后列表
     */
    List<MergedAfterSalesVO> selectMergedAfterSalesList(@Param("maps") MergedAfterSalesQueryParam param);

    /**
     * 查询合并的订单列表（商城订单+直播订单）
     *
     * @param param 查询参数
     * @return 合并后的订单列表
     */
    @Select({"<script> " +
            "SELECT * FROM ( " +
            "  SELECT " +
            "    o.id, " +
            "    NULL AS order_id, " +
            "    NULL AS live_id, " +
            "    NULL AS after_sales_id, " +
            "    o.order_code, " +
            "    o.pay_price, " +
            "    o.status, " +
            "    o.is_package, " +
            "    o.package_json, " +
            "    o.item_json, " +
            "    o.delivery_id, " +
            "    o.finish_time, " +
            "    o.create_time, " +
            "    NULL AS total_num, " +
            "    NULL AS discount_money, " +
            "    1 AS order_type " +
            "  FROM fs_store_order_scrm o " +
            "  WHERE o.is_del = 0 AND o.is_sys_del = 0 " +
            "  <if test = 'maps.status != null and maps.status != \"\"'> " +
            "    AND o.status = #{maps.status} " +
            "  </if> " +
            "  <if test = 'maps.keyword != null and maps.keyword != \"\"'> " +
            "    AND o.order_code LIKE CONCAT('%', #{maps.keyword}, '%') " +
            "  </if> " +
            "  <if test = 'maps.deliveryStatus != null'> " +
            "    AND o.delivery_status = #{maps.deliveryStatus} " +
            "  </if> " +
            "  <if test = 'maps.userId != null'> " +
            "    AND o.user_id = #{maps.userId} " +
            "  </if> " +
            "  UNION ALL " +
            "  SELECT " +
            "    NULL AS id, " +
            "    o.order_id, " +
            "    o.live_id, " +
            "    a.id AS after_sales_id, " +
            "    o.order_code, " +
            "    o.pay_price, " +
            "    o.status, " +
            "    NULL AS is_package, " +
            "    NULL AS package_json, " +
            "    o.item_json, " +
            "    o.delivery_sn AS delivery_id, " +
            "    o.finish_time, " +
            "    o.create_time, " +
            "    o.total_num, " +
            "    o.discount_money, " +
            "    2 AS order_type " +
            "  FROM live_order o " +
            "  LEFT JOIN ( " +
            "    SELECT t.*, ROW_NUMBER() OVER (PARTITION BY t.order_id ORDER BY t.create_time DESC) AS rn " +
            "    FROM live_after_sales t " +
            "  ) a ON o.order_id = a.order_id AND a.rn = 1 " +
            "  WHERE o.is_del = 0 " +
            "  <if test = 'maps.status != null and maps.status != \"\"'> " +
            "    AND o.status = #{maps.status} " +
            "  </if> " +
            "  <if test = 'maps.keyword != null and maps.keyword != \"\"'> " +
            "    AND o.order_code LIKE CONCAT('%', #{maps.keyword}, '%') " +
            "  </if> " +
            "  <if test = 'maps.deliveryStatus != null'> " +
            "    AND o.delivery_status = #{maps.deliveryStatus} " +
            "  </if> " +
            "  <if test = 'maps.userId != null'> " +
            "    AND o.user_id = #{maps.userId} " +
            "  </if> " +
            ") AS merged_orders " +
            "ORDER BY create_time DESC " +
            "</script>"})
    List<FsMergedOrderListQueryVO> selectMergedOrderListVO(@Param("maps") FsMyStoreOrderQueryParam param);
}

