CreateOrUpdateActionConfigurationCommandHandler.cs
8.64 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
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
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 CreateOrUpdateActionConfigurationCommandHandler : IConsumer<CreateOrUpdateActionConfigurationCommand>
{
private readonly ILogger<CreateOrUpdateActionConfigurationCommandHandler> _logger;
private readonly IActionConfigurationRepository _repository;
public CreateOrUpdateActionConfigurationCommandHandler(
ILogger<CreateOrUpdateActionConfigurationCommandHandler> logger,
IActionConfigurationRepository repository)
{
_logger = logger;
_repository = repository;
}
public async Task Consume(ConsumeContext<CreateOrUpdateActionConfigurationCommand> context)
{
var command = context.Message;
try
{
if (!Guid.TryParse(command.ActionConfigId, out Guid actionConfigId))
{
var existing = await _repository.GetByActionNameAsync(command.ActionName, context.CancellationToken);
if (existing != null)
{
throw new InvalidOperationException($"动作名称 {command.ActionName} 已存在");
}
var entity = new ActionConfiguration
{
ActionConfigId = Guid.NewGuid(),
ActionCategory = (ActionCategory)command.ActionCategory,
ActionCategoryName = command.ActionCategoryName,
Manufacturer = command.Manufacturer,
RobotType = (RobotType)command.RobotType,
ActionName = command.ActionName,
ActionDescription = command.ActionDescription,
ExecutionScope = command.ExecutionScope,
BlockingType = (ActionBlockType)command.BlockingType,
IsEnabled = command.IsEnabled,
SortOrder = command.SortOrder,
Remarks = command.Remarks,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
};
if (command.Parameters != null && command.Parameters.Count > 0)
{
foreach (var paramDto in command.Parameters)
{
var param = new ActionParameterDefinition
{
ParamId = Guid.NewGuid(),
ActionConfigId = entity.ActionConfigId,
ParameterName = paramDto.ParameterName,
ParameterValueType = (ParameterValueType)paramDto.ParameterValueType,
ParameterDescription = paramDto.ParameterDescription,
IsMandatory = paramDto.IsMandatory,
DefaultValue = paramDto.DefaultValue,
ParameterSourceType = paramDto.ParameterSourceType.HasValue ? (ParameterSourceType)paramDto.ParameterSourceType.Value : null,
ParameterSourcePath = paramDto.ParameterSourcePath,
ValueConstraints = paramDto.ValueConstraints,
Remarks = paramDto.Remarks,
SortOrder = paramDto.SortOrder,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
};
entity.Parameters.Add(param);
}
}
await _repository.AddAsync(entity, context.CancellationToken);
}
else
{
var entity = await _repository.GetWithParametersAsync(actionConfigId, context.CancellationToken);
if (entity == null)
{
throw new InvalidOperationException($"未找到ID为 {actionConfigId} 的动作配置");
}
entity.ActionCategory = (ActionCategory)command.ActionCategory;
entity.ActionCategoryName = command.ActionCategoryName;
entity.Manufacturer = command.Manufacturer;
entity.RobotType = (RobotType)command.RobotType;
entity.ActionName = command.ActionName;
entity.ActionDescription = command.ActionDescription;
entity.ExecutionScope = command.ExecutionScope;
entity.BlockingType = (ActionBlockType)command.BlockingType;
entity.IsEnabled = command.IsEnabled;
entity.SortOrder = command.SortOrder;
entity.Remarks = command.Remarks;
entity.UpdatedAt = DateTime.Now;
var existingParams = entity.Parameters.ToList();
var commandParamIds = command.Parameters?
.Where(p => !string.IsNullOrEmpty(p.ParamId) && Guid.TryParse(p.ParamId, out _))
.Select(p => Guid.Parse(p.ParamId!))
.ToHashSet() ?? new HashSet<Guid>();
foreach (var existingParam in existingParams)
{
if (!commandParamIds.Contains(existingParam.ParamId))
{
entity.Parameters.Remove(existingParam);
}
}
if (command.Parameters != null)
{
foreach (var paramDto in command.Parameters)
{
if (string.IsNullOrEmpty(paramDto.ParamId) || !Guid.TryParse(paramDto.ParamId, out Guid paramId))
{
var newParam = new ActionParameterDefinition
{
ParamId = Guid.NewGuid(),
ActionConfigId = entity.ActionConfigId,
ParameterName = paramDto.ParameterName,
ParameterValueType = (ParameterValueType)paramDto.ParameterValueType,
ParameterDescription = paramDto.ParameterDescription,
IsMandatory = paramDto.IsMandatory,
DefaultValue = paramDto.DefaultValue,
ParameterSourceType = paramDto.ParameterSourceType.HasValue ? (ParameterSourceType)paramDto.ParameterSourceType.Value : null,
ParameterSourcePath = paramDto.ParameterSourcePath,
ValueConstraints = paramDto.ValueConstraints,
Remarks = paramDto.Remarks,
SortOrder = paramDto.SortOrder,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
};
entity.Parameters.Add(newParam);
}
else
{
var existingParam = entity.Parameters.FirstOrDefault(p => p.ParamId == paramId);
if (existingParam != null)
{
existingParam.ParameterName = paramDto.ParameterName;
existingParam.ParameterValueType = (ParameterValueType)paramDto.ParameterValueType;
existingParam.ParameterDescription = paramDto.ParameterDescription;
existingParam.IsMandatory = paramDto.IsMandatory;
existingParam.DefaultValue = paramDto.DefaultValue;
existingParam.ParameterSourceType = paramDto.ParameterSourceType.HasValue ? (ParameterSourceType)paramDto.ParameterSourceType.Value : null;
existingParam.ParameterSourcePath = paramDto.ParameterSourcePath;
existingParam.ValueConstraints = paramDto.ValueConstraints;
existingParam.Remarks = paramDto.Remarks;
existingParam.SortOrder = paramDto.SortOrder;
existingParam.UpdatedAt = DateTime.Now;
}
}
}
}
await _repository.UpdateAsync(entity, context.CancellationToken);
}
await context.RespondAsync(ApiResponse.Successful());
}
catch (Exception ex)
{
await context.RespondAsync(ApiResponse.Failed(ex.Message));
}
}
}