PreStoreApplyQueue.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.Models;
  8. using Library;
  9. using LitJson;
  10. public class PreStoreApplyHelper
  11. {
  12. public readonly static PreStoreApplyHelper Instance = new PreStoreApplyHelper();
  13. private PreStoreApplyHelper()
  14. {
  15. }
  16. public void StartEverTime()
  17. {
  18. Thread th = new Thread(StartEverTimeDo);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. private void StartEverTimeDo()
  23. {
  24. while (true)
  25. {
  26. WebCMSEntities db = new WebCMSEntities();
  27. try
  28. {
  29. string data = RedisDbconn.Instance.RPop<string>("PreStoreApplyQueue");
  30. if (!string.IsNullOrEmpty(data))
  31. {
  32. function.WriteLog("data:" + data, "创客预发额度变动日志");
  33. JsonData jsonObj = JsonMapper.ToObject(data);
  34. if (jsonObj["Kind"].ToString() == "1") // 购买创客预发临时额度
  35. {
  36. int OrderId = int.Parse(jsonObj["Data"]["OrderId"].ToString());
  37. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  38. if (order != null)
  39. {
  40. decimal TotalPrice = order.TotalPrice;
  41. AddAmount2(db, 1, order.UserId, TotalPrice, order.PayMode, 1, order.Id);
  42. }
  43. }
  44. else if (jsonObj["Kind"].ToString() == "2") // 增减创客预发临时额度
  45. {
  46. int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
  47. decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
  48. int OperateType = int.Parse(jsonObj["Data"]["OperateType"].ToString());
  49. AddAmount(db, 2, UserId, Amount, OperateType);
  50. }
  51. else if (jsonObj["Kind"].ToString() == "3") // 调低创客预发额度返回余额
  52. {
  53. int UserId = int.Parse(jsonObj["Data"]["UserId"].ToString());
  54. decimal Amount = decimal.Parse(jsonObj["Data"]["Amount"].ToString());
  55. int PayMode = int.Parse(jsonObj["Data"]["PayMode"].ToString());
  56. AddAmount2(db, 3, UserId, Amount, PayMode, 0);
  57. if (PayMode != 1)
  58. {
  59. decimal BalanceAmount = Amount;
  60. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  61. if (account == null)
  62. {
  63. account = db.UserAccount.Add(new UserAccount()
  64. {
  65. Id = UserId,
  66. UserId = UserId,
  67. }).Entity;
  68. db.SaveChanges();
  69. }
  70. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  71. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  72. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  73. account.BalanceAmount += BalanceAmount;
  74. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  75. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  76. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  77. UserAccountRecord add = db.UserAccountRecord.Add(new UserAccountRecord()
  78. {
  79. CreateDate = DateTime.Now,
  80. UpdateDate = DateTime.Now,
  81. UserId = UserId, //创客
  82. ChangeType = 126, //变动类型
  83. ChangeAmount = BalanceAmount, //变更金额
  84. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  85. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  86. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  87. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  88. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  89. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  90. }).Entity;
  91. db.SaveChanges();
  92. PublicFunction.SplitUserAccountRecord(add);
  93. }
  94. }
  95. db.SaveChanges();
  96. }
  97. else
  98. {
  99. Thread.Sleep(5000);
  100. }
  101. }
  102. catch (Exception ex)
  103. {
  104. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "创客预发临时额度变动线程异常");
  105. }
  106. db.Dispose();
  107. }
  108. }
  109. public void AddAmount(WebCMSEntities db, int Kind, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
  110. {
  111. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  112. if (account == null)
  113. {
  114. account = db.UserAccount.Add(new UserAccount()
  115. {
  116. Id = UserId,
  117. UserId = UserId,
  118. }).Entity;
  119. db.SaveChanges();
  120. }
  121. decimal BeforeTotalAmount = account.ValidPreAmount; //变更前总金额
  122. if (OperateType == 1)
  123. {
  124. account.ValidPreAmount += Amount;
  125. }
  126. else
  127. {
  128. account.ValidPreAmount -= Amount;
  129. }
  130. decimal AfterTotalAmount = account.ValidPreAmount; //变更后总金额
  131. PreAmountRecord preAmountRecord = db.PreAmountRecord.Add(new PreAmountRecord()
  132. {
  133. CreateDate = DateTime.Now,
  134. UpdateDate = DateTime.Now,
  135. OperateType = OperateType,
  136. AfterAmount = AfterTotalAmount,
  137. BeforeAmount = BeforeTotalAmount,
  138. UseAmount = Amount,
  139. UserId = UserId,
  140. QueryCount = OrderId,
  141. Sort = Kind,
  142. }).Entity;
  143. db.SaveChanges();
  144. }
  145. public void AddAmount2(WebCMSEntities db, int Kind, int UserId, decimal Amount, int PayMode, int OperateType = 1, int OrderId = 0)
  146. {
  147. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  148. if (account == null)
  149. {
  150. account = db.UserAccount.Add(new UserAccount()
  151. {
  152. Id = UserId,
  153. UserId = UserId,
  154. }).Entity;
  155. db.SaveChanges();
  156. }
  157. decimal BeforeTotalAmount = account.ValidPreAmount; //变更前总金额
  158. if (OperateType == 1)
  159. {
  160. if (PayMode == 3)//余额
  161. {
  162. account.PreTempAmountForBalance += Amount;
  163. }
  164. else
  165. {
  166. account.PreTempAmount += Amount;
  167. }
  168. account.ValidPreAmount += Amount;
  169. }
  170. else
  171. {
  172. if (PayMode == 3)
  173. {
  174. account.PreTempAmountForBalance -= Amount;
  175. }
  176. else
  177. {
  178. account.PreTempAmount -= Amount;
  179. }
  180. account.ValidPreAmount -= Amount;
  181. }
  182. decimal AfterTotalAmount = account.ValidPreAmount; //变更后总金额
  183. PreAmountRecord preAmountRecord = db.PreAmountRecord.Add(new PreAmountRecord()
  184. {
  185. CreateDate = DateTime.Now,
  186. UpdateDate = DateTime.Now,
  187. OperateType = OperateType,
  188. AmountType = PayMode,
  189. AfterAmount = AfterTotalAmount,
  190. BeforeAmount = BeforeTotalAmount,
  191. UseAmount = Amount,
  192. UserId = UserId,
  193. QueryCount = OrderId,
  194. Sort = Kind,
  195. PayMode = PayMode,
  196. }).Entity;
  197. db.SaveChanges();
  198. }
  199. }