using System.Collections.Generic; using System.Threading.Tasks; using Dapper; using Datory; using GxPress.Common.Tools; using GxPress.EnumConst; using GxPress.Request.Reply; using GxPress.Result.Reply; using System.Transactions; using System.Linq; namespace GxPress.Service.Implement.Reply { /// /// 点赞 /// public partial class ReplyService { /// /// 获取 笔记话题本赞 /// /// /// public async Task> GetNotePraiseAsync(ReplyRequest request) { var topicNoteConstValue = AllTypeConst.TopicNote.GetHashCode(); var noteConstValue = AllTypeConst.Note.GetHashCode(); string sqlStr = string.Empty; if (!string.IsNullOrEmpty(request.KeyWord)) { sqlStr = $@" AND ( b.Title LIKE '%{request.KeyWord}%' OR b.Content LIKE '%{request.KeyWord}%' or c.Name like '%{request.KeyWord}%')"; } string sql = $@" SELECT a.Id, a.SourceId, a.TypeValue, a.IsRead, b.Title, b.Content, c.Name as UserName, c.AvatarUrl, b.CreatedDate, a.UserId, b.IsTopic, d.Name FROM tede_analyze a INNER JOIN tede_note b ON a.SourceId = b.Id INNER JOIN tede_user c ON c.Id = a.UserId INNER JOIN tede_user d ON d.Id = b.UserId INNER JOIN tede_middle e ON e.MiddleId = b.Id WHERE a.TypeValue in ({topicNoteConstValue},{noteConstValue}) AND e.IsDelete = 0 AND e.FolderType in({topicNoteConstValue},{noteConstValue}) AND a.CommentId = 0 AND e.UserId = b.UserId AND a.AnalyzeType = 1 AND b.UserId ={request.UserId} And b.IsTopic=1 {sqlStr} ORDER BY a.CreatedDate DESC "; var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString")); var connection = database.GetConnection(); var result = await connection.QueryAsync(sql); foreach (var item in result) { item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl); item.CommentContent = "点赞了你的" + ((AllTypeConst)item.TypeValue).GetDescriptionOriginal(); item.Title = string.IsNullOrEmpty(item.Title) ? GetTitleText(item.Content) : item.Title; item.Remark = ((AllTypeConst)item.TypeValue).GetDescriptionOriginal(); item.Content = string.Empty; } return result; } /// /// 获取评论点赞 /// /// /// public async Task> GetCommentPraiseAsync(ReplyRequest request) { var sqlStr = string.Empty; if (!string.IsNullOrEmpty(request.KeyWord)) { sqlStr = $@" AND (a.Content LIKE '%{request.KeyWord}%' OR c.Name LIKE '%{request.KeyWord}%')"; } string sql = $@" SELECT a.Id, a.Content, a.TypeValue, b.IsRead, b.UserId, c.Name AS UserName, c.AvatarUrl, c.Id AS UserId, d.Name, b.CreatedDate FROM tede_comment a INNER JOIN tede_analyze b ON a.Id = b.CommentId INNER JOIN tede_user c ON c.Id = b.UserId INNER JOIN tede_user d ON d.Id = a.UserId WHERE a.TypeValue IN ({AllTypeConst.Topic.GetHashCode()},{AllTypeConst.Notice.GetHashCode()},{AllTypeConst.Note.GetHashCode()},{AllTypeConst.TopicNote.GetHashCode()}) AND a.UserId = {request.UserId} {sqlStr} AND b.AnalyzeType = 2 ORDER BY a.CreatedDate DESC"; var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString")); var connection = database.GetConnection(); var result = await connection.QueryAsync(sql); foreach (var item in result) { item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl); item.CommentContent = "点赞了你的评论"; item.Title = item.Content; item.IsComment = true; item.Remark = ((AllTypeConst)item.TypeValue).GetDescriptionOriginal(); } return result; } /// /// 获取通知点赞 /// /// /// public async Task> GetNoticePraiseAsync(ReplyRequest request) { var sqlStr = string.Empty; if (!string.IsNullOrEmpty(request.KeyWord)) { sqlStr = $@" AND (b.Title LIKE '%{request.KeyWord}%' OR b.Content LIKE '%{request.KeyWord}%' OR c.Name LIKE '%{request.KeyWord}%')"; } string sql = $@" SELECT a.Id, a.SourceId, a.TypeValue, a.IsRead, b.Title, b.Content, c.Name, c.AvatarUrl, b.CreatedDate, a.UserId, d.Name AS UserName FROM tede_analyze a INNER JOIN tede_notice b ON a.SourceId = b.Id INNER JOIN tede_user c ON c.Id = a.UserId INNER JOIN tede_user d ON d.Id = b.UserId INNER JOIN tede_middle e ON e.MiddleId = b.Id WHERE b.UserId = {request.UserId} AND e.UserId = b.UserId AND a.TypeValue ={AllTypeConst.Notice.GetHashCode()} AND e.IsDelete = 0 AND a.CommentId = 0 AND a.AnalyzeType = 1 {sqlStr} ORDER BY a.CreatedDate DESC"; var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString")); var connection = database.GetConnection(); var result = await connection.QueryAsync(sql); foreach (var item in result) { item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl); item.CommentContent = "点赞了你的" + ((AllTypeConst)item.TypeValue).GetDescriptionOriginal(); item.Title = string.IsNullOrEmpty(item.Title) ? GetTitleText(item.Content) : item.Title; item.Remark = ((AllTypeConst)item.TypeValue).GetDescriptionOriginal(); item.Content = string.Empty; } return result; } /// /// 获取话题点赞 /// /// /// public async Task> GetTopicPraiseAsync(ReplyRequest request) { var sqlStr = string.Empty; if (!string.IsNullOrEmpty(request.KeyWord)) { sqlStr = $@" AND ( b.Title LIKE '%{request.KeyWord}%' OR b.Content LIKE '%{request.KeyWord}%' OR c.Name LIKE '%{request.KeyWord}%')"; } string sql = $@" SELECT a.Id, a.SourceId, a.TypeValue, a.IsRead, b.Title, b.Content, c.Name as UserName, c.AvatarUrl, b.CreatedDate, a.UserId, d.Name, g.Id as GroupId, g.Name as GroupName FROM tede_analyze a INNER JOIN tede_topic b ON a.SourceId = b.Id INNER JOIN tede_user c ON c.Id = a.UserId INNER JOIN tede_user d ON d.Id = b.UserId INNER JOIN tede_middle e ON e.MiddleId = b.Id INNER JOIN tede_group g ON g.Id = b.GroupId WHERE a.TypeValue ={AllTypeConst.Topic.GetHashCode()} AND e.IsDelete = 0 AND a.CommentId = 0 AND e.UserId = b.UserId AND a.AnalyzeType = 1 AND b.UserId = {request.UserId} {sqlStr} ORDER BY a.CreatedDate DESC"; var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString")); var connection = database.GetConnection(); var result = await connection.QueryAsync(sql); foreach (var item in result) { item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl); item.CommentContent = "点赞了你的" + ((AllTypeConst)item.TypeValue).GetDescriptionOriginal(); item.Title = string.IsNullOrEmpty(item.Title) ? GetTitleText(item.Content) : item.Title; item.Remark = ((AllTypeConst)item.TypeValue).GetDescriptionOriginal(); item.IsGroup = true; item.Content = string.Empty; } return result; } /// /// 未读点赞数量 /// /// /// public async Task GetUReadPraiseCountAsync(int userId) { //笔记话题本 var sql = $@" SELECT COUNT(1) FROM tede_analyze a INNER JOIN tede_note b ON a.SourceId = b.Id INNER JOIN tede_user c ON c.Id = a.UserId INNER JOIN tede_user d ON d.Id = b.UserId INNER JOIN tede_middle e ON e.MiddleId = b.Id WHERE a.TypeValue in({AllTypeConst.Note.GetHashCode()},{AllTypeConst.TopicNote.GetHashCode()}) AND e.IsDelete = 0 AND a.CommentId = 0 AND e.UserId = b.UserId AND a.AnalyzeType = 1 AND b.UserId = {userId} AND a.IsRead = 0"; var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString")); var connection = database.GetConnection(); var count = await connection.ExecuteScalarAsync(sql); //评论 sql = $@" SELECT count(1) FROM tede_comment a INNER JOIN tede_analyze b ON a.Id = b.CommentId INNER JOIN tede_user c ON c.Id = b.UserId INNER JOIN tede_user d ON d.Id = a.UserId WHERE a.TypeValue IN ({AllTypeConst.Topic.GetHashCode()},{AllTypeConst.Notice.GetHashCode()},{AllTypeConst.Note.GetHashCode()},{AllTypeConst.TopicNote.GetHashCode()}) AND a.UserId ={userId} AND b.AnalyzeType = 2 AND b.IsRead = 0"; count += await connection.ExecuteScalarAsync(sql); //通知 sql = $@" SELECT COUNT(1) FROM tede_analyze a INNER JOIN tede_notice b ON a.SourceId = b.Id INNER JOIN tede_user c ON c.Id = a.UserId INNER JOIN tede_user d ON d.Id = b.UserId INNER JOIN tede_middle e ON e.MiddleId = b.Id WHERE b.UserId ={userId} AND e.UserId = b.UserId AND a.TypeValue ={AllTypeConst.Notice.GetHashCode()} AND e.IsDelete = 0 AND a.CommentId = 0 AND a.AnalyzeType = 1 AND a.IsRead = 0 "; count += await connection.ExecuteScalarAsync(sql); //小组话题 sql = $@" SELECT count(1) FROM tede_analyze a INNER JOIN tede_topic b ON a.SourceId = b.Id INNER JOIN tede_user c ON c.Id = a.UserId INNER JOIN tede_user d ON d.Id = b.UserId INNER JOIN tede_middle e ON e.MiddleId = b.Id INNER JOIN tede_group g ON g.Id = b.GroupId WHERE a.TypeValue = {AllTypeConst.Topic.GetHashCode()} AND e.IsDelete = 0 AND a.CommentId = 0 AND e.UserId = b.UserId AND a.AnalyzeType = 1 AND b.UserId ={userId} AND a.IsRead = 0 "; count += await connection.ExecuteScalarAsync(sql); return count; } /// /// 修改未读点赞数量 /// /// /// public async Task UpdateUReadPraiseAsync(int userId) { try { using (var tran = new TransactionScope()) { var intList = new List(); //话题本/笔记本点赞 var sql = $@"SELECT a.Id FROM tede_analyze a INNER JOIN tede_note b ON a.SourceId = b.Id INNER JOIN tede_user c ON c.Id = a.UserId INNER JOIN tede_user d ON d.Id = b.UserId INNER JOIN tede_middle e ON e.MiddleId = b.Id WHERE a.TypeValue in({AllTypeConst.Note.GetHashCode()},{AllTypeConst.TopicNote.GetHashCode()}) AND e.IsDelete = 0 AND a.CommentId = 0 AND e.UserId = b.UserId AND a.AnalyzeType = 1 AND b.UserId = {userId} AND a.IsRead = 0"; var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString")); var connection = database.GetConnection(); intList.AddRange(await connection.QueryAsync(sql)); //小组话题 通知 笔记 评论点赞 sql = $@" SELECT b.Id FROM tede_comment a INNER JOIN tede_analyze b ON a.Id = b.CommentId INNER JOIN tede_user c ON c.Id = b.UserId INNER JOIN tede_user d ON d.Id = a.UserId WHERE a.TypeValue IN ({AllTypeConst.Topic.GetHashCode()},{AllTypeConst.Notice.GetHashCode()},{AllTypeConst.Note.GetHashCode()},{AllTypeConst.TopicNote.GetHashCode()}) AND a.UserId ={userId} AND b.AnalyzeType = 2 AND b.IsRead = 0"; intList.AddRange(await connection.QueryAsync(sql)); //通知点赞 sql = $@" SELECT a.Id FROM tede_analyze a INNER JOIN tede_notice b ON a.SourceId = b.Id INNER JOIN tede_user c ON c.Id = a.UserId INNER JOIN tede_user d ON d.Id = b.UserId INNER JOIN tede_middle e ON e.MiddleId = b.Id WHERE b.UserId ={userId} AND e.UserId = b.UserId AND a.TypeValue ={AllTypeConst.Notice.GetHashCode()} AND e.IsDelete = 0 AND a.CommentId = 0 AND a.AnalyzeType = 1"; intList.AddRange(await connection.QueryAsync(sql)); sql = $@" SELECT a.Id FROM tede_analyze a INNER JOIN tede_topic b ON a.SourceId = b.Id INNER JOIN tede_user c ON c.Id = a.UserId INNER JOIN tede_user d ON d.Id = b.UserId INNER JOIN tede_middle e ON e.MiddleId = b.Id INNER JOIN tede_group g ON g.Id = b.GroupId WHERE a.TypeValue = {AllTypeConst.Topic.GetHashCode()} AND e.IsDelete = 0 AND a.CommentId = 0 AND e.UserId = b.UserId AND a.AnalyzeType = 1 AND b.UserId ={userId}"; intList.AddRange(await connection.QueryAsync(sql)); if (intList.Count() > 0) { var query = Q.NewQuery(); query.Set(nameof(Entity.Analyze.Analyze.IsRead), true); query.WhereIn(nameof(Entity.Analyze.Analyze.Id), intList); var result = await analyzeRepository.UpdateAsync(query); } tran.Complete(); } } catch { return false; } return true; } } }