StatNewService.cs 11 KB

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