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

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

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

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