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

namespace Edu.Repository.System
{
    /// <summary>
    /// 岗位角色表仓储层
    /// </summary>
    public class RB_Post_RoleRepository : BaseRepository<RB_Post_Role>
    {
        /// <summary>
        /// 获取岗位角色分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Post_Role_ViewModel> GetPostRolePageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Post_Role_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT *
FROM RB_Post_Role
WHERE 1=1
");
            if (query != null)
            {
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Post_Role_ViewModel.Id), query.Id);
                }
                if (query.PostId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Post_Role_ViewModel.PostId), query.PostId);
                }
                if (query.RoleId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Post_Role_ViewModel.RoleId), query.RoleId);
                }
            }
            return GetPage<RB_Post_Role_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取岗位角色列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Post_Role_ViewModel> GetPostRoleListRepository(RB_Post_Role_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT *
FROM RB_Post_Role
WHERE 1=1
");
            if (query != null)
            {
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Post_Role_ViewModel.Id), query.Id);
                }
                if (query.PostId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Post_Role_ViewModel.PostId), query.PostId);
                }
                if (query.RoleId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Post_Role_ViewModel.RoleId), query.RoleId);
                }
            }
            return Get<RB_Post_Role_ViewModel>(builder.ToString()).ToList();
        }
    }
}
