using Myshipping.Core; using Furion.DependencyInjection; using Furion.DynamicApiController; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; using System.Linq; using System.Threading.Tasks; using Furion.Logging; using Microsoft.Extensions.Logging; using Myshipping.Core.Entity; using Furion.FriendlyException; using System; using Furion.EventBus; namespace Myshipping.Core.Service { /// /// 大简云消息服务 /// [ApiDescriptionSettings(Name = "DjyMessage", Order = 1)] public class DjyMessageService : IDjyMessageService, IDynamicApiController, ITransient { private readonly SqlSugarRepository _rep; private readonly ISysCacheService _cacheService; private readonly ILogger _logger; private readonly IEventPublisher _publisher; public DjyMessageService(SqlSugarRepository rep, ILogger logger, ISysCacheService cacheService, IEventPublisher publisher) { _rep = rep; _logger = logger; _cacheService = cacheService; this._publisher = publisher; } /// /// 消息列表 /// /// /// [HttpPost("/DjyMessage/Page")] public async Task> Page(DjyMessageQueryInput input) { var query = _rep.AsQueryable() .WhereIF(input.TypeCode.HasValue, x => x.TypeCode == input.TypeCode.Value.ToString()) .WhereIF(input.Module.HasValue, x => x.Module == input.Module.Value.ToString()) .WhereIF(input.IsRead.HasValue, x => x.IsRead == input.IsRead) .WhereIF(input.ProcStatus.HasValue, x => x.ProcStatus == input.ProcStatus.Value.ToString()); if (!string.IsNullOrEmpty(input.SortField) || input.MultiSort == null || input.MultiSort.Count == 0) { query = query.OrderBy(PageInputOrder.OrderBuilder(input.SortField, input.DescSort)); } else { query = query.OrderBy(PageInputOrder.MultiOrderBuilder(input.MultiSort)); } var entities = await query.ToPagedListAsync(input.PageNo, input.PageSize); var list = entities.Adapt>(); return list; } /// /// 消息删除 /// /// /// [HttpPost("/DjyMessage/Delete")] public async Task Delete(long id) { await _rep.AsUpdateable(new DjyMessage() { Id = id, IsDeleted = true }).UpdateColumns(x => new { x.IsDeleted }).ExecuteCommandAsync(); } /// /// 保存消息 /// /// /// [HttpPost("/DjyMessage/Save")] public Task Save(DjyMessageSaveInput input) { throw new System.NotImplementedException(); } /// /// 消息详情 /// /// /// [HttpPost("/DjyMessage/Get")] public async Task Get(long id) { var msg = await _rep.FirstOrDefaultAsync(x => x.Id == id); return msg.Adapt(); } /// /// 读取消息 /// /// /// [HttpPost("/DjyMessage/Read")] public async Task Read(long id) { await _rep.AsUpdateable(new DjyMessage() { Id = id, IsRead = true, ReadTime = DateTime.Now, ReadUser = UserManager.Name }).UpdateColumns(x => new { x.IsRead, x.ReadTime, x.ReadUser }).ExecuteCommandAsync(); } /// /// 处理消息 /// /// [HttpPost("/DjyMessage/Process")] public async Task Process(DjyMessageProcessInput input) { var msg = await _rep.FirstOrDefaultAsync(x => x.Id == input.Id); if (msg.ProcStatus != MessageProcessStatus.UnProcess.ToString()) { throw Oops.Bah($"当前消息已被{msg.ProcUser}在{(msg.ProcTime.HasValue ? msg.ProcTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : "")}处理过"); } msg.ProcStatus = input.ProcStatus.ToString(); msg.ProcResult = input.Comment; msg.ProcUser = UserManager.Name; msg.ProcTime = DateTime.Now; await _rep.AsUpdateable(msg).UpdateColumns(x => new { x.ProcStatus, x.ProcResult, x.ProcUser }).ExecuteCommandAsync(); //发布已处理事件消息 await _publisher.PublishAsync(new ChannelEventSource($"Message:Process:{msg.Module}:{msg.TypeCode}", input.Id)); } } }