ReleaseStorageLocationCommandHandler.cs
2.36 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
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands.StorageLocations;
using Rcs.Cyaninetech.Services;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands.StorageLocations;
/// <summary>
/// 释放库位命令处理器
/// </summary>
public class ReleaseStorageLocationCommandHandler : IConsumer<ReleaseStorageLocationCommand>
{
private readonly ILogger<ReleaseStorageLocationCommandHandler> _logger;
private readonly IStorageLocationRepository _storageLocationRepository;
private readonly ILanYinService _lanYinService;
public ReleaseStorageLocationCommandHandler(
ILogger<ReleaseStorageLocationCommandHandler> logger,
IStorageLocationRepository storageLocationRepository,
ILanYinService lanYinService)
{
_logger = logger;
_storageLocationRepository = storageLocationRepository;
_lanYinService = lanYinService;
}
public async Task Consume(ConsumeContext<ReleaseStorageLocationCommand> context)
{
var command = context.Message;
try
{
var location = await _storageLocationRepository.GetByIdAsync(command.LocationId, context.CancellationToken);
if (location == null)
{
await context.RespondAsync(ApiResponse.Failed($"库位不存在: {command.LocationId}"));
return;
}
var releaseResult = await _lanYinService.ReleaseStoreLocationAsync(location.LocationCode, context.CancellationToken);
if (!releaseResult.Success)
{
await context.RespondAsync(ApiResponse.Failed(releaseResult.Message));
return;
}
location.Status = StorageLocationStatus.Empty;
location.UpdatedAt = DateTime.Now;
await _storageLocationRepository.UpdateAsync(location, context.CancellationToken);
await _storageLocationRepository.SaveChangesAsync(context.CancellationToken);
await context.RespondAsync(ApiResponse.Successful("释放库位成功"));
}
catch (Exception ex)
{
_logger.LogError(ex, "释放库位失败: {LocationId}", command.LocationId);
await context.RespondAsync(ApiResponse.Failed(ex.Message));
}
}
}