UserMonthFeeHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Linq;
  5. using System.Data;
  6. using MySystem;
  7. using MySystem.PxcModels;
  8. using Library;
  9. using LitJson;
  10. public class UserMonthFeeHelper
  11. {
  12. public readonly static UserMonthFeeHelper Instance = new UserMonthFeeHelper();
  13. private UserMonthFeeHelper()
  14. {
  15. }
  16. public void Start()
  17. {
  18. Thread th = new Thread(DoWorks);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. private void DoWorks()
  23. {
  24. while (true)
  25. {
  26. try
  27. {
  28. string content = RedisDbconn.Instance.RPop<string>("UserMonthFeeQueue");
  29. if(!string.IsNullOrEmpty(content))
  30. {
  31. int uid = int.Parse(content);
  32. WebCMSEntities db = new WebCMSEntities();
  33. DoSomething(db, uid);
  34. db.Dispose();
  35. RedisDbconn.Instance.AddList("PosExpiredPayQueue", content);
  36. }
  37. else
  38. {
  39. Thread.Sleep(60000);
  40. }
  41. }
  42. catch (Exception ex)
  43. {
  44. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "创客每月月费扣款异常");
  45. }
  46. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "创客每月月费扣款日志");
  47. }
  48. }
  49. public void DoSomething(WebCMSEntities db, int UserId)
  50. {
  51. DateTime Start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
  52. DateTime WithoutStart = DateTime.Parse("2023-02-20 00:00:00"); //收支明细服务费记录开始时间
  53. UserAccount User = db.UserAccount.FirstOrDefault(m => m.Id == UserId && m.BalanceAmount > 0);
  54. if(User != null)
  55. {
  56. bool check = db.UserAccountRecord.Any(m => m.UserId == User.Id && m.CreateDate >= Start && m.CreateDate >= WithoutStart && m.ChangeType == 125);
  57. if(!check)
  58. {
  59. if(User.BalanceAmount > 10)
  60. {
  61. AlipayPayBack2Service.Instance.ChangeAccount(db, new Orders(), User.Id, -10, 125);
  62. }
  63. else
  64. {
  65. AlipayPayBack2Service.Instance.ChangeAccount(db, new Orders(), User.Id, -User.BalanceAmount, 125);
  66. }
  67. }
  68. }
  69. }
  70. public void Start2()
  71. {
  72. Thread th = new Thread(DoWorks2);
  73. th.IsBackground = true;
  74. th.Start();
  75. }
  76. private void DoWorks2()
  77. {
  78. while (true)
  79. {
  80. try
  81. {
  82. if(DateTime.Now.Hour == 1 && DateTime.Now < DateTime.Parse("2023-02-20 00:00:00"))
  83. {
  84. string check = function.ReadInstance("/UserMonthFee/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  85. if(string.IsNullOrEmpty(check))
  86. {
  87. function.WritePage("/UserMonthFee/", DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  88. WebCMSEntities db = new WebCMSEntities();
  89. DoSomething2(db);
  90. db.Dispose();
  91. }
  92. }
  93. }
  94. catch (Exception ex)
  95. {
  96. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "创客每月月费补扣款异常");
  97. }
  98. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "创客每月月费补扣款日志");
  99. Thread.Sleep(60000);
  100. }
  101. }
  102. public void DoSomething2(WebCMSEntities db)
  103. {
  104. DateTime Start = DateTime.Parse("2023-01-19 00:00:00"); //收支明细服务费记录开始时间
  105. // DateTime ExpireDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00").AddDays(-90); //创客认证超过90天比对时间
  106. // //超过90天创客ID集合
  107. // List<int> UserIds = db.Users.Where(m => m.AuthFlag == 1 && m.AuthDate < ExpireDate).ToList().Select(m => m.Id).ToList();
  108. List<int> UserIds = new List<int>();
  109. UserIds.Add(311);
  110. //已扣费创客ID集合
  111. List<int> UserAccountRecordIds = db.UserAccountRecord.Where(m => UserIds.Contains(m.UserId) && m.CreateDate >= Start && m.ChangeType == 125).ToList().Select(m => m.UserId).ToList();
  112. //应该扣除而未扣的创客集合
  113. var Users = db.UserAccount.Select(m => new { m.Id, m.BalanceAmount }).Where(m => !UserAccountRecordIds.Contains(m.Id) && UserIds.Contains(m.Id) && m.BalanceAmount >= 10).ToList();
  114. foreach(var User in Users)
  115. {
  116. AlipayPayBack2Service.Instance.ChangeAccount(db, new Orders(), User.Id, -10, 125); //执行扣费
  117. }
  118. }
  119. }