using Edu.Common.Enum;
using Edu.Model.Entity.Customer;
using Edu.Model.ViewModel.Customer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Edu.Repository.Customer
{
    /// <summary>
    /// 学员约访表仓储层
    /// </summary>
    public class RB_Student_AppointmentRepository : BaseRepository<RB_Student_Appointment>
    {
        /// <summary>
        /// 获取学员约访分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_Appointment_Extend> GetStudentAppointmentPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Student_Appointment_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.* 
FROM RB_Student_Appointment AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Appointment_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Appointment_Extend.Group_Id), query.Group_Id);
                }
                if (query.StuId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Appointment_Extend.StuId), query.StuId);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Appointment_Extend.Id), query.Id);
                }

            }
            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_Student_Appointment_Extend.Id));

            return GetPage<RB_Student_Appointment_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }


        /// <summary>
        /// 获取约访统计列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_Appointment_Extend> GetStudentVisitStaticRepository(RB_Student_Appointment_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.CreateBy,DATE_FORMAT(A.CreateTime,'%Y-%m-%d') AS CreateTime,Count(1) AS AppointmentCount
FROM RB_Student_Appointment AS A INNER  JOIN rb_student AS B ON A.StuId=B.StuId
WHERE 1=1 
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Appointment_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Appointment_Extend.Group_Id), query.Group_Id);
                }
                if (query.StuId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Appointment_Extend.StuId), query.StuId);
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Appointment_Extend.CreateBy), query.CreateBy);
                }
                if (!string.IsNullOrEmpty(query.QueryDate))
                {
                    //指定日期的约访统计
                    builder.AppendFormat(" AND DATE_FORMAT(A.{0},'%y/%m/%d')= DATE_FORMAT('{1}','%y/%m/%d') ", nameof(RB_Student_Visit_Extend.CreateTime), Common.ConvertHelper.FormatDate(query.QueryDate));
                }
                if (!string.IsNullOrEmpty(query.StartDate))
                {
                    builder.AppendFormat(" AND A.{0}>='{1}' ", nameof(RB_Student_Visit_Extend.CreateTime), Common.ConvertHelper.FormatDate(query.StartDate));
                }
                if (!string.IsNullOrEmpty(query.EndDate))
                {
                    builder.AppendFormat(" AND A.{0}<='{1} 23:59:59' ", nameof(RB_Student_Visit_Extend.CreateTime), Common.ConvertHelper.FormatDate(query.EndDate));
                }
            }
            builder.AppendFormat(" GROUP BY A.CreateBy,DATE_FORMAT(A.CreateTime,'%Y/%m/%d') ");
            return Get<RB_Student_Appointment_Extend>(builder.ToString()).ToList();
        }
    }
}
