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

namespace Edu.Repository.Reserve
{
    /// <summary>
    /// 访客约课仓储层
    /// </summary>
    public class RB_Visitor_ReserveRepository : BaseRepository<RB_Visitor_Reserve>
    {
        /// <summary>
        /// 获取访客约课分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Visitor_Reserve_Extend> GetVisitorReservePageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Visitor_Reserve_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.Append(@"
SELECT A.*,IFNULL(B.`Name`,'') AS VisitorName,C.ClassDate,C.ClassTime,C.EndTime,C.ClassContent,IFNULL(D.TeacherName,'') AS TeacherName
       ,IFNULL(E.RoomName,'') AS RoomName,F.Id AS AccountId
FROM RB_Visitor_Reserve AS A LEFT JOIN rb_visitor AS B ON A.Visitor_Id=B.Id
     LEFT JOIN rb_reserve_class AS C ON A.ReserveClassId=C.ReserveClassId
     LEFT JOIN rb_teacher AS D ON C.TeacherId=D.TId
     LEFT JOIN rb_account AS F On C.TeacherId=F.AccountId AND F.AccountType=2
     LEFT JOIN rb_class_room AS E ON C.ClassRoomId=E.RoomId
WHERE 1=1 
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.StartClassDate))
                {
                    builder.AppendFormat(" AND C.{0}>='{1}' ", nameof(RB_Visitor_Reserve_Extend.ClassDate), query.StartClassDate);
                }
                if (!string.IsNullOrEmpty(query.EndClassDate))
                {
                    builder.AppendFormat(" AND C.{0}<='{1} 23:59:59' ", nameof(RB_Visitor_Reserve_Extend.ClassDate), query.EndClassDate);
                }
                if (query.TeacherId > 0)
                {
                    builder.AppendFormat(" AND C.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.TeacherId), query.TeacherId);
                }
                if (query.ClassRoomId > 0)
                {
                    builder.AppendFormat(" AND C.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.ClassRoomId), query.ClassRoomId);
                }
                if (!string.IsNullOrEmpty(query.Q_ReserveStatus))
                {
                    Int32.TryParse(query.Q_ReserveStatus, out int result);
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.ReserveStatus), result);
                }
                if (!string.IsNullOrEmpty(query.VisitorName))
                {
                    builder.AppendFormat(" AND B.{0} LIKE @VisitorName ", nameof(RB_Visitor.Name));
                    parameters.Add("VisitorName", "%" + query.VisitorName.Trim() + "%");
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.Id), query.Id);
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.CreateBy), query.CreateBy);
                }
            }
            return GetPage<RB_Visitor_Reserve_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取访客约课列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Visitor_Reserve_Extend> GetVisitorReserveListRepository(RB_Visitor_Reserve_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.Append(@"
SELECT A.*,IFNULL(B.`Name`,'') AS VisitorName,C.ClassDate,C.ClassTime,C.EndTime,C.ClassContent,IFNULL(D.TeacherName,'') AS TeacherName,IFNULL(E.RoomName,'') AS RoomName
FROM RB_Visitor_Reserve AS A LEFT JOIN rb_visitor AS B ON A.Visitor_Id=B.Id
     LEFT JOIN rb_reserve_class AS C ON A.ReserveClassId=C.ReserveClassId
     LEFT JOIN rb_teacher AS D ON C.TeacherId=D.TId
     LEFT JOIN rb_class_room AS E ON C.ClassRoomId=E.RoomId
WHERE 1=1 
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.StartClassDate))
                {
                    builder.AppendFormat(" AND C.{0}>='{1}' ", nameof(RB_Visitor_Reserve_Extend.ClassDate), query.StartClassDate);
                }
                if (!string.IsNullOrEmpty(query.EndClassDate))
                {
                    builder.AppendFormat(" AND C.{0}<='{1} 23:59:59' ", nameof(RB_Visitor_Reserve_Extend.ClassDate), query.EndClassDate);
                }
                if (query.TeacherId > 0)
                {
                    builder.AppendFormat(" AND C.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.TeacherId), query.TeacherId);
                }
                if (query.ClassRoomId > 0)
                {
                    builder.AppendFormat(" AND C.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.ClassRoomId), query.ClassRoomId);
                }
                if (!string.IsNullOrEmpty(query.Q_ReserveStatus))
                {
                    Int32.TryParse(query.Q_ReserveStatus, out int result);
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.ReserveStatus), result);
                }
                if (!string.IsNullOrEmpty(query.VisitorName))
                {
                    builder.AppendFormat(" AND B.{0} LIKE @VisitorName ", nameof(RB_Visitor.Name));
                    parameters.Add("VisitorName", "%" + query.VisitorName.Trim() + "%");
                }
            }
            return Get<RB_Visitor_Reserve_Extend>(builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 统计访客约课列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Visitor_Reserve_Extend> GetVisitorReserveStaticsListRepository(RB_Visitor_Reserve_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.AppendFormat(" AND A.ReserveStatus!=3 AND A.Status=0 ");
            if (!string.IsNullOrEmpty(query.Q_ReserveClassIds))
            {
                builder.AppendFormat(@" AND A.{0} IN ({1}) ", nameof(RB_Visitor_Reserve_Extend.ReserveClassId), query.Q_ReserveClassIds);
            }
            string sql = string.Format(@"
SELECT A.*,IFNULL(B.`Name`,'') AS VisitorName
FROM RB_Visitor_Reserve AS A LEFT JOIN rb_visitor AS B ON A.Visitor_Id=B.Id
WHERE 1=1 {0}
", builder.ToString());
            return Get<RB_Visitor_Reserve_Extend>(sql, parameters).ToList();
        }


        /// <summary>
        /// 根据约课信息获取访客列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Visitor_ViewModel> GetVisitorRepository(RB_Visitor_Reserve_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.Append(@"
SELECT B.*
FROM RB_Visitor_Reserve AS A INNER JOIN rb_visitor AS B ON A.Visitor_Id=B.Id
WHERE 1=1 
");
            builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Visitor_Reserve_Extend.Group_Id), query.Group_Id);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}>='{1}' ", nameof(RB_Visitor_Reserve_Extend.Id), query.Id);
                }
            }
            return Get<RB_Visitor_ViewModel>(builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 检查是否存在此访客的约课信息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public bool ExistsVisitorReserveRepository(RB_Visitor_Reserve_Extend query)
        {
            int result = 0;
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT 1
FROM RB_Visitor_Reserve AS A
WHERE 1=1 AND A.Status=0
 ");
            if (query.Visitor_Id > 0)
            {
                builder.AppendFormat(" AND A.Visitor_Id={0} ", query.Visitor_Id);
            }
            if (query.ReserveClassId > 0)
            {
                builder.AppendFormat(" AND A.ReserveClassId={0} ", query.ReserveClassId);
            }
            if (query.Id > 0)
            {
                builder.AppendFormat(" AND A.Id<>{0} ", query.Id);
            }
            var obj = base.ExecuteScalar(builder.ToString());
            if (obj != null)
            {
                Int32.TryParse(obj.ToString(), out result);
            }
            return result > 0;
        }
    }
}
