using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using Domain;
using Dao;

namespace Service.Implement
{
    public class ManualVisitManager : GenericManagerBase<ManualVisit>, IManualVisitManager
    {
        public IList<ManualVisit> LoadAllByPage(out long total, int page, int rows, string order, string sort, DateTime? startDate, DateTime? endDate)
        {
            var list = CurrentRepository.LoadAll();
            if (startDate.HasValue && endDate.HasValue)
            {
                list = list.Where(r => r.CreatedDate >= startDate && r.CreatedDate <= endDate);
            }
            total = list.LongCount();
            list = list.OrderBy(sort + " " + order);
            list = list.Skip((page - 1) * rows).Take(rows);
            return list.ToList();
        }
        /// <summary>
        /// 验证验证码是否正确
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <param name="validateCode"></param>
        /// <returns></returns>
        public bool ValidateCode(string phoneNumber, string validateCode)
        {
            ManualVisit item = CurrentRepository.LoadAll()
                .Where(r => r.PhoneNumber == phoneNumber && r.ValidateCode == validateCode)
                .OrderByDescending(r => r.ID)
                .FirstOrDefault();
            if (item != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
