StatService.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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(10000);
  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(-100).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 20");
  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),sum(MerchantActualAmount) 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 = decimal.Parse(selfDr[9].ToString());
  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. merchant.ActMaxAmount += MaxDivi; //活动应返金额
  136. if(IsAct == 1)
  137. {
  138. merchant.TotalActAmount += TradeAmount;
  139. merchant.TotalActActual += ActualAmount;
  140. if(merchant.ActivationStatus == 0 && merchant.TotalActAmount >= 10000M)
  141. {
  142. // RedisDbconn.Instance.AddList("ActProfitQueue", MerchantId.ToString());
  143. merchant.ActivationStatus = 1;
  144. merchant.ActivationDate = DateTime.Now;
  145. }
  146. }
  147. }
  148. db.SaveChanges();
  149. //码牌交易额
  150. QrCodeAmountSummay qrcode = db.QrCodeAmountSummay.FirstOrDefault(m => m.SnNo == SnNo && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode);
  151. if (qrcode == null)
  152. {
  153. qrcode = db.QrCodeAmountSummay.Add(new QrCodeAmountSummay()
  154. {
  155. SnNo = SnNo,
  156. TradeMonth = TradeMonth,
  157. TradeDate = TradeDate,
  158. PayMode = PayMode,
  159. IsAct = IsAct,
  160. }).Entity;
  161. db.SaveChanges();
  162. }
  163. qrcode.TradeAmount += TradeAmount;
  164. qrcode.TradeCount += TradeCount;
  165. qrcode.TotalActual += ActualAmount;
  166. db.SaveChanges();
  167. string jsonString = "{\"TradeDate\":\"" + TradeDate + "\",\"PayMoney\":\"" + TradeAmount + "\",\"TradeCount\":\"" + TradeCount + "\",\"MerchantActualAmount\":\"" + ActualAmount + "\",\"MerchantId\":\"" + MerchantId + "\",\"PayMode\":\"" + PayMode + "\",\"IsAct\":\"" + IsAct + "\"}";
  168. RedisDbconn.Instance.AddList("StatMerchantTradeSummaryQueue", jsonString);
  169. }
  170. OtherMySqlConn.op("update ConsumerOrders set Version=1 where Id in (" + ids.TrimEnd(',') + ")");
  171. }
  172. }
  173. tran.Commit();
  174. }
  175. catch (Exception ex)
  176. {
  177. tran.Rollback();
  178. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计商户和创客交易额异常");
  179. }
  180. }
  181. db.Dispose();
  182. maindb.Dispose();
  183. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "统计商户和创客交易额日志");
  184. }
  185. // 统计创客激活数
  186. public void StartPosActNum()
  187. {
  188. Thread th = new Thread(StartPosActNumFor);
  189. th.IsBackground = true;
  190. th.Start();
  191. }
  192. public void StartPosActNumFor()
  193. {
  194. while (true)
  195. {
  196. if(DateTime.Now.Hour >= 3)
  197. {
  198. StartPosActNumEverTime();
  199. }
  200. Thread.Sleep(20000);
  201. }
  202. }
  203. public void StartPosActNumEverTime()
  204. {
  205. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "实时执行创客激活数日志");
  206. WebCMSEntities db = new WebCMSEntities();
  207. KxsMainModels.WebCMSEntities maindb = new KxsMainModels.WebCMSEntities();
  208. try
  209. {
  210. DataTable idsDt = CustomerSqlConn.dtable("select Id from MerchantInfo where ActStat=0 and (Status=2 or QueryCount=2) and UserId>0 limit 50", AppConfig.Base.SqlConn);
  211. if(idsDt.Rows.Count > 0)
  212. {
  213. string ids = "";
  214. foreach (DataRow idsDr in idsDt.Rows)
  215. {
  216. ids += idsDr["Id"].ToString() + ",";
  217. }
  218. DataTable userDt = CustomerSqlConn.dtable("select UserId,DATE_FORMAT(SignDate,'%Y%m%d'),count(Id) from MerchantInfo where Id in (" + ids.TrimEnd(',') + ") group by UserId,DATE_FORMAT(SignDate,'%Y%m%d')", AppConfig.Base.SqlConn);
  219. if (userDt.Rows.Count > 0)
  220. {
  221. function.WriteLog("人数:" + userDt.Rows.Count + "\n\n", "实时执行创客激活数日志");
  222. foreach (DataRow userDr in userDt.Rows)
  223. {
  224. int UserId = int.Parse(userDr["UserId"].ToString());
  225. string TradeDate = userDr[1].ToString();
  226. int ActCount = int.Parse(function.CheckInt(userDr[2].ToString()));
  227. string TradeMonth = TradeDate.Substring(0, 6);
  228. string date = TradeDate.Substring(0, 4) + "-" + TradeDate.Substring(4, 2) + "-" + TradeDate.Substring(6, 2);
  229. string start = date + " 00:00:00";
  230. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  231. KxsMainModels.Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId) ?? new KxsMainModels.Users();
  232. string ParentNav = user.ParentNav;
  233. UserTradeMonthSummary selfStat = db.UserTradeMonthSummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoKeyword == TradeDate && m.SeoTitle == "self");
  234. if (selfStat == null)
  235. {
  236. selfStat = db.UserTradeMonthSummary.Add(new UserTradeMonthSummary()
  237. {
  238. UserId = UserId,
  239. TradeMonth = TradeMonth,
  240. SeoKeyword = TradeDate,
  241. SeoTitle = "self",
  242. }).Entity;
  243. db.SaveChanges();
  244. }
  245. selfStat.ActiveBuddyMerStatus += ActCount;
  246. ParentNav += "," + UserId + ",";
  247. if (!string.IsNullOrEmpty(ParentNav))
  248. {
  249. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  250. foreach (string NavUserIdString in ParentNavList)
  251. {
  252. int NavUserId = int.Parse(NavUserIdString);
  253. UserTradeMonthSummary teamStat = db.UserTradeMonthSummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.SeoKeyword == TradeDate && m.SeoTitle == "team");
  254. if (teamStat == null)
  255. {
  256. teamStat = db.UserTradeMonthSummary.Add(new UserTradeMonthSummary()
  257. {
  258. UserId = NavUserId,
  259. TradeMonth = TradeMonth,
  260. SeoKeyword = TradeDate,
  261. SeoTitle = "team",
  262. }).Entity;
  263. db.SaveChanges();
  264. }
  265. teamStat.ActiveBuddyMerStatus += ActCount;
  266. }
  267. }
  268. }
  269. db.SaveChanges();
  270. CustomerSqlConn.op("update MerchantInfo set ActStat=1 where Id in (" + ids.TrimEnd(',') + ")", AppConfig.Base.SqlConn);
  271. }
  272. }
  273. }
  274. catch (Exception ex)
  275. {
  276. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时执行创客激活数异常");
  277. }
  278. db.Dispose();
  279. maindb.Dispose();
  280. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时执行创客激活数日志");
  281. }
  282. public void Other()
  283. {
  284. string connstr = Library.ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  285. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "统计商户和创客交易额日志");
  286. WebCMSEntities db = new WebCMSEntities();
  287. KxsMainModels.WebCMSEntities maindb = new KxsMainModels.WebCMSEntities();
  288. try
  289. {
  290. string start = "2023-10-01 00:00:00";
  291. string end = DateTime.Now.ToString("2023-10-09 18:26:00");
  292. DataTable selfDt = CustomerSqlConn.dtable("select UserId,MerchantId,SnNo,PayMode,IsAct,DATE_FORMAT(CreateDate,'%Y%m%d'),sum(PayMoney),count(Id),sum(MaxDivi),sum(MerchantActualAmount) from ConsumerOrders where MerchantId>0 and CreateDate>='" + start + "' and CreateDate<'" + end + "' and Status>0 group by UserId,MerchantId,SnNo,PayMode,IsAct,DATE_FORMAT(CreateDate,'%Y%m%d')", connstr);
  293. if (selfDt.Rows.Count > 0)
  294. {
  295. function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "统计商户和创客交易额日志");
  296. foreach (DataRow selfDr in selfDt.Rows)
  297. {
  298. int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
  299. int PayMode = int.Parse(selfDr["PayMode"].ToString());
  300. int IsAct = int.Parse(selfDr["IsAct"].ToString());
  301. string TradeDate = selfDr[5].ToString();
  302. decimal TradeAmount = decimal.Parse(selfDr[6].ToString());
  303. int TradeCount = int.Parse(selfDr[7].ToString());
  304. decimal ActualAmount = decimal.Parse(selfDr[9].ToString());
  305. string jsonString = "{\"TradeDate\":\"" + TradeDate + "\",\"PayMoney\":\"" + TradeAmount + "\",\"TradeCount\":\"" + TradeCount + "\",\"MerchantActualAmount\":\"" + ActualAmount + "\",\"MerchantId\":\"" + MerchantId + "\",\"PayMode\":\"" + PayMode + "\",\"IsAct\":\"" + IsAct + "\"}";
  306. RedisDbconn.Instance.AddList("StatMerchantTradeSummaryQueue", jsonString);
  307. }
  308. }
  309. }
  310. catch (Exception ex)
  311. {
  312. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计商户和创客交易额异常");
  313. }
  314. db.Dispose();
  315. maindb.Dispose();
  316. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "统计商户和创客交易额日志");
  317. }
  318. }
  319. }