StatTradeService.cs 22 KB

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