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

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

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

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