StorageAreaRepository.cs 1.55 KB
using Microsoft.EntityFrameworkCore;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;
using Rcs.Infrastructure.DB.MsSql;

namespace Rcs.Infrastructure.DB.Repositories
{
    /// <summary>
    /// 库区仓储实现
    /// @author zzy
    /// </summary>
    public class StorageAreaRepository : Repository<StorageArea>, IStorageAreaRepository
    {
        public StorageAreaRepository(AppDbContext context) : base(context)
        {
        }

        public async Task<StorageArea?> GetByAreaCodeAsync(string areaCode, CancellationToken cancellationToken = default)
        {
            return await _dbSet.Include(a => a.StorageLocations)
                .FirstOrDefaultAsync(a => a.AreaCode == areaCode, cancellationToken);
        }

        /// <summary>
        /// 根据地图ID获取库区列表(通过库位的MapNode关联)
        /// @author zzy
        /// </summary>
        public async Task<IEnumerable<StorageArea>> GetByMapIdAsync(Guid mapId, CancellationToken cancellationToken)
        {
            return await _dbSet
                .Include(a => a.StorageLocations)
                .Where(a => a.StorageLocations.Any(l => l.MapNodeId != null && l.MapNode.MapId == mapId))
                .ToListAsync(cancellationToken);
        }


        public async Task<StorageArea?> GetWithLocationsAsync(Guid areaId, CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .Include(a => a.StorageLocations)
                .FirstOrDefaultAsync(a => a.AreaId == areaId, cancellationToken);
        }
    }
}