using Edu.AOP.CustomerAttribute;
using Edu.Model.ViewModel.System;
using Edu.Model.ViewModel.User;
using Edu.Repository.System;
using Edu.Repository.User;
using System.Collections.Generic;
using VT.FW.DB;

namespace Edu.Module.User
{
    /// <summary>
    /// 岗位处理类
    /// </summary>
    public class PostModule
    {
        /// <summary>
        /// 岗位仓储层对象
        /// </summary>
        private readonly RB_PostRepository postRepository = new RB_PostRepository();

        /// <summary>
        /// 岗位权限仓储层对象
        /// </summary>
        private readonly RB_Post_RoleRepository postRoleRepository = new RB_Post_RoleRepository();

        /// <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> GetPostPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Post_ViewModel query)
        {
            var list = postRepository.GetPostPageListRepository(pageIndex, pageSize, out rowsCount, query);
            return list;
        }

        /// <summary>
        /// 获取岗位列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Post_ViewModel> GetPostListModule(RB_Post_ViewModel query)
        {
            return postRepository.GetPostListRepository(query);
        }

        /// <summary>
        /// 新增修改岗位
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetPostModule(RB_Post_ViewModel extModel)
        {
            bool flag;
            if (extModel.PostId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Post_ViewModel.PostName),extModel.PostName },
                    {nameof(RB_Post_ViewModel.RB_Dept_Id),extModel.RB_Dept_Id },
                    {nameof(RB_Post_ViewModel.UpdateBy),extModel.UpdateBy },
                    {nameof(RB_Post_ViewModel.UpdateTime),extModel.UpdateTime },
                };
                flag = postRepository.Update(fileds, new WhereHelper(nameof(RB_Post_ViewModel.PostId), extModel.PostId));
            }
            else
            {
                var newId = postRepository.Insert(extModel);
                extModel.PostId = newId;
                flag = newId > 0;
            }
            postRoleRepository.DeleteOne(new WhereHelper(nameof(RB_Post_Role_ViewModel.PostId), extModel.PostId));
            if (extModel.PostRoleList != null && extModel.PostRoleList.Count > 0)
            {
                foreach (var item in extModel.PostRoleList)
                {
                    item.Id = 0;
                    item.PostId = extModel.PostId;
                    flag = postRoleRepository.Insert(item) > 0;
                }
            }
            return flag;
        }

        /// <summary>
        /// 根据岗位编号获取部门岗位
        /// </summary>
        /// <param name="DeptId"></param>
        /// <returns></returns>
        public RB_Post_ViewModel GetPostModule(object PostId)
        {
            var model= postRepository.GetEntity<RB_Post_ViewModel>(PostId);
            if (model != null && model.PostId > 0)
            {
               var postRoleList= postRoleRepository.GetPostRoleListRepository(new RB_Post_Role_ViewModel() { PostId = model.PostId });
                model.PostRoleList = postRoleList ?? new List<RB_Post_Role_ViewModel>();
            }
            return model;
        }

        /// <summary>
        /// 修改岗位状态
        /// </summary>
        /// <param name="PostId">岗位编号</param>
        /// <param name="Status">状态</param>
        /// <returns></returns>
        public bool RemovePostModule(int PostId, int Status)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Post_ViewModel.Status),Status }
            };
            return postRepository.Update(fileds, new WhereHelper(nameof(RB_Post_ViewModel.PostId), PostId));
        }
    }
}