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

namespace Dao.Implement
{
    public class SysUserRepository : RepositoryBase<SysUsers>, ISysUserRepository
    {
        public IQueryable<SysUsers> LoadAllByPage(out long total, int page, int rows, string order, string sort, int type, int? groupId, int? hotelID)
        {
            var list = this.LoadAll().Where(r => r.HotelID == 0 && !r.IsDeleted);
            if (groupId.HasValue)
            {
                switch (type)
                {
                    case 0://组用户
                        list = list.Where(r => r.SysHotelGroup.ID == groupId || (r.SysHotelGroup.Parent != null && r.SysHotelGroup.Parent.ID == groupId)
                             || (r.SysHotelGroup.Parent != null && r.SysHotelGroup.Parent.Parent != null && r.SysHotelGroup.Parent.Parent.ID == groupId));
                        break;
                    case 1://酒店用户
                        list = this.LoadAll().Where(r => r.HotelID == hotelID &&
                            (r.Group.ID == groupId || (r.Group.Parent != null && r.Group.Parent.ID == groupId)));
                        //|| (r.Group.Parent != null && r.Group.Parent.Parent != null && r.Group.Parent.Parent.ID == groupId)));
                        break;
                }
            }
            total = list.LongCount();
            list = list.OrderBy(sort + " " + order);
            list = list.Skip((page - 1) * rows).Take(rows);
            return list;
        }

        public SysUsers Get(string account)
        {
            return this.LoadAll().Where(r => r.Account == account && !r.IsDeleted).FirstOrDefault();
        }

        public IList<SysUsers> GetByRole(int roleId)
        {
            return this.LoadAll().Where(r => r.Role.ID == roleId && !r.IsDeleted).ToList();
        }

        public IList<SysUsers> GetByGroup(Group group)
        {
            return this.LoadAll().Where(r => r.Group == group && !r.IsDeleted).ToList();
        }
    }
}
