RobotRepository.cs
6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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);
}
}
}