Ejemplo n.º 1
0
        /// <summary>
        /// 更新私信的阅读状态
        /// </summary>
        /// <param name="sessionId">私信会话Id</param>
        /// <param name="userId">会话拥有者UserId</param>
        public bool SetIsRead(long sessionId, long userId)
        {
            var  sql    = Sql.Builder;
            bool isRead = false;

            Database dao = CreateDAO();

            dao.OpenSharedConnection();

            sql.Select("MessageId")
            .From("tn_Messages")
            .Append("Where ReceiverUserId = @0 and IsRead = 0", userId)
            .Append(" and exists(select 1 from tn_MessagesInSessions where tn_Messages.MessageId = tn_MessagesInSessions.MessageId and tn_MessagesInSessions.SessionId = @0)", sessionId);

            //获取未读私信Id
            List <long> ids = dao.Fetch <long>(sql);

            if (ids.Count() > 0)
            {
                sql = Sql.Builder.Append("update tn_Messages")
                      .Append("Set IsRead = 1")
                      .Where("MessageId in (@MessageIds)", new { MessageIds = ids });

                dao.Execute(sql);



                //更新会话的未读信息数
                MessageSessionRepository repository = new MessageSessionRepository();
                MessageSession           session    = repository.Get(sessionId);
                if (session != null)
                {
                    session.UnreadMessageCount = session.UnreadMessageCount > ids.Count() ? session.UnreadMessageCount - ids.Count() : 0;
                    repository.Update(session);
                }

                #region 处理缓存

                RealTimeCacheHelper.IncreaseAreaVersion("ReceiverUserId", userId);

                #endregion 处理缓存

                isRead = true;
            }

            dao.CloseSharedConnection();

            return(isRead);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 把实体entity添加到数据库
        /// </summary>
        /// <param name="entity">待创建实体</param>
        /// <returns>实体主键</returns>
        public override object Insert(Message entity)
        {
            var         sql = Sql.Builder;
            IList <Sql> sqls = new List <Sql>();
            long        senderSessionId = 0, receiverSessionId = 0;
            MessageSessionRepository messageSessionRepository = new MessageSessionRepository();

            PetaPocoDatabase dao = CreateDAO();

            dao.OpenSharedConnection();

            object id = base.Insert(entity);

            sql.Append("update tn_MessageSessions")
            .Append("set LastMessageId = @0,MessageCount = MessageCount + 1,LastModified = @1", id, entity.DateCreated)
            .Where("UserId = @0", entity.SenderUserId)
            .Where("OtherUserId = @0", entity.ReceiverUserId);

            //判断发件人的会话是否存在
            if (dao.Execute(sql) == 0)
            {
                sql = Sql.Builder;
                sql.Append(@"insert into tn_MessageSessions(UserId,OtherUserId,LastMessageId,MessageType,MessageCount,LastModified) values (@0,@1,@2,@3,1,@4)", entity.SenderUserId, entity.ReceiverUserId, id, (int)entity.MessageType, entity.DateCreated);
                dao.Execute(sql);
            }

            //获取发件人的会话Id
            sql = Sql.Builder;
            sql.Select("SessionId")
            .From("tn_MessageSessions")
            .Where("UserId = @0", entity.SenderUserId)
            .Where("OtherUserId = @0", entity.ReceiverUserId);
            senderSessionId = dao.FirstOrDefault <long>(sql);

            //判断收件人的会话是否存在
            sql = Sql.Builder;
            sql.Append("update tn_MessageSessions")
            .Append("set LastMessageId = @0,MessageCount = MessageCount + 1,UnreadMessageCount = UnreadMessageCount + 1,LastModified = @1", id, entity.DateCreated)
            .Where("UserId = @0", entity.ReceiverUserId)
            .Where("OtherUserId = @0", entity.SenderUserId);
            if (dao.Execute(sql) == 0)
            {
                sql = Sql.Builder;
                sql.Append("insert into tn_MessageSessions(UserId,OtherUserId,LastMessageId,MessageType,MessageCount,UnreadMessageCount,LastModified) values (@0,@1,@2,@3,1,1,@4)", entity.ReceiverUserId, entity.SenderUserId, id, (int)entity.MessageType, entity.DateCreated);
                dao.Execute(sql);
            }

            //获取收件人的会话Id
            sql = Sql.Builder;
            sql.Select("SessionId")
            .From("tn_MessageSessions")
            .Where("UserId = @0", entity.ReceiverUserId)
            .Where("OtherUserId = @0", entity.SenderUserId);
            receiverSessionId = dao.FirstOrDefault <long>(sql);

            //添加会话与私信的关系
            sqls.Add(Sql.Builder.Append("insert into tn_MessagesInSessions (SessionId,MessageId) values (@0,@1)", senderSessionId, id));
            sqls.Add(Sql.Builder.Append("insert into tn_MessagesInSessions (SessionId,MessageId) values (@0,@1)", receiverSessionId, id));
            dao.Execute(sqls);

            dao.CloseSharedConnection();

            #region 缓存处理

            if (RealTimeCacheHelper.EnableCache)
            {
                RealTimeCacheHelper.IncreaseAreaVersion("UserId", entity.SenderUserId);
                RealTimeCacheHelper.IncreaseAreaVersion("UserId", entity.ReceiverUserId);

                var realTimeCacheHelper = EntityData.ForType(typeof(MessageInSession)).RealTimeCacheHelper;
                realTimeCacheHelper.IncreaseAreaVersion("SessionId", senderSessionId);
                realTimeCacheHelper.IncreaseAreaVersion("SessionId", receiverSessionId);

                realTimeCacheHelper = EntityData.ForType(typeof(MessageSession)).RealTimeCacheHelper;
                realTimeCacheHelper.IncreaseAreaVersion("UserId", entity.SenderUserId);
                realTimeCacheHelper.IncreaseAreaVersion("UserId", entity.ReceiverUserId);

                realTimeCacheHelper.IncreaseEntityCacheVersion(senderSessionId);
                realTimeCacheHelper.IncreaseEntityCacheVersion(receiverSessionId);


                string         cacheKey      = realTimeCacheHelper.GetCacheKeyOfEntity(senderSessionId);
                MessageSession senderSession = cacheService.Get <MessageSession>(cacheKey);
                if (senderSession != null)
                {
                    senderSession.LastMessageId = entity.MessageId;
                    senderSession.LastModified  = entity.DateCreated;
                    senderSession.MessageCount++;
                }

                cacheKey = realTimeCacheHelper.GetCacheKeyOfEntity(receiverSessionId);
                MessageSession receiverSession = cacheService.Get <MessageSession>(cacheKey);
                if (receiverSession != null)
                {
                    receiverSession.LastMessageId = entity.MessageId;
                    receiverSession.LastModified  = entity.DateCreated;
                    receiverSession.MessageCount++;
                    receiverSession.UnreadMessageCount++;
                }

                cacheKey = GetCacheKey_UnreadCount(entity.ReceiverUserId);
                int?count = cacheService.Get(cacheKey) as int?;
                count = count ?? 0;
                count++;
                cacheService.Set(cacheKey, count, CachingExpirationType.SingleObject);
            }
            #endregion 缓存处理

            return(id);
        }