TimeOutPosChargeService.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Threading;
  6. using MySystem.Models;
  7. using Library;
  8. namespace MySystem
  9. {
  10. /// <summary>
  11. /// 过期机具执行扣费
  12. /// </summary>
  13. public class TimeOutPosChargeService
  14. {
  15. public readonly static TimeOutPosChargeService Instance = new TimeOutPosChargeService();
  16. private TimeOutPosChargeService()
  17. { }
  18. public void Start()
  19. {
  20. Thread th = new Thread(doSomething);
  21. th.IsBackground = true;
  22. th.Start();
  23. }
  24. public void doSomething()
  25. {
  26. while (true)
  27. {
  28. if (DateTime.Now.Hour < 3)
  29. {
  30. try
  31. {
  32. string check = function.ReadInstance("/TimeOutPosCharge/check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
  33. if (string.IsNullOrEmpty(check))
  34. {
  35. function.WritePage("/TimeOutPosCharge/", "check" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString("HH:mm:ss"));
  36. WebCMSEntities db = new WebCMSEntities();
  37. var date = DateTime.Now.AddDays(-60).ToString("yyyy-MM-dd 00:00:00");//过期限制时间
  38. var time = DateTime.Parse(date);//过期限制时间
  39. DataTable dt = CustomerSqlConn.dtable("SELECT BuyUserId FROM PosMachinesTwo WHERE `Status`>-1 AND BuyUserId>0 AND BindingState=0 AND ActivationState=0 AND ScanQrTrade=0 AND RecycEndDate <'" + date + "' GROUP BY BuyUserId", MysqlConn.SqlConnStr);//扣费创客
  40. var query = db.PosMachinesTwo.Where(m => m.Status > -1 && m.BuyUserId > 0 && m.BindingState == 0 && m.ActivationState == 0 && m.ScanQrTrade == 0 && m.RecycEndDate < time).ToList();//循环过期超过15天机具
  41. var brandInfo = db.KqProducts.ToList();
  42. foreach (DataRow item in dt.Rows)
  43. {
  44. int BuyUserId = int.Parse(item["BuyUserId"].ToString());
  45. var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == BuyUserId);
  46. if (userAccount == null)
  47. {
  48. userAccount = db.UserAccount.Add(new UserAccount()
  49. {
  50. Id = BuyUserId,
  51. UserId = BuyUserId,
  52. }).Entity;
  53. db.SaveChanges();
  54. }
  55. var posInfo = query.Where(m => m.BuyUserId == BuyUserId);
  56. var amount = 0;
  57. foreach (var pos in posInfo)
  58. {
  59. var poss = db.PosMachinesTwo.FirstOrDefault(m => m.Id == pos.Id) ?? new PosMachinesTwo();
  60. poss.ScanQrTrade = 999;
  61. var Brand = brandInfo.FirstOrDefault(m => m.Id == pos.BrandId);
  62. if (Brand.Name.Contains("电签"))
  63. {
  64. amount = 200;
  65. }
  66. if (Brand.Name.Contains("大POS"))
  67. {
  68. amount = 300;
  69. }
  70. userAccount.ToChargeAmount += amount;//增加预扣款
  71. var toChargeBackRecord = db.ToChargeBackRecord.Add(new ToChargeBackRecord
  72. {
  73. CreateDate = DateTime.Now,
  74. UserId = BuyUserId,
  75. ChargeAmount = amount,
  76. ChargeType = 124,//过期机具货款扣费
  77. Remark = pos.PosSn,
  78. }).Entity;
  79. }
  80. }
  81. db.SaveChanges();
  82. DoChargeAmount(db);
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "过期机具执行扣费异常");
  88. }
  89. }
  90. Thread.Sleep(1000);
  91. }
  92. }
  93. public void StartDoChargeAmount()
  94. {
  95. Thread th = new Thread(StartDoChargeAmountReady);
  96. th.IsBackground = true;
  97. th.Start();
  98. }
  99. public void StartDoChargeAmountReady()
  100. {
  101. while(true)
  102. {
  103. WebCMSEntities db = new WebCMSEntities();
  104. DoChargeAmount(db);
  105. db.Dispose();
  106. Thread.Sleep(600000);
  107. }
  108. }
  109. public void ListenChargeAmount()
  110. {
  111. Thread th = new Thread(ListenChargeAmountReady);
  112. th.IsBackground = true;
  113. th.Start();
  114. }
  115. public void ListenChargeAmountReady()
  116. {
  117. while(true)
  118. {
  119. string content = RedisDbconn.Instance.RPop<string>("DoChargeAmountQueue");
  120. if(!string.IsNullOrEmpty(content))
  121. {
  122. WebCMSEntities db = new WebCMSEntities();
  123. DoChargeAmount(db, int.Parse(function.CheckInt(content)));
  124. db.Dispose();
  125. }
  126. }
  127. }
  128. public void DoChargeAmount(WebCMSEntities db, int UserId = 0)
  129. {
  130. IQueryable<ToChargeBackRecord> list = db.ToChargeBackRecord.Where(m => m.Status == 0);//过期机具扣费记录
  131. if(UserId > 0)
  132. {
  133. list = list.Where(m => m.UserId == UserId);
  134. }
  135. var info = list.ToList();
  136. foreach (var items in info)
  137. {
  138. var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == items.UserId) ?? new UserAccount();
  139. var record = info.FirstOrDefault(m => m.Id == items.Id);
  140. if (userAccount.BalanceAmount >= record.ChargeAmount)
  141. {
  142. var userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord
  143. {
  144. CreateDate = DateTime.Now,
  145. UserId = items.UserId,
  146. BeforeBalanceAmount = userAccount.BalanceAmount,
  147. AfterBalanceAmount = userAccount.BalanceAmount - record.ChargeAmount,
  148. ChangeAmount = record.ChargeAmount,
  149. ChangeType = 124,//过期机具货款扣费
  150. Remark = "扣机具货款",
  151. }).Entity;
  152. record.Status = 1;
  153. if (userAccount.ToChargeAmount >= record.ChargeAmount)
  154. {
  155. userAccount.ToChargeAmount -= record.ChargeAmount;//扣减预扣款
  156. }
  157. userAccount.BalanceAmount -= record.ChargeAmount;//扣减余额
  158. }
  159. }
  160. db.SaveChanges();
  161. }
  162. }
  163. }