StatNewService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 StatNewService
  12. {
  13. public readonly static StatNewService Instance = new StatNewService();
  14. private StatNewService()
  15. { }
  16. // 统计交易额V2
  17. public void StartStat()
  18. {
  19. Thread th = new Thread(StartStatDo);
  20. th.IsBackground = true;
  21. th.Start();
  22. }
  23. public void StartStatDo()
  24. {
  25. while (true)
  26. {
  27. string content = RedisDbconn.Instance.RPop<string>("StatTradeAmountQueue");
  28. if(!string.IsNullOrEmpty(content))
  29. {
  30. StatTradeAmount(content, 1);
  31. StatTradeAmount(content, 2);
  32. }
  33. Thread.Sleep(10000);
  34. }
  35. }
  36. public void StatTradeAmount(string content, int Kind)
  37. {
  38. WebCMSEntities db = new WebCMSEntities();
  39. try
  40. {
  41. JsonData selfDr = JsonMapper.ToObject(content);
  42. int UserId = int.Parse(selfDr["UserId"].ToString());
  43. Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  44. string ParentNav = user.ParentNav;
  45. int BrandId = int.Parse(selfDr["BrandId"].ToString());
  46. int BankCardType = int.Parse(selfDr["BankCardType"].ToString());
  47. int QrPayFlag = int.Parse(selfDr["QrPayFlag"].ToString());
  48. int MerHelpFlag = int.Parse(selfDr["MerHelpFlag"].ToString());
  49. int Version = int.Parse(selfDr["Version"].ToString());
  50. int CapFlag = int.Parse(selfDr["CapFlag"].ToString());
  51. int VipFlag = int.Parse(selfDr["VipFlag"].ToString());
  52. int PayType = int.Parse(selfDr["PayType"].ToString());
  53. string TradeDate = selfDr["TradeDate"].ToString();
  54. decimal TradeAmount = decimal.Parse(selfDr["TradeAmount"].ToString());
  55. int TradeCount = int.Parse(selfDr["TradeCount"].ToString());
  56. if(Kind == 2)
  57. {
  58. TradeDate = TradeDate.Substring(0, 6);
  59. }
  60. if(string.IsNullOrEmpty(RedisDbconn.Instance.Get<string>("TradeDaySummary" + TradeDate)))
  61. {
  62. CustomerSqlConn.op(AppConfig.Base.CreateTradeDaySummary.Replace("#DateTime#", TradeDate), AppConfig.Base.StatSqlConn);
  63. RedisDbconn.Instance.Set("TradeDaySummary" + TradeDate, "1");
  64. int sec = 3600 * 13;
  65. if(Kind == 2)
  66. {
  67. sec = 3600 * 12 * 35;
  68. }
  69. RedisDbconn.Instance.SetExpire("TradeDaySummary" + TradeDate, sec);
  70. Thread.Sleep(2000);
  71. }
  72. string Id = "0";
  73. DataTable check = CustomerSqlConn.dtable("select Id from TradeDaySummary" + TradeDate + " where UserId=" + UserId + " and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='self'", AppConfig.Base.StatSqlConn);
  74. if(check.Rows.Count < 1)
  75. {
  76. check = CustomerSqlConn.dtable("insert into TradeDaySummary" + TradeDate + " (UserId,BrandId,QueryCount,VipFlag,PayType,SeoTitle) values (" + UserId + "," + BrandId + "," + QrPayFlag + "," + VipFlag + "," + PayType + ",'self');select @@IDENTITY", AppConfig.Base.StatSqlConn);
  77. if(check.Rows.Count > 0)
  78. {
  79. Id = check.Rows[0][0].ToString();
  80. }
  81. }
  82. else
  83. {
  84. Id = check.Rows[0][0].ToString();
  85. }
  86. string selfStat = "";
  87. if (BankCardType == 0)
  88. {
  89. if (Version == 1)
  90. {
  91. selfStat += "ProfitDirectDebitTradeAmt = ProfitDirectDebitTradeAmt+" + TradeAmount + ",";
  92. if (CapFlag == 1)
  93. {
  94. selfStat += "ProfitDirectDebitCapTradeAmt = ProfitDirectDebitCapTradeAmt+" + TradeAmount + ",";
  95. selfStat += "ProfitDirectDebitCapNum = ProfitDirectDebitCapNum+" + TradeCount + ",";
  96. }
  97. }
  98. else if (MerHelpFlag == 1)
  99. {
  100. selfStat += "HelpDirectDebitTradeAmt = HelpDirectDebitTradeAmt+" + TradeAmount + ",";
  101. if (CapFlag == 1)
  102. {
  103. selfStat += "HelpDirectDebitCapTradeAmt = HelpDirectDebitCapTradeAmt+" + TradeAmount + ",";
  104. selfStat += "HelpDirectDebitCapNum = HelpDirectDebitCapNum+" + TradeCount + ",";
  105. }
  106. }
  107. else
  108. {
  109. selfStat += "NotHelpDirectDebitTradeAmt = NotHelpDirectDebitTradeAmt+" + TradeAmount + ",";
  110. if (CapFlag == 1)
  111. {
  112. selfStat += "NotHelpDirectDebitCapTradeAmt = NotHelpDirectDebitCapTradeAmt+" + TradeAmount + ",";
  113. selfStat += "NotHelpDirectDebitCapNum = NotHelpDirectDebitCapNum+" + TradeCount + ",";
  114. }
  115. }
  116. }
  117. else if (BankCardType != 0)
  118. {
  119. if (Version == 1)
  120. {
  121. selfStat += "ProfitDirectTradeAmt = ProfitDirectTradeAmt+" + TradeAmount + ",";
  122. }
  123. else if (MerHelpFlag == 1)
  124. {
  125. selfStat += "HelpDirectTradeAmt = HelpDirectTradeAmt+" + TradeAmount + ",";
  126. }
  127. else
  128. {
  129. selfStat += "NotHelpDirectTradeAmt = NotHelpDirectTradeAmt+" + TradeAmount + ",";
  130. }
  131. }
  132. CustomerSqlConn.op("update TradeDaySummary" + TradeDate + " set " + selfStat.TrimEnd(',') + " where Id=" + Id, AppConfig.Base.StatSqlConn);
  133. ParentNav += "," + UserId + ",";
  134. if (!string.IsNullOrEmpty(ParentNav))
  135. {
  136. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  137. foreach (string NavUserIdString in ParentNavList)
  138. {
  139. int NavUserId = int.Parse(NavUserIdString);
  140. string PId = "0";
  141. DataTable pcheck = CustomerSqlConn.dtable("select Id from TradeDaySummary" + TradeDate + " where UserId=" + NavUserId + " and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='team'", AppConfig.Base.StatSqlConn);
  142. if(pcheck.Rows.Count < 1)
  143. {
  144. pcheck = CustomerSqlConn.dtable("insert into TradeDaySummary" + TradeDate + " (UserId,BrandId,QueryCount,VipFlag,PayType,SeoTitle) values (" + NavUserId + "," + BrandId + "," + QrPayFlag + "," + VipFlag + "," + PayType + ",'team');select @@IDENTITY", AppConfig.Base.StatSqlConn);
  145. if(pcheck.Rows.Count > 0)
  146. {
  147. PId = pcheck.Rows[0][0].ToString();
  148. }
  149. }
  150. else
  151. {
  152. PId = pcheck.Rows[0][0].ToString();
  153. }
  154. string teamStat = "";
  155. if (BankCardType == 0)
  156. {
  157. if (Version == 1)
  158. {
  159. teamStat += "ProfitNonDirectDebitTradeAmt = ProfitNonDirectDebitTradeAmt+" + TradeAmount + ",";
  160. if (CapFlag == 1)
  161. {
  162. teamStat += "ProfitDirectDebitCapTradeAmt = ProfitDirectDebitCapTradeAmt+" + TradeAmount + ",";
  163. teamStat += "ProfitDirectDebitCapNum = ProfitDirectDebitCapNum+" + TradeCount + ",";
  164. }
  165. }
  166. else if (MerHelpFlag == 1)
  167. {
  168. teamStat += "HelpNonDirectDebitTradeAmt = HelpNonDirectDebitTradeAmt+" + TradeAmount + ",";
  169. if (CapFlag == 1)
  170. {
  171. teamStat += "HelpDirectDebitCapTradeAmt = HelpDirectDebitCapTradeAmt+" + TradeAmount + ",";
  172. teamStat += "HelpDirectDebitCapNum = HelpDirectDebitCapNum+" + TradeCount + ",";
  173. }
  174. }
  175. else
  176. {
  177. teamStat += "NotHelpNonDirectDebitTradeAmt = NotHelpNonDirectDebitTradeAmt+" + TradeAmount + ",";
  178. if (CapFlag == 1)
  179. {
  180. teamStat += "NotHelpDirectDebitCapTradeAmt = NotHelpDirectDebitCapTradeAmt+" + TradeAmount + ",";
  181. teamStat += "NotHelpDirectDebitCapNum = NotHelpDirectDebitCapNum+" + TradeCount + ",";
  182. }
  183. }
  184. }
  185. else if (BankCardType != 0)
  186. {
  187. if (Version == 1)
  188. {
  189. teamStat += "ProfitNonDirectTradeAmt = ProfitNonDirectTradeAmt+" + TradeAmount + ",";
  190. }
  191. else if (MerHelpFlag == 1)
  192. {
  193. teamStat += "HelpNonDirectTradeAmt = HelpNonDirectTradeAmt+" + TradeAmount + ",";
  194. }
  195. else
  196. {
  197. teamStat += "NotHelpNonDirectTradeAmt = NotHelpNonDirectTradeAmt+" + TradeAmount + ",";
  198. }
  199. }
  200. CustomerSqlConn.op("update TradeDaySummary" + TradeDate + " set " + teamStat.TrimEnd(',') + " where Id=" + PId, AppConfig.Base.StatSqlConn);
  201. }
  202. }
  203. db.SaveChanges();
  204. }
  205. catch (Exception ex)
  206. {
  207. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时统计交易额日志RDS异常");
  208. }
  209. db.Dispose();
  210. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时统计交易额日志RDS");
  211. }
  212. }
  213. }