RobotRepository.cs 6.15 KB
using Microsoft.EntityFrameworkCore;
using Rcs.Application.DTOs;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;
using Rcs.Infrastructure.DB.MsSql;

namespace Rcs.Infrastructure.DB.Repositories
{
    /// <summary>
    /// 机器人仓储实现
    /// </summary>
    public class RobotRepository : Repository<Robot>, IRobotRepository
    {
        public RobotRepository(AppDbContext context) : base(context)
        {
        }

        public async Task<Robot?> GetByRobotCodeAsync(
            string robotCode,
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .FirstOrDefaultAsync(r => r.RobotCode == robotCode, cancellationToken);
        }

        public async Task<Robot?> GetByIdFullDataAsync(Guid robotId, CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .Include(r => r.Map)
                .Include(r => r.MapNode)
                .FirstOrDefaultAsync(r => r.RobotId == robotId, cancellationToken);
        }

        public async Task<Robot?> GetBySerialNumberAsync(
            string serialNumber,
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .FirstOrDefaultAsync(r => r.RobotSerialNumber == serialNumber, cancellationToken);
        }

        /// <summary>
        /// 根据制造商和序列号获取机器人
        /// @author zzy
        /// </summary>
        public async Task<Robot?> GetByManufacturerAndSerialNumberAsync(
            string manufacturer,
            string serialNumber,
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .FirstOrDefaultAsync(r => r.RobotManufacturer == manufacturer && r.RobotSerialNumber == serialNumber, cancellationToken);
        }

        public async Task<IEnumerable<Robot>> GetOnlineRobotsAsync(
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .Where(r => r.Online == OnlineStatus.Online)
                .ToListAsync(cancellationToken);
        }

        public async Task<IEnumerable<Robot>> GetActiveRobotsAsync(
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .Where(r => r.Active)
                .OrderBy(r => r.RobotCode)
                .ToListAsync(cancellationToken);
        }

        public async Task<IEnumerable<Robot>> GetByStatusAsync(
            RobotStatus status,
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .Where(r => r.Status == status)
                .ToListAsync(cancellationToken);
        }

        public async Task<IEnumerable<Robot>> GetByRobotTypeAsync(
            RobotType robotType,
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .Where(r => r.RobotType == robotType)
                .ToListAsync(cancellationToken);
        }

        public async Task<IEnumerable<Robot>> GetIdleRobotsAsync(
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .Where(r => r.Status == RobotStatus.Idle && r.Active)
                .ToListAsync(cancellationToken);
        }

        public async Task<IEnumerable<Robot>> GetByManufacturerAsync(
            string manufacturer,
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .Where(r => r.RobotManufacturer == manufacturer)
                .ToListAsync(cancellationToken);
        }

        public async Task<IEnumerable<Robot>> GetByCurrentNodeAsync(
            Guid nodeCodeId,
            CancellationToken cancellationToken = default)
        {
            return await _dbSet
                .Where(r => r.CurrentNodeId == nodeCodeId)
                .ToListAsync(cancellationToken);
        }

        public async Task<(IEnumerable<Robot> Robots, int TotalCount)> GetByFilterAsync(
            string? robotCode = null,
            string? robotName = null,
            string? robotManufacturer = null,
            string? robotSerialNumber = null,
            string? robotType = null,
            bool? active = null,
            int pageNumber = 1,
            int pageSize = 10,
            CancellationToken cancellationToken = default)
        {
            var queryable = _dbSet.AsQueryable();

            // 按机器人编码筛选
            if (!string.IsNullOrWhiteSpace(robotCode))
            {
                queryable = queryable.Where(r => r.RobotCode.Contains(robotCode));
            }

            // 按机器人名称筛选
            if (!string.IsNullOrWhiteSpace(robotName))
            {
                queryable = queryable.Where(r => r.RobotName.Contains(robotName));
            }

            // 按制造商筛选
            if (!string.IsNullOrWhiteSpace(robotManufacturer))
            {
                queryable = queryable.Where(r => r.RobotManufacturer.Contains(robotManufacturer));
            }

            // 按序列号筛选
            if (!string.IsNullOrWhiteSpace(robotSerialNumber))
            {
                queryable = queryable.Where(r => r.RobotSerialNumber.Contains(robotSerialNumber));
            }

            // 按机器人类型筛选
            if (!string.IsNullOrWhiteSpace(robotType))
            {
                if (Enum.TryParse<RobotType>(robotType, true, out var robotTypeEnum))
                {
                    queryable = queryable.Where(r => r.RobotType == robotTypeEnum);
                }
            }

            // 按是否启用筛选
            if (active.HasValue)
            {
                queryable = queryable.Where(r => r.Active == active.Value);
            }
            var totalCount = await queryable.CountAsync(cancellationToken);

            // 应用分页
            var robots = await queryable
                .OrderBy(r => r.RobotCode)
                .Skip((pageNumber - 1) * pageSize)
                .Take(pageSize)
                .ToListAsync(cancellationToken);
            return (robots, totalCount);
        }
    }
}