using System;
using System.Collections.Generic;
using System.Linq;
using MySystem.MainModels;

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

        #region 获取列表
        public List<KqProducts> GetList()
        {
            string key = "KqProducts";
            List<KqProducts> list = new List<KqProducts>();
            if (RedisDbconn.Instance.Exists(key))
            {
                list = RedisDbconn.Instance.GetList<KqProducts>(key, 1, 100);
                if (list.Count > 0)
                {
                    return list;
                }
            }
            List<KqProducts> newlist = new List<KqProducts>();
            RedisDbconn.Instance.GetLock(key + ":lock");
            WebCMSEntities db = new WebCMSEntities();
            var mysqllist = db.KqProducts.Where(m => m.Status == 1).OrderBy(m => m.Id).ToList();
            if (mysqllist.Count > 0)
            {
                foreach (var sub in mysqllist)
                {
                    newlist.Add(sub);
                }
                RedisDbconn.Instance.Clear(key);
                foreach (KqProducts sub in newlist)
                {
                    RedisDbconn.Instance.AddRightList(key, sub);
                }
            }
            db.Dispose();
            RedisDbconn.Instance.ReleaseLock(key + ":lock");
            return newlist;
        }
        #endregion
    }
}
