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

namespace Edu.Module.Public
{
    /// <summary>
    /// 基础配置相关
    /// </summary>
    public class ClassConfigModule
    {
        /// <summary>
        /// 课程相关设置仓储层对象
        /// </summary>
        private readonly RB_Class_ConfigRepository class_ConfigRepository = new RB_Class_ConfigRepository();

        /// <summary>
        /// 班级类型仓储层对象
        /// </summary>
        private readonly RB_Class_TypeRepository class_TypeRepository = new RB_Class_TypeRepository();

        /// <summary>
        /// 班级仓储层对象
        /// </summary>
        private readonly RB_ClassRepository classRepository = new RB_ClassRepository();

        #region  班级基础设置
        /// <summary>
        /// 获取班级配置实体类
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public RB_Class_Config_ViewModel GetClassConfigModule(RB_Class_Config_ViewModel query)
        {
            return class_ConfigRepository.GetClassConfigRepository(query);
        }

        /// <summary>
        /// 添加修改班级基础设置
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        public bool SetClassConfigModule(RB_Class_Config_ViewModel extModel)
        {
            var oldModel = GetClassConfigModule(extModel);
            bool flag;
            if (oldModel != null && oldModel.ConfigId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Class_Config_ViewModel.BasicHourFee),extModel.BasicHourFee },
                    {nameof(RB_Class_Config_ViewModel.BasicClassNum),extModel.BasicClassNum },
                    {nameof(RB_Class_Config_ViewModel.BasicMinutes),extModel.BasicMinutes },
                    {nameof(RB_Class_Config_ViewModel.UpdateBy),extModel.UpdateBy },
                    {nameof(RB_Class_Config_ViewModel.UpdateTime),extModel.UpdateTime },
                    {nameof(RB_Class_Config_ViewModel.BasicHours),extModel.BasicHours },
                };
                flag = class_ConfigRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Config_ViewModel.ConfigId), oldModel.ConfigId));
            }
            else
            {
                var newId = class_ConfigRepository.Insert(extModel);
                flag = newId > 0;
                extModel.ConfigId = newId;
            }
            return flag;
        }

        #endregion

        #region 班级类型设置

        /// <summary>
        /// 获取班级类型费用分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Type_ViewModel> GetClassTypePageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Class_Type_ViewModel query)
        {
            var list= class_TypeRepository.GetClassTypePageListRepository(pageIndex, pageSize, out rowsCount, query);
            if (list != null && list.Count > 0)
            {
                List<ClassTypeStatic_ViewModel> staticList = new List<ClassTypeStatic_ViewModel>();
                var ids = string.Join(",", list.Select(qitem => qitem.CTypeId));
                if (!string.IsNullOrEmpty(ids))
                {
                    staticList= classRepository.GetClassTypeStaticListRepository(ids);
                }
                foreach (var item in list)
                {
                    var fModel = staticList?.Where(qitem => qitem.ClassType == item.CTypeId)?.FirstOrDefault();
                    item.TotalClassNum = fModel?.TotalClassNum ?? 0;
                    item.CompleteClassNum = fModel?.CompleteClassNum ?? 0;
                }
            }
            return list;
        }

        /// <summary>
        /// 获取班级类型列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Class_Type_ViewModel> GetClassTypeListModule(RB_Class_Type_ViewModel query)
        {
            return class_TypeRepository.GetClassTypeListRepository(query);
        }

        /// <summary>
        /// 新增修改班级类型
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        public bool SetClassTypeModule(RB_Class_Type_ViewModel extModel)
        {
            bool flag;
            if (extModel.CTypeId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                   {nameof(RB_Class_Type_ViewModel.CTypeName),extModel.CTypeName},
                   {nameof(RB_Class_Type_ViewModel.AddHourFee),extModel.AddHourFee},
                   {nameof(RB_Class_Type_ViewModel.DefaultClassNum),extModel.DefaultClassNum},
                   {nameof(RB_Class_Type_ViewModel.UpdateBy),extModel.UpdateBy},
                   {nameof(RB_Class_Type_ViewModel.UpdateTime),extModel.UpdateTime},
                };
                flag = class_TypeRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Type_ViewModel.CTypeId),extModel.CTypeId));
            }
            else
            {
                var newId = class_TypeRepository.Insert(extModel);
                extModel.CTypeId = newId;
                flag = newId > 0;
            }
            return flag;
        }

        /// <summary>
        /// 获取班级类型实体类
        /// </summary>
        /// <param name="CTypeId"></param>
        /// <returns></returns>
        public RB_Class_Type_ViewModel GetClassTypeModule(object CTypeId)
        {
            return class_TypeRepository.GetEntity<RB_Class_Type_ViewModel>(CTypeId);
        }

        /// <summary>
        /// 删除班级类型
        /// </summary>
        /// <param name="CTypeId"></param>
        /// <returns></returns>
        public bool RemoveClassTypeModule(int CTypeId)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
               {nameof(RB_Class_Type_ViewModel.Status),(int) DateStateEnum.Delete}
            };
            bool flag = class_TypeRepository.Update(fileds, new WhereHelper(nameof(RB_Class_Type_ViewModel.CTypeId), CTypeId));
            return flag;
        }
        #endregion
    }
}
