package com.qaup.collision.pathconflict.repository;

import com.qaup.collision.pathconflict.model.entity.ObjectRouteAssignment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 实时对象路径分配Repository
 * 
 * @author AI Assistant
 * @version 1.0
 * @since 2025-01-17
 */
@Repository
public interface ObjectRouteAssignmentRepository extends JpaRepository<ObjectRouteAssignment, Long> {
    
    /**
     * 根据对象名称和对象类型查找最新的路径分配记录
     */
    Optional<ObjectRouteAssignment> findFirstByObjectNameAndObjectTypeOrderByAssignedAtDesc(
        String objectName,
        ObjectRouteAssignment.ObjectType objectType
    );

    /**
     * 根据路径ID查找所有分配到该路径的对象
     */
    List<ObjectRouteAssignment> findByAssignedRouteId(Long assignedRouteId);
    
    /**
     * 根据对象名称查找该对象的最新路由分配记录
     */
    @Query("SELECT ora FROM ObjectRouteAssignment ora " +
           "WHERE ora.objectName = :objectName AND ora.objectType = :objectType " +
           "ORDER BY ora.assignedAt DESC")
    List<ObjectRouteAssignment> findByObjectNameAndObjectTypeOrderByAssignedAtDesc(
        @Param("objectName") String objectName,
        @Param("objectType") ObjectRouteAssignment.ObjectType objectType
    );
} 