StoreHouseDbconn.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. WebCMSEntities db = new WebCMSEntities();
  15. StoreHouse order = db.StoreHouse.FirstOrDefault(m => m.Id == Id);
  16. if (order != null)
  17. {
  18. }
  19. db.Dispose();
  20. return order;
  21. }
  22. #endregion
  23. #region 获取列表
  24. public List<int> GetList(int UserId, int pageNum = 1, int pageSize = 10)
  25. {
  26. string key = "StoreHouseList:" + UserId;
  27. if (UserId == 0)
  28. {
  29. key = "StoreHouseList";
  30. }
  31. List<int> list = new List<int>();
  32. if (RedisDbconn.Instance.Exists(key))
  33. {
  34. list = RedisDbconn.Instance.GetList<int>(key, pageNum, pageSize);
  35. if (list.Count > 0)
  36. {
  37. return list;
  38. }
  39. }
  40. List<int> newlist = new List<int>();
  41. RedisDbconn.Instance.GetLock(key + ":lock");
  42. WebCMSEntities db = new WebCMSEntities();
  43. var mysqllist = db.StoreHouse.Select(m => new { m.Id, m.LaveNum, m.UserId, m.Status }).Where(m => m.Status > -1);
  44. if (UserId > 0)
  45. {
  46. mysqllist = mysqllist.Where(m => m.UserId == UserId);
  47. }
  48. else
  49. {
  50. mysqllist = mysqllist.Where(m => m.LaveNum > 0);
  51. }
  52. var resultlist = mysqllist.OrderByDescending(m => m.Id).ToList();
  53. if (resultlist.Count > 0)
  54. {
  55. foreach (var sub in resultlist)
  56. {
  57. newlist.Add(sub.Id);
  58. }
  59. RedisDbconn.Instance.Clear(key);
  60. foreach (int sub in newlist)
  61. {
  62. RedisDbconn.Instance.AddRightList(key, sub);
  63. }
  64. RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
  65. return newlist;
  66. }
  67. db.Dispose();
  68. RedisDbconn.Instance.ReleaseLock(key + ":lock");
  69. return newlist;
  70. }
  71. #endregion
  72. }
  73. }