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

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

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

        #region 获取列表
        public List<PosMerchantInfo> GetList(int UserId, int pageNum = 1, int pageSize = 10)
        {
            string key = "PosMerchantInfoList:" + UserId;
            List<PosMerchantInfo> list = new List<PosMerchantInfo>();
            if (RedisDbconn.Instance.Exists(key))
            {
                list = RedisDbconn.Instance.GetList<PosMerchantInfo>(key, pageNum, pageSize);
                if (list.Count > 0)
                {
                    return list;
                }
            }
            RedisDbconn.Instance.GetLock(key + ":lock");
            WebCMSEntities db = new WebCMSEntities();
            List<PosMerchantInfo> newlist = new List<PosMerchantInfo>();
            var mysqllist = db.PosMachinesTwo.Select(m => new { m.BuyUserId, m.UserId, m.BindingState, m.Id, m.BindMerchantId, m.Status }).Where(m => m.Status > -1 && m.BuyUserId == UserId && m.BindingState == 1).OrderByDescending(m => m.Id).ToList();
            if (mysqllist.Count > 0)
            {
                foreach (var sub in mysqllist)
                {
                    PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == sub.BindMerchantId) ?? new PosMerchantInfo();
                    newlist.Add(merchant);
                }
                RedisDbconn.Instance.Clear(key);
                foreach (PosMerchantInfo sub in newlist)
                {
                    RedisDbconn.Instance.AddRightList(key, sub);
                }
                RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(300, 600));
            }
            db.Dispose();
            RedisDbconn.Instance.ReleaseLock(key + ":lock");
            return newlist;
        }
        #endregion


        #region 获取列表子账号
        public List<PosMerchantInfo> GetChildList(int PartnerId, int UserId, int pageNum = 1, int pageSize = 10)
        {
            string key = "PosMerchantInfoList:" + PartnerId;
            List<PosMerchantInfo> list = new List<PosMerchantInfo>();
            if (RedisDbconn.Instance.Exists(key))
            {
                list = RedisDbconn.Instance.GetList<PosMerchantInfo>(key, pageNum, pageSize);
                if (list.Count > 0)
                {
                    return list;
                }
            }
            RedisDbconn.Instance.GetLock(key + ":lock");
            WebCMSEntities db = new WebCMSEntities();
            List<int> posId = new List<int>();
            var Id = db.BusinessPartnerPos.Where(m => m.Id == PartnerId).ToList();
            foreach (var item in Id)
            {
                posId.Add(item.PosId);
            }
            List<PosMerchantInfo> newlist = new List<PosMerchantInfo>();
            var mysqllist = db.PosMachinesTwo.Select(m => new { m.BuyUserId, m.UserId, m.BindingState, m.Id, m.BindMerchantId, m.Status }).Where(m => m.Status > -1 && m.BuyUserId == UserId && m.BindingState == 1 && posId.Contains(m.Id)).OrderByDescending(m => m.Id).ToList();
            if (mysqllist.Count > 0)
            {
                foreach (var sub in mysqllist)
                {
                    PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == sub.BindMerchantId) ?? new PosMerchantInfo();
                    newlist.Add(merchant);
                }
                RedisDbconn.Instance.Clear(key);
                foreach (PosMerchantInfo sub in newlist)
                {
                    RedisDbconn.Instance.AddRightList(key, sub);
                }
                RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(300, 600));
            }
            db.Dispose();
            RedisDbconn.Instance.ReleaseLock(key + ":lock");
            return newlist;
        }
        #endregion
    }
}
