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

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

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

        #region 获取列表
        public List<int> GetList(int MerchantId, int pageNum = 1, int pageSize = 10)
        {
            // string key = "PosMachineList:" + MerchantId;
            // 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;
            //     }
            // }
            int skip = (pageNum - 1) * pageSize;
            WebCMSEntities db = new WebCMSEntities();
            List<int> newlist = new List<int>();
            var mysqllist = db.PosMachines.Select(m => new { m.Id, m.BindMerchantId, m.DeviceKind }).Where(m => m.BindMerchantId == MerchantId && m.DeviceKind == "2").OrderByDescending(m => m.Id).Skip(skip).Take(pageSize).ToList();
            if (mysqllist.Count > 0)
            {
                foreach (var sub in mysqllist)
                {
                    newlist.Add(sub.Id);
                }
            }
            db.Dispose();
            return newlist;
        }
        #endregion
    }
}
