PosMachinesDbconn.cs 3.2 KB

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