using Edu.Common.Enum;
using Edu.Model.Entity.User;
using Edu.Model.ViewModel.User;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB.Dapper;

namespace Edu.Repository.User
{
    /// <summary>
    /// 岗位管理仓储层
    /// </summary>
    public class RB_PostRepository : BaseRepository<RB_Post>
    {
        /// <summary>
        /// 获取岗位分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Post_ViewModel> GetPostPageListRepository(int pageIndex, int pageSize, out long rowsCount, RB_Post_ViewModel query)
        {
            rowsCount = 0;
            DynamicParameters parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.DeptName,'') AS DeptName
FROM rb_post AS A LEFT JOIN rb_department AS B ON A.RB_Dept_Id=B.DeptId
WHERE 1=1
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.Append($" AND A.{nameof(RB_Post_ViewModel.Group_Id)}={query.Group_Id} ");
                }
                if (query.PostId > 0)
                {
                    builder.Append($" AND A.{nameof(RB_Post_ViewModel.PostId)}={query.PostId} ");
                }
                if (!string.IsNullOrEmpty(query.PostName))
                {
                    builder.Append($" AND A.{nameof(RB_Post_ViewModel.PostName)} LIKE @PostName ");
                    parameters.Add("PostName", "%" + query.PostName.Trim() + "%");
                }
                if (query.Status != null && (int)query.Status > -1)
                {
                    builder.Append($" AND A.{nameof(RB_Post_ViewModel.Status)}={(int)query.Status} ");
                }
                return GetPage<RB_Post_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(),parameters).ToList();
            }
            else
            {
                return new List<RB_Post_ViewModel>();
            }
        }

        /// <summary>
        /// 获取岗位列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Post_ViewModel> GetPostListRepository(RB_Post_ViewModel query)
        {
            DynamicParameters parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.* 
FROM rb_post AS A
WHERE 1=1
");
            builder.Append($" AND A.{nameof(RB_Post_ViewModel.Status)}={(int)DateStateEnum.Normal} ");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.Append($" AND A.{nameof(RB_Post_ViewModel.Group_Id)}={query.Group_Id} ");
                }
                if (query.PostId > 0)
                {
                    builder.Append($" AND A.{nameof(RB_Post_ViewModel.PostId)}={query.PostId} ");
                }
                if (query.RB_Dept_Id > 0)
                {
                    builder.Append($" AND A.{nameof(RB_Post_ViewModel.RB_Dept_Id)}={query.RB_Dept_Id} ");
                }
                if (!string.IsNullOrEmpty(query.QPostIds))
                {
                    builder.Append($" AND A.{nameof(RB_Post_ViewModel.PostId)} IN({query.QPostIds}) ");
                }
                if (!string.IsNullOrEmpty(query.PostName))
                {
                    builder.Append($" AND A.{nameof(RB_Post_ViewModel.PostName)} LIKE @PostName ");
                    parameters.Add("PostName", "%" + query.PostName.Trim() + "%");
                }
                return Get<RB_Post_ViewModel>(builder.ToString(),parameters).ToList();
            }
            else
            {
                return new List<RB_Post_ViewModel>();
            }
        }
    }
}