using Edu.Common.Enum;
using Edu.Model.ViewModel.Course;
using Edu.Repository.Course;
using System.Collections.Generic;
using VT.FW.DB;

namespace Edu.Module.Course
{
    /// <summary>
    /// 班级管理处理类
    /// </summary>
    public class ClassModule
    {
        /// <summary>
        /// 班级仓储层对象
        /// </summary>
        private readonly RB_ClassRepository classRepository = new RB_ClassRepository();

        /// <summary>
        /// 获取班级列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_ViewModel> GetClassListModule(RB_Class_ViewModel query)
        {
            return classRepository.GetClassListRepository(query);
        }

        /// <summary>
        /// 获取班级分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_ViewModel> GetClassPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Class_ViewModel query)
        {
            return classRepository.GetClassPageListRepository(pageIndex, pageSize, out rowsCount, query);
        }

        /// <summary>
        /// 检查班级是否存在
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool ExistsClassModule(RB_Class_ViewModel model)
        {
            List<WhereHelper> wheres = new List<WhereHelper>()
            {
               new WhereHelper (nameof(RB_Class_ViewModel.ClassName),model.ClassName.Trim()),
            };
            if (model.ClassId > 0)
            {
                wheres.Add(new WhereHelper(nameof(RB_Class_ViewModel.ClassId), model.ClassId, OperatorEnum.NotEqual));
            }
            return classRepository.Exists(wheres);
        }

        /// <summary>
        /// 新增修改班级
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetClassModule(RB_Class_ViewModel model)
        {
            if (model.ClassId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_Class_ViewModel.ClassName),model.ClassName},
                    { nameof(RB_Class_ViewModel.CouseId),model.CouseId},
                    { nameof(RB_Class_ViewModel.Teacher_Id),model.Teacher_Id},
                    { nameof(RB_Class_ViewModel.Assist_Id),model.Assist_Id},
                };
                return classRepository.Update(fileds, new WhereHelper(nameof(RB_Class_ViewModel.ClassId), model.ClassId));
            }
            else
            {
                var newId = classRepository.Insert(model);
                model.ClassId = newId;
                return newId > 0;
            }
        }

        /// <summary>
        /// 根据班级编号获取班级详细信息
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        public RB_Class_ViewModel GetClassModule(object ClassId)
        {
            return classRepository.GetEntity<RB_Class_ViewModel>(ClassId);
        }

        /// <summary>
        /// 删除班级
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        public bool RemoveClassModule(object ClassId)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_Class_ViewModel.Status),(int)DateStateEnum.Delete},
            };
            return classRepository.Update(fileds, new WhereHelper(nameof(RB_Class_ViewModel.ClassId), ClassId));
        }
    }
}
