using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Edu.Common.Enum;
using Edu.Model.Entity.LearningGarden;
using Edu.Model.ViewModel.LearningGarden;
using VT.FW.DB.Dapper;

namespace Edu.Repository.LearningGarden
{
   public class RB_LearningGardenRepository:BaseRepository<RB_LearningGarden>
    {
        /// <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> GetLearningGardenPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_LearningGarden_ViewModel query)
        {
            rowsCount = 0;
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.Append(@"
SELECT A.* 
FROM RB_LearningGarden AS A
WHERE 1=1
");

            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_LearningGarden_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query == null)
            {
                return new List<RB_LearningGarden_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_LearningGarden_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.GardenType > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_LearningGarden_ViewModel.GardenType), (int)query.GardenType);
                }
                if (!string.IsNullOrEmpty(query.Title))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Title ", nameof(RB_LearningGarden_ViewModel.Title));
                    parameters.Add("Title", "%" + query.Title.Trim() + "%");
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_LearningGarden_ViewModel.CreateDate));
            return GetPage<RB_LearningGarden_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取学习园地列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_LearningGarden_ViewModel> GetLearningGardenListRepository(RB_LearningGarden_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.Append(@"
SELECT A.* 
FROM RB_LearningGarden AS A
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_LearningGarden_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query == null)
            {
                return new List<RB_LearningGarden_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_LearningGarden_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.Title))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Title ", nameof(RB_LearningGarden_ViewModel.Title));
                    parameters.Add("Title", "%" + query.Title.Trim() + "%");
                }
            }

            builder.AppendFormat(" order by a.UpdateTime desc ");
            return Get<RB_LearningGarden_ViewModel>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool BatchInsertLearningGardenRepository(List<RB_LearningGarden_ViewModel> list)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("INSERT INTO RB_LearningGarden (Title,Img,Content,Digest,LinkUrl,TypeId,Status,CreateDate,CreateBy,Group_Id,School_Id,UpdateTime,WeChatAccountId,Author,GardenType) VALUES");
            list.ForEach(x =>
            {
                builder.Append($"('{x.Title}','{x.Img}','{x.Content}','{x.Digest}','{x.LinkUrl}','{x.TypeId}',{(int)x.Status},'{x.CreateDate}',{x.CreateBy},{x.Group_Id},{x.School_Id},'{x.UpdateTime}','{x.WeChatAccountId}','{x.Author}',{x.GardenType}),");
            });
            builder = builder.Remove(builder.Length - 1, 1);
            return base.Execute(builder.ToString()) > 0;
        }

        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool BatchUpdateLearningGardenRepository(List<RB_LearningGarden_ViewModel> list)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("INSERT INTO RB_LearningGarden (Id,Title,Img,Content,TypeId,Digest,LinkUrl,UpdateTime) VALUES");
            list.ForEach(x =>
            {
            
                builder.Append($"({x.Id},'{x.Title}','{x.Img}','{x.Content}','{x.TypeId}','{x.Digest}','{x.LinkUrl}','{x.UpdateTime}'),");
            });
            builder = builder.Remove(builder.Length - 1, 1);
            builder.Append("ON DUPLICATE KEY UPDATE Title=VALUES(Title),Img=VALUES(Img),Content=VALUES(Content),TypeId=VALUES(TypeId),Digest=VALUES(Digest),LinkUrl=VALUES(LinkUrl),UpdateTime=VALUES(UpdateTime);");
            return Execute(builder.ToString()) > 0;
        }
    }
}
