OpenRewardDetailDbconn.cs 2.4 KB

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