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

namespace Edu.Repository.System
{
    public class RB_Msg_LogRepository : BaseRepository<RB_Msg_Log>
    {
        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get { return nameof(RB_Msg_Log); } }

        /// <summary>
        /// 获取发送短信记录信息
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Msg_Log> GetListRepository(RB_Msg_Log 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_Msg_Log.Group_Id)}={query.Group_Id}");
                }
                if (query.School_Id > 0)
                {
                    builder.Append($" AND {nameof(RB_Msg_Log.School_Id)}={query.School_Id}");
                }
                if (query.SendStatus.HasValue&& query.SendStatus > 0)
                {
                    builder.Append($" AND {nameof(RB_Msg_Log.SendStatus)}={(int)query.SendStatus}");
                }

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


        /// <summary>
        /// 获取发送短信记录信息
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Msg_Log_ViewModel> GetPageListRepository(int pageIndex, int pageSize, out long rowsCount, Model.ViewModel.System.RB_Msg_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_Msg_Log.Group_Id)}={query.Group_Id}");
                }
                if (query.School_Id > 0)
                {
                    builder.Append($" AND {nameof(RB_Msg_Log.School_Id)}={query.School_Id}");
                }

                if (query.SendStatus.HasValue && query.SendStatus > 0)
                {
                    builder.Append($" AND {nameof(RB_Msg_Log.SendStatus)}={(int)query.SendStatus}");
                }
                if (!string.IsNullOrWhiteSpace(query.StartDate))
                {
                    builder.Append($" and  DATE_FORMAT({nameof(RB_Msg_Log.CreateDate)},'%Y-%m-%d' )>=DATE_FORMAT('{query.StartDate}','%Y-%m-%d' ) ");
                }
                if (!string.IsNullOrWhiteSpace(query.EndDate))
                {
                    builder.Append($" and  DATE_FORMAT({nameof(RB_Msg_Log.CreateDate)},'%Y-%m-%d' )<= DATE_FORMAT('{query.EndDate}','%Y-%m-%d' ) ");
                }
                if (!string.IsNullOrWhiteSpace(query.ReceiverName))
                {
                    builder.Append($" and ( {nameof(RB_Msg_Log.ReceiverName)} like '%{query.ReceiverName}%'  or  {nameof(RB_Msg_Log.ReceiverPhone)} like '%{query.ReceiverName}%' )");
                }
                if (!string.IsNullOrWhiteSpace(query.SendContent))
                {
                    builder.Append($" and  {nameof(RB_Msg_Log.SendContent)} like '%{query.SendContent}%' ");
                }
            }
            return GetPage<RB_Msg_Log_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }
    }
}
