PosExpiredHelper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 PosExpiredHelper
  11. {
  12. public readonly static PosExpiredHelper Instance = new PosExpiredHelper();
  13. private PosExpiredHelper()
  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>("PosExpiredRingQueue");
  29. if(!string.IsNullOrEmpty(content))
  30. {
  31. WebCMSEntities db = new WebCMSEntities();
  32. DoSomething(db);
  33. db.Dispose();
  34. }
  35. }
  36. catch (Exception ex)
  37. {
  38. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "过期机具提醒异常");
  39. }
  40. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "过期机具提醒日志");
  41. Thread.Sleep(60000);
  42. }
  43. }
  44. public void DoSomething(WebCMSEntities db)
  45. {
  46. DateTime now = DateTime.Now;
  47. List<int> BuyUserIds = db.PosMachinesTwo.Select(m => new { m.BuyUserId, m.RecycEndDate, m.Status, m.BindingState }).Where(m => m.RecycEndDate < now && m.Status > -1 && m.BindingState == 0).ToList().Select(m => m.BuyUserId).ToList();
  48. foreach(int BuyUserId in BuyUserIds)
  49. {
  50. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  51. {
  52. UserId = Convert.ToInt32(BuyUserId), //创客
  53. Title = "机具循环过期提醒", //标题
  54. Content = "尊敬的创客您好<br />您的部分机具循环已过期,请在五日内完成激活或联系在线客服回寄该机具。", //内容
  55. Summary = "尊敬的创客您好,您的部分机具循环已过期,请在五日内完成激活或联系在线客服回寄该机具。",
  56. CreateDate = DateTime.Now,
  57. }));
  58. }
  59. }
  60. public void StartPay()
  61. {
  62. Thread th = new Thread(DoPay);
  63. th.IsBackground = true;
  64. th.Start();
  65. }
  66. private void DoPay()
  67. {
  68. while (true)
  69. {
  70. try
  71. {
  72. string content = RedisDbconn.Instance.RPop<string>("PosExpiredPayQueue");
  73. if(!string.IsNullOrEmpty(content))
  74. {
  75. WebCMSEntities db = new WebCMSEntities();
  76. using (var tran = db.Database.BeginTransaction())
  77. {
  78. try
  79. {
  80. GoPay(db, int.Parse(content));
  81. tran.Commit();
  82. }
  83. catch(Exception ex)
  84. {
  85. tran.Rollback();
  86. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "过期机具扣费异常");
  87. }
  88. }
  89. db.Dispose();
  90. }
  91. else
  92. {
  93. Thread.Sleep(60000);
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "过期机具扣费异常");
  99. }
  100. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "过期机具扣费日志");
  101. }
  102. }
  103. public void GoPay(WebCMSEntities db, int UserId)
  104. {
  105. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new UserAccount();
  106. decimal BalanceAmount = account.BalanceAmount;
  107. List<int> BrandIdList = new List<int>();
  108. BrandIdList.Add(3);
  109. BrandIdList.Add(5);
  110. BrandIdList.Add(9);
  111. BrandIdList.Add(11);
  112. DateTime now = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00").AddDays(-60);
  113. Dictionary<int, decimal> UserList = new Dictionary<int, decimal>();
  114. var PosList = db.PosMachinesTwo.Select(m => new { m.Id, m.BuyUserId, m.RecycEndDate, m.Status, m.BindingState, m.BrandId, m.ScanQrTrade }).Where(m => m.BuyUserId == UserId && m.RecycEndDate < now && m.Status > -1 && m.BindingState == 0 && m.ScanQrTrade < 999).OrderBy(m => m.Id).ToList();
  115. if(PosList.Count > 0)
  116. {
  117. foreach(var Pos in PosList)
  118. {
  119. decimal PayMoney = 200;
  120. if(BrandIdList.Contains(Pos.BrandId))
  121. {
  122. PayMoney = 300;
  123. }
  124. PayMoney -= Pos.ScanQrTrade;
  125. if(BalanceAmount > 0)
  126. {
  127. if(BalanceAmount > PayMoney)
  128. {
  129. if(UserList.ContainsKey(Pos.BuyUserId))
  130. {
  131. UserList[Pos.BuyUserId] += PayMoney;
  132. }
  133. else
  134. {
  135. UserList.Add(Pos.BuyUserId, PayMoney);
  136. }
  137. BalanceAmount -= PayMoney;
  138. }
  139. else
  140. {
  141. PayMoney = BalanceAmount;
  142. if(UserList.ContainsKey(Pos.BuyUserId))
  143. {
  144. UserList[Pos.BuyUserId] += PayMoney;
  145. }
  146. else
  147. {
  148. UserList.Add(Pos.BuyUserId, PayMoney);
  149. }
  150. BalanceAmount = 0;
  151. }
  152. PosMachinesTwo EditPos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == Pos.Id);
  153. if(EditPos != null)
  154. {
  155. EditPos.ScanQrTrade += PayMoney;
  156. if(BrandIdList.Contains(Pos.BrandId) && EditPos.ScanQrTrade >= 300)
  157. {
  158. EditPos.ScanQrTrade = 999;
  159. }
  160. else if(!BrandIdList.Contains(Pos.BrandId) && EditPos.ScanQrTrade >= 200)
  161. {
  162. EditPos.ScanQrTrade = 999;
  163. }
  164. }
  165. }
  166. }
  167. db.SaveChanges();
  168. }
  169. foreach(int UsrId in UserList.Keys)
  170. {
  171. AlipayPayBack2Service.Instance.ChangeAccount(db, new Orders(), UsrId, -UserList[UsrId], 124, "扣机具货款");
  172. }
  173. }
  174. }