StatService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 StatService
  12. {
  13. public readonly static StatService Instance = new StatService();
  14. private StatService()
  15. { }
  16. // 统计交易额V2
  17. public void Start()
  18. {
  19. Thread th = new Thread(StartDo);
  20. th.IsBackground = true;
  21. th.Start();
  22. }
  23. public void StartDo()
  24. {
  25. while (true)
  26. {
  27. StatTradeAmount();
  28. Thread.Sleep(30000);
  29. }
  30. }
  31. public void StatTradeAmount()
  32. {
  33. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  34. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "统计商户和创客交易额日志");
  35. WebCMSEntities db = new WebCMSEntities();
  36. KxsMainModels.WebCMSEntities maindb = new KxsMainModels.WebCMSEntities();
  37. using (var tran = db.Database.BeginTransaction())
  38. {
  39. try
  40. {
  41. string start = DateTime.Now.AddDays(-10).ToString("yyyy-MM-dd HH:mm:ss");
  42. DataTable idsDt = OtherMySqlConn.dtable("select Id from ConsumerOrders where CreateDate>='" + start + "' and Status>0 and Version=0 order by Id limit 50");
  43. if(idsDt.Rows.Count > 0)
  44. {
  45. string ids = "";
  46. foreach (DataRow idsDr in idsDt.Rows)
  47. {
  48. ids += idsDr["Id"].ToString() + ",";
  49. }
  50. DataTable selfDt = OtherMySqlConn.dtable("select UserId,MerchantId,SnNo,PayMode,IsAct,DATE_FORMAT(CreateDate,'%Y%m%d'),sum(PayMoney),count(Id),sum(MaxDivi) from ConsumerOrders where Id in (" + ids.TrimEnd(',') + ") group by UserId,MerchantId,SnNo,PayMode,IsAct,DATE_FORMAT(CreateDate,'%Y%m%d')");
  51. if (selfDt.Rows.Count > 0)
  52. {
  53. function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "统计商户和创客交易额日志");
  54. foreach (DataRow selfDr in selfDt.Rows)
  55. {
  56. int UserId = int.Parse(selfDr["UserId"].ToString());
  57. KxsMainModels.Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId) ?? new KxsMainModels.Users();
  58. string ParentNav = user.ParentNav;
  59. int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
  60. int PayMode = int.Parse(selfDr["PayMode"].ToString());
  61. int IsAct = int.Parse(selfDr["IsAct"].ToString());
  62. string SnNo = selfDr["SnNo"].ToString();
  63. string TradeDate = selfDr[5].ToString();
  64. decimal TradeAmount = decimal.Parse(selfDr[6].ToString());
  65. int TradeCount = int.Parse(selfDr[7].ToString());
  66. decimal MaxDivi = decimal.Parse(selfDr[8].ToString());
  67. decimal ActualAmount = TradeAmount - MaxDivi;
  68. string TradeMonth = TradeDate.Substring(0, 6);
  69. //创客交易额
  70. UserAmountSummary selfStat = db.UserAmountSummary.FirstOrDefault(m => m.UserId == UserId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode && m.SeoTitle == "self");
  71. if (selfStat == null)
  72. {
  73. selfStat = db.UserAmountSummary.Add(new UserAmountSummary()
  74. {
  75. UserId = UserId,
  76. TradeMonth = TradeMonth,
  77. TradeDate = TradeDate,
  78. PayMode = PayMode,
  79. IsAct = IsAct,
  80. SeoTitle = "self",
  81. }).Entity;
  82. db.SaveChanges();
  83. }
  84. selfStat.TotalAmount += TradeAmount;
  85. selfStat.TradeCount += TradeCount;
  86. ParentNav += "," + UserId + ",";
  87. if (!string.IsNullOrEmpty(ParentNav))
  88. {
  89. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  90. foreach (string NavUserIdString in ParentNavList)
  91. {
  92. int NavUserId = int.Parse(NavUserIdString);
  93. UserAmountSummary teamStat = db.UserAmountSummary.FirstOrDefault(m => m.UserId == NavUserId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode && m.SeoTitle == "team");
  94. if (teamStat == null)
  95. {
  96. teamStat = db.UserAmountSummary.Add(new UserAmountSummary()
  97. {
  98. UserId = NavUserId,
  99. TradeMonth = TradeMonth,
  100. TradeDate = TradeDate,
  101. PayMode = PayMode,
  102. IsAct = IsAct,
  103. SeoTitle = "team",
  104. }).Entity;
  105. db.SaveChanges();
  106. }
  107. teamStat.TotalAmount += TradeAmount;
  108. teamStat.TradeCount += TradeCount;
  109. }
  110. }
  111. db.SaveChanges();
  112. //商户交易额
  113. MerchantAmountSummay merchantStat = db.MerchantAmountSummay.FirstOrDefault(m => m.MerchantId == MerchantId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode);
  114. if (merchantStat == null)
  115. {
  116. merchantStat = db.MerchantAmountSummay.Add(new MerchantAmountSummay()
  117. {
  118. MerchantId = MerchantId,
  119. TradeMonth = TradeMonth,
  120. TradeDate = TradeDate,
  121. PayMode = PayMode,
  122. IsAct = IsAct,
  123. }).Entity;
  124. db.SaveChanges();
  125. }
  126. merchantStat.TradeAmount += TradeAmount;
  127. merchantStat.TradeCount += TradeCount;
  128. merchantStat.TotalActual += ActualAmount;
  129. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
  130. if(merchant != null)
  131. {
  132. merchant.TotalAmount += TradeAmount;
  133. merchant.TotalConsumeCount += TradeCount;
  134. merchant.TotalActual += ActualAmount;
  135. if(IsAct == 1)
  136. {
  137. merchant.TotalActAmount += TradeAmount;
  138. merchant.TotalActActual += ActualAmount;
  139. if(merchant.Version == 0 && merchant.TotalActAmount >= 10000M)
  140. {
  141. RedisDbconn.Instance.AddList("ActProfitQueue", MerchantId.ToString());
  142. merchant.Version = 1;
  143. }
  144. }
  145. }
  146. db.SaveChanges();
  147. //码牌交易额
  148. QrCodeAmountSummay qrcode = db.QrCodeAmountSummay.FirstOrDefault(m => m.SnNo == SnNo && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode);
  149. if (qrcode == null)
  150. {
  151. qrcode = db.QrCodeAmountSummay.Add(new QrCodeAmountSummay()
  152. {
  153. SnNo = SnNo,
  154. TradeMonth = TradeMonth,
  155. TradeDate = TradeDate,
  156. PayMode = PayMode,
  157. IsAct = IsAct,
  158. }).Entity;
  159. db.SaveChanges();
  160. }
  161. qrcode.TradeAmount += TradeAmount;
  162. qrcode.TradeCount += TradeCount;
  163. qrcode.TotalActual += ActualAmount;
  164. db.SaveChanges();
  165. }
  166. OtherMySqlConn.op("update ConsumerOrders set Version=1 where Id in (" + ids.TrimEnd(',') + ")");
  167. }
  168. }
  169. tran.Commit();
  170. }
  171. catch (Exception ex)
  172. {
  173. tran.Rollback();
  174. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计商户和创客交易额异常");
  175. }
  176. }
  177. db.Dispose();
  178. maindb.Dispose();
  179. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "统计商户和创客交易额日志");
  180. }
  181. }
  182. }