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

namespace Edu.Repository.System
{
    /// <summary>
    /// 学习目的仓储层
    /// </summary>
    public class RB_LearningGoalsRepository : BaseRepository<RB_LearningGoals>
    {
        /// <summary>
        /// 获取学习目的分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_LearningGoals_Extend> GetLearningGoalsPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_LearningGoals_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_LearningGoals AS A
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_LearningGoals_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_LearningGoals_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_LearningGoals_Extend.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
            }
            return GetPage<RB_LearningGoals_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }


        /// <summary>
        /// 获取学习目的列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_LearningGoals_Extend> GetLearningGoalsListRepository(RB_LearningGoals_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_LearningGoals AS A
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_LearningGoals_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_LearningGoals_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_LearningGoals_Extend.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.QGoalIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_LearningGoals_Extend.Id), query.QGoalIds);
                }
            }
            return Get<RB_LearningGoals_Extend>(builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取学习目的实体类
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public RB_LearningGoals_Extend GetLearningGoalsExtEntityRepository(int Id)
        {
            var extModel = base.GetEntity<RB_LearningGoals_Extend>(Id);
            return extModel;
        }
    }
}