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

namespace Edu.Repository.StudyAbroad
{
    /// <summary>
    /// 留学就业类型仓储层
    /// </summary>
    public class RB_Studyabroad_TypeRepository : BaseRepository<RB_Studyabroad_Type>
    {
        /// <summary>
        /// 获取留学就业类型分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Studyabroad_Type_Extend> GetStudyabroadTypePageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Studyabroad_Type_Extend query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Studyabroad_Type AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Studyabroad_Type_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Studyabroad_Type_Extend.Group_Id), query.Group_Id);
                }
                if (query.Type > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Studyabroad_Type_Extend.Type), query.Type);
                }
                if (!string.IsNullOrEmpty(query.CountryId))
                {
                    builder.AppendFormat(" AND FIND_IN_SET('{0}', A.CountryId) ", query.CountryId);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_Studyabroad_Type_Extend.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
            }

            builder.AppendFormat(" ORDER BY A.{0} ", nameof(RB_Studyabroad_Type_Extend.Num));
            return GetPage<RB_Studyabroad_Type_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }


        /// <summary>
        /// 获取留学就业类型列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Studyabroad_Type_Extend> GetStudyabroadTypeListRepository(RB_Studyabroad_Type_Extend query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Studyabroad_Type AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Studyabroad_Type_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Studyabroad_Type_Extend.Group_Id), query.Group_Id);
                }
                if (query.Type > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Studyabroad_Type_Extend.Type), query.Type);
                }
                if (!string.IsNullOrEmpty(query.CountryId))
                {
                    builder.AppendFormat(" AND FIND_IN_SET('{0}',A.CountryId ) ",  query.CountryId);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @CourseName ", nameof(RB_Studyabroad_Type_Extend.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
            }
            //builder.AppendFormat(" OR A.CountryId=0 ");
            builder.AppendFormat(" ORDER BY A.{0} ", nameof(RB_Studyabroad_Type_Extend.Num));
            return Get<RB_Studyabroad_Type_Extend>(builder.ToString(), parameters).ToList();
        }
    }
}
