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.*
FROM rb_post AS A 
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 (!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>();
            }
        }

        /// <summary>
        /// 获取部门岗位管理列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Post_ViewModel> GetPostListExtRepository(RB_Post_ViewModel query)
        {
            DynamicParameters parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,B.Dept_Id
FROM rb_post AS A INNER JOIN rb_department_post AS B ON A.PostId=B.PostId
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 (!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>();
            }
        }

        /// <summary>
        /// 根据部门编号获取岗位列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Post_ViewModel> GetDeptPostListRepository(RB_Post_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT B.PostId,B.PostName
FROM rb_department_post AS A INNER JOIN rb_post AS B ON A.PostId=B.PostId
WHERE 1=1
");
            if (query != null)
            {
                if (!string.IsNullOrEmpty(query.QDeptIds))
                {
                    builder.AppendFormat(@" AND A.Dept_Id IN({0}) ", query.QDeptIds);
                }
            }

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