using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mall.Common;
using Mall.Common.Plugin;
using Newtonsoft.Json;
using Mall.Common.API;
using Newtonsoft.Json.Linq;
using VT.FW.DB;
using Mall.Repository.Miai;
using Mall.Model.Extend.Miai;

namespace Mall.Module.Miai
{
    /// <summary>
    /// 相亲处理层
    /// </summary>
    public class MiaiModule
    {
        private readonly RB_Miai_ForumRepository miai_ForumRepository = new RB_Miai_ForumRepository();
        private readonly RB_Miai_ActivityRepository miai_ActivityRepository = new RB_Miai_ActivityRepository();
        private readonly RB_Miai_ActivityEnrollRepository miai_ActivityEnrollRepository = new RB_Miai_ActivityEnrollRepository();
        private readonly RB_Miai_ActivityDiscussRepository miai_ActivityDiscussRepository = new RB_Miai_ActivityDiscussRepository();

        #region 活动版块
        /// <summary>
        /// 获取活动版本分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Miai_Forum_Extend> GetForumPageList(int pageIndex, int pageSize, out long count, RB_Miai_Forum_Extend dmodel)
        {
            return miai_ForumRepository.GetPageList(pageIndex, pageSize, out count, dmodel);
        }

        /// <summary>
        /// 获取活动版块列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Miai_Forum_Extend> GetForumList(RB_Miai_Forum_Extend demodel)
        {
            return miai_ForumRepository.GetList(demodel);
        }

        /// <summary>
        /// 保存活动版块
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public bool SetForumInfo(RB_Miai_Forum_Extend demodel)
        {
            if (demodel.Id > 0)
            {
                Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                    { nameof(RB_Miai_Forum_Extend.Name),demodel.Name},
                    { nameof(RB_Miai_Forum_Extend.Icon),demodel.Icon},
                    { nameof(RB_Miai_Forum_Extend.Sort),demodel.Sort},
                    { nameof(RB_Miai_Forum_Extend.IsShow),demodel.IsShow},
                    { nameof(RB_Miai_Forum_Extend.UpdateDate),demodel.UpdateDate},
                };
                List<WhereHelper> wheres = new List<WhereHelper>() {
                    new WhereHelper(){
                         FiledName=nameof(RB_Miai_Forum_Extend.Id),
                          FiledValue=demodel.Id,
                           OperatorEnum=OperatorEnum.Equal
                    }
                };
                bool flag = miai_ForumRepository.Update(keyValues, wheres);
                return flag;
            }
            else {
                bool flag = miai_ForumRepository.Insert(demodel) > 0;
                return flag;
            }
        }

