using DataSharing.RawData;
using DotNetCore.CAP;
using Hotline.Share.Dtos.Article;
using MapsterMapper;
using Microsoft.Extensions.Logging;
using XF.Domain.Dependency;
using XF.Domain.Repository;

namespace DataSharing.Application.Receivers
{
    /// <summary>
    /// 公告
    /// </summary>
    public class BulletinReceiver : ICapSubscribe, IScopeDependency
    {
        private readonly IMapper _mapper;
        private readonly ILogger<BulletinReceiver> _logger;
        private readonly IRepository<DsBulletin> _dsBulletinRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        /// <param name="dsBulletinRepository"></param>
        public BulletinReceiver(IMapper mapper, ILogger<BulletinReceiver> logger, IRepository<DsBulletin> dsBulletinRepository)
        {
            _mapper = mapper;
            _logger = logger;
            _dsBulletinRepository = dsBulletinRepository;
        }

        /// <summary>
        /// 新增公告数据
        /// </summary>
        /// <param name="a"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [CapSubscribe(Hotline.Share.Mq.EventNames.HotlinePushBulletin)]
        public async Task PushBulletin(PublishBulletinDto dto, CancellationToken cancellationToken)
        {
            var data = _mapper.Map<DsBulletin>(dto);
            data.BulletinId = dto.Id;
            data.BulletinTime = dto.ExaminTime;
            await _dsBulletinRepository.AddAsync(data, cancellationToken);
        }
    }
}
