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
{
    /// <summary>
    /// 回复笔记或者话题
    /// </summary>
    public partial class ReplyService
    {
        /// <summary>
        /// 获取回复我的笔记或者话题
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<IEnumerable<ReplyResult>> GetReplyNoteOrTopicResults(ReplyRequest request)
        {
            var noteConstValue = AllTypeConst.TopicNote.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
                                            and b.IsTopic=1
                                            {sqlStr}
                                    ORDER BY a.CreatedDate DESC
          ";
            var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
            var connection = database.GetConnection();
            return await connection.QueryAsync<ReplyResult, Entity.Note.Note, Entity.User, Entity.User, ReplyResult>(sql,
                        (result, note, user, userModel) =>
                        {
                            result.UserName = user != null ? user.Name : "";
                            result.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
                            result.Title = string.IsNullOrEmpty(note.Title) ? GetTitleText(note.Content) : note.Title; ;
                            //result.Content = note.Content;
                            result.CreatedDate = note.CreatedDate;
                            result.Content = string.Empty;
                            result.Name = userModel.Name;
                            result.Remark =((AllTypeConst)result.TypeValue).GetDescriptionOriginal();
                            return result;
                        }, splitOn: "Id,Title,Name,Name");

        }
        /// <summary>
        /// 获取回复我的小组话题
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<IEnumerable<ReplyResult>> GetReplyGroupTopicResults(ReplyRequest request)
        {
            var topicConstValue = AllTypeConst.Topic.GetHashCode();
            string sqlStr = string.Empty;
            if (!string.IsNullOrEmpty(request.KeyWord))
            {
                sqlStr = $@" AND (c.Name LIKE '%{request.KeyWord}%'
                                            OR a.Content LIKE '%{request.KeyWord}%'
                                            OR b.Title LIKE '%{request.KeyWord}%'
                                            OR b.Content 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.GroupId,
                                        c.Name AS UserName,
                                        c.AvatarUrl,
                                        d.Name,
                                        g.Id AS GroupId,
                                        g.Name AS GroupName
                                    FROM
                                        tede_comment a
                                            INNER JOIN
                                        tede_topic 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
                                            INNER JOIN
                                        tede_middle e ON e.MiddleId = b.Id
                                            INNER JOIN
                                        tede_group g ON g.id = b.GroupId
                                    WHERE
                                        1 = 1 AND a.TypeValue = {topicConstValue}
                                            AND b.GroupId > 0
                                            AND e.IsDelete = 0
                                            AND a.Content <> ''
                                            {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<ReplyResult>(sql);
            foreach (var item in result)
            {
                item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
                item.Title = string.IsNullOrEmpty(item.Title) ? GetTitleText(item.Content) : item.Title; ;
                //result.Content = note.Content;
                item.Remark = item.GroupName;
                item.Content = string.Empty;
                item.IsGroup = true;
            }
            return result;
        }
        public string GetTitleText(string content)
        {
            var data = JsonConvert.DeserializeObject<List<CollectionContentJsonData>>(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;
        }

        /// <summary>
        /// 获取回复我的通知站内信
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<IEnumerable<ReplyResult>> GetReplyNoticeTopicResults(ReplyRequest request)
        {
            string sqlStr = string.Empty;
            if (!string.IsNullOrEmpty(request.KeyWord))
            {
                sqlStr = $@" AND (c.Name LIKE '%{request.KeyWord}%'
                                            OR a.Content LIKE '%{request.KeyWord}%'
                                            OR b.Title LIKE '%{request.KeyWord}%'
                                            OR b.Content 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,
                                        c.Name AS UserName,
                                        c.AvatarUrl,
                                        d.Name
                                    FROM
                                        tede_comment a
                                            INNER JOIN
                                        tede_notice 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
                                            INNER JOIN
                                        tede_middle e ON e.MiddleId = b.Id
                                    WHERE
                                        1 = 1 
                                            AND e.IsDelete = 0 and e.UserId=b.UserId
                                            AND a.Content <> '' and a.TypeValue in(5,6)
                                            {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<ReplyResult>(sql);
            foreach (var item in result)
            {
                item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
                item.Title = string.IsNullOrEmpty(item.Title) ? GetTitleText(item.Content) : item.Title; ;
                //result.Content = note.Content;
                item.Remark = ((AllTypeConst)item.TypeValue).GetDescriptionOriginal();
                item.Content = string.Empty;

            }
            return result;
        }
    }
}