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

namespace Edu.Repository.User
{
    /// <summary>
    /// 学生仓储层
    /// </summary>
    public class RB_StudentRepository : BaseRepository<Model.Entity.User.RB_Student>
    {
        /// <summary>
        /// 获取学生列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_ViewModel> GetStudentListRepository(RB_Student_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT t.*,g.GroupName,s.SName 
FROM rb_student AS t LEFT JOIN rb_group AS g ON t.Group_Id=g.GId
     LEFT JOIN rb_school AS s ON t.School_Id=s.SId
WHERE 1=1 
");
            builder.AppendFormat(" AND t.{0}={1} ", nameof(RB_Student_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND t.{0}={1} ", nameof(RB_Student_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.School_Id > -1)
                {
                    builder.AppendFormat(" AND t.{0}={1} ", nameof(RB_Student_ViewModel.School_Id), query.School_Id);
                }
                if (!string.IsNullOrWhiteSpace(query.StuName))
                {
                    builder.AppendFormat(" AND t.{0} LIKE @StuName ", nameof(RB_Student_ViewModel.StuName));
                    parameters.Add("StuName", "%" + query.StuName.Trim() + "%");
                }
                if (!string.IsNullOrWhiteSpace(query.StuTel))
                {
                    builder.AppendFormat(" AND t.{0} LIKE @StuTel ", nameof(RB_Student_ViewModel.StuTel));
                    parameters.Add("StuTel", "%" + query.StuTel.Trim() + "%");
                }
                if (query.ProviceId > 0)
                {
                    builder.AppendFormat(" AND t.{0}={1} ", nameof(RB_Student_ViewModel.ProviceId), query.ProviceId);
                }
                if (query.CityId > 0)
                {
                    builder.AppendFormat(" AND t.{0}={1} ", nameof(RB_Student_ViewModel.CityId), query.CityId);
                }
                if (query.AreaId > 0)
                {
                    builder.AppendFormat(" AND t.{0}={1} ", nameof(RB_Student_ViewModel.AreaId), query.AreaId);
                }
                //if (query.StuStatus > 0)
                //{
                //    builder.AppendFormat(" AND t.{0}={1} ", nameof(RB_Student_ViewModel.StuStatus), query.StuStatus);
                //}
                //else 
                //{
                //    builder.AppendFormat(" AND t.{0} in({1}) ", nameof(RB_Student_ViewModel.StuStatus), "1,2");
                //}
            }
            return Get<RB_Student_ViewModel>(builder.ToString(),parameters).ToList();
        }

        /// <summary>
        /// 获取学生分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_ViewModel> GetStudentPageListRepository(int pageIndex,int pageSize,out long rowsCount,RB_Student_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT t.*,g.GroupName,s.SName,IFNULL(a.Account,'') AS StudentAccount 
FROM rb_student AS t LEFT JOIN rb_group AS g ON t.Group_Id=g.GId
     LEFT JOIN rb_school AS s ON t.School_Id=s.SId
     LEFT JOIN rb_account as a ON (t.StuId=a.AccountId AND a.AccountType=4 )
WHERE 1=1 
");
            if (query != null)
            {
                if (query.Status >= 0)
                {
                    builder.AppendFormat(" AND t.{0}={1} ", nameof(RB_Student_ViewModel.Status), (int)query.Status);
                }
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND t.{0}={1} ", nameof(RB_Student_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.School_Id > 0)
                {
                    builder.AppendFormat(" AND t.{0}={1} ", nameof(RB_Student_ViewModel.School_Id), query.School_Id);
                }
                if (!string.IsNullOrWhiteSpace(query.StuName))
                {
                    builder.AppendFormat(" AND t.{0} LIKE @StuName ", nameof(RB_Student_ViewModel.StuName));
                    parameters.Add("StuName", "%" + query.StuName.Trim() + "%");
                }
                if (!string.IsNullOrWhiteSpace(query.StuTel))
                {
                    builder.AppendFormat(" AND t.{0} LIKE @StuTel ", nameof(RB_Student_ViewModel.StuTel));
                    parameters.Add("StuTel", "%" + query.StuTel.Trim() + "%");
                }
                if (query.ProviceId > 0)
                {
                    builder.AppendFormat(" AND t.{0}={1} ", nameof(RB_Student_ViewModel.ProviceId), query.ProviceId);
                }
                if (query.CityId > 0)
                {
                    builder.AppendFormat(" AND t.{0}={1} ", nameof(RB_Student_ViewModel.CityId), query.CityId);
                }
                if (query.AreaId > 0)
                {
                    builder.AppendFormat(" AND t.{0}={1} ", nameof(RB_Student_ViewModel.AreaId), query.AreaId);
                }
            }
            return GetPage<RB_Student_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }



        /// <summary>
        /// 根据学生id获取同班账户,必须是未开班/学习中的
        /// </summary>
        /// <param name="teacherIds"></param>
        /// <returns></returns>
        public List<RB_Student_ViewModel> GetListByStudentId(int Student_Id, int Group_Id)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat($@"SELECT c.*,rbc.ClassName from rb_student as c 
LEFT JOIN rb_student_orderguest as sog  on sog.Student_Id=c.StuId
LEFT JOIN rb_class as rbc  on rbc.ClassId=sog.ClassId
where sog.ClassId in(SELECT a.ClassId from rb_student_orderguest as a LEFT JOIN rb_class as b on a.ClassId=b.ClassId 
LEFT JOIN rb_student as c  on c.StuId =a.Student_Id
LEFT JOIN rb_order as o on a.OrderId=o.OrderId
where b.`Status`=0 and b.ClassStatus in(1,2) and  a.status=0 and  a.Account_Id={Student_Id} and o.OrderState=1 AND c.Group_Id={Group_Id} )  and sog.Account_Id!={Student_Id}
 AND c.Group_Id={Group_Id}");
            return Get<RB_Student_ViewModel>(builder.ToString(), parameters).ToList();
        }
    }
}