TradeChangeService.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using System.Data;
  7. using System.Threading;
  8. using MySystem.PxcModels;
  9. namespace MySystem
  10. {
  11. public class TradeChangeService
  12. {
  13. public readonly static TradeChangeService Instance = new TradeChangeService();
  14. private TradeChangeService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(dosomething);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. public void dosomething()
  23. {
  24. while (true)
  25. {
  26. string data = RedisDbconn.Instance.RPop<string>("TradeChangeQueue");
  27. if (!string.IsNullOrEmpty(data))
  28. {
  29. try
  30. {
  31. JsonData jsonOj = JsonMapper.ToObject(data);
  32. int Id = int.Parse(function.CheckInt(jsonOj["Id"].ToString()));
  33. int OldType = int.Parse(function.CheckInt(jsonOj["OldType"].ToString()));
  34. int NewType = int.Parse(function.CheckInt(jsonOj["NewType"].ToString()));
  35. StatTradeAmount(Id, OldType, NewType);
  36. }
  37. catch (Exception ex)
  38. {
  39. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "设置激活奖励异常");
  40. }
  41. }
  42. else
  43. {
  44. Thread.Sleep(500);
  45. }
  46. }
  47. }
  48. public void StatTradeAmount(int Id, int OldType, int NewType)
  49. {
  50. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "交易额类型变更");
  51. WebCMSEntities db = new WebCMSEntities();
  52. try
  53. {
  54. DataTable selfDt = CustomerSqlConn.dtable("select UserId,ParentNav,BrandId,BankCardType,QrPayFlag,MerHelpFlag,Version,CapFlag,VipFlag,PayType,DATE_FORMAT(CreateDate,'%Y%m%d'),TradeAmount from TradeRecord where Id=" + Id + "", MysqlConn.SqlConnStr);
  55. if (selfDt.Rows.Count > 0)
  56. {
  57. function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "交易额类型变更");
  58. foreach (DataRow selfDr in selfDt.Rows)
  59. {
  60. int UserId = int.Parse(selfDr["UserId"].ToString());
  61. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  62. string ParentNav = user.ParentNav; //selfDr["ParentNav"].ToString();
  63. int BrandId = int.Parse(selfDr["BrandId"].ToString());
  64. int BankCardType = int.Parse(selfDr["BankCardType"].ToString());
  65. int QrPayFlag = int.Parse(selfDr["QrPayFlag"].ToString());
  66. int MerHelpFlag = int.Parse(selfDr["MerHelpFlag"].ToString());
  67. int Version = int.Parse(selfDr["Version"].ToString());
  68. int CapFlag = int.Parse(selfDr["CapFlag"].ToString());
  69. int VipFlag = int.Parse(selfDr["VipFlag"].ToString());
  70. int PayType = int.Parse(selfDr["PayType"].ToString());
  71. string TradeDate = selfDr[10].ToString();
  72. decimal TradeAmount = decimal.Parse(selfDr["TradeAmount"].ToString());
  73. string TradeMonth = TradeDate.Substring(0, 6);
  74. TradeDaySummary selfStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.VipFlag == VipFlag && m.PayType == PayType && m.SeoTitle == "self");
  75. if (selfStat != null)
  76. {
  77. if (BankCardType == 0)
  78. {
  79. if(OldType == 2)
  80. {
  81. if(selfStat.NotHelpDirectDebitTradeAmt >= TradeAmount)
  82. {
  83. selfStat.NotHelpDirectDebitTradeAmt -= TradeAmount;
  84. if(NewType == 1)
  85. {
  86. selfStat.HelpDirectDebitTradeAmt += TradeAmount;
  87. }
  88. }
  89. if (CapFlag == 1)
  90. {
  91. if(selfStat.NotHelpDirectDebitCapTradeAmt >= TradeAmount)
  92. {
  93. selfStat.NotHelpDirectDebitCapTradeAmt -= TradeAmount;
  94. if(NewType == 1)
  95. {
  96. selfStat.HelpDirectDebitCapTradeAmt += TradeAmount;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. else if (BankCardType != 0)
  103. {
  104. if(OldType == 2)
  105. {
  106. if(selfStat.NotHelpDirectTradeAmt >= TradeAmount)
  107. {
  108. selfStat.NotHelpDirectTradeAmt -= TradeAmount;
  109. if(NewType == 1)
  110. {
  111. selfStat.HelpDirectTradeAmt += TradeAmount;
  112. }
  113. }
  114. }
  115. }
  116. ParentNav += "," + UserId + ",";
  117. if (!string.IsNullOrEmpty(ParentNav))
  118. {
  119. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  120. foreach (string NavUserIdString in ParentNavList)
  121. {
  122. int NavUserId = int.Parse(NavUserIdString);
  123. TradeDaySummary teamStat = db.TradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.VipFlag == VipFlag && m.PayType == PayType && m.SeoTitle == "team");
  124. if (teamStat != null)
  125. {
  126. if (BankCardType == 0)
  127. {
  128. if(OldType == 2)
  129. {
  130. if(teamStat.NotHelpNonDirectDebitTradeAmt >= TradeAmount)
  131. {
  132. teamStat.NotHelpNonDirectDebitTradeAmt -= TradeAmount;
  133. if(NewType == 1)
  134. {
  135. teamStat.HelpNonDirectDebitTradeAmt += TradeAmount;
  136. }
  137. }
  138. if (CapFlag == 1)
  139. {
  140. if(teamStat.NotHelpDirectDebitCapTradeAmt >= TradeAmount)
  141. {
  142. teamStat.NotHelpDirectDebitCapTradeAmt -= TradeAmount;
  143. if(NewType == 1)
  144. {
  145. teamStat.HelpDirectDebitCapTradeAmt += TradeAmount;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. else if (BankCardType != 0)
  152. {
  153. if(OldType == 2)
  154. {
  155. if(teamStat.NotHelpNonDirectTradeAmt >= TradeAmount)
  156. {
  157. teamStat.NotHelpNonDirectTradeAmt -= TradeAmount;
  158. if(NewType == 1)
  159. {
  160. teamStat.HelpNonDirectTradeAmt += TradeAmount;
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171. db.SaveChanges();
  172. }
  173. catch (Exception ex)
  174. {
  175. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "交易额类型变更异常");
  176. }
  177. db.Dispose();
  178. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "交易额类型变更");
  179. }
  180. }
  181. }