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

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

        #region 获取单个字段
        public MerchantInfo Get(int Id)
        {
            WebCMSEntities db = new WebCMSEntities();
            MerchantInfo order = db.MerchantInfo.FirstOrDefault(m => m.Id == Id);
            if (order != null)
            {
            }
            db.Dispose();
            return order;
        }
        #endregion

        #region 获取列表
        public List<int> GetList(int UserId, int pageNum = 1, int pageSize = 10)
        {
            string key = "MerchantInfoList:" + UserId;
            List<int> list = new List<int>();
            if (RedisDbconn.Instance.Exists(key))
            {
                list = RedisDbconn.Instance.GetList<int>(key, pageNum, pageSize);
                if (list.Count > 0)
                {
                    return list;
                }
            }
            WebCMSEntities db = new WebCMSEntities();
            var mysqllist = db.MerchantInfo.Select(m => new { m.Id, m.UserId, m.QueryCount }).Where(m => m.UserId == UserId && m.QueryCount == 1).OrderByDescending(m => m.Id).ToList();
            if (mysqllist.Count > 0)
            {
                List<int> newlist = new List<int>();
                foreach (var sub in mysqllist)
                {
                    newlist.Add(sub.Id);
                }
                RedisDbconn.Instance.Clear(key);
                foreach (int sub in newlist)
                {
                    RedisDbconn.Instance.AddRightList(key, sub);
                }
                RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
            }
            db.Dispose();
            return list;
        }
        public List<MerchantInfo> GetTmpList(int UserId, int pageNum = 1, int pageSize = 10)
        {
            string key = "TmpMerchantInfo:" + UserId;
            List<MerchantInfo> list = new List<MerchantInfo>();
            if (RedisDbconn.Instance.Exists(key))
            {
                list = RedisDbconn.Instance.GetList<MerchantInfo>(key, pageNum, pageSize);
                if (list.Count > 0)
                {
                    return list;
                }
            }
            WebCMSEntities db = new WebCMSEntities();
            var mysqllist = db.MerchantInfo.Where(m => m.UserId == UserId && m.QueryCount == 1).OrderByDescending(m => m.Id).ToList();
            if (mysqllist.Count > 0)
            {
                List<MerchantInfo> newlist = new List<MerchantInfo>();
                foreach (var sub in mysqllist)
                {
                    newlist.Add(sub);
                }
                RedisDbconn.Instance.Clear(key);
                foreach (MerchantInfo sub in newlist)
                {
                    RedisDbconn.Instance.AddRightList(key, sub);
                }
                RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
            }
            db.Dispose();
            return list;
        }
        #endregion
    }
}