        /// <summary>
        /// 设置版块状态
        /// </summary>
        /// <param name="forumId"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool SetForumState(int forumId, int type)
        {
            if (type == 1)
            {
                var model = miai_ForumRepository.GetEntity(forumId);
                if (model == null) { return false; }
                Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                    { nameof(RB_Miai_Forum_Extend.IsShow),model.IsShow==1?2:1},
                    { nameof(RB_Miai_Forum_Extend.UpdateDate),DateTime.Now},
                };
                List<WhereHelper> wheres = new List<WhereHelper>() {
                    new WhereHelper(){
                         FiledName=nameof(RB_Miai_Forum_Extend.Id),
                          FiledValue=forumId,
                           OperatorEnum=OperatorEnum.Equal
                    }
                };
                bool flag = miai_ForumRepository.Update(keyValues, wheres);
                return flag;
            }
            else {
                Dictionary<string, object> keyValues = new Dictionary<string, object>() {
                    { nameof(RB_Miai_Forum_Extend.Status),1},
                    { nameof(RB_Miai_Forum_Extend.UpdateDate),DateTime.Now},
                };
                List<WhereHelper> wheres = new List<WhereHelper>() {
                    new WhereHelper(){
                         FiledName=nameof(RB_Miai_Forum_Extend.Id),
                          FiledValue=forumId,
                           OperatorEnum=OperatorEnum.Equal
                    }
                };
                bool flag = miai_ForumRepository.Update(keyValues, wheres);
                return flag;
            }
        }

        /// <summary>
        /// 获取活动分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Miai_Activity_Extend> GetActivityPageList(int pageIndex, int pageSize, out long count, RB_Miai_Activity_Extend demodel)
        {
            var list = miai_ActivityRepository.GetPageList(pageIndex, pageSize, out count, demodel);
            if (list.Any())
            {
                string ActivityIds = string.Join(",", list.Select(x => x.Id));
                //查询活动回复数量
                var rlist = miai_ActivityDiscussRepository.GetActivityReplyNumList(ActivityIds);
                foreach (var item in list)
                {
                    item.ImageList = new List<string>();
                    if (!string.IsNullOrEmpty(item.Images) && item.Images != "[]")
                    {
                        item.ImageList = JsonConvert.DeserializeObject<List<string>>(item.Images);
                    }
                    item.ReplyNum = rlist.Where(x => x.ActivityId == item.Id).FirstOrDefault()?.ReplyNum ?? 0;
                }
            }
            return list;
        }

        /// <summary>
        /// 获取活动信息
        /// </summary>
        /// <param name="activityId"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public object GetActivityInfo(int activityId, AppletUserInfo userInfo)
        {
            var model = miai_ActivityRepository.GetEntity(activityId);
            if (model == null) { return ""; }
            //获取活动报名人数
            int PeopleNum = miai_ActivityEnrollRepository.GetActivityEnrollNum(activityId);
            //获取当前用户是否已报名
            bool IsEnroll = miai_ActivityEnrollRepository.GetList(new RB_Miai_ActivityEnroll_Extend() { TenantId = model.TenantId, ActivityId = activityId, UserId = userInfo.UserId }).Any();
            //查询活动回复数量
            var rlist = miai_ActivityDiscussRepository.GetActivityReplyNumList(activityId.ToString());
            return new
            {
                model.Id,
                model.ActivityTitle,
                model.Content,
                ImageList = !string.IsNullOrEmpty(model.Images) ? JsonConvert.DeserializeObject<List<string>>(model.Images) : new List<string>() { },
                model.UserId,
                Deadline = model.Deadline.ToString("yyyy-MM-dd"),
                CreateDate = model.CreateDate.ToString("yyyy-MM-dd HH:mm:ss"),
                PeopleNum,
                IsEnroll,
                ReplyNum = rlist.FirstOrDefault()?.ReplyNum ?? 0
            };
        }

        /// <summary>
        /// 获取活动评论分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Miai_ActivityDiscuss_Extend> GetActivityDiscussPageList(int pageIndex, int pageSize, out long count, RB_Miai_ActivityDiscuss_Extend demodel)
        {
            demodel.ParentId = -1;//查询主评论
            var list = miai_ActivityDiscussRepository.GetPageList(pageIndex, pageSize, out count, demodel);
            if (list.Any()) {
                //查询评论下  前几条回复记录
                List<int> PidsList = list.Select(x => x.Id).ToList();
                var clist = miai_ActivityDiscussRepository.GetActivityDiscussChildList(PidsList, 4);
                //查询评论回复数量
                var rlist = miai_ActivityDiscussRepository.GetDiscussReplyNumList(string.Join(",", PidsList));
                foreach (var item in list) {
                    item.ReplyList = clist.Where(x => x.ParentId == item.Id).OrderBy(x => x.Id).ToList();
                    item.ReplyNum = rlist.Where(x => x.ParentId == item.Id).FirstOrDefault()?.ReplyNum ?? 0;
                }
            }
            return list;
        }

        /// <summary>
        /// 获取评论回复分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_Miai_ActivityDiscuss_Extend> GetActivityReplyPageList(int pageIndex, int pageSize, out long count, RB_Miai_ActivityDiscuss_Extend demodel)
        {
            var list = miai_ActivityDiscussRepository.GetPageList(pageIndex, pageSize, out count, demodel);
            return list;
        }
        #endregion


    }
}
