package com.erp.order.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.order.entity.Order;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderRepository extends BaseMapper<Order> {

    IPage<Order> selectOrderPage(Page<Order> page,
                                 @Param("platform") String platform,
                                 @Param("shopId") Long shopId,
                                 @Param("orderStatus") String orderStatus,
                                 @Param("auditStatus") String auditStatus,
                                 @Param("deliveryStatus") String deliveryStatus,
                                 @Param("keyword") String keyword,
                                 @Param("startDate") LocalDateTime startDate,
                                 @Param("endDate") LocalDateTime endDate);

    @Select("SELECT DISTINCT platform FROM orders ORDER BY platform")
    List<String> selectDistinctPlatforms();

    @Select("SELECT shop_id, shop_name FROM orders GROUP BY shop_id, shop_name ORDER BY shop_name")
    List<ShopOption> selectDistinctShops();

    /**
     * 分页查询待审核订单（audit_status = 'pending'）
     */
    IPage<Order> selectPendingAuditPage(Page<Order> page,
                                       @Param("platform") String platform,
                                       @Param("shopId") Long shopId,
                                       @Param("keyword") String keyword);

    /**
     * 分页查询待匹配订单（存在订单项但 erp_sku_id 为空的订单）
     */
    IPage<Order> selectPendingMatchPage(Page<Order> page,
                                        @Param("platform") String platform,
                                        @Param("shopId") Long shopId,
                                        @Param("keyword") String keyword);

    interface ShopOption {
        Long getShopId();
        String getShopName();
    }
}
