StatService2.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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.PxcModels2;
  9. namespace MySystem
  10. {
  11. public class StatService2
  12. {
  13. public readonly static StatService2 Instance = new StatService2();
  14. private StatService2()
  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. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "统计商户和创客交易额日志");
  34. WebCMSEntities db = new WebCMSEntities();
  35. KxsMainModels.WebCMSEntities maindb = new KxsMainModels.WebCMSEntities();
  36. try
  37. {
  38. string start = DateTime.Now.AddDays(-100).ToString("yyyy-MM-dd HH:mm:ss");
  39. DataTable idsDt = CustomerSqlConn.dtable("select Id from ConsumerOrders where CreateDate>='" + start + "' and Status>0 and Version=0 order by Id limit 50", AppConfig.Base.SqlConn2);
  40. if(idsDt.Rows.Count > 0)
  41. {
  42. string ids = "";
  43. foreach (DataRow idsDr in idsDt.Rows)
  44. {
  45. ids += idsDr["Id"].ToString() + ",";
  46. }
  47. 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 Id in (" + ids.TrimEnd(',') + ") group by UserId,MerchantId,SnNo,PayMode,IsAct,DATE_FORMAT(CreateDate,'%Y%m%d')", AppConfig.Base.SqlConn2);
  48. if (selfDt.Rows.Count > 0)
  49. {
  50. function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "统计商户和创客交易额日志");
  51. foreach (DataRow selfDr in selfDt.Rows)
  52. {
  53. int UserId = int.Parse(selfDr["UserId"].ToString());
  54. KxsMainModels.Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId) ?? new KxsMainModels.Users();
  55. string ParentNav = user.ParentNav;
  56. int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
  57. int PayMode = int.Parse(selfDr["PayMode"].ToString());
  58. int IsAct = int.Parse(selfDr["IsAct"].ToString());
  59. string SnNo = selfDr["SnNo"].ToString();
  60. string TradeDate = selfDr[5].ToString();
  61. decimal TradeAmount = decimal.Parse(selfDr[6].ToString());
  62. int TradeCount = int.Parse(selfDr[7].ToString());
  63. decimal MaxDivi = decimal.Parse(selfDr[8].ToString());
  64. decimal ActualAmount = decimal.Parse(selfDr[9].ToString());
  65. string TradeMonth = TradeDate.Substring(0, 6);
  66. //创客交易额
  67. UserAmountSummary selfStat = db.UserAmountSummary.FirstOrDefault(m => m.UserId == UserId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode && m.SeoTitle == "self");
  68. if (selfStat == null)
  69. {
  70. selfStat = db.UserAmountSummary.Add(new UserAmountSummary()
  71. {
  72. UserId = UserId,
  73. TradeMonth = TradeMonth,
  74. TradeDate = TradeDate,
  75. PayMode = PayMode,
  76. IsAct = IsAct,
  77. SeoTitle = "self",
  78. }).Entity;
  79. db.SaveChanges();
  80. }
  81. selfStat.TotalAmount += TradeAmount;
  82. selfStat.TradeCount += TradeCount;
  83. ParentNav += "," + UserId + ",";
  84. if (!string.IsNullOrEmpty(ParentNav))
  85. {
  86. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  87. foreach (string NavUserIdString in ParentNavList)
  88. {
  89. int NavUserId = int.Parse(NavUserIdString);
  90. UserAmountSummary teamStat = db.UserAmountSummary.FirstOrDefault(m => m.UserId == NavUserId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode && m.SeoTitle == "team");
  91. if (teamStat == null)
  92. {
  93. teamStat = db.UserAmountSummary.Add(new UserAmountSummary()
  94. {
  95. UserId = NavUserId,
  96. TradeMonth = TradeMonth,
  97. TradeDate = TradeDate,
  98. PayMode = PayMode,
  99. IsAct = IsAct,
  100. SeoTitle = "team",
  101. }).Entity;
  102. db.SaveChanges();
  103. }
  104. teamStat.TotalAmount += TradeAmount;
  105. teamStat.TradeCount += TradeCount;
  106. }
  107. }
  108. db.SaveChanges();
  109. //商户交易额
  110. MerchantAmountSummay merchantStat = db.MerchantAmountSummay.FirstOrDefault(m => m.MerchantId == MerchantId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode);
  111. if (merchantStat == null)
  112. {
  113. merchantStat = db.MerchantAmountSummay.Add(new MerchantAmountSummay()
  114. {
  115. MerchantId = MerchantId,
  116. TradeMonth = TradeMonth,
  117. TradeDate = TradeDate,
  118. PayMode = PayMode,
  119. IsAct = IsAct,
  120. }).Entity;
  121. db.SaveChanges();
  122. }
  123. merchantStat.TradeAmount += TradeAmount;
  124. merchantStat.TradeCount += TradeCount;
  125. merchantStat.TotalActual += ActualAmount;
  126. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
  127. if(merchant != null)
  128. {
  129. merchant.TotalAmount += TradeAmount;
  130. merchant.TotalConsumeCount += TradeCount;
  131. merchant.TotalActual += ActualAmount;
  132. merchant.ActMaxAmount += MaxDivi; //活动应返金额
  133. if(IsAct == 1)
  134. {
  135. merchant.TotalActAmount += TradeAmount;
  136. merchant.TotalActActual += ActualAmount;
  137. if(merchant.ActivationStatus == 0 && merchant.TotalActAmount >= 10000M)
  138. {
  139. // RedisDbconn.Instance.AddList("ActProfitQueue", MerchantId.ToString());
  140. merchant.ActivationStatus = 1;
  141. merchant.ActivationDate = DateTime.Now;
  142. }
  143. }
  144. }
  145. db.SaveChanges();
  146. //码牌交易额
  147. QrCodeAmountSummay qrcode = db.QrCodeAmountSummay.FirstOrDefault(m => m.SnNo == SnNo && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode);
  148. if (qrcode == null)
  149. {
  150. qrcode = db.QrCodeAmountSummay.Add(new QrCodeAmountSummay()
  151. {
  152. SnNo = SnNo,
  153. TradeMonth = TradeMonth,
  154. TradeDate = TradeDate,
  155. PayMode = PayMode,
  156. IsAct = IsAct,
  157. }).Entity;
  158. db.SaveChanges();
  159. }
  160. qrcode.TradeAmount += TradeAmount;
  161. qrcode.TradeCount += TradeCount;
  162. qrcode.TotalActual += ActualAmount;
  163. db.SaveChanges();
  164. string jsonString = "{\"TradeDate\":\"" + TradeDate + "\",\"PayMoney\":\"" + TradeAmount + "\",\"TradeCount\":\"" + TradeCount + "\",\"MerchantActualAmount\":\"" + ActualAmount + "\",\"MerchantId\":\"" + MerchantId + "\",\"PayMode\":\"" + PayMode + "\",\"IsAct\":\"" + IsAct + "\"}";
  165. RedisDbconn.Instance.AddList("StatMerchantTradeSummaryQueue2", jsonString);
  166. }
  167. CustomerSqlConn.op("update ConsumerOrders set Version=1 where Id in (" + ids.TrimEnd(',') + ")", AppConfig.Base.SqlConn2);
  168. }
  169. }
  170. }
  171. catch (Exception ex)
  172. {
  173. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计商户和创客交易额异常");
  174. }
  175. db.Dispose();
  176. maindb.Dispose();
  177. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "统计商户和创客交易额日志");
  178. }
  179. // 统计创客激活数
  180. public void StartPosActNum()
  181. {
  182. Thread th = new Thread(StartPosActNumFor);
  183. th.IsBackground = true;
  184. th.Start();
  185. }
  186. public void StartPosActNumFor()
  187. {
  188. while (true)
  189. {
  190. if(DateTime.Now.Hour >= 3)
  191. {
  192. StartPosActNumEverTime();
  193. }
  194. Thread.Sleep(20000);
  195. }
  196. }
  197. public void StartPosActNumEverTime()
  198. {
  199. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "实时执行创客激活数日志");
  200. WebCMSEntities db = new WebCMSEntities();
  201. KxsMainModels.WebCMSEntities maindb = new KxsMainModels.WebCMSEntities();
  202. try
  203. {
  204. DataTable idsDt = CustomerSqlConn.dtable("select Id from MerchantInfo where ActStat=0 and (Status=2 or QueryCount=2) and UserId>0 limit 50", AppConfig.Base.SqlConn2);
  205. if(idsDt.Rows.Count > 0)
  206. {
  207. string ids = "";
  208. foreach (DataRow idsDr in idsDt.Rows)
  209. {
  210. ids += idsDr["Id"].ToString() + ",";
  211. }
  212. DataTable userDt = CustomerSqlConn.dtable("select UserId,DATE_FORMAT(UpdateDate,'%Y%m%d'),count(Id) from MerchantInfo where Id in (" + ids.TrimEnd(',') + ") group by UserId,DATE_FORMAT(UpdateDate,'%Y%m%d')", AppConfig.Base.SqlConn2);
  213. if (userDt.Rows.Count > 0)
  214. {
  215. function.WriteLog("人数:" + userDt.Rows.Count + "\n\n", "实时执行创客激活数日志");
  216. foreach (DataRow userDr in userDt.Rows)
  217. {
  218. int UserId = int.Parse(userDr["UserId"].ToString());
  219. string TradeDate = userDr[1].ToString();
  220. int ActCount = int.Parse(function.CheckInt(userDr[2].ToString()));
  221. string TradeMonth = TradeDate.Substring(0, 6);
  222. string date = TradeDate.Substring(0, 4) + "-" + TradeDate.Substring(4, 2) + "-" + TradeDate.Substring(6, 2);
  223. string start = date + " 00:00:00";
  224. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  225. KxsMainModels.Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId) ?? new KxsMainModels.Users();
  226. string ParentNav = user.ParentNav;
  227. UserTradeMonthSummary selfStat = db.UserTradeMonthSummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoKeyword == TradeDate && m.SeoTitle == "self");
  228. if (selfStat == null)
  229. {
  230. selfStat = db.UserTradeMonthSummary.Add(new UserTradeMonthSummary()
  231. {
  232. UserId = UserId,
  233. TradeMonth = TradeMonth,
  234. SeoKeyword = TradeDate,
  235. SeoTitle = "self",
  236. }).Entity;
  237. db.SaveChanges();
  238. }
  239. selfStat.ActiveBuddyMerStatus += ActCount;
  240. ParentNav += "," + UserId + ",";
  241. if (!string.IsNullOrEmpty(ParentNav))
  242. {
  243. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  244. foreach (string NavUserIdString in ParentNavList)
  245. {
  246. int NavUserId = int.Parse(NavUserIdString);
  247. UserTradeMonthSummary teamStat = db.UserTradeMonthSummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.SeoKeyword == TradeDate && m.SeoTitle == "team");
  248. if (teamStat == null)
  249. {
  250. teamStat = db.UserTradeMonthSummary.Add(new UserTradeMonthSummary()
  251. {
  252. UserId = NavUserId,
  253. TradeMonth = TradeMonth,
  254. SeoKeyword = TradeDate,
  255. SeoTitle = "team",
  256. }).Entity;
  257. db.SaveChanges();
  258. }
  259. teamStat.ActiveBuddyMerStatus += ActCount;
  260. }
  261. }
  262. }
  263. db.SaveChanges();
  264. CustomerSqlConn.op("update MerchantInfo set ActStat=1 where Id in (" + ids.TrimEnd(',') + ")", AppConfig.Base.SqlConn2);
  265. }
  266. }
  267. }
  268. catch (Exception ex)
  269. {
  270. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时执行创客激活数异常");
  271. }
  272. db.Dispose();
  273. maindb.Dispose();
  274. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时执行创客激活数日志");
  275. }
  276. public void test()
  277. {
  278. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "实时执行创客激活数日志");
  279. WebCMSEntities db = new WebCMSEntities();
  280. KxsMainModels.WebCMSEntities maindb = new KxsMainModels.WebCMSEntities();
  281. try
  282. {
  283. DataTable idsDt = CustomerSqlConn.dtable("select 646 Id", AppConfig.Base.SqlConn2);
  284. if(idsDt.Rows.Count > 0)
  285. {
  286. string ids = "";
  287. foreach (DataRow idsDr in idsDt.Rows)
  288. {
  289. ids += idsDr["Id"].ToString() + ",";
  290. }
  291. 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.SqlConn2);
  292. if (userDt.Rows.Count > 0)
  293. {
  294. function.WriteLog("人数:" + userDt.Rows.Count + "\n\n", "实时执行创客激活数日志");
  295. foreach (DataRow userDr in userDt.Rows)
  296. {
  297. int UserId = int.Parse(userDr["UserId"].ToString());
  298. string TradeDate = userDr[1].ToString();
  299. int ActCount = int.Parse(function.CheckInt(userDr[2].ToString()));
  300. string TradeMonth = TradeDate.Substring(0, 6);
  301. string date = TradeDate.Substring(0, 4) + "-" + TradeDate.Substring(4, 2) + "-" + TradeDate.Substring(6, 2);
  302. string start = date + " 00:00:00";
  303. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  304. KxsMainModels.Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId) ?? new KxsMainModels.Users();
  305. string ParentNav = user.ParentNav;
  306. UserTradeMonthSummary selfStat = db.UserTradeMonthSummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoKeyword == TradeDate && m.SeoTitle == "self");
  307. if (selfStat == null)
  308. {
  309. selfStat = db.UserTradeMonthSummary.Add(new UserTradeMonthSummary()
  310. {
  311. UserId = UserId,
  312. TradeMonth = TradeMonth,
  313. SeoKeyword = TradeDate,
  314. SeoTitle = "self",
  315. }).Entity;
  316. db.SaveChanges();
  317. }
  318. selfStat.ActiveBuddyMerStatus += ActCount;
  319. ParentNav += "," + UserId + ",";
  320. if (!string.IsNullOrEmpty(ParentNav))
  321. {
  322. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  323. foreach (string NavUserIdString in ParentNavList)
  324. {
  325. int NavUserId = int.Parse(NavUserIdString);
  326. UserTradeMonthSummary teamStat = db.UserTradeMonthSummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.SeoKeyword == TradeDate && m.SeoTitle == "team");
  327. if (teamStat == null)
  328. {
  329. teamStat = db.UserTradeMonthSummary.Add(new UserTradeMonthSummary()
  330. {
  331. UserId = NavUserId,
  332. TradeMonth = TradeMonth,
  333. SeoKeyword = TradeDate,
  334. SeoTitle = "team",
  335. }).Entity;
  336. db.SaveChanges();
  337. }
  338. teamStat.ActiveBuddyMerStatus += ActCount;
  339. }
  340. }
  341. }
  342. db.SaveChanges();
  343. CustomerSqlConn.op("update MerchantInfo set ActStat=1 where Id in (" + ids.TrimEnd(',') + ")", AppConfig.Base.SqlConn2);
  344. }
  345. }
  346. }
  347. catch (Exception ex)
  348. {
  349. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时执行创客激活数异常");
  350. }
  351. db.Dispose();
  352. maindb.Dispose();
  353. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时执行创客激活数日志");
  354. }
  355. }
  356. }