StatServiceTmp.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 StatServiceTmp
  12. {
  13. public readonly static StatServiceTmp Instance = new StatServiceTmp();
  14. private StatServiceTmp()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(StartEverDay);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. // 每天统计头一天的交易额
  23. public void StartEverDay()
  24. {
  25. string chk = function.ReadInstance("/log/临时重置盛付通交易额.txt");
  26. if(string.IsNullOrEmpty(chk))
  27. {
  28. function.WritePage("/log/", "临时重置盛付通交易额.txt", DateTime.Now.ToString());
  29. OtherMySqlConn.connstr = Library.ConfigurationManager.AppSettings["Pxc1SqlConnStr"].ToString();
  30. DateTime end = DateTime.Parse("2022-06-23 00:00:00");
  31. DateTime check = DateTime.Parse("2022-06-14");
  32. while (check <= end)
  33. {
  34. StatTradeAmountEverDay(check.ToString("yyyy-MM-dd"));
  35. check = check.AddDays(1);
  36. Thread.Sleep(1000);
  37. }
  38. }
  39. }
  40. public void StatTradeAmountEverDay(object sender)
  41. {
  42. string date = sender.ToString();
  43. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "临时重置交易额日志");
  44. WebCMSEntities db = new WebCMSEntities();
  45. try
  46. {
  47. string TradeDate = date.Replace("-", "");
  48. string TradeMonth = TradeDate.Substring(0, 6);
  49. string start = date + " 00:00:00";
  50. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  51. string startId = "0", endId = "9999999999";
  52. List<string> uids = new List<string>();
  53. DataTable startDt = OtherMySqlConn.dtable("select min(Id) from TradeRecord where CreateDate>='" + start + "'");
  54. if (startDt.Rows.Count > 0)
  55. {
  56. startId = startDt.Rows[0][0].ToString();
  57. }
  58. function.WriteLog(startId + "\n\n", "临时重置交易额日志");
  59. DataTable userDt = OtherMySqlConn.dtable("select Id,ParentNav from Users where Id in (select DISTINCT UserId from TradeRecord where Id>=" + startId + " and Id<=" + endId + " and CreateDate>='" + start + "' and CreateDate<'" + end + "' and BrandId=7)");
  60. function.WriteLog("交易人数:" + userDt.Rows.Count + "\n\n", "临时重置交易额日志");
  61. foreach (DataRow userDr in userDt.Rows)
  62. {
  63. int UserId = int.Parse(userDr["Id"].ToString());
  64. string ParentNav = userDr["ParentNav"].ToString();
  65. function.WriteLog(UserId + ":" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "临时重置交易额日志");
  66. DataTable selfdt = OtherMySqlConn.dtable("select BrandId,BankCardType,QrPayFlag,sum(TradeAmount) from TradeRecord where Id>=" + startId + " and Id<=" + endId + " and CreateDate>='" + start + "' and CreateDate<'" + end + "' and UserId=" + UserId + " and BrandId=7 group by BrandId,BankCardType,QrPayFlag");
  67. function.WriteLog(UserId + ":" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "临时重置交易额日志");
  68. OtherMySqlConn.op("delete from UserTradeDaySummary where UserId=" + UserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=7 and SeoTitle='self'");
  69. foreach (DataRow selfDr in selfdt.Rows)
  70. {
  71. int BrandId = int.Parse(selfDr["BrandId"].ToString());
  72. int BankCardType = int.Parse(selfDr["BankCardType"].ToString());
  73. int QrPayFlag = int.Parse(selfDr["QrPayFlag"].ToString());
  74. decimal TradeAmount = decimal.Parse(selfDr[3].ToString());
  75. UserTradeDaySummary selfStat = db.UserTradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.SeoTitle == "self");
  76. if (selfStat == null)
  77. {
  78. selfStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  79. {
  80. UserId = UserId,
  81. TradeMonth = TradeMonth,
  82. TradeDate = TradeDate,
  83. BrandId = BrandId,
  84. QueryCount = QrPayFlag,
  85. SeoTitle = "self",
  86. }).Entity;
  87. db.SaveChanges();
  88. }
  89. if (BankCardType == 0)
  90. {
  91. selfStat.DirectDebitTradeAmt += TradeAmount;
  92. }
  93. else if (BankCardType != 0)
  94. {
  95. selfStat.DirectTradeAmt += TradeAmount;
  96. }
  97. db.SaveChanges();
  98. }
  99. if (!string.IsNullOrEmpty(ParentNav))
  100. {
  101. ParentNav += "," + UserId + ",";
  102. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  103. foreach (string NavUserIdString in ParentNavList)
  104. {
  105. if (!uids.Contains(NavUserIdString + start))
  106. {
  107. uids.Add(NavUserIdString + start);
  108. int NavUserId = int.Parse(NavUserIdString);
  109. function.WriteLog(NavUserId + ":team:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "临时重置交易额日志");
  110. DataTable teamDt = OtherMySqlConn.dtable("select BrandId,BankCardType,QrPayFlag,sum(TradeAmount) from TradeRecord where Id>=" + startId + " and Id<=" + endId + " and CreateDate>='" + start + "' and CreateDate<'" + end + "' and UserId in (select Id from Users where ParentNav like '%," + NavUserId + ",%' or Id=" + NavUserId + ") and BrandId=7 group by BrandId,BankCardType,QrPayFlag");
  111. function.WriteLog(NavUserId + ":team:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "临时重置交易额日志");
  112. OtherMySqlConn.op("delete from UserTradeDaySummary where UserId=" + NavUserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=7 and SeoTitle='team'");
  113. foreach (DataRow teamDr in teamDt.Rows)
  114. {
  115. int BrandId = int.Parse(teamDr["BrandId"].ToString());
  116. int BankCardType = int.Parse(teamDr["BankCardType"].ToString());
  117. int QrPayFlag = int.Parse(teamDr["QrPayFlag"].ToString());
  118. decimal TradeAmount = decimal.Parse(teamDr[3].ToString());
  119. UserTradeDaySummary teamStat = db.UserTradeDaySummary.FirstOrDefault(m => m.UserId == NavUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.SeoTitle == "team");
  120. if (teamStat == null)
  121. {
  122. teamStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  123. {
  124. UserId = NavUserId,
  125. TradeMonth = TradeMonth,
  126. TradeDate = TradeDate,
  127. BrandId = BrandId,
  128. QueryCount = QrPayFlag,
  129. SeoTitle = "team",
  130. }).Entity;
  131. db.SaveChanges();
  132. }
  133. if (BankCardType == 0)
  134. {
  135. teamStat.NonDirectDebitTradeAmt += TradeAmount;
  136. }
  137. else if (BankCardType != 0)
  138. {
  139. teamStat.NonDirectTradeAmt += TradeAmount;
  140. }
  141. db.SaveChanges();
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. catch (Exception ex)
  149. {
  150. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "临时重置交易额异常");
  151. }
  152. db.Dispose();
  153. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "临时重置交易额日志");
  154. StatMerchantTrade(date);
  155. }
  156. //统计商户交易额
  157. private void StatMerchantTrade(string date)
  158. {
  159. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "执行商户交易额日志");
  160. try
  161. {
  162. string TradeDate = date.Replace("-", "");
  163. string start = date + " 00:00:00";
  164. string end = DateTime.Parse(date).AddDays(1).ToString("yyyy-MM-dd") + " 00:00:00";
  165. OtherMySqlConn.op("delete from PosMerchantTradeSummay where TradeDate='" + TradeDate + "' and BrandId=7");
  166. OtherMySqlConn.op("insert into PosMerchantTradeSummay (MerchantId,BrandId,TradeDate,TradeMonth,TradeAmount,CreateDate) select *,now() from (select MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y%m%d') as TradeDate,DATE_FORMAT(CreateDate,'%Y%m') as TradeMonth,sum(TradeAmount) as TradeAmount from TradeRecord where CreateDate>='" + start + "' and CreateDate<'" + end + "' and BrandId=7 group by MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y%m%d'),TradeDate,DATE_FORMAT(CreateDate,'%Y%m') order by MerchantId,BrandId,DATE_FORMAT(CreateDate,'%Y%m%d')) tb");
  167. RedisDbconn.Instance.Clear("PosMerchantAmount:*");
  168. RedisDbconn.Instance.Clear("PosMerchantAmount:*:" + date.Replace("-", "").Substring(0, 6)); //商户当月交易
  169. RedisDbconn.Instance.Clear("PosMerchantAmount:*:" + date.Replace("-", "")); //商户当日交易
  170. }
  171. catch (Exception ex)
  172. {
  173. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "临时重置商户的交易额");
  174. }
  175. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "临时重置商户交易额日志");
  176. }
  177. }
  178. }