StatTradeCheckService.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 StatTradeCheckService
  12. {
  13. public readonly static StatTradeCheckService Instance = new StatTradeCheckService();
  14. private StatTradeCheckService()
  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. string content = RedisDbconn.Instance.RPop<string>("StatTradeCheckQueue");
  28. if(!string.IsNullOrEmpty(content))
  29. {
  30. StatTradeAmountDo(content);
  31. Thread.Sleep(1);
  32. }
  33. else
  34. {
  35. Thread.Sleep(10000);
  36. }
  37. }
  38. }
  39. public void StatTradeAmountDo(string date)
  40. {
  41. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "统计交易额日志");
  42. try
  43. {
  44. int pageNum = 0;
  45. bool check = true;
  46. while(check)
  47. {
  48. string sql = "";
  49. DataTable dt = CustomerSqlConn.dtable("select UserId,ParentNav,BrandId,BankCardType,QrPayFlag,MerHelpFlag,Version,CapFlag,VipFlag,PayType,DATE_FORMAT(CreateDate,'%Y%m%d'),sum(TradeAmount),count(Id),SnNo,TradeDate from TradeRecord where Id>20000000 and ActStatus=1 and BrandId!=14 and UserId>0 and CreateDate>='" + date + " 00:00:00' and CreateDate<='" + date + " 23:59:59' group by UserId,ParentNav,BrandId,BankCardType,QrPayFlag,MerHelpFlag,Version,CapFlag,VipFlag,PayType,DATE_FORMAT(CreateDate,'%Y%m%d'),SnNo,TradeDate order by UserId limit " + pageNum + ",50", AppConfig.Base.TmpReadSqlConn);
  50. if(dt.Rows.Count > 0)
  51. {
  52. foreach (DataRow dr in dt.Rows)
  53. {
  54. int UserId = int.Parse(dr["UserId"].ToString());
  55. string SnNo = dr["SnNo"].ToString();
  56. string BindingTime = dr["TradeDate"].ToString();
  57. string ParentNav = dr["ParentNav"].ToString();
  58. int BrandId = int.Parse(dr["BrandId"].ToString());
  59. int BankCardType = int.Parse(dr["BankCardType"].ToString());
  60. int QrPayFlag = int.Parse(dr["QrPayFlag"].ToString());
  61. int MerHelpFlag = int.Parse(dr["MerHelpFlag"].ToString());
  62. int Version = int.Parse(dr["Version"].ToString());
  63. int CapFlag = int.Parse(dr["CapFlag"].ToString());
  64. int VipFlag = int.Parse(dr["VipFlag"].ToString());
  65. int PayType = int.Parse(dr["PayType"].ToString());
  66. string TradeDate = dr[10].ToString();
  67. decimal TradeAmount = decimal.Parse(dr[11].ToString());
  68. int TradeCount = 1;
  69. string TradeMonth = TradeDate.Substring(0, 6);
  70. ParentNav += "," + UserId + ",";
  71. string[] UserIdList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  72. if(Version < 2)
  73. {
  74. //个人业绩
  75. decimal ProfitDirectDebitTradeAmt = 0;
  76. decimal ProfitDirectDebitCapTradeAmt = 0;
  77. decimal ProfitDirectDebitCapNum = 0;
  78. decimal HelpDirectDebitTradeAmt = 0;
  79. decimal HelpDirectDebitCapTradeAmt = 0;
  80. decimal HelpDirectDebitCapNum = 0;
  81. decimal NotHelpDirectDebitTradeAmt = 0;
  82. decimal NotHelpDirectDebitCapTradeAmt = 0;
  83. decimal NotHelpDirectDebitCapNum = 0;
  84. decimal ProfitDirectTradeAmt = 0;
  85. decimal HelpDirectTradeAmt = 0;
  86. decimal NotHelpDirectTradeAmt = 0;
  87. if (BankCardType == 0)
  88. {
  89. if (Version == 1)
  90. {
  91. ProfitDirectDebitTradeAmt += TradeAmount;
  92. if (CapFlag == 1)
  93. {
  94. ProfitDirectDebitCapTradeAmt += TradeAmount;
  95. ProfitDirectDebitCapNum += TradeCount;
  96. }
  97. }
  98. else if (MerHelpFlag == 1)
  99. {
  100. HelpDirectDebitTradeAmt += TradeAmount;
  101. if (CapFlag == 1)
  102. {
  103. HelpDirectDebitCapTradeAmt += TradeAmount;
  104. HelpDirectDebitCapNum += TradeCount;
  105. }
  106. }
  107. else
  108. {
  109. NotHelpDirectDebitTradeAmt += TradeAmount;
  110. if (CapFlag == 1)
  111. {
  112. NotHelpDirectDebitCapTradeAmt += TradeAmount;
  113. NotHelpDirectDebitCapNum += TradeCount;
  114. }
  115. }
  116. }
  117. else if (BankCardType != 0)
  118. {
  119. if (Version == 1)
  120. {
  121. ProfitDirectTradeAmt += TradeAmount;
  122. }
  123. else if (MerHelpFlag == 1)
  124. {
  125. HelpDirectTradeAmt += TradeAmount;
  126. }
  127. else
  128. {
  129. NotHelpDirectTradeAmt += TradeAmount;
  130. }
  131. }
  132. sql += InsertOrUpdate("TradeDaySummary", "ProfitDirectDebitTradeAmt,ProfitDirectDebitCapTradeAmt,ProfitDirectDebitCapNum,HelpDirectDebitTradeAmt,HelpDirectDebitCapTradeAmt,HelpDirectDebitCapNum,NotHelpDirectDebitTradeAmt,NotHelpDirectDebitCapTradeAmt,NotHelpDirectDebitCapNum,ProfitDirectTradeAmt,HelpDirectTradeAmt,NotHelpDirectTradeAmt", ProfitDirectDebitTradeAmt + "," + ProfitDirectDebitCapTradeAmt + "," + ProfitDirectDebitCapNum + "," + HelpDirectDebitTradeAmt + "," + HelpDirectDebitCapTradeAmt + "," + HelpDirectDebitCapNum + "," + NotHelpDirectDebitTradeAmt + "," + NotHelpDirectDebitCapTradeAmt + "," + NotHelpDirectDebitCapNum + "," + ProfitDirectTradeAmt + "," + HelpDirectTradeAmt + "," + NotHelpDirectTradeAmt, "UserId=" + UserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='self'");
  133. if (DateTime.Parse(BindingTime) < DateTime.Parse("2023-07-01 00:00:00"))
  134. {
  135. sql += InsertOrUpdate("TradeDaySummaryBefore", "ProfitDirectDebitTradeAmt,ProfitDirectDebitCapTradeAmt,ProfitDirectDebitCapNum,HelpDirectDebitTradeAmt,HelpDirectDebitCapTradeAmt,HelpDirectDebitCapNum,NotHelpDirectDebitTradeAmt,NotHelpDirectDebitCapTradeAmt,NotHelpDirectDebitCapNum,ProfitDirectTradeAmt,HelpDirectTradeAmt,NotHelpDirectTradeAmt", ProfitDirectDebitTradeAmt + "," + ProfitDirectDebitCapTradeAmt + "," + ProfitDirectDebitCapNum + "," + HelpDirectDebitTradeAmt + "," + HelpDirectDebitCapTradeAmt + "," + HelpDirectDebitCapNum + "," + NotHelpDirectDebitTradeAmt + "," + NotHelpDirectDebitCapTradeAmt + "," + NotHelpDirectDebitCapNum + "," + ProfitDirectTradeAmt + "," + HelpDirectTradeAmt + "," + NotHelpDirectTradeAmt, "UserId=" + UserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='self'");
  136. }
  137. else
  138. {
  139. sql += InsertOrUpdate("TradeDaySummaryAfter", "ProfitDirectDebitTradeAmt,ProfitDirectDebitCapTradeAmt,ProfitDirectDebitCapNum,HelpDirectDebitTradeAmt,HelpDirectDebitCapTradeAmt,HelpDirectDebitCapNum,NotHelpDirectDebitTradeAmt,NotHelpDirectDebitCapTradeAmt,NotHelpDirectDebitCapNum,ProfitDirectTradeAmt,HelpDirectTradeAmt,NotHelpDirectTradeAmt", ProfitDirectDebitTradeAmt + "," + ProfitDirectDebitCapTradeAmt + "," + ProfitDirectDebitCapNum + "," + HelpDirectDebitTradeAmt + "," + HelpDirectDebitCapTradeAmt + "," + HelpDirectDebitCapNum + "," + NotHelpDirectDebitTradeAmt + "," + NotHelpDirectDebitCapTradeAmt + "," + NotHelpDirectDebitCapNum + "," + ProfitDirectTradeAmt + "," + HelpDirectTradeAmt + "," + NotHelpDirectTradeAmt, "UserId=" + UserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='self'");
  140. }
  141. //团队业绩
  142. foreach(string SubUserId in UserIdList)
  143. {
  144. decimal ProfitNonDirectDebitTradeAmt = 0;
  145. ProfitDirectDebitCapTradeAmt = 0;
  146. ProfitDirectDebitCapNum = 0;
  147. decimal HelpNonDirectDebitTradeAmt = 0;
  148. HelpDirectDebitCapTradeAmt = 0;
  149. HelpDirectDebitCapNum = 0;
  150. decimal NotHelpNonDirectDebitTradeAmt = 0;
  151. NotHelpDirectDebitCapTradeAmt = 0;
  152. NotHelpDirectDebitCapNum = 0;
  153. decimal ProfitNonDirectTradeAmt = 0;
  154. decimal HelpNonDirectTradeAmt = 0;
  155. decimal NotHelpNonDirectTradeAmt = 0;
  156. if (BankCardType == 0)
  157. {
  158. if (Version == 1)
  159. {
  160. ProfitNonDirectDebitTradeAmt += TradeAmount;
  161. if (CapFlag == 1)
  162. {
  163. ProfitDirectDebitCapTradeAmt += TradeAmount;
  164. ProfitDirectDebitCapNum += TradeCount;
  165. }
  166. }
  167. else if (MerHelpFlag == 1)
  168. {
  169. HelpNonDirectDebitTradeAmt += TradeAmount;
  170. if (CapFlag == 1)
  171. {
  172. HelpDirectDebitCapTradeAmt += TradeAmount;
  173. HelpDirectDebitCapNum += TradeCount;
  174. }
  175. }
  176. else
  177. {
  178. NotHelpNonDirectDebitTradeAmt += TradeAmount;
  179. if (CapFlag == 1)
  180. {
  181. NotHelpDirectDebitCapTradeAmt += TradeAmount;
  182. NotHelpDirectDebitCapNum += TradeCount;
  183. }
  184. }
  185. }
  186. else if (BankCardType != 0)
  187. {
  188. if (Version == 1)
  189. {
  190. ProfitNonDirectTradeAmt += TradeAmount;
  191. }
  192. else if (MerHelpFlag == 1)
  193. {
  194. HelpNonDirectTradeAmt += TradeAmount;
  195. }
  196. else
  197. {
  198. NotHelpNonDirectTradeAmt += TradeAmount;
  199. }
  200. }
  201. sql += InsertOrUpdate("TradeDaySummary", "ProfitNonDirectDebitTradeAmt,ProfitDirectDebitCapTradeAmt,ProfitDirectDebitCapNum,HelpNonDirectDebitTradeAmt,HelpDirectDebitCapTradeAmt,HelpDirectDebitCapNum,NotHelpNonDirectDebitTradeAmt,NotHelpDirectDebitCapTradeAmt,NotHelpDirectDebitCapNum,ProfitNonDirectTradeAmt,HelpNonDirectTradeAmt,NotHelpNonDirectTradeAmt", ProfitNonDirectDebitTradeAmt + "," + ProfitDirectDebitCapTradeAmt + "," + ProfitDirectDebitCapNum + "," + HelpNonDirectDebitTradeAmt + "," + HelpDirectDebitCapTradeAmt + "," + HelpDirectDebitCapNum + "," + NotHelpNonDirectDebitTradeAmt + "," + NotHelpDirectDebitCapTradeAmt + "," + NotHelpDirectDebitCapNum + "," + ProfitNonDirectTradeAmt + "," + HelpNonDirectTradeAmt + "," + NotHelpNonDirectTradeAmt, "UserId=" + SubUserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='team'");
  202. if (DateTime.Parse(BindingTime) < DateTime.Parse("2023-07-01 00:00:00"))
  203. {
  204. sql += InsertOrUpdate("TradeDaySummaryBefore", "ProfitNonDirectDebitTradeAmt,ProfitDirectDebitCapTradeAmt,ProfitDirectDebitCapNum,HelpNonDirectDebitTradeAmt,HelpDirectDebitCapTradeAmt,HelpDirectDebitCapNum,NotHelpNonDirectDebitTradeAmt,NotHelpDirectDebitCapTradeAmt,NotHelpDirectDebitCapNum,ProfitNonDirectTradeAmt,HelpNonDirectTradeAmt,NotHelpNonDirectTradeAmt", ProfitNonDirectDebitTradeAmt + "," + ProfitDirectDebitCapTradeAmt + "," + ProfitDirectDebitCapNum + "," + HelpNonDirectDebitTradeAmt + "," + HelpDirectDebitCapTradeAmt + "," + HelpDirectDebitCapNum + "," + NotHelpNonDirectDebitTradeAmt + "," + NotHelpDirectDebitCapTradeAmt + "," + NotHelpDirectDebitCapNum + "," + ProfitNonDirectTradeAmt + "," + HelpNonDirectTradeAmt + "," + NotHelpNonDirectTradeAmt, "UserId=" + SubUserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='team'");
  205. }
  206. else
  207. {
  208. sql += InsertOrUpdate("TradeDaySummaryAfter", "ProfitNonDirectDebitTradeAmt,ProfitDirectDebitCapTradeAmt,ProfitDirectDebitCapNum,HelpNonDirectDebitTradeAmt,HelpDirectDebitCapTradeAmt,HelpDirectDebitCapNum,NotHelpNonDirectDebitTradeAmt,NotHelpDirectDebitCapTradeAmt,NotHelpDirectDebitCapNum,ProfitNonDirectTradeAmt,HelpNonDirectTradeAmt,NotHelpNonDirectTradeAmt", ProfitNonDirectDebitTradeAmt + "," + ProfitDirectDebitCapTradeAmt + "," + ProfitDirectDebitCapNum + "," + HelpNonDirectDebitTradeAmt + "," + HelpDirectDebitCapTradeAmt + "," + HelpDirectDebitCapNum + "," + NotHelpNonDirectDebitTradeAmt + "," + NotHelpDirectDebitCapTradeAmt + "," + NotHelpDirectDebitCapNum + "," + ProfitNonDirectTradeAmt + "," + HelpNonDirectTradeAmt + "," + NotHelpNonDirectTradeAmt, "UserId=" + SubUserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='team'");
  209. }
  210. }
  211. }
  212. else
  213. {
  214. decimal ProfitDebitTradeAmt = 0;
  215. decimal ProfitDebitCapTradeAmt = 0;
  216. decimal ProfitDebitCapNum = 0;
  217. decimal ProfitTradeAmt = 0;
  218. if (BankCardType == 0)
  219. {
  220. if (Version == 2)
  221. {
  222. ProfitDebitTradeAmt += TradeAmount;
  223. if (CapFlag == 1)
  224. {
  225. ProfitDebitCapTradeAmt += TradeAmount;
  226. ProfitDebitCapNum += TradeCount;
  227. }
  228. }
  229. }
  230. else if (BankCardType != 0)
  231. {
  232. if (Version == 2)
  233. {
  234. ProfitTradeAmt += TradeAmount;
  235. }
  236. }
  237. sql += InsertOrUpdate("TradeDaySummary2", "ProfitDebitTradeAmt,ProfitDebitCapTradeAmt,ProfitDebitCapNum,ProfitTradeAmt", ProfitDebitTradeAmt + "," + ProfitDebitCapTradeAmt + "," + ProfitDebitCapNum + "," + ProfitTradeAmt, "UserId=" + UserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='self'");
  238. if (DateTime.Parse(BindingTime) < DateTime.Parse("2023-07-01 00:00:00"))
  239. {
  240. sql += InsertOrUpdate("TradeDaySummary2Before", "ProfitDebitTradeAmt,ProfitDebitCapTradeAmt,ProfitDebitCapNum,ProfitTradeAmt", ProfitDebitTradeAmt + "," + ProfitDebitCapTradeAmt + "," + ProfitDebitCapNum + "," + ProfitTradeAmt, "UserId=" + UserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='self'");
  241. }
  242. else
  243. {
  244. sql += InsertOrUpdate("TradeDaySummary2After", "ProfitDebitTradeAmt,ProfitDebitCapTradeAmt,ProfitDebitCapNum,ProfitTradeAmt", ProfitDebitTradeAmt + "," + ProfitDebitCapTradeAmt + "," + ProfitDebitCapNum + "," + ProfitTradeAmt, "UserId=" + UserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='self'");
  245. }
  246. foreach(string SubUserId in UserIdList)
  247. {
  248. sql += InsertOrUpdate("TradeDaySummary2", "ProfitDebitTradeAmt,ProfitDebitCapTradeAmt,ProfitDebitCapNum,ProfitTradeAmt", ProfitDebitTradeAmt + "," + ProfitDebitCapTradeAmt + "," + ProfitDebitCapNum + "," + ProfitTradeAmt, "UserId=" + SubUserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='team'");
  249. if (DateTime.Parse(BindingTime) < DateTime.Parse("2023-07-01 00:00:00"))
  250. {
  251. sql += InsertOrUpdate("TradeDaySummary2Before", "ProfitDebitTradeAmt,ProfitDebitCapTradeAmt,ProfitDebitCapNum,ProfitTradeAmt", ProfitDebitTradeAmt + "," + ProfitDebitCapTradeAmt + "," + ProfitDebitCapNum + "," + ProfitTradeAmt, "UserId=" + SubUserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='team'");
  252. }
  253. else
  254. {
  255. sql += InsertOrUpdate("TradeDaySummary2After", "ProfitDebitTradeAmt,ProfitDebitCapTradeAmt,ProfitDebitCapNum,ProfitTradeAmt", ProfitDebitTradeAmt + "," + ProfitDebitCapTradeAmt + "," + ProfitDebitCapNum + "," + ProfitTradeAmt, "UserId=" + SubUserId + " and TradeMonth='" + TradeMonth + "' and TradeDate='" + TradeDate + "' and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='team'");
  256. }
  257. }
  258. }
  259. }
  260. pageNum += 1;
  261. }
  262. else
  263. {
  264. check = false;
  265. }
  266. CustomerSqlConn.op(sql, AppConfig.Base.SqlConn);
  267. }
  268. }
  269. catch (Exception ex)
  270. {
  271. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计交易额异常");
  272. }
  273. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "统计交易额日志");
  274. }
  275. public string InsertOrUpdate(string table, string fields, string vals, string condition)
  276. {
  277. string sql = "";
  278. string check = RedisDbconn.Instance.Get<string>("statreset:" + table + ":" + function.MD5_16(condition));
  279. if(string.IsNullOrEmpty(check))
  280. {
  281. RedisDbconn.Instance.Set("statreset:" + table + ":" + function.MD5_16(condition), fields + "#cut#" + vals);
  282. RedisDbconn.Instance.SetExpire("statreset:" + table + ":" + function.MD5_16(condition), 3600);
  283. string[] conditionList = condition.Split(new string[]{ " and " }, StringSplitOptions.None);
  284. foreach(string sub in conditionList)
  285. {
  286. string[] subCondition = sub.Split('=');
  287. fields += "," + subCondition[0];
  288. vals += "," + subCondition[1];
  289. }
  290. sql = "insert into " + table + " (" + fields + ") values (" + vals + ");\n";
  291. }
  292. else
  293. {
  294. string setVals = "";
  295. string[] fieldList = fields.Split(',');
  296. string[] valList = vals.Split(',');
  297. for (int i = 0; i < fieldList.Length; i++)
  298. {
  299. setVals += fieldList[i] + "=" + fieldList[i] + "+" + valList[i] + ",";
  300. }
  301. sql = "update " + table + " set " + setVals.TrimEnd(',') + " where " + condition + ";\n";
  302. }
  303. return sql;
  304. }
  305. }
  306. }