using System; using System.Data; using System.Threading.Tasks; using Yitter.IdGenerator; using Znyc.Recruitment.Admin.Commons.Cache; using Znyc.Recruitment.Admin.Commons.Services; using Znyc.Recruitment.Admin.Security.Dtos; using Znyc.Recruitment.Admin.Security.Entitys; using Znyc.Recruitment.Admin.Security.IRepositories; using Znyc.Recruitment.Admin.Security.IServices; namespace Znyc.Recruitment.Admin.Security.Services { /// /// public class MessageLogsService : BaseService, IMessageLogsService { private readonly IMessageLogsRepository _messageLogsRepository; private readonly IMessageRepository _messageRepository; public MessageLogsService( IMessageLogsRepository messageLogsRepository, IMessageRepository messageRepository ) : base(messageLogsRepository) { _messageLogsRepository = messageLogsRepository; _messageRepository = messageRepository; } /// /// 同步新增实体。 /// /// 实体 /// 事务对象 /// public override long Insert(MessageLogsEntity entity, IDbTransaction trans = null) { entity.Id = YitIdHelper.NextId(); long result = repository.Insert(entity, trans); return result; } /// /// 异步更新实体。 /// /// 实体 /// 主键ID /// 事务对象 /// public override async Task UpdateAsync(MessageLogsEntity entity, long id, IDbTransaction trans = null) { bool result = await repository.UpdateAsync(entity, id, trans); return result; } /// /// 异步步新增实体。 /// /// 实体 /// 事务对象 /// public override async Task InsertAsync(MessageLogsEntity entity, IDbTransaction trans = null) { entity.Id = YitIdHelper.NextId(); int result = await repository.InsertAsync(entity, trans); return result; } /// /// 添加消息记录 /// /// /// public async Task AddAsync(MessageInputDto input) { MessageEntity entity = new MessageEntity { Id = YitIdHelper.NextId(), SendId = input.CreatedUserId, ProductId = input.ProductId, Title = input.Title, MessageTitle = input.MessageTitle, ProductType = input.ProductType, Type = 2, GroupId = 0, Content = "", SendTime = DateTime.Now, CreatedTime = DateTime.Now, CreatedUserId = input.CreatedUserId, ModifiedTime = DateTime.Now }; int id = await _messageRepository.InsertAsync(entity); MessageLogsEntity messageLogs = new MessageLogsEntity { Id = YitIdHelper.NextId(), ReceiverId = input.UserId, MessageId = entity.Id, Status = 0, //未读 CreatedTime = DateTime.Now, ModifiedTime = DateTime.Now, CreatedUserId = input.CreatedUserId }; await _messageLogsRepository.InsertAsync(messageLogs); CacheHelper cacheHelper = new CacheHelper(); string key = string.Format("unread:number:{0}", input.UserId); cacheHelper.Remove(key); return true; } } }