using MediatR; using Microsoft.Extensions.Logging; using TakeoutSaaS.Application.App.Merchants.Commands; using TakeoutSaaS.Application.App.Merchants.Dto; using TakeoutSaaS.Domain.Merchants.Entities; using TakeoutSaaS.Domain.Merchants.Repositories; namespace TakeoutSaaS.Application.App.Merchants.Handlers; /// /// 创建商户命令处理器。 /// public sealed class CreateMerchantCommandHandler(IMerchantRepository merchantRepository, ILogger logger) : IRequestHandler { private readonly IMerchantRepository _merchantRepository = merchantRepository; private readonly ILogger _logger = logger; /// public async Task Handle(CreateMerchantCommand request, CancellationToken cancellationToken) { var merchant = new Merchant { BrandName = request.BrandName.Trim(), BrandAlias = request.BrandAlias?.Trim(), LogoUrl = request.LogoUrl?.Trim(), Category = request.Category?.Trim(), ContactPhone = request.ContactPhone.Trim(), ContactEmail = request.ContactEmail?.Trim(), Status = request.Status, JoinedAt = DateTime.UtcNow }; await _merchantRepository.AddMerchantAsync(merchant, cancellationToken); await _merchantRepository.SaveChangesAsync(cancellationToken); _logger.LogInformation("创建商户 {MerchantId} - {BrandName}", merchant.Id, merchant.BrandName); return MapToDto(merchant); } private static MerchantDto MapToDto(Merchant merchant) => new() { Id = merchant.Id, TenantId = merchant.TenantId, BrandName = merchant.BrandName, BrandAlias = merchant.BrandAlias, LogoUrl = merchant.LogoUrl, Category = merchant.Category, ContactPhone = merchant.ContactPhone, ContactEmail = merchant.ContactEmail, Status = merchant.Status, JoinedAt = merchant.JoinedAt, CreatedAt = merchant.CreatedAt }; }