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

namespace Edu.Repository.Duty
{
    /// <summary>
    /// 访客仓储层
    /// </summary>
    public class RB_VisitorRepository : BaseRepository<RB_Visitor>
    {
        /// <summary>
        /// 获取访客分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Visitor_ViewModel> GetVisitorPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Visitor_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.* 
FROM RB_Visitor AS A
WHERE 1=1
");
            if (query == null)
            {
                rowsCount = 0;
                return new List<RB_Visitor_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Visitor_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.PlanId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Visitor_ViewModel.PlanId), query.PlanId);
                }
                if (!string.IsNullOrEmpty(query.KeyWords))
                {
                    builder.AppendFormat(" AND (A.{0} LIKE @KeyWord  OR A.{1} LIKE @KeyWord OR A.{2} LIKE @KeyWord)", nameof(RB_Visitor_ViewModel.Name), nameof(RB_Visitor_ViewModel.Tel), nameof(RB_Visitor_ViewModel.WeChatNum));
                    parameters.Add("KeyWord", "%" + query.KeyWords.Trim() + "%");
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Visitor_ViewModel.CreateBy), query.CreateBy);
                }
                if(!string.IsNullOrEmpty(query.QReserveClassIds))
                {
                    builder.AppendFormat(@" AND A.Id IN(SELECT Visitor_Id FROM rb_visitor_reserve WHERE ReserveClassId IN({0}) )",query.QReserveClassIds);
                }
                if (!string.IsNullOrEmpty(query.StartTime))
                {
                    builder.AppendFormat(@" AND A.{0} >='{1}' ", nameof(RB_Visitor_ViewModel.CreateTime), query.StartTime);
                }
                if (!string.IsNullOrEmpty(query.EndTime))
                {
                    builder.AppendFormat(@" AND A.{0} <='{1} 23:59:59' ", nameof(RB_Visitor_ViewModel.CreateTime), query.EndTime);
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_Visitor_ViewModel.Id));
            return GetPage<RB_Visitor_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取访客
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public int GetVisitorCountRepository(RB_Visitor_ViewModel query)
        {
            int result = 0;
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT COUNT(1) 
FROM rb_visitor 
WHERE 1=1
");
            if (query != null)
            {
                if (!string.IsNullOrEmpty(query.WeChatNum) && !string.IsNullOrEmpty(query.Tel))
                {
                    builder.AppendFormat(@"  AND ( WeChatNum='{0}' OR Tel='{1}') ", query.WeChatNum, query.Tel);
                }
                if (!string.IsNullOrEmpty(query.WeChatNum) && string.IsNullOrEmpty(query.Tel))
                {
                    builder.AppendFormat(@"  AND ( WeChatNum='{0}' ) ", query.WeChatNum);
                }
                if (string.IsNullOrEmpty(query.WeChatNum) && !string.IsNullOrEmpty(query.Tel))
                {
                    builder.AppendFormat(@"  AND ( Tel='{0}' ) ", query.Tel);
                }
            }
            var obj = base.ExecuteScalar(builder.ToString());
            if (obj != null)
            {
                Int32.TryParse(obj.ToString(), out result);
            }
            return result;
        }
    }
}
