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

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

        #region 获取单个字段
        public UserAddress Get(int Id)
        {
            // string key = "UserAddress:" + Id;
            // if (RedisDbconn.Instance.Exists(key))
            // {
            //     UserAddress obj = RedisDbconn.Instance.Get<UserAddress>(key);
            //     if (obj != null)
            //     {
            //         return obj;
            //     }
            // }
            WebCMSEntities db = new WebCMSEntities();
            UserAddress order = db.UserAddress.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;
        }
        #endregion

        #region 获取列表
        public List<int> GetList(int UserId, int pageNum = 1, int pageSize = 10)
        {
            // string key = "UserAddressList:" + 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();
            List<int> newlist = new List<int>();
            var mysqllist = db.UserAddress.Select(m => new { m.Id, m.UserId }).Where(m => m.UserId == UserId).OrderByDescending(m => m.Id).ToList();
            if (mysqllist.Count > 0)
            {
                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 newlist;
        }
        #endregion
    }
}
