StatTradeCheckService.cs 24 KB

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