UserAccountDbconn.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. WebCMSEntities db = new WebCMSEntities();
  15. UserAccount order = db.UserAccount.FirstOrDefault(m => m.Id == Id);
  16. if (order != null)
  17. {
  18. }
  19. db.Dispose();
  20. return order;
  21. }
  22. public decimal GetIncome(int Id, string TradeMonth)
  23. {
  24. string key = "UserAccount:Income:" + Id + ":" + TradeMonth;
  25. if (RedisDbconn.Instance.Exists(key))
  26. {
  27. decimal obj = RedisDbconn.Instance.Get<decimal>(key);
  28. return obj;
  29. }
  30. WebCMSEntities db = new WebCMSEntities();
  31. decimal result = 0;
  32. DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
  33. DateTime end = start.AddMonths(1);
  34. List<int> ChangeTypes = PublicFunction.IncomeTypes();
  35. bool check = db.UserAccountRecord.Any(m => m.UserId == Id && m.CreateDate >= start && m.CreateDate < end && ChangeTypes.Contains(m.ChangeType));
  36. if (check)
  37. {
  38. result = db.UserAccountRecord.Where(m => m.UserId == Id && m.CreateDate >= start && m.CreateDate < end && ChangeTypes.Contains(m.ChangeType)).Sum(m => m.ChangeAmount);
  39. RedisDbconn.Instance.Set(key, result);
  40. RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(180, 300));
  41. }
  42. return result;
  43. }
  44. public decimal GetExpend(int Id, string TradeMonth)
  45. {
  46. string key = "UserAccount:Expend:" + Id + ":" + TradeMonth;
  47. if (RedisDbconn.Instance.Exists(key))
  48. {
  49. decimal obj = RedisDbconn.Instance.Get<decimal>(key);
  50. return obj;
  51. }
  52. decimal result = 0;
  53. WebCMSEntities db = new WebCMSEntities();
  54. DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
  55. DateTime end = start.AddMonths(1);
  56. List<int> ChangeTypes = PublicFunction.ExpendTypes();
  57. bool check = db.UserAccountRecord.Any(m => m.UserId == Id && m.CreateDate >= start && m.CreateDate < end && ChangeTypes.Contains(m.ChangeType));
  58. if (check)
  59. {
  60. result += db.UserAccountRecord.Where(m => m.UserId == Id && m.CreateDate >= start && m.CreateDate < end && ChangeTypes.Contains(m.ChangeType)).Sum(m => m.ChangeAmount);
  61. }
  62. DateTime checkTime = DateTime.Parse("2022-04-11 19:31:39");
  63. check = db.UserCashRecord.Any(m => m.UserId == Id && m.CreateDate < checkTime && m.CreateDate >= start && m.CreateDate < end && m.Status == 1);
  64. if (check)
  65. {
  66. result += db.UserCashRecord.Where(m => m.UserId == Id && m.CreateDate < checkTime && m.CreateDate >= start && m.CreateDate < end && m.Status == 1).Sum(m => m.TradeAmount);
  67. }
  68. RedisDbconn.Instance.Set(key, result);
  69. RedisDbconn.Instance.SetExpire(key, Library.function.get_Random(180, 600));
  70. return result;
  71. }
  72. #endregion
  73. }
  74. }