using AutoMapper;
using Kean.Domain.Material.Commands;
using Kean.Domain.Material.Events;
using Kean.Domain.Material.Repositories;
using System.Threading;
using System.Threading.Tasks;

namespace Kean.Domain.Material.CommandHandlers
{
    /// <summary>
    /// 创建物料命令处理程序
    /// </summary>
    public sealed class CreateMaterialCommandHandler : CommandHandler<CreateMaterialCommand>
    {
        private readonly ICommandBus _commandBus; // 命令总线
        private readonly IMapper _mapper; // 模型映射
        private readonly IMaterialRepository _materialRepository; // 物料仓库

        /// <summary>
        /// 依赖注入
        /// </summary>
        public CreateMaterialCommandHandler(
            ICommandBus commandBus,
            IMapper mapper,
            IMaterialRepository materialRepository)
        {
            _commandBus = commandBus;
            _mapper = mapper;
            _materialRepository = materialRepository;
        }

        /// <summary>
        /// 处理程序
        /// </summary>
        public override async Task Handle(CreateMaterialCommand command, CancellationToken cancellationToken)
        {
            if (command.ValidationResult.IsValid)
            {
                if (command.Category.HasValue && !await _materialRepository.IsCategoryExist(command.Category.Value))
                {
                    await _commandBus.Notify(nameof(command.Category), "品类不存在", command.Category, nameof(ErrorCode.Gone),
                        cancellationToken: cancellationToken);
                    return;
                }
                if (await _materialRepository.IsMaterialCodeExist(command.Code, null))
                {
                    await _commandBus.Notify(nameof(command.Code), "料号重复", command.Code, nameof(ErrorCode.Conflict),
                        cancellationToken: cancellationToken);
                    return;
                }
                //if (await _materialRepository.IsMaterialNameExist(command.Name, null))
                //{
                //    await _commandBus.Notify(nameof(command.Name), "名称重复", command.Name, nameof(ErrorCode.Conflict),
                //        cancellationToken: cancellationToken);
                //    return;
                //}
                Output(nameof(command.Id), await _materialRepository.CreateMaterial(_mapper.Map<Models.Material>(command)));
                await _commandBus.Trigger(_mapper.Map<CreateMaterialSuccessEvent>(command), cancellationToken);
            }
            else
            {
                await _commandBus.Notify(command.ValidationResult,
                    cancellationToken: cancellationToken);
            }
        }
    }
}