using System.Collections.Generic;
using System.Linq;
using System.Text;
using Edu.Model.Entity.System;
using Edu.Model.ViewModel.System;

namespace Edu.Repository.System
{
    /// <summary>
    /// 系统消息记录仓储层
    /// </summary>
    public class RB_System_LogRepository : BaseRepository<RB_System_Log>
    {
        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get { return nameof(RB_System_Log); } }

        /// <summary>
        /// 获取发送系统消息记录信息
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_System_Log_ViewModel> GetListRepository(RB_System_Log_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append($" SELECT * FROM {TableName} where 1=1 ");
            if (query != null)
            {
                if (query.ID > 0)
                {
                    builder.Append($" AND {nameof(RB_System_Log.ID)}={query.ID}");
                }
                if (query.Group_Id > 0)
                {
                    builder.Append($" AND {nameof(RB_System_Log.Group_Id)}={query.Group_Id}");
                }
                if (query.School_Id > 0)
                {
                    builder.Append($" AND {nameof(RB_System_Log.School_Id)}={query.School_Id}");
                }
                if (query.Student_Id > 0)
                {
                    builder.Append($" AND {nameof(RB_System_Log.Student_Id)}={query.Student_Id}");
                }
                if (query.SelectSendState == 1)
                {
                    builder.Append($" AND {nameof(RB_System_Log.SendStatus)}>=3");
                }
                if (query.SendStatus.HasValue && query.SendStatus > 0)
                {
                    builder.Append($" AND {nameof(RB_System_Log.SendStatus)}={(int)query.SendStatus}");
                }

            }
            return Get<RB_System_Log_ViewModel>(builder.ToString()).ToList();
        }


        /// <summary>
        /// 获取发送系统消息记录信息
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_System_Log_ViewModel> GetPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_System_Log_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append($" SELECT * FROM {TableName} where 1=1 ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.Append($" AND {nameof(RB_System_Log.Group_Id)}={query.Group_Id}");
                }
                if (query.School_Id > 0)
                {
                    builder.Append($" AND {nameof(RB_System_Log.School_Id)}={query.School_Id}");
                }
                if (query.Student_Id > 0)
                {
                    builder.Append($" AND {nameof(RB_System_Log.Student_Id)}={query.Student_Id}");
                }
                if (query.SelectSendState == 1)
                {
                    builder.Append($" AND {nameof(RB_System_Log.SendStatus)}>=3");
                }
                if (query.SendStatus.HasValue && query.SendStatus > 0)
                {
                    builder.Append($" AND {nameof(RB_System_Log.SendStatus)}={(int)query.SendStatus}");
                }
                //if (!string.IsNullOrWhiteSpace(query.StartDate))
                //{
                //    builder.Append($" and  DATE_FORMAT({nameof(RB_System_Log.CreateDate)},'%Y-%m-%d' )>=DATE_FORMAT('{query.StartDate}','%Y-%m-%d' ) ");
                //}
                //if (!string.IsNullOrWhiteSpace(query.EndDate))
                //{
                //    builder.Append($" and  DATE_FORMAT({nameof(RB_System_Log.CreateDate)},'%Y-%m-%d' )<= DATE_FORMAT('{query.EndDate}','%Y-%m-%d' ) ");
                //}

                if (!string.IsNullOrWhiteSpace(query.SendContent))
                {
                    builder.Append($" and  {nameof(RB_System_Log.SendContent)} like '%{query.SendContent}%' ");
                }
            }
            return GetPage<RB_System_Log_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }
    }
}
