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

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

        #region 获取单个字段
        public UserStoreChange Get(int Id)
        {
            string key = "UserStoreChange:" + Id;
            if (RedisDbconn.Instance.Exists(key))
            {
                UserStoreChange obj = RedisDbconn.Instance.Get<UserStoreChange>(key);
                if (obj != null)
                {
                    return obj;
                }
            }
            WebCMSEntities db = new WebCMSEntities();
            UserStoreChange orderPro = db.UserStoreChange.FirstOrDefault(m => m.Id == Id);
            if (orderPro != null)
            {
                RedisDbconn.Instance.Set(key, orderPro);
                RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
            }
            return orderPro;
        }
        #endregion


        #region 获取列表
        public List<UserStoreChange> GetList(int UserId, int BrandId, int pageNum = 1, int pageSize = 10)
        {
            string key = "UserStoreChangeList:" + UserId + ":" + BrandId;
            List<UserStoreChange> list = new List<UserStoreChange>();
            // if (RedisDbconn.Instance.Exists(key))
            // {
            //     list = RedisDbconn.Instance.GetList<UserStoreChange>(key, pageNum, pageSize);
            //     if (list.Count > 0)
            //     {
            //         return list;
            //     }
            // }
            WebCMSEntities db = new WebCMSEntities();
            IQueryable<UserStoreChange> mysqllist = db.UserStoreChange.Where(m => m.UserId == UserId && m.BrandId == BrandId).OrderByDescending(m => m.Id);
            if (pageNum == 1)
            {
                mysqllist = mysqllist.Take(pageSize);
            }
            else
            {
                int skipNum = pageSize * (pageNum - 1);
                mysqllist = mysqllist.Skip(skipNum).Take(pageSize);
            }
            List<UserStoreChange> result = mysqllist.ToList();
            db.Dispose();
            return result;
            // if (mysqllist.Count > 0)
            // {
            //     List<UserStoreChange> newlist = new List<UserStoreChange>();
            //     foreach (UserStoreChange sub in mysqllist)
            //     {
            //         newlist.Add(sub);
            //     }
            //     RedisDbconn.Instance.Clear(key);
            //     foreach (UserStoreChange sub in newlist)
            //     {
            //         RedisDbconn.Instance.AddRightList(key, sub);
            //     }
            //     RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
            // }
            // db.Dispose();
            // return list;
        }
        #endregion
    }
}
