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_VisitRepository : BaseRepository<RB_Student_Visit>
    {
        /// <summary>
        /// 获取学员到访分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_Visit_Extend> GetStudentVisitPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Student_Visit_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.* 
FROM RB_Student_Visit AS A
WHERE 1=1
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Visit_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Visit_Extend.Group_Id), query.Group_Id);
                }
                if (query.StuId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Visit_Extend.StuId), query.StuId);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Visit_Extend.Id), query.Id);
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_Student_Visit_Extend.Id));
            return GetPage<RB_Student_Visit_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString()).ToList();
        }
        
        /// <summary>
        /// 获取到访列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_Visit_Extend> GetStudentVisitListRepository(RB_Student_Visit_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.StuName,'') AS StuName,IFNULL(B.StuTel,'') AS StuTel
FROM RB_Student_Visit 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_Visit_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Visit_Extend.Group_Id), query.Group_Id);
                }
                if (query.StuId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Visit_Extend.StuId), query.StuId);
                }
                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));
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_Student_Visit_Extend.Id));
            return Get<RB_Student_Visit_Extend>(  builder.ToString()).ToList();
        }


        /// <summary>
        /// 获取到访列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_Visit_Extend> GetStudentVisitStaticRepository(RB_Student_Visit_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.CreateBy,DATE_FORMAT(A.CreateTime,'%Y-%m-%d') AS CreateTime ,Count(1) AS VisitCount
FROM RB_Student_Visit 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_Visit_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Visit_Extend.Group_Id), query.Group_Id);
                }
                if (query.StuId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Visit_Extend.StuId), query.StuId);
                }
                if (!string.IsNullOrEmpty(query.QStuIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Student_Visit_Extend.StuId), query.QStuIds);
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Student_Visit_Extend.CreateBy), query.CreateBy);
                }
                if (!string.IsNullOrEmpty(query.QCreateByIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Student_Visit_Extend.CreateBy), query.QCreateByIds);
                }
                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') ");
            builder.AppendFormat(" ORDER BY A.CreateBy, DATE_FORMAT(A.CreateTime, '%Y/%m/%d') ");
            return Get<RB_Student_Visit_Extend>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 市场、课程顾问学员到访统计
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public Dictionary<string, List<RB_Student_Visit_Extend>> GetVisitStaticRepository(RB_Student_Visit_Extend query)
        {
            Dictionary<string, List<RB_Student_Visit_Extend>> dicList = new Dictionary<string, List<RB_Student_Visit_Extend>>();
            string sql = @"
SELECT A.CreateBy,Count(1) AS VisitCount,IFNULL(C.OrderCount,0) AS OrderCount
FROM RB_Student_Visit AS A INNER  JOIN rb_student AS B ON A.StuId=B.StuId
     LEFT JOIN (
                 SELECT sog.Student_Id,COUNT(1) AS OrderCount 
		         FROM rb_student_orderguest AS sog INNER JOIN rb_order AS o ON sog.OrderId=o.OrderId
				 WHERE o.OrderState IN(1,2)
				 GROUP BY sog.Student_Id
			   ) AS C ON A.StuId=C.Student_Id
WHERE 1=1 AND B.Status=0 {0}
";
            if (query.CreateBy > 0)
            {
                sql += string.Format(" AND A.CreateBy={0} ", query.CreateBy);
            }
            sql += " GROUP BY A.CreateBy ";
            string todayWhere = "";
            if (!string.IsNullOrEmpty(query.StartDate))
            {
                todayWhere += string.Format(" AND to_days(A.CreateTime)=to_days('{0}') ", query.StartDate);
            }
            else
            {
                todayWhere += string.Format(" AND to_days(A.CreateTime)=to_days(now()) ");
            }

            //今日
            var todayList = Get<RB_Student_Visit_Extend>(string.Format(sql.ToString(), todayWhere)).ToList();
            dicList.Add("today", todayList);

            //昨日
            string yesWhere = "";
            if (!string.IsNullOrEmpty(query.StartDate))
            {
                yesWhere += string.Format(" AND TO_DAYS('{0}')-TO_DAYS(A.CreateTime)=1 ", query.StartDate);
            }
            else
            {
                yesWhere += string.Format(" AND TO_DAYS(now())-TO_DAYS(A.CreateTime)=1 ");
            }
            var yesList = Get<RB_Student_Visit_Extend>(string.Format(sql, yesWhere)).ToList();
            dicList.Add("yestoday", yesList);

            //本周
            string curWeek = "";
            if (!string.IsNullOrEmpty(query.StartDate))
            {
                curWeek += string.Format(" AND YEARWEEK(date_format(A.CreateTime,'%Y-%m-%d' )) = YEARWEEK('{0}') ", query.StartDate);
            }
            else
            {
                curWeek += string.Format(" AND YEARWEEK(date_format(A.CreateTime,'%Y-%m-%d')) = YEARWEEK(now()) ");
            }
            var curWeekList = Get<RB_Student_Visit_Extend>(string.Format(sql, curWeek)).ToList();
            dicList.Add("currentWeek", curWeekList);

            //上周
            string lastWeek = "";
            if (!string.IsNullOrEmpty(query.StartDate))
            {
                lastWeek += string.Format(" AND YEARWEEK(date_format(A.CreateTime,'%Y-%m-%d')) = YEARWEEK('{0}')-1 ", query.StartDate);
            }
            else
            {
                lastWeek += string.Format(" AND YEARWEEK(date_format(A.CreateTime,'%Y-%m-%d')) = YEARWEEK(now())-1 ");
            }
            var lastWeekList = Get<RB_Student_Visit_Extend>(string.Format(sql, lastWeek)).ToList();
            dicList.Add("lastWeek", lastWeekList);

            //本月
            string curMonth = "";
            if (!string.IsNullOrEmpty(query.StartDate))
            {
                curMonth += string.Format(" AND DATE_FORMAT(A.CreateTime,'%Y%m')=DATE_FORMAT('{0}','%Y%m') ", query.StartDate);
            }
            else
            {
                curMonth += string.Format(" AND DATE_FORMAT(A.CreateTime,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') ");
            }
            var curMonthList = Get<RB_Student_Visit_Extend>(string.Format(sql, curMonth)).ToList();
            dicList.Add("curMonth", curMonthList);

            //上月
            string lastMonth = "";
            if (!string.IsNullOrEmpty(query.StartDate))
            {
                lastMonth += string.Format(" AND PERIOD_DIFF(date_format('{0}','%Y%m'),date_format(A.CreateTime,'%Y%m')) =1 ", query.StartDate);
            }
            else
            {
                lastMonth += string.Format(" AND PERIOD_DIFF(date_format(now(),'%Y%m'),date_format(A.CreateTime,'%Y%m')) =1 ");
            }
            var lastMonthList = Get<RB_Student_Visit_Extend>(string.Format(sql, lastMonth)).ToList();
            dicList.Add("lastMonth", lastMonthList);
            return dicList;
        }
    }
}
