using COMMON;
using DB_Server;
using Models;
using Models.Models;
using Models.Models.LOGDB;
using SERVER.LIB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SERVER
{
    /// <summary>
    /// 缓存信息列表
    /// </summary>
    public class CacheData
    {
        /// <summary>
        /// 缓存枚举
        /// </summary>
        public enum CacheDataEnum
        {
            /// <summary>
            /// 酒店组缓存
            /// </summary>
            TBL_HOTEL_GROUP_INFO,
            /// <summary>
            /// 酒店缓存
            /// </summary>
            TBL_HOTEL_BASIC_INFO,
            /// <summary>
            /// 房型列表
            /// </summary>
            TBL_ROOM_TYPE_LIST,
            /// <summary>
            /// 模型文件数据
            /// </summary>
            TBL_MODEL_FILE_DATA,
            /// <summary>
            /// ruc
            /// </summary>
            TBL_RCU_BASIC_INFO
        }
        /// <summary>
        /// UDPlog 数量 10 分钟有效
        /// </summary>
        public static int UDPcount => int.Parse(
        XC_Redis.Redis.GET("Getudplogcount", () =>
        {
            return new ASLS().GetLogs_(0, "* | select count(*) sum", ConfigEntity.Instance.STARTTIME, DateTime.Now).Result[0]["sum"];
        }
        , 60 * 10));

        /// <summary>
        /// TBL_HOTEL_GROUP_INFO 酒店分组
        /// </summary>
        public static List<TBL_HOTEL_GROUP_INFO> TBL_HOTEL_GROUP_INFO { get { return GetData<TBL_HOTEL_GROUP_INFO>("TBL_HOTEL_GROUP_INFO", DbHelperSQL.DBSel.BLV_RCU_DB); } }

        /// <summary>
        /// rcu
        /// </summary>
        public static List<TBL_RCU_BASIC_INFO> TBL_RCU_BASIC_INFO { get { return GetData<TBL_RCU_BASIC_INFO>("TBL_RCU_BASIC_INFO", DbHelperSQL.DBSel.BLV_RCU_DB); } }

        /// <summary>
        /// 模型文件数据 
        /// </summary>
        public static List<TBL_MODEL_FILE_DATA> TBL_MODEL_FILE_DATA { get { return GetData<TBL_MODEL_FILE_DATA>("TBL_MODEL_FILE_DATA", DbHelperSQL.DBSel.BLV_RCU_DB,10); } }



        /// <summary>
        /// 酒店表 
        /// </summary>
        public static List<TBL_HOTEL_BASIC_INFO> TBL_HOTEL_BASIC_INFO { get { return GetData<TBL_HOTEL_BASIC_INFO>("TBL_HOTEL_BASIC_INFO", DbHelperSQL.DBSel.BLV_RCU_DB); } }
        /// <summary>
        /// TBL_ROOM_TYPE_LIST 房型表 
        /// </summary>
        public static List<TBL_ROOM_TYPE_LIST> TBL_ROOM_TYPE_LIST { get { return GetData<TBL_ROOM_TYPE_LIST>("TBL_ROOM_TYPE_LIST", DbHelperSQL.DBSel.BLV_RCU_DB); } }

        /// <summary>
        /// 公共的获取缓存方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="dB"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        private static List<T> GetData<T>(string name, DbHelperSQL.DBSel dB,int time = -1) where T : new()
        {
            return XC_Redis.Redis.GET(name, () =>
              {
                  var data = new DbHelperSQL(dB);
                  return UtilsSharp.DataTableHelper.ToEntities<T>(data.ExecuteTable("select * from " + name));
              }, time
              );

        }
        /// <summary>
        /// 清除指定缓存 或者全部缓存
        /// </summary>
        /// <param name="cache"></param>
        /// <returns></returns>
        public static bool Clear(CacheDataEnum? cache)
        {
            if (cache == null)
            {
                foreach (int myCode in Enum.GetValues(typeof(CacheDataEnum)))
                {
                    string strName = Enum.GetName(typeof(CacheDataEnum), myCode);//获取名称
                    if (!XC_Redis.Redis.Remove(strName))
                    {
                        return false;
                    };
                }
                return true;
            }
            return XC_Redis.Redis.Remove(cache.ToString());
        }
    }
}
