StatNewService.cs 12 KB

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