TaskTemplateRepository.cs
3.21 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
using Microsoft.EntityFrameworkCore;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;
using Rcs.Infrastructure.DB.MsSql;
namespace Rcs.Infrastructure.DB.Repositories
{
public class TaskTemplateRepository : Repository<TaskTemplate>, ITaskTemplateRepository
{
public TaskTemplateRepository(AppDbContext context) : base(context)
{
}
public async Task<TaskTemplate?> GetByTemplateCodeAsync(
string templateCode,
CancellationToken cancellationToken = default)
{
return await _dbSet
.FirstOrDefaultAsync(t => t.TemplateCode == templateCode, cancellationToken);
}
public async Task<IEnumerable<TaskTemplate>> GetEnabledAsync(
CancellationToken cancellationToken = default)
{
return await _dbSet
.Where(t => t.IsEnabled)
.OrderBy(t => t.TemplateCode)
.ToListAsync(cancellationToken);
}
public async Task<IEnumerable<TaskTemplate>> GetByRobotTypeAsync(
RobotType robotType,
CancellationToken cancellationToken = default)
{
return await _dbSet
.Where(t => t.RobotType == robotType)
.OrderBy(t => t.TemplateCode)
.ToListAsync(cancellationToken);
}
public async Task<TaskTemplate?> GetWithStepsAsync(
Guid templateId,
CancellationToken cancellationToken = default)
{
return await _dbSet
.Include(t => t.TaskSteps)
.FirstOrDefaultAsync(t => t.TemplateId == templateId, cancellationToken);
}
public async Task<TaskTemplate?> GetWithStepsByCodeAsync(
string templateCode,
CancellationToken cancellationToken = default)
{
return await _dbSet
.Include(t => t.TaskSteps)
.FirstOrDefaultAsync(t => t.TemplateCode == templateCode, cancellationToken);
}
public async Task<TaskTemplate?> GetWithFullDetailsAsync(
Guid templateId,
CancellationToken cancellationToken = default)
{
return await _dbSet
.Include(t => t.TaskSteps)
.ThenInclude(s => s.Properties)
.ThenInclude(p => p.Actions)
.FirstOrDefaultAsync(t => t.TemplateId == templateId, cancellationToken);
}
/// <summary>
/// 获取指定机器人类型和制造商的默认模板
/// @author zzy
/// </summary>
public async Task<TaskTemplate?> GetDefaultTemplateAsync(
RobotType robotType,
string? manufacturer,
Guid? excludeTemplateId = null,
CancellationToken cancellationToken = default)
{
var query = _dbSet.Where(t =>
t.RobotType == robotType &&
t.Manufacturer == manufacturer &&
t.IsDefault);
if (excludeTemplateId.HasValue)
{
query = query.Where(t => t.TemplateId != excludeTemplateId.Value);
}
return await query.FirstOrDefaultAsync(cancellationToken);
}
}
}