using COMMON;
using Microsoft.EntityFrameworkCore;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SERVER
{
    /// <summary>
    /// ruc 服务 数据查询处理中心
    /// </summary>
    public class RcuServer
    {
        /// <summary>
        /// 查询 rcu 的连接 信息 绑定房间酒店信息
        /// </summary>
        /// <param name="hotel_id"></param>
        /// <param name="count_"></param>
        /// <param name="count"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="roomnumber"></param>
        /// <param name="room_type"></param>
        /// <param name="room_online_satatus"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public static List<dynamic> SetRucServer(out int count, out int count_, int hotel_id,int start, int length,int room_online_satatus, string roomnumber, int room_type, HotelServiceContext db = null)
        {
            // 房态判断 
            var room_type_ = new int[] { };
            if (room_type == -1)
                room_type_ = new int[] {2,4,8,16 }; 
            IEnumerable<dynamic>  res =  new  List<dynamic>();
            if (db == null)
                // 一般情况下都会从控制器传入过来
                db = XC_Data.GetMinDataBase();
            // 通过连接信息表 查询出在线的 rcu  每个 mac 应该只是出现一次
            var tBL_RCU_CONN_INFO = db.TBL_RCU_CONN_INFOS.Where(x => x.ONLINE_SATATUS == 1).Select(x =>  x.MAC.ToUpper()).Distinct();
            res =
                (
                from r in db.TBL_ROOM_BASIC_INFOS
                join h in db.TBL_HOTEL_BASIC_INFOS on r.HOTEL_OLD_ID
                equals h.IDOLD
                join rcu in db.TBL_RCU_BASIC_INFOS on r.MAC
                equals rcu.MAC
                where r.HOTEL_OLD_ID == hotel_id && (string.IsNullOrEmpty(roomnumber) || r.ROOM_NUMBER.Contains(roomnumber)) && (r.RoomStatusID == room_type || room_type==0  || (room_type == -1 && !room_type_.Contains(r.RoomStatusID)))
                select new
                {
                    rcu.REGISTER_DATE,
                    r.RoomStatusID,
                    rcu.REGISTER_STATUS,
                    rcu.UDP_KEY,
                    rcu.UPDATE_TIMEMARK,
                    rcu.IsImport,
                    rcu.RCU_ID,
                    rcu.MAC,
                    rcu.CORE_MODUEL,
                    rcu.UUID,
                    rcu.BRAND,
                    rcu.MODEL,
                    r.HOTEL_OLD_ID,
                    r.ROOM_NUMBER,
                    h.HOTEL_NAME_CN,
                    ONLINE_SATATUS =
                // 如果在连接信息表查询出有信息 那么 就是在线
                  tBL_RCU_CONN_INFO.Contains(r.MAC.ToUpper())? 1 : 0 ,
                COMM_TIMEMARK = db.TBL_RCU_CONN_INFOS.Where(x => x.MAC.ToUpper() == r.MAC.ToUpper()).Select(x => x.COMM_TIMEMARK).FirstOrDefault()
        }).Where(x=> room_online_satatus == -1 || x.ONLINE_SATATUS == room_online_satatus).OrderByDescending(x=>x.ONLINE_SATATUS);
                // 取出数据到内存后 根据mac 只取出前一条  因为mac 可能出现重复  一个是自己注册  一个是 mac 导入
                // 暂时不考虑 重复mac 就不需要分组
                // 如果一次取出全部数据 那么又失去了分页的必要性   
                //.AsEnumerable()
                //.GroupBy(x=>x.MAC).Select(x=>x.First())
                
            count = db.TBL_ROOM_BASIC_INFOS.Where(x => x.HOTEL_OLD_ID == hotel_id && x.MAC.Length > 0).Count();
            count_ = res.Count();
            return res.Skip(start).Take(length)
                .ToList();
        }
    }
}
