using Edu.Model.Entity.Course;
using Edu.Model.ViewModel.Course;
using System.Text;
using System.Linq;

namespace Edu.Repository.Course
{
    /// <summary>
    /// 班级基础配置仓储层
    /// </summary>
    public class RB_Class_ConfigRepository : BaseRepository<RB_Class_Config>
    {
        /// <summary>
        /// 班级仓储层对象
        /// </summary>
        private readonly RB_ClassRepository classRepository = new RB_ClassRepository();

        /// <summary>
        /// 获取班级基础配置实体
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public RB_Class_Config_ViewModel GetClassConfigRepository(RB_Class_Config_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(@"
SELECT *
FROM RB_Class_Config
WHERE 1=1 
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND {0}={1} ", nameof(RB_Class_Config_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.ConfigId > 0)
                {
                    builder.AppendFormat(@" AND {0}={1} ", nameof(RB_Class_Config_ViewModel.ConfigId), query.ConfigId);
                }
            }
            return Get<RB_Class_Config_ViewModel>(builder.ToString()).FirstOrDefault();
        }

        /// <summary>
        /// 获取基础课时分钟数
        /// </summary>
        /// <param name="Group_Id"></param>
        /// <returns></returns>
        public int GetBasicMinutesRepository(int Group_Id)
        {
            var BasicMinutes = GetClassConfigRepository(new RB_Class_Config_ViewModel() { Group_Id = Group_Id })?.BasicMinutes ?? 45;
            return BasicMinutes;
        }

        /// <summary>
        /// 获取班级基础课时分钟数
        /// </summary>
        /// <param name="classId"></param>
        /// <returns></returns>
        public int GetClassMinutesRepository(int classId)
        {
            var classMinutes= classRepository.GetEntity(classId)?.ClassHourMinute ?? 0;
            if (classMinutes <= 0)
            {
                classMinutes = 45;
            }
            return classMinutes;
        }
    }
}
