MapResourceRepository.cs 4.42 KB
using Microsoft.EntityFrameworkCore;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;
using Rcs.Infrastructure.DB.MsSql;

namespace Rcs.Infrastructure.DB.Repositories
{
    /// <summary>
    /// 地图资源仓储实现
    /// </summary>
    public class MapResourceRepository : Repository<MapResource>, IMapResourceRepository
    {
        public MapResourceRepository(AppDbContext context) : base(context)
        {
        }

        /// <summary>
        /// 根据地图ID获取所有资源
        /// </summary>
        public async Task<IEnumerable<MapResource>> GetByMapIdAsync(
            Guid mapId,
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .Where(r => r.MapId == mapId && r.Active)
                .OrderBy(r => r.ResourceCode)
                .ToListAsync(cancellationToken);
        }

        /// <summary>
        /// 根据节点ID获取所有资源
        /// </summary>
        public async Task<IEnumerable<MapResource>> GetByNodeIdAsync(
            Guid nodeId,
            CancellationToken cancellationToken = default)
        {
            // 通过连接查询获取与指定节点关联的资源
            return await _context.MapNodes
                .Where(n => n.NodeId == nodeId)
                .SelectMany(n => _context.MapResources
                    .Where(r => r.MapId == n.MapId && r.Active))
                .Distinct()
                .OrderBy(r => r.ResourceCode)
                .ToListAsync(cancellationToken);
        }

        /// <summary>
        /// 根据资源类型获取资源列表
        /// </summary>
        public async Task<IEnumerable<MapResource>> GetByTypeAsync(
            MapResourcesTYPE type,
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .Where(r => r.Type == type && r.Active)
                .OrderBy(r => r.ResourceCode)
                .ToListAsync(cancellationToken);
        }

        /// <summary>
        /// 根据地图ID和资源类型获取资源列表
        /// </summary>
        public async Task<IEnumerable<MapResource>> GetByMapIdAndTypeAsync(
            Guid mapId,
            MapResourcesTYPE type,
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .Where(r => r.MapId == mapId && r.Type == type && r.Active)
                .OrderBy(r => r.ResourceCode)
                .ToListAsync(cancellationToken);
        }

        /// <summary>
        /// 根据资源名称获取资源
        /// </summary>
        public async Task<MapResource?> GetByResourceNameAsync(
            string resourceName,
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .FirstOrDefaultAsync(r => r.ResourceName == resourceName && r.Active, cancellationToken);
        }

        /// <summary>
        /// 根据资源编码获取资源
        /// </summary>
        public async Task<MapResource?> GetByResourceCodeAsync(
            string resourceCode,
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .FirstOrDefaultAsync(r => r.ResourceCode == resourceCode && r.Active, cancellationToken);
        }

        /// <summary>
        /// 根据地图ID和资源编码获取资源
        /// </summary>
        public async Task<MapResource?> GetByMapIdAndResourceCodeAsync(
            Guid mapId,
            string resourceCode,
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .FirstOrDefaultAsync(r => r.MapId == mapId && r.ResourceCode == resourceCode && r.Active, cancellationToken);
        }

        /// <summary>
        /// 根据节点编码获取所有资源
        /// </summary>
        public async Task<IEnumerable<MapResource>> GetByNodeCodeAsync(
            string nodeCode,
            CancellationToken cancellationToken = default)
        {
            // 通过节点编码找到对应的地图,然后获取该地图的所有资源
            return await _context.MapNodes
                .Where(n => n.NodeCode == nodeCode)
                .SelectMany(n => _context.MapResources
                    .Where(r => r.MapId == n.MapId && r.Active))
                .Distinct()
                .OrderBy(r => r.ResourceCode)
                .ToListAsync(cancellationToken);
        }
    }
}