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

namespace Edu.Repository.Log
{
    /// <summary>
    /// 用户信息改变日志仓储层
    /// </summary>
    public class RB_User_ChangeLogRepository : BaseRepository<RB_User_ChangeLog>
    {
        /// <summary>
        /// 获取用户信息改变日志列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_User_ChangeLog_ViewModel> GetUserChangeLogListRepository(RB_User_ChangeLog_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT * 
FROM RB_User_ChangeLog 
WHERE 1=1 
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat($" AND {nameof(RB_User_ChangeLog_ViewModel.Group_Id)}={query.Group_Id} ");
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat($" AND {nameof(RB_User_ChangeLog_ViewModel.School_Id)}={query.School_Id} ");
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat($" AND {nameof(RB_User_ChangeLog_ViewModel.CreateBy)}={query.CreateBy} ");
                }
                if (query.Type > 0)
                {
                    builder.AppendFormat($" AND {nameof(RB_User_ChangeLog_ViewModel.Type)}={query.Type} ");
                }
                if (query.AccountType > 0)
                {
                    builder.AppendFormat($" AND {nameof(RB_User_ChangeLog_ViewModel.AccountType)}={(int)query.AccountType} ");
                }
                if (query.SourceId > 0)
                {
                    builder.AppendFormat($" AND {nameof(RB_User_ChangeLog_ViewModel.SourceId)}={query.SourceId} ");
                }
            }
            builder.Append(" ORDER BY CreateTime DESC ");
            return Get<RB_User_ChangeLog_ViewModel>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取日志分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_User_ChangeLog_ViewModel> GetPageList(int pageIndex, int pageSize, out long count, RB_User_ChangeLog_ViewModel query) {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"SELECT * FROM RB_User_ChangeLog WHERE 1=1 ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat($" AND {nameof(RB_User_ChangeLog_ViewModel.Group_Id)}={query.Group_Id} ");
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat($" AND {nameof(RB_User_ChangeLog_ViewModel.School_Id)}={query.School_Id} ");
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat($" AND {nameof(RB_User_ChangeLog_ViewModel.CreateBy)}={query.CreateBy} ");
                }
                if (query.Type > 0)
                {
                    builder.AppendFormat($" AND {nameof(RB_User_ChangeLog_ViewModel.Type)}={query.Type} ");
                }
                if (query.SourceId > 0)
                {
                    builder.AppendFormat($" AND {nameof(RB_User_ChangeLog_ViewModel.SourceId)}={query.SourceId} ");
                }
            }
            builder.AppendFormat($" order by Id desc");
            return GetPage<RB_User_ChangeLog_ViewModel>(pageIndex, pageSize, out count, builder.ToString()).ToList();
        }
    }
}