using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Edu.Model.ViewModel.LearningGarden;
using Edu.Repository.LearningGarden;
using VT.FW.DB;

namespace Edu.Module.Course
{
    public class LearningGardenModule
    {
        /// <summary>
        /// 订单
        /// </summary>
        private readonly RB_LearningGardenRepository learningGardenRepository = new RB_LearningGardenRepository();

        #region 学习园地
        /// <summary>
        /// 获取学习园地分页列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示调试</param>
        /// <param name="rowsCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_LearningGarden_ViewModel> GetLearningGardenPageModule(int pageIndex, int pageSize, out long rowsCount, RB_LearningGarden_ViewModel query)
        {
            return learningGardenRepository.GetLearningGardenPageRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 获取学习园地列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_LearningGarden_ViewModel> GetLearningGardenListModule(RB_LearningGarden_ViewModel query)
        {
            return learningGardenRepository.GetLearningGardenListRepository(query);
        }

        /// <summary>
        /// 新增修改新闻
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetLearningGardenModule(RB_LearningGarden_ViewModel model)
        {
            bool flag;
            if (model.Id > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_LearningGarden_ViewModel.Title),model.Title },
                    {nameof(RB_LearningGarden_ViewModel.Img),model.Img },
                    {nameof(RB_LearningGarden_ViewModel.Content),model.Content },
                    {nameof(RB_LearningGarden_ViewModel.TypeId),model.TypeId },
                    {nameof(RB_LearningGarden_ViewModel.Digest),model.Digest },
                    {nameof(RB_LearningGarden_ViewModel.LinkUrl),model.LinkUrl }
                };
                flag = learningGardenRepository.Update(fileds, new WhereHelper(nameof(RB_LearningGarden_ViewModel.Id), model.Id));
            }
            else
            {
                var newId = learningGardenRepository.Insert(model);
                model.Id = newId;
                flag = newId > 0;
            }
            return flag;
        }

        /// <summary>
        /// 批量新增修改学习园地
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetBatchWebNewsModule(List<RB_LearningGarden_ViewModel> list)
        {
            bool flag = false;
            Task.Run(() =>
            {
                foreach (var model in list)
                {
                    if (model.Id > 0)
                    {
                        Dictionary<string, object> fileds = new Dictionary<string, object>()
                        {
                            {nameof(RB_LearningGarden_ViewModel.Title),model.Title },
                            {nameof(RB_LearningGarden_ViewModel.Img),model.Img },
                            {nameof(RB_LearningGarden_ViewModel.Content),model.Content },
                            {nameof(RB_LearningGarden_ViewModel.TypeId),model.TypeId },
                            {nameof(RB_LearningGarden_ViewModel.Digest),model.Digest },
                            {nameof(RB_LearningGarden_ViewModel.LinkUrl),model.LinkUrl },
                            {nameof(RB_LearningGarden_ViewModel.UpdateTime),model.UpdateTime }
                        };
                        flag = learningGardenRepository.Update(fileds, new WhereHelper(nameof(RB_LearningGarden_ViewModel.Id), model.Id));
                    }
                    else
                    {
                        var newId = learningGardenRepository.Insert(model);
                        model.Id = newId;
                        flag = newId > 0;
                    }
                }
            });
            return flag;
        }

        /// <summary>
        /// 根据编号获取学习园地详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public RB_LearningGarden_ViewModel GetLearningGardenModule(object Id)
        {
            return learningGardenRepository.GetEntity<RB_LearningGarden_ViewModel>(Id);
        }

        /// <summary>
        /// 修改学习园地状态
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Status">0-正常,1-删除</param>
        /// <returns></returns>
        public bool RemoveLearningGardenStatusModule(int Id, int Status)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_LearningGarden_ViewModel.Status),Status },
            };
            var flag = learningGardenRepository.Update(fileds, new WhereHelper(nameof(RB_LearningGarden_ViewModel.Id), Id));
            return flag;
        }

        #endregion
    }
}
