package com.erp.product.sku.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.erp.product.sku.entity.Platform;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 平台SKU Repository
 */
@Mapper
public interface PlatformRepository extends BaseMapper<Platform> {
    
    /**
     * 查询未绑定的平台SKU
     */
    @Select("SELECT p.* FROM platforms p " +
            "WHERE NOT EXISTS (SELECT 1 FROM platform_sku_bindings b " +
            "WHERE b.platform_sku_id = p.id AND b.is_active = true) " +
            "ORDER BY p.updated_at DESC")
    List<Platform> findUnmatched();
    
    /**
     * 根据状态查询未绑定的平台SKU
     */
    @Select("SELECT p.* FROM platforms p " +
            "WHERE NOT EXISTS (SELECT 1 FROM platform_sku_bindings b " +
            "WHERE b.platform_sku_id = p.id AND b.is_active = true) " +
            "AND p.status = #{status} " +
            "ORDER BY p.updated_at DESC")
    List<Platform> findUnmatchedByStatus(@Param("status") String status);
    
    /**
     * 根据平台SKU ID列表查询
     */
    @Select("SELECT p.* FROM platforms p " +
            "WHERE p.id IN (#{ids}) " +
            "AND NOT EXISTS (SELECT 1 FROM platform_sku_bindings b " +
            "WHERE b.platform_sku_id = p.id AND b.is_active = true)")
    List<Platform> findUnmatchedByIds(@Param("ids") List<Long> ids);
}
