using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using Datory;
using GxPress.Common.Tools;
using GxPress.EnumConst;
using GxPress.Result.Reply;

namespace GxPress.Service.Implement.Reply
{
    /// <summary>
    /// 获取我收到的或者我回复的评论
    /// </summary>
    public partial class ReplyService
    {
        /// <summary>
        /// 获取我收到的评论
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<ReplyResult>> GetReceiptCommentResult(int userId)
        {
            //获取回复我的
            string sql = $@"SELECT 
                                    a.Id,
                                    a.ArticleId AS SourceId,
                                    a.Content AS CommentContent,
                                    a.TypeValue,
                                    a.CreatedDate,
                                    a.Pid,
                                    b.Id AS UserId,
                                    b.Name as UserName
                                FROM
                                    tede_comment a
                                        INNER JOIN
                                    tede_user b ON a.UserId = b.Id
                                WHERE
                                    a.Pid IN (SELECT 
                                            id
                                        FROM
                                            tede_comment
                                        WHERE
                                            id IN (SELECT 
                                                    Pid
                                                FROM
                                                    tede_comment
                                                WHERE
                                                    pid > 0)
                                                AND userId = {userId})";
            var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
            var connection = database.GetConnection();
            var receipt = await connection.QueryAsync<ReplyResult>(sql);
            sql = $@"SELECT 
                            a.Id,
                            a.ArticleId AS SourceId,
                            a.Content AS CommentContent,
                            a.TypeValue,
                            a.CreatedDate,
                            a.Pid,
                            b.Id AS UserId,
                            b.Name
                        FROM
                            tede_comment a
                                INNER JOIN
                            tede_user b ON a.UserId = b.Id
                        WHERE
                            a.id IN (SELECT 
                                    Pid
                                FROM
                                    tede_comment
                                WHERE
                                    pid > 0)
                                AND userId = {userId}";
            var my = await connection.QueryAsync<ReplyResult>(sql);
            foreach (var item in receipt)
            {
                if (item.Pid > 0)
                {
                    ReplyResult replyResult1 = my.FirstOrDefault(n => n.Id == item.Pid);
                    ReplyResult replyResult = replyResult1;
                    item.Title = replyResult.CommentContent;
                    item.Name = replyResult.Name;
                }
                item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
                if (item.TypeValue == AllTypeConst.Note.GetHashCode())
                    item.TypeValue = AllTypeConst.Topic.GetHashCode();
                item.Remark = ((AllTypeConst)item.TypeValue).GetDescriptionOriginal();
                item.IsComment = true;
            }
            return receipt;
        }
        /// <summary>
        /// 获取我回复的评论
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<ReplyResult>> GetMyReceiptCommentResult(int userId)
        {
            //获取回复我的
            string sql = $@"SELECT 
                                a.Id,
                                a.ArticleId AS SourceId,
                                a.Content AS CommentContent,
                                a.TypeValue,
                                a.CreatedDate,
                                a.Pid,
                                b.Id AS UserId,
                                b.Name AS UserName
                            FROM
                                tede_comment a
                                    INNER JOIN
                                tede_user b ON a.UserId = b.Id
                            WHERE
                                a.pid > 0 AND a.userId = {userId}";
            var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
            var connection = database.GetConnection();
            var receipt = await connection.QueryAsync<ReplyResult>(sql);
            sql = $@"SELECT 
                            a.Id,
                            a.ArticleId AS SourceId,
                            a.Content AS CommentContent,
                            a.TypeValue,
                            a.CreatedDate,
                            a.Pid,
                            b.Id AS UserId,
                            b.Name
                        FROM
                            tede_comment a
                                INNER JOIN
                            tede_user b ON a.UserId = b.Id
                        WHERE
                            a.id IN (SELECT 
                                            a.Pid
                                        FROM
                                            tede_comment a
                                                INNER JOIN
                                            tede_user b ON a.UserId = b.Id
                                        WHERE
                                            a.pid > 0 AND a.userId = {userId})";
            var my = await connection.QueryAsync<ReplyResult>(sql);
            foreach (var item in receipt)
            {
                if (item.Pid > 0)
                {
                    ReplyResult replyResult1 = my.FirstOrDefault(n => n.Id == item.Pid);
                    ReplyResult replyResult = replyResult1;
                    item.Title = replyResult.CommentContent;
                    item.Name = replyResult.Name;
                }
                item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
                if (item.TypeValue == AllTypeConst.Note.GetHashCode())
                    item.TypeValue = AllTypeConst.Topic.GetHashCode();
                item.Remark = ((AllTypeConst)item.TypeValue).GetDescriptionOriginal();
                item.IsComment = true;
            }
            return receipt;
        }
    }
}