OrderProductDbconn.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.MainModels;
  5. namespace MySystem
  6. {
  7. public class OrderProductDbconn
  8. {
  9. public readonly static OrderProductDbconn Instance = new OrderProductDbconn();
  10. #region 获取单个字段
  11. public OrderProduct Get(int Id)
  12. {
  13. WebCMSEntities db = new WebCMSEntities();
  14. OrderProduct orderPro = db.OrderProduct.FirstOrDefault(m => m.Id == Id);
  15. if (orderPro != null)
  16. {
  17. }
  18. db.Dispose();
  19. return orderPro;
  20. }
  21. #endregion
  22. #region 获取列表
  23. public List<int> GetList(int OrderId, int pageNum = 1, int pageSize = 10)
  24. {
  25. string key = "OrderProductList:" + OrderId;
  26. List<int> list = new List<int>();
  27. if (RedisDbconn.Instance.Exists(key))
  28. {
  29. list = RedisDbconn.Instance.GetList<int>(key, pageNum, pageSize);
  30. if (list.Count > 0)
  31. {
  32. return list;
  33. }
  34. }
  35. List<int> newlist = new List<int>();
  36. RedisDbconn.Instance.GetLock(key + ":lock");
  37. WebCMSEntities db = new WebCMSEntities();
  38. var mysqllist = db.OrderProduct.Select(m => new { m.Id, m.OrderId }).Where(m => m.OrderId == OrderId).OrderByDescending(m => m.Id).ToList();
  39. if (mysqllist.Count > 0)
  40. {
  41. foreach (var sub in mysqllist)
  42. {
  43. newlist.Add(sub.Id);
  44. }
  45. RedisDbconn.Instance.Clear(key);
  46. foreach (int sub in newlist)
  47. {
  48. RedisDbconn.Instance.AddRightList(key, sub);
  49. }
  50. RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
  51. }
  52. db.Dispose();
  53. RedisDbconn.Instance.ReleaseLock(key + ":lock");
  54. return newlist;
  55. }
  56. #endregion
  57. }
  58. }