MerchantInfoDbconn.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.MainModels;
  5. namespace MySystem
  6. {
  7. public class MerchantInfoDbconn
  8. {
  9. public readonly static MerchantInfoDbconn Instance = new MerchantInfoDbconn();
  10. #region 获取单个字段
  11. public MerchantInfo Get(int Id)
  12. {
  13. WebCMSEntities db = new WebCMSEntities();
  14. MerchantInfo order = db.MerchantInfo.FirstOrDefault(m => m.Id == Id);
  15. if (order != null)
  16. {
  17. }
  18. db.Dispose();
  19. return order;
  20. }
  21. #endregion
  22. #region 获取列表
  23. public List<int> GetList(int UserId, int pageNum = 1, int pageSize = 10)
  24. {
  25. string key = "MerchantInfoList:" + UserId;
  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. WebCMSEntities db = new WebCMSEntities();
  36. var mysqllist = db.MerchantInfo.Select(m => new { m.Id, m.UserId, m.QueryCount }).Where(m => m.UserId == UserId && m.QueryCount == 1).OrderByDescending(m => m.Id).ToList();
  37. if (mysqllist.Count > 0)
  38. {
  39. List<int> newlist = new List<int>();
  40. foreach (var sub in mysqllist)
  41. {
  42. newlist.Add(sub.Id);
  43. }
  44. RedisDbconn.Instance.Clear(key);
  45. foreach (int sub in newlist)
  46. {
  47. RedisDbconn.Instance.AddRightList(key, sub);
  48. }
  49. RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
  50. }
  51. db.Dispose();
  52. return list;
  53. }
  54. public List<MerchantInfo> GetTmpList(int UserId, int pageNum = 1, int pageSize = 10)
  55. {
  56. string key = "TmpMerchantInfo:" + UserId;
  57. List<MerchantInfo> list = new List<MerchantInfo>();
  58. if (RedisDbconn.Instance.Exists(key))
  59. {
  60. list = RedisDbconn.Instance.GetList<MerchantInfo>(key, pageNum, pageSize);
  61. if (list.Count > 0)
  62. {
  63. return list;
  64. }
  65. }
  66. WebCMSEntities db = new WebCMSEntities();
  67. var mysqllist = db.MerchantInfo.Where(m => m.UserId == UserId && m.QueryCount == 1).OrderByDescending(m => m.Id).ToList();
  68. if (mysqllist.Count > 0)
  69. {
  70. List<MerchantInfo> newlist = new List<MerchantInfo>();
  71. foreach (var sub in mysqllist)
  72. {
  73. newlist.Add(sub);
  74. }
  75. RedisDbconn.Instance.Clear(key);
  76. foreach (MerchantInfo 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. }