UserAccountDbconn.cs 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 UserAccountDbconn
  9. {
  10. public readonly static UserAccountDbconn Instance = new UserAccountDbconn();
  11. #region 获取单个字段
  12. public UserAccount Get(int Id)
  13. {
  14. // string key = "UserAccount:" + Id;
  15. // if (RedisDbconn.Instance.Exists(key))
  16. // {
  17. // UserAccount obj = RedisDbconn.Instance.Get<UserAccount>(key);
  18. // if (obj != null)
  19. // {
  20. // return obj;
  21. // }
  22. // }
  23. WebCMSEntities db = new WebCMSEntities();
  24. UserAccount order = db.UserAccount.FirstOrDefault(m => m.Id == Id);
  25. if (order != null)
  26. {
  27. // RedisDbconn.Instance.Set(key, order);
  28. // RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(1800, 5400));
  29. }
  30. db.Dispose();
  31. return order;
  32. }
  33. public decimal GetIncome(int Id, string TradeMonth)
  34. {
  35. string key = "UserAccount:Income:" + Id + ":" + TradeMonth;
  36. if (RedisDbconn.Instance.Exists(key))
  37. {
  38. decimal obj = RedisDbconn.Instance.Get<decimal>(key);
  39. return obj;
  40. }
  41. WebCMSEntities db = new WebCMSEntities();
  42. decimal result = 0;
  43. DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
  44. DateTime end = start.AddMonths(1);
  45. bool check = db.UserAccountRecord.Any(m => m.UserId == Id && m.CreateDate >= start && m.CreateDate < end && (m.ChangeType == 0 || m.ChangeType == 1 || m.ChangeType == 12 || m.ChangeType == 31 || m.ChangeType == 50 || m.ChangeType == 60 || m.ChangeType == 64 || m.ChangeType == 111 || m.ChangeType == 112 || m.ChangeType == 113 || m.ChangeType == 114 || m.ChangeType == 115 || m.ChangeType == 116 || m.ChangeType == 117 || m.ChangeType == 118 || m.ChangeType == 119));
  46. if (check)
  47. {
  48. result = db.UserAccountRecord.Where(m => m.UserId == Id && m.CreateDate >= start && m.CreateDate < end && (m.ChangeType == 0 || m.ChangeType == 1 || m.ChangeType == 12 || m.ChangeType == 31 || m.ChangeType == 50 || m.ChangeType == 60 || m.ChangeType == 64 || m.ChangeType == 111 || m.ChangeType == 112 || m.ChangeType == 113 || m.ChangeType == 114 || m.ChangeType == 115 || m.ChangeType == 116 || m.ChangeType == 117 || m.ChangeType == 118 || m.ChangeType == 119)).Sum(m => m.ChangeAmount);
  49. RedisDbconn.Instance.Set(key, result);
  50. RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(180, 300));
  51. }
  52. return result;
  53. }
  54. public decimal GetExpend(int Id, string TradeMonth)
  55. {
  56. string key = "UserAccount:Expend:" + Id + ":" + TradeMonth;
  57. if (RedisDbconn.Instance.Exists(key))
  58. {
  59. decimal obj = RedisDbconn.Instance.Get<decimal>(key);
  60. return obj;
  61. }
  62. decimal result = 0;
  63. WebCMSEntities db = new WebCMSEntities();
  64. DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
  65. DateTime end = start.AddMonths(1);
  66. bool check = db.UserAccountRecord.Any(m => m.UserId == Id && m.CreateDate >= start && m.CreateDate < end && (m.ChangeType == 3 || m.ChangeType == 4 || m.ChangeType == 5 || m.ChangeType == 20 || m.ChangeType == 63));
  67. if (check)
  68. {
  69. result += db.UserAccountRecord.Where(m => m.UserId == Id && m.CreateDate >= start && m.CreateDate < end && (m.ChangeType == 3 || m.ChangeType == 4 || m.ChangeType == 5 || m.ChangeType == 20 || m.ChangeType == 63)).Sum(m => m.ChangeAmount);
  70. }
  71. DateTime checkTime = DateTime.Parse("2022-04-11 19:31:39");
  72. check = db.UserCashRecord.Any(m => m.UserId == Id && m.CreateDate < checkTime && m.CreateDate >= start && m.CreateDate < end && m.Status == 1);
  73. if (check)
  74. {
  75. result += db.UserCashRecord.Where(m => m.UserId == Id && m.CreateDate < checkTime && m.CreateDate >= start && m.CreateDate < end && m.Status == 1).Sum(m => m.TradeAmount);
  76. }
  77. RedisDbconn.Instance.Set(key, result);
  78. RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(180, 600));
  79. return result;
  80. }
  81. #endregion
  82. }
  83. }