PreStoreApplyQueue.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  78. {
  79. CreateDate = DateTime.Now,
  80. UpdateDate = DateTime.Now,
  81. UserId = UserId, //创客
  82. ChangeType = 130, //变动类型
  83. ChangeAmount = BalanceAmount, //变更金额
  84. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  85. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  86. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  87. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  88. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  89. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  90. }).Entity;
  91. }
  92. }
  93. db.SaveChanges();
  94. }
  95. else
  96. {
  97. Thread.Sleep(5000);
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "创客预发临时额度变动线程异常");
  103. }
  104. db.Dispose();
  105. }
  106. }
  107. public void AddAmount(WebCMSEntities db, int Kind, int UserId, decimal Amount, int OperateType = 1, int OrderId = 0)
  108. {
  109. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  110. if (account == null)
  111. {
  112. account = db.UserAccount.Add(new UserAccount()
  113. {
  114. Id = UserId,
  115. UserId = UserId,
  116. }).Entity;
  117. db.SaveChanges();
  118. }
  119. decimal BeforeTotalAmount = account.ValidPreAmount; //变更前总金额
  120. if (OperateType == 1)
  121. {
  122. account.ValidPreAmount += Amount;
  123. }
  124. else
  125. {
  126. account.ValidPreAmount -= Amount;
  127. }
  128. decimal AfterTotalAmount = account.ValidPreAmount; //变更后总金额
  129. PreAmountRecord preAmountRecord = db.PreAmountRecord.Add(new PreAmountRecord()
  130. {
  131. CreateDate = DateTime.Now,
  132. UpdateDate = DateTime.Now,
  133. OperateType = OperateType,
  134. AfterAmount = AfterTotalAmount,
  135. BeforeAmount = BeforeTotalAmount,
  136. UseAmount = Amount,
  137. UserId = UserId,
  138. QueryCount = OrderId,
  139. Sort = Kind,
  140. }).Entity;
  141. db.SaveChanges();
  142. }
  143. public void AddAmount2(WebCMSEntities db, int Kind, int UserId, decimal Amount, int PayMode, int OperateType = 1, int OrderId = 0)
  144. {
  145. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  146. if (account == null)
  147. {
  148. account = db.UserAccount.Add(new UserAccount()
  149. {
  150. Id = UserId,
  151. UserId = UserId,
  152. }).Entity;
  153. db.SaveChanges();
  154. }
  155. decimal BeforeTotalAmount = account.ValidPreAmount; //变更前总金额
  156. if (OperateType == 1)
  157. {
  158. if (PayMode == 3)//余额
  159. {
  160. account.PreTempAmountForBalance += Amount;
  161. }
  162. else
  163. {
  164. account.PreTempAmount += Amount;
  165. }
  166. account.ValidPreAmount += Amount;
  167. }
  168. else
  169. {
  170. if (PayMode == 3)
  171. {
  172. account.PreTempAmountForBalance -= Amount;
  173. }
  174. else
  175. {
  176. account.PreTempAmount -= Amount;
  177. }
  178. account.ValidPreAmount -= Amount;
  179. }
  180. decimal AfterTotalAmount = account.ValidPreAmount; //变更后总金额
  181. PreAmountRecord preAmountRecord = db.PreAmountRecord.Add(new PreAmountRecord()
  182. {
  183. CreateDate = DateTime.Now,
  184. UpdateDate = DateTime.Now,
  185. OperateType = OperateType,
  186. AmountType = PayMode,
  187. AfterAmount = AfterTotalAmount,
  188. BeforeAmount = BeforeTotalAmount,
  189. UseAmount = Amount,
  190. UserId = UserId,
  191. QueryCount = OrderId,
  192. Sort = Kind,
  193. PayMode = PayMode,
  194. }).Entity;
  195. db.SaveChanges();
  196. }
  197. }