StatService2.cs 22 KB

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