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;
using GxPress.Result.Reply;
using Newtonsoft.Json;
namespace GxPress.Service.Implement.Reply
{
///
/// 回复笔记或者话题
///
public partial class ReplyService
{
///
/// 获取回复我的笔记或者话题
///
///
///
public async Task> GetReplyNoteOrTopicResults(ReplyRequest request)
{
var noteConstValue = AllTypeConst.Note.GetHashCode();
string sqlStr = string.Empty;
if (!string.IsNullOrEmpty(request.KeyWord))
{
sqlStr = $@"AND (a.Content LIKE '%{request.KeyWord}%'
OR b.Title LIKE '%{request.KeyWord}%'
OR b.Content LIKE '%{request.KeyWord}%' or c.Name like '%{request.KeyWord}%')";
}
if (request.TypeValue == 0)
sqlStr = $@" and b.UserId = {request.UserId}";
else
sqlStr = $@" and a.UserId = {request.UserId}";
string sql = $@"
SELECT
a.Id,
a.UserId,
a.ArticleId as SourceId,
a.Content as CommentContent,
a.Pid,
a.TypeValue,
b.Title,
b.Content,
b.CreatedDate,
b.IsTopic,
c.Name,
c.AvatarUrl,
d.Name
FROM
tede_comment a
INNER JOIN
tede_note b ON a.ArticleId = b.Id
INNER JOIN
tede_user c ON c.Id = a.UserId
INNER JOIN
tede_user d ON d.Id = b.UserId
WHERE
1=1 AND a.TypeValue ={noteConstValue}
AND b.IsDelete = 0
{sqlStr}
ORDER BY a.CreatedDate DESC
";
var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
var connection = database.GetConnection();
return await connection.QueryAsync(sql,
(result, note, user, userModel) =>
{
result.UserName = user != null ? user.Name : "";
result.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
result.IsTopic = note.IsTop;
result.Title = string.IsNullOrEmpty(note.Title) ? GetTitleText(note.Content) : note.Title; ;
//result.Content = note.Content;
result.CreatedDate = note.CreatedDate;
result.Name = userModel.Name;
result.Remark = result.IsTopic ? AllTypeConst.Topic.GetDescriptionOriginal() : AllTypeConst.Note.GetDescriptionOriginal();
return result;
}, splitOn: "Id,Title,Name,Name");
}
public string GetTitleText(string content)
{
var data = JsonConvert.DeserializeObject>(content);
int i = 0;
foreach (var item in data)
{
if (item.Type == AllTypeConst.Text.GetHashCode() && i == 0)
return item.Text;
else if (item.Type == AllTypeConst.Image.GetHashCode())
return "[图片]";
else if (item.Type == AllTypeConst.File.GetHashCode())
return "[附件]";
else
return "[附件]";
}
return string.Empty;
}
}
}