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

namespace Dao.Implement
{
    public class RoomTypeModalRepository : RepositoryBase<RoomTypeModal>, IRoomTypeModalRepository
    {
        public IQueryable<RoomTypeModal> LoadAllByPage(out long total, int page, int rows, string order, string sort, int roomTypeId)
        {
            var list = this.LoadAll();

            list = list.Where(r => r.RoomType != null && r.RoomType.ID == roomTypeId);

            total = list.LongCount();

            list = list.OrderBy(sort + " " + order);
            list = list.Skip((page - 1) * rows).Take(rows);

            return list;
        }

        public void DeleteByRoomTypeID(int roomTypeID, int sourceType)
        {
            IList<RoomTypeModal> lists;
            if (sourceType == -1)
            {
                lists = this.LoadAll().Where(r => r.RoomType.ID == roomTypeID).ToList();
            }
            else
            {
                lists = this.LoadAll().Where(r => r.RoomType.ID == roomTypeID && r.SourceType == sourceType).ToList();
            }
            foreach (RoomTypeModal list in lists)
            {
                this.Delete(list.ID);
            }
        }

        public void DeleteByAddress(int roomTypeID, string address)
        {
            IList<RoomTypeModal> lists;
            lists = this.LoadAll().Where(r => r.RoomType.ID == roomTypeID && r.ModalAddress.Equals(address)).ToList();
            foreach (RoomTypeModal list in lists)
            {
                this.Delete(list.ID);
            }
        }
    }
}
