using System;
using System.Collections.Generic;
using Library;
using System.Linq;
using MySystem.Models;

namespace MySystem
{
    public class MysqlDbconn
    {
        public readonly static MysqlDbconn Instance = new MysqlDbconn();

        #region 获取单个字段
        public PageUpdateInfo GetPage(string pageName, string Kind = "default")
        {
            string key = "PageUpdateInfo:" + Kind + ":" + pageName;
            string FileName = pageName + ".html";
            if (RedisDbconn.Instance.Exists(key))
            {
                PageUpdateInfo obj = RedisDbconn.Instance.Get<PageUpdateInfo>(key);
                if (obj != null)
                {
                    return obj;
                }
            }
            WebCMSEntities db = new WebCMSEntities();
            PageUpdateInfo page = db.PageUpdateInfo.FirstOrDefault(m => m.ModulePath == FileName && m.SeoKeyword == Kind);
            if (page != null)
            {
                RedisDbconn.Instance.Set(key, page);
            }
            return page;
        }
        #endregion

        #region 获取列表
        public List<PageUpdateInfo> GetPageList(string Kind = "default")
        {
            string key = "PageUpdateInfo:" + Kind;
            List<PageUpdateInfo> list = new List<PageUpdateInfo>();
            if (RedisDbconn.Instance.Exists(key))
            {
                list = RedisDbconn.Instance.GetList<PageUpdateInfo>(key, 1, 10000);
                if (list.Count > 0)
                {
                    return list;
                }
            }
            WebCMSEntities db = new WebCMSEntities();
            var mysqllist = db.PageUpdateInfo.Where(m => m.SeoKeyword == Kind).ToList();
            RedisDbconn.Instance.Clear(key);
            RedisDbconn.Instance.AddList(key, mysqllist.ToArray());
            db.Dispose();
            return list;
        }
        public List<AppBottomNavs> GetAppNavList(string Kind = "default")
        {
            string key = "AppBottomNavs:" + Kind;
            List<AppBottomNavs> list = new List<AppBottomNavs>();
            if (RedisDbconn.Instance.Exists(key))
            {
                list = RedisDbconn.Instance.GetList<AppBottomNavs>(key, 1, 1000);
                if (list.Count > 0)
                {
                    return list;
                }
            }
            WebCMSEntities db = new WebCMSEntities();
            int test = 0;
            if (Kind.EndsWith("test"))
            {
                test = 1;
            }
            var mysqllist = db.AppBottomNavs.Where(m => m.SeoKeyword == Kind && m.QueryCount == test).OrderByDescending(m => m.Sort).ThenBy(m => m.Id).ToList();
            RedisDbconn.Instance.Clear(key);
            RedisDbconn.Instance.AddList(key, mysqllist.ToArray());
            db.Dispose();
            return list;
        }
        public List<AppVersion> GetAppVersionList(string Kind = "default")
        {
            string key = "AppVersion:" + Kind;
            List<AppVersion> list = new List<AppVersion>();
            if (RedisDbconn.Instance.Exists(key))
            {
                list = RedisDbconn.Instance.GetList<AppVersion>(key, 1, 10000);
                if (list.Count > 0)
                {
                    return list;
                }
            }
            WebCMSEntities db = new WebCMSEntities();
            var mysqllist = db.AppVersion.Where(m => m.SeoKeyword == Kind).ToList();
            RedisDbconn.Instance.Clear(key);
            RedisDbconn.Instance.AddList(key, mysqllist.ToArray());
            db.Dispose();
            return list;
        }
        public List<FileUpdateInfo> GetFileList(string Kind = "default")
        {
            string key = "FileUpdateInfo:" + Kind;
            List<FileUpdateInfo> list = new List<FileUpdateInfo>();
            if (RedisDbconn.Instance.Exists(key))
            {
                list = RedisDbconn.Instance.GetList<FileUpdateInfo>(key, 1, 100000);
                if (list.Count > 0)
                {
                    return list;
                }
            }
            WebCMSEntities db = new WebCMSEntities();
            var mysqllist = db.FileUpdateInfo.Where(m => m.SeoKeyword == Kind).ToList();
            RedisDbconn.Instance.Clear(key);
            RedisDbconn.Instance.AddList(key, mysqllist.ToArray());
            db.Dispose();
            return list;
        }
        #endregion
    }
}
