
//--------------------------------------------------------------------
//     此代码由liquid模板自动生成  byzhouhang 20240909
//     生成时间 2024-10-10 08:28:28Z 
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------
using IRaCIS.Core.Domain.Models;
using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Infrastructure.Extention;
using System.Threading.Tasks;
using IRaCIS.Core.Infra.EFCore;
using MassTransit.Mediator;
using Newtonsoft.Json;
using MassTransit;
using IRaCIS.Core.Domain;
using IRaCIS.Core.Domain.Share;
using System.Globalization;
namespace IRaCIS.Core.Application.Service;

[ApiExplorerSettings(GroupName = "Common")]
public class EventStoreRecordService(IRepository<EventStoreRecord> _eventStoreRecordRepository, IMediator _mediator,IPublishEndpoint _publishEndpoint, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService
{
    /// <summary>
    /// 邮件事件消息列表
    /// </summary>
    /// <param name="inQuery"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<PageOutput<EventStoreRecordView>> GetEventStoreRecordList(EventStoreRecordQuery inQuery)
    {

        var eventStoreRecordQueryable = _eventStoreRecordRepository.WhereIf(inQuery.EventTypeName.IsNotNullOrEmpty(),t=>t.EventTypeName.Contains(inQuery.EventTypeName))
                                    .ProjectTo<EventStoreRecordView>(_mapper.ConfigurationProvider);

        var pageList = await eventStoreRecordQueryable.ToPagedListAsync(inQuery,nameof(EventStoreRecordView.CreateTime));

        return pageList;
    }

    /// <summary>
    /// 重新发布失败的事件消息
    /// </summary>
    /// <param name="eventId"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<IResponseOutput> RePublishEvent(Guid eventId)
    {
        var storedEvent = await _eventStoreRecordRepository.FirstOrDefaultAsync(t => t.Id == eventId);

        var domainEvent = storedEvent.EventData.JsonStrToObject(Type.GetType(storedEvent.EventType));

        //IMediator  发布的时候，设置当前线程的CultureInfo  消费者会与发布线程CultureInfo 一致    分布式的话 _publishEndpoint 会不一致，需要手动设置

        await _publishEndpoint.Publish(domainEvent.GetType(), domainEvent);

        return ResponseOutput.Ok();

    }
}



