CreateOrUpdateTaskTemplateCommandHandler.cs
8.45 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.Dtos;
using Rcs.Application.MessageBus.Commands;
using Rcs.Domain.Entities;
using Rcs.Domain.Enums;
using Rcs.Domain.Repositories;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;
public class CreateOrUpdateTaskTemplateCommandHandler : IConsumer<CreateOrUpdateTaskTemplateCommand>
{
private readonly ILogger<CreateOrUpdateTaskTemplateCommandHandler> _logger;
private readonly ITaskTemplateRepository _taskTemplateRepository;
public CreateOrUpdateTaskTemplateCommandHandler(
ILogger<CreateOrUpdateTaskTemplateCommandHandler> logger,
ITaskTemplateRepository taskTemplateRepository)
{
_logger = logger;
_taskTemplateRepository = taskTemplateRepository;
}
public async Task Consume(ConsumeContext<CreateOrUpdateTaskTemplateCommand> context)
{
var command = context.Message;
try
{
if (!Enum.IsDefined(typeof(RobotType), command.RobotType))
{
throw new InvalidOperationException($"Invalid robot type: {command.RobotType}");
}
var robotType = (RobotType)command.RobotType;
var normalizedManufacturer = NormalizeManufacturer(command.Manufacturer);
var isUpdate = Guid.TryParse(command.TemplateId, out var templateId);
if (command.IsDefault)
{
Guid? excludeId = isUpdate ? templateId : null;
var existingDefault = await _taskTemplateRepository.GetDefaultTemplateAsync(
robotType,
normalizedManufacturer,
excludeId,
context.CancellationToken);
if (existingDefault != null)
{
throw new InvalidOperationException(
$"Default template already exists for robotType={robotType}, manufacturer={normalizedManufacturer ?? "null"}. Existing={existingDefault.TemplateName}");
}
}
if (!isUpdate)
{
await CreateTemplateAsync(command, robotType, normalizedManufacturer, context.CancellationToken);
}
else
{
await UpdateTemplateAsync(command, templateId, robotType, normalizedManufacturer, context.CancellationToken);
}
await context.RespondAsync(ApiResponse.Successful());
}
catch (Exception ex)
{
_logger.LogError(ex, "Create or update task template failed");
await context.RespondAsync(ApiResponse.Failed(ex.Message));
}
}
private async Task CreateTemplateAsync(
CreateOrUpdateTaskTemplateCommand command,
RobotType robotType,
string? manufacturer,
CancellationToken cancellationToken)
{
var existingTemplate = await _taskTemplateRepository.GetByTemplateCodeAsync(
command.TemplateCode,
cancellationToken);
if (existingTemplate != null)
{
throw new InvalidOperationException($"TemplateCode already exists: {command.TemplateCode}");
}
var template = TaskTemplate.Create(
command.TemplateCode,
command.TemplateName,
robotType,
command.Description,
manufacturer,
command.IsEnabled,
command.IsDefault);
BuildTemplateSteps(template, command.TaskSteps);
await _taskTemplateRepository.AddAsync(template, cancellationToken);
}
private async Task UpdateTemplateAsync(
CreateOrUpdateTaskTemplateCommand command,
Guid templateId,
RobotType robotType,
string? manufacturer,
CancellationToken cancellationToken)
{
var template = await _taskTemplateRepository.GetWithFullDetailsAsync(templateId, cancellationToken);
if (template == null)
{
throw new InvalidOperationException($"Task template not found: {templateId}");
}
template.Update(
command.TemplateCode,
command.TemplateName,
robotType,
command.Description,
manufacturer,
command.IsEnabled,
command.IsDefault);
ClearTemplateSteps(template);
BuildTemplateSteps(template, command.TaskSteps);
await _taskTemplateRepository.UpdateAsync(template, cancellationToken);
}
private static string? NormalizeManufacturer(string? manufacturer)
{
return string.IsNullOrWhiteSpace(manufacturer) ? null : manufacturer.Trim();
}
private static void ClearTemplateSteps(TaskTemplate template)
{
foreach (var step in template.TaskSteps.ToList())
{
foreach (var property in step.Properties.ToList())
{
foreach (var action in property.Actions.ToList())
{
property.Actions.Remove(action);
}
step.Properties.Remove(property);
}
template.TaskSteps.Remove(step);
}
}
private static void BuildTemplateSteps(TaskTemplate template, IEnumerable<TaskStepDto>? stepDtos)
{
if (stepDtos == null)
{
return;
}
foreach (var stepDto in stepDtos)
{
var step = new TaskStep
{
StepId = Guid.NewGuid(),
TemplateId = template.TemplateId,
Type = (TaskStepType)stepDto.Type,
StepName = stepDto.StepName,
Description = stepDto.Description,
Order = stepDto.Order,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
};
foreach (var propertyDto in stepDto.Properties ?? Enumerable.Empty<StepPropertyDto>())
{
var property = new StepProperty
{
PropertyId = Guid.NewGuid(),
StepId = step.StepId,
PropertyType = (StepPropertyType)propertyDto.PropertyType,
NodeValue = (NodeValueType)propertyDto.NodeValue,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
};
property.PreAction1Type = propertyDto.PreAction1Type.HasValue
? (ActionType)propertyDto.PreAction1Type.Value
: null;
property.PreNetActions1 = propertyDto.PreNetActions1?.Where(n => Guid.TryParse(n, out _)).Select(Guid.Parse).ToList();
property.PostAction1Type = propertyDto.PostAction1Type.HasValue
? (ActionType)propertyDto.PostAction1Type.Value
: null;
property.PostNetActions1 = propertyDto.PostNetActions1?.Where(n => Guid.TryParse(n, out _)).Select(Guid.Parse).ToList();
property.PreAction2Type = propertyDto.PreAction2Type.HasValue
? (ActionType)propertyDto.PreAction2Type.Value
: null;
property.PreNetActions2 = propertyDto.PreNetActions2?.Where(n => Guid.TryParse(n, out _)).Select(Guid.Parse).ToList();
property.PostAction2Type = propertyDto.PostAction2Type.HasValue
? (ActionType)propertyDto.PostAction2Type.Value
: null;
property.PostNetActions2 = propertyDto.PostNetActions2?.Where(n => Guid.TryParse(n, out _)).Select(Guid.Parse).ToList();
foreach (var actionDto in propertyDto.Actions ?? Enumerable.Empty<ActionDto>())
{
var action = new StepAction
{
ActionId = Guid.NewGuid(),
PropertyId = property.PropertyId,
ActionConfigId = Guid.Parse(actionDto.ActionConfigId),
Type = Enum.Parse<ActionCategory>(actionDto.Type),
ActionName = actionDto.ActionName,
Description = actionDto.Description,
BlockingType = (ActionBlockType)actionDto.BlockingType,
Order = actionDto.Order,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
};
property.Actions.Add(action);
}
step.Properties.Add(property);
}
template.TaskSteps.Add(step);
}
}
}