StoreHouseDbconn.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using System.Linq;
  5. using MySystem.MainModels;
  6. namespace MySystem
  7. {
  8. public class StoreHouseDbconn
  9. {
  10. public readonly static StoreHouseDbconn Instance = new StoreHouseDbconn();
  11. #region 获取单个字段
  12. public StoreHouse Get(int Id)
  13. {
  14. // string key = "StoreHouse:" + Id;
  15. // if (RedisDbconn.Instance.Exists(key))
  16. // {
  17. // StoreHouse obj = RedisDbconn.Instance.Get<StoreHouse>(key);
  18. // if (obj != null)
  19. // {
  20. // return obj;
  21. // }
  22. // }
  23. WebCMSEntities db = new WebCMSEntities();
  24. StoreHouse order = db.StoreHouse.FirstOrDefault(m => m.Id == Id);
  25. if (order != null)
  26. {
  27. // RedisDbconn.Instance.Set(key, order);
  28. // RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
  29. }
  30. db.Dispose();
  31. return order;
  32. }
  33. #endregion
  34. #region 获取列表
  35. public List<int> GetList(int UserId, int pageNum = 1, int pageSize = 10)
  36. {
  37. string key = "StoreHouseList:" + UserId;
  38. if (UserId == 0)
  39. {
  40. key = "StoreHouseList";
  41. }
  42. List<int> list = new List<int>();
  43. if (RedisDbconn.Instance.Exists(key))
  44. {
  45. list = RedisDbconn.Instance.GetList<int>(key, pageNum, pageSize);
  46. if (list.Count > 0)
  47. {
  48. return list;
  49. }
  50. }
  51. List<int> newlist = new List<int>();
  52. RedisDbconn.Instance.GetLock(key + ":lock");
  53. WebCMSEntities db = new WebCMSEntities();
  54. var mysqllist = db.StoreHouse.Select(m => new { m.Id, m.LaveNum, m.UserId, m.Status }).Where(m => m.Status > -1);
  55. if (UserId > 0)
  56. {
  57. mysqllist = mysqllist.Where(m => m.UserId == UserId);
  58. }
  59. else
  60. {
  61. mysqllist = mysqllist.Where(m => m.LaveNum > 0);
  62. }
  63. var resultlist = mysqllist.OrderByDescending(m => m.Id).ToList();
  64. if (resultlist.Count > 0)
  65. {
  66. foreach (var sub in resultlist)
  67. {
  68. newlist.Add(sub.Id);
  69. }
  70. RedisDbconn.Instance.Clear(key);
  71. foreach (int sub in newlist)
  72. {
  73. RedisDbconn.Instance.AddRightList(key, sub);
  74. }
  75. RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
  76. return newlist;
  77. }
  78. db.Dispose();
  79. RedisDbconn.Instance.ReleaseLock(key + ":lock");
  80. return newlist;
  81. }
  82. #endregion
  83. }
  84. }