using System.Collections.Generic;
using System.Text;
using System.Linq;
using Edu.Model.Entity.Log;
using Edu.Repository;

namespace VT.Repository
{
    /// <summary>
    /// 消息推送仓储层
    /// </summary>
    public class RB_MsgLogRepository: BaseRepository<RB_MsgLog>
    {
        /// <summary>
        /// 获取消息推送列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_MsgLog> GetMsgLogListRepository(RB_MsgLog query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT * 
FROM RB_MsgLog
WHERE 1=1
");
            if (query == null)
            {
                return new List<RB_MsgLog>();
            }
            else
            {
                if (!string.IsNullOrEmpty(query.AppId))
                {
                    builder.AppendFormat(" AND {0}='{1}' ", nameof(RB_MsgLog.AppId), query.AppId);
                }
                if (query.CategoryId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_MsgLog.CategoryId), query.CategoryId);
                }
                if (query.Platform > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_MsgLog.Platform), query.Platform);
                }
                if (!string.IsNullOrEmpty(query.ReceiveId))
                {
                    builder.AppendFormat(" AND {0}='{1}' ", nameof(RB_MsgLog.ReceiveId), query.ReceiveId);
                }
                if (query.IsRead >= 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_MsgLog.IsRead), query.IsRead);
                }
            }
            builder.AppendFormat(" ORDER BY {0} DESC ", nameof(RB_MsgLog.Id));
            return Get<RB_MsgLog>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取推送日志分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_MsgLog> GetMsgLogPageListRepository(int pageIndex,int pageSize,out long rowsCount, RB_MsgLog query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT * 
FROM RB_MsgLog
WHERE 1=1
");
            if (query == null)
            {
                rowsCount = 0;
                return new List<RB_MsgLog>();
            }
            else
            {
                if (!string.IsNullOrEmpty(query.AppId))
                {
                    builder.AppendFormat(" AND {0}='{1}' ", nameof(RB_MsgLog.AppId), query.AppId);
                }
                if (query.CategoryId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_MsgLog.CategoryId), query.CategoryId);
                }
                if (query.Platform > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_MsgLog.Platform), query.Platform);
                }
                if (query.Status > -1)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_MsgLog.Status), query.Status);
                }
                if (!string.IsNullOrEmpty(query.ReceiveId))
                {
                    builder.AppendFormat(" AND {0}='{1}' ", nameof(RB_MsgLog.ReceiveId), query.ReceiveId);
                }
                if (query.IsRead >= 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_MsgLog.IsRead), query.IsRead);
                }
            }
            return GetPage<RB_MsgLog>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }
    }
}
