TkLogCore.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  10. using System.Net;
  11. using System.Security.Cryptography;
  12. using Spire.Pdf.Exporting.XPS.Schema;
  13. using System.Xml.Linq;
  14. using static QRCoder.PayloadGenerator;
  15. using TencentCloud.Ssl.V20191205.Models;
  16. using CSRedis;
  17. namespace molilian.core
  18. {
  19. public class TkLogCore
  20. {
  21. static string queue_tb_key = "queue:logs:tb";
  22. static string queue_jd_key = "queue:logs:jd";
  23. static string queue_parse_tb_key = "queue:parse_logs:tb";
  24. static string queue_parse_jd_key = "queue:parse_logs:jd";
  25. static string queue_parse_dy_key = "queue:parse_logs:dy";
  26. static string queue_parse_tool_key = "queue:parse_logs:tool";
  27. static string queue_coupon_key = "queue:coupon_logs";
  28. public static int BatchInsertLogDB(int limit)
  29. {
  30. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  31. {
  32. if (!node.is_public_api) return 0;
  33. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  34. return BatchInsertLogDB(limit, redis);
  35. });
  36. return result.Sum();
  37. }
  38. public static int BatchInsertLogDB(int limit, CSRedisClient redis)
  39. {
  40. int total = 0;
  41. using var connection = DBContext.GetOpenConnection();
  42. connection.Open();
  43. using var transaction = connection.BeginTransaction();
  44. try
  45. {
  46. for (int i = 0; i < limit; i++)
  47. {
  48. var data = redis.LPop<TkDataDTO>(queue_tb_key);
  49. if (data == null) break;
  50. if ("127.0.0.1".Equals(data.ip) || data.elapsedTime > 2000)
  51. {
  52. new DBContext.Table(connection, "tk_logs_test")
  53. .Add("end_point", data.end_point)
  54. .Add("channel", (int)data.channel)
  55. .Add("accountId", data.accountId)
  56. .Add("accountName", data.accountName)
  57. .Add("rawContent", data.rawContent)
  58. .Add("rawContent2", data.rawContent2)
  59. .Add("success", data.success)
  60. .Add("message", data.message)
  61. .Add("reason", data.reason)
  62. .Add("content", data.content)
  63. .Add("couponAmount", data.couponAmount)
  64. .Add("itemId", data.itemId)
  65. .Add("itemName", data.itemName)
  66. .Add("pic", data.pic)
  67. .Add("promotionPrice", data.promotionPrice)
  68. .Add("taoToken", data.taoToken)
  69. .Add("shortLinkurl", data.shortLinkurl)
  70. .Add("deeplink_url", data.deeplink_url)
  71. .Add("num_iid", data.num_iid)
  72. .Add("elapsedTime", data.elapsedTime)
  73. .Add("ip", data.ip)
  74. .Add("oaid", data.oaid)
  75. .Add("create_time", data.create_time)
  76. .Create(DBContext.InsertType.NORMAL, transaction);
  77. }
  78. else
  79. {
  80. data.id = new DBContext.Table(connection, "tk_logs")
  81. .Add("end_point", data.end_point)
  82. .Add("channel", (int)data.channel)
  83. .Add("accountId", data.accountId)
  84. .Add("accountName", data.accountName)
  85. .Add("rawContent", data.rawContent)
  86. .Add("rawContent2", data.rawContent2)
  87. .Add("success", data.success)
  88. .Add("message", data.message)
  89. .Add("reason", data.reason)
  90. .Add("content", data.content)
  91. .Add("couponAmount", data.couponAmount)
  92. .Add("itemId", data.itemId)
  93. .Add("itemName", data.itemName)
  94. .Add("pic", data.pic)
  95. .Add("promotionPrice", data.promotionPrice)
  96. .Add("taoToken", data.taoToken)
  97. .Add("shortLinkurl", data.shortLinkurl)
  98. .Add("deeplink_url", data.deeplink_url)
  99. .Add("num_iid", data.num_iid)
  100. .Add("elapsedTime", data.elapsedTime)
  101. .Add("ip", data.ip)
  102. .Add("oaid", data.oaid)
  103. .Add("create_time", data.create_time)
  104. .Create(DBContext.InsertType.NORMAL, transaction);
  105. if (data.success)
  106. {
  107. new DBContext.Table(connection, "tk_success_logs")
  108. .Add("end_point", data.end_point)
  109. .Add("channel", (int)data.channel)
  110. .Add("accountId", data.accountId)
  111. .Add("accountName", data.accountName)
  112. .Add("rawContent", data.rawContent)
  113. .Add("rawContent2", data.rawContent2)
  114. .Add("success", data.success)
  115. .Add("message", data.message)
  116. .Add("reason", data.reason)
  117. .Add("content", data.content)
  118. .Add("couponAmount", data.couponAmount)
  119. .Add("itemId", data.itemId)
  120. .Add("itemName", data.itemName)
  121. .Add("pic", data.pic)
  122. .Add("promotionPrice", data.promotionPrice)
  123. .Add("taoToken", data.taoToken)
  124. .Add("shortLinkurl", data.shortLinkurl)
  125. .Add("deeplink_url", data.deeplink_url)
  126. .Add("num_iid", data.num_iid)
  127. .Add("elapsedTime", data.elapsedTime)
  128. .Add("ip", data.ip)
  129. .Add("oaid", data.oaid)
  130. .Add("create_time", data.create_time)
  131. .Create(DBContext.InsertType.NORMAL, transaction);
  132. }
  133. if (!string.IsNullOrEmpty(data.itemId)) TkOrderTrackingCore.SaveLinkSummary(data);
  134. }
  135. total++;
  136. }
  137. for (int i = 0; i < limit; i++)
  138. {
  139. var data = redis.LPop<JdDataDTO>(queue_jd_key);
  140. if (data == null) break;
  141. new DBContext.Table(connection, "tk_logs")
  142. .Add("end_point", data.end_point)
  143. .Add("channel", (int)data.channel)
  144. .Add("accountId", data.accountId)
  145. .Add("accountName", data.accountName)
  146. .Add("rawContent", data.rawContent)
  147. .Add("rawContent2", data.rawContent2)
  148. .Add("success", data.success)
  149. .Add("message", data.message)
  150. .Add("reason", data.reason)
  151. .Add("shortLinkurl", data.shortLinkurl)
  152. .Add("deeplink_url", data.deeplink_url)
  153. .Add("elapsedTime", data.elapsedTime)
  154. .Add("ip", data.ip)
  155. .Add("oaid", data.oaid)
  156. .Add("create_time", data.create_time)
  157. .Create(DBContext.InsertType.NORMAL, transaction);
  158. total++;
  159. }
  160. for (int i = 0; i < limit; i++)
  161. {
  162. var data = redis.LPop<TkDataDTO>(queue_parse_tb_key);
  163. if (data == null) break;
  164. if ("127.0.0.1".Equals(data.ip) || data.elapsedTime > 2000)
  165. {
  166. new DBContext.Table(connection, "tk_parse_logs_test")
  167. .Add("end_point", data.end_point)
  168. .Add("channel", (int)data.channel)
  169. .Add("accountId", data.accountId)
  170. .Add("accountName", data.accountName)
  171. .Add("rawContent", data.rawContent)
  172. .Add("success", data.success)
  173. .Add("message", data.message)
  174. .Add("reason", data.reason)
  175. .Add("content", data.content)
  176. .Add("itemId", data.itemId)
  177. .Add("itemName", data.itemName)
  178. .Add("pic", data.pic)
  179. .Add("couponAmount", data.couponAmount)
  180. .Add("promotionPrice", data.promotionPrice)
  181. .Add("taoToken", data.taoToken)
  182. .Add("shortLinkurl", data.shortLinkurl)
  183. .Add("deeplink_url", data.deeplink_url)
  184. .Add("num_iid", data.num_iid)
  185. .Add("elapsedTime", data.elapsedTime)
  186. .Add("elapsedTime2", data.elapsedTime2)
  187. .Add("elapsedTime3", data.elapsedTime3)
  188. .Add("ip", data.ip)
  189. .Add("oaid", data.oaid)
  190. .Add("create_time", data.create_time)
  191. .Create(DBContext.InsertType.NORMAL, transaction);
  192. }
  193. else
  194. {
  195. data.id = new DBContext.Table(connection, "tk_parse_logs")
  196. .Add("end_point", data.end_point)
  197. .Add("channel", (int)data.channel)
  198. .Add("accountId", data.accountId)
  199. .Add("accountName", data.accountName)
  200. .Add("rawContent", data.rawContent)
  201. .Add("success", data.success)
  202. .Add("message", data.message)
  203. .Add("reason", data.reason)
  204. .Add("content", data.content)
  205. .Add("itemId", data.itemId)
  206. .Add("itemName", data.itemName)
  207. .Add("pic", data.pic)
  208. .Add("couponAmount", data.couponAmount)
  209. .Add("promotionPrice", data.promotionPrice)
  210. .Add("taoToken", data.taoToken)
  211. .Add("shortLinkurl", data.shortLinkurl)
  212. .Add("deeplink_url", data.deeplink_url)
  213. .Add("num_iid", data.num_iid)
  214. .Add("elapsedTime", data.elapsedTime)
  215. .Add("elapsedTime2", data.elapsedTime2)
  216. .Add("elapsedTime3", data.elapsedTime3)
  217. .Add("ip", data.ip)
  218. .Add("oaid", data.oaid)
  219. .Add("create_time", data.create_time)
  220. .Create(DBContext.InsertType.NORMAL, transaction);
  221. if (data.success)
  222. {
  223. new DBContext.Table(connection, "tk_success_parse_logs")
  224. .Add("end_point", data.end_point)
  225. .Add("channel", (int)data.channel)
  226. .Add("accountId", data.accountId)
  227. .Add("accountName", data.accountName)
  228. .Add("rawContent", data.rawContent)
  229. .Add("success", data.success)
  230. .Add("message", data.message)
  231. .Add("reason", data.reason)
  232. .Add("content", data.content)
  233. .Add("itemId", data.itemId)
  234. .Add("itemName", data.itemName)
  235. .Add("pic", data.pic)
  236. .Add("couponAmount", data.couponAmount)
  237. .Add("promotionPrice", data.promotionPrice)
  238. .Add("taoToken", data.taoToken)
  239. .Add("shortLinkurl", data.shortLinkurl)
  240. .Add("deeplink_url", data.deeplink_url)
  241. .Add("num_iid", data.num_iid)
  242. .Add("elapsedTime", data.elapsedTime)
  243. .Add("elapsedTime2", data.elapsedTime2)
  244. .Add("elapsedTime3", data.elapsedTime3)
  245. .Add("ip", data.ip)
  246. .Add("oaid", data.oaid)
  247. .Add("create_time", data.create_time)
  248. .Create(DBContext.InsertType.NORMAL, transaction);
  249. }
  250. if (!string.IsNullOrEmpty(data.itemId)) TkOrderTrackingCore.SaveLinkSummary(data);
  251. }
  252. total++;
  253. }
  254. for (int i = 0; i < limit; i++)
  255. {
  256. var data = redis.LPop<JdDataDTO>(queue_parse_jd_key);
  257. if (data == null) break;
  258. new DBContext.Table(connection, "tk_parse_logs")
  259. .Add("end_point", data.end_point)
  260. .Add("channel", (int)data.channel)
  261. .Add("accountId", data.accountId)
  262. .Add("accountName", data.accountName)
  263. .Add("rawContent", data.rawContent)
  264. .Add("success", data.success)
  265. .Add("message", data.message)
  266. .Add("reason", data.reason)
  267. .Add("content", data.content)
  268. .Add("itemId", data.itemId)
  269. .Add("itemName", data.itemName)
  270. .Add("pic", data.pic)
  271. .Add("couponAmount", data.couponAmount)
  272. .Add("promotionPrice", data.promotionPrice)
  273. .Add("taoToken", data.taoToken)
  274. .Add("shortLinkurl", data.shortLinkurl)
  275. .Add("deeplink_url", data.deeplink_url)
  276. .Add("elapsedTime", data.elapsedTime)
  277. .Add("ip", data.ip)
  278. .Add("oaid", data.oaid)
  279. .Add("create_time", data.create_time)
  280. .Create(DBContext.InsertType.NORMAL, transaction);
  281. total++;
  282. }
  283. for (int i = 0; i < limit; i++)
  284. {
  285. var data = redis.LPop<DyDataDTO>(queue_parse_dy_key);
  286. if (data == null) break;
  287. new DBContext.Table(connection, "tk_parse_logs")
  288. .Add("end_point", data.end_point)
  289. .Add("channel", (int)data.channel)
  290. .Add("accountId", data.accountId)
  291. .Add("accountName", data.accountName)
  292. .Add("rawContent", data.rawContent)
  293. .Add("success", data.success)
  294. .Add("message", data.message)
  295. .Add("reason", data.reason)
  296. .Add("content", data.content)
  297. .Add("itemId", data.itemId)
  298. .Add("itemName", data.itemName)
  299. .Add("pic", data.pic)
  300. .Add("couponAmount", data.couponAmount)
  301. .Add("promotionPrice", data.promotionPrice)
  302. .Add("taoToken", data.taoToken)
  303. .Add("shortLinkurl", data.shortLinkurl)
  304. .Add("deeplink_url", data.deeplink_url)
  305. .Add("elapsedTime", data.elapsedTime)
  306. .Add("ip", data.ip)
  307. .Add("oaid", data.oaid)
  308. .Add("create_time", data.create_time)
  309. .Create(DBContext.InsertType.NORMAL, transaction);
  310. total++;
  311. }
  312. for (int i = 0; i < limit; i++)
  313. {
  314. var data = redis.LPop<ToolParseDataDTO>(queue_parse_tool_key);
  315. if (data == null) break;
  316. new DBContext.Table(connection, "tool_parse_logs")
  317. .Add("end_point", data.end_point)
  318. .Add("channel", (int)data.channel)
  319. .Add("rawContent", data.rawContent)
  320. .Add("success", data.success)
  321. .Add("message", data.message)
  322. .Add("reason", data.reason)
  323. .Add("content", data.content)
  324. .Add("taoToken", data.taoToken)
  325. .Add("shortLinkurl", data.shortLinkurl)
  326. .Add("deeplink_url", data.deeplink_url)
  327. .Add("elapsedTime", data.elapsedTime)
  328. .Add("ip", data.ip)
  329. .Add("oaid", data.oaid)
  330. .Add("create_time", data.create_time)
  331. .Create(DBContext.InsertType.NORMAL, transaction);
  332. total++;
  333. }
  334. for (int i = 0; i < limit; i++)
  335. {
  336. var data = redis.LPop<UnionCouponDTO>(queue_coupon_key);
  337. if (data == null) break;
  338. connection.Insert(data);
  339. total++;
  340. }
  341. transaction.Commit();
  342. }
  343. catch (Exception ex)
  344. {
  345. transaction.Rollback();
  346. _ = new LoggerLibrary("database_error", "parse_log")
  347. .Info(ex.Message, ex.StackTrace)
  348. .SaveAsync();
  349. NotifyCore.Notify(new NifyMessage
  350. {
  351. message = $"【写入日志异常】\n{ex.Message}\n{ex.StackTrace}",
  352. priority = NifyMessagePriority.high,
  353. tags = ["red_circle"]
  354. });
  355. }
  356. finally
  357. {
  358. connection.Close();
  359. }
  360. return total;
  361. }
  362. public static async Task LogAsync(JdDataDTO response)
  363. {
  364. try
  365. {
  366. var ts = DateTime.Now - response.create_time;
  367. response.elapsedTime = (int)ts.TotalMilliseconds;
  368. _ = RedisHelper.RPushAsync(queue_jd_key, response);
  369. saveCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  370. }
  371. catch (Exception ex) { }
  372. }
  373. public static async Task LogAsync(TkDataDTO response, AlimamaPlus? alimamaPlus = null)
  374. {
  375. try
  376. {
  377. var ts = DateTime.Now - response.create_time;
  378. response.elapsedTime = (int)ts.TotalMilliseconds;
  379. _ = RedisHelper.RPushAsync(queue_tb_key, response);
  380. saveCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  381. if (!response.success && "nologin".Equals(response.message))
  382. {
  383. switch (response.channel)
  384. {
  385. case TkChannelEnum.tb:
  386. await Task.Run(() =>
  387. {
  388. if (alimamaPlus != null)
  389. {
  390. (bool success, string message) = alimamaPlus.RenewCookie();
  391. if (success) return;
  392. }
  393. TkPoolCore.Disabled(response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  394. });
  395. break;
  396. }
  397. }
  398. if ("没有匹配账号".Equals(response.reason))
  399. {
  400. TkPoolCore.AccountExhausted();
  401. }
  402. }
  403. catch (Exception ex)
  404. {
  405. _ = new LoggerLibrary("unionParse", "database_error")
  406. .Info(response.rawContent, response.rawContent2)
  407. .Info(response.Convert2Json())
  408. .Info(ex.Message, ex.StackTrace)
  409. .SaveAsync();
  410. }
  411. }
  412. public static async Task ParseLogAsync(TkDataDTO response, AlimamaPlus? alimamaPlus = null)
  413. {
  414. #if DEBUG
  415. //return;
  416. #endif
  417. try
  418. {
  419. var ts = DateTime.Now - response.create_time;
  420. response.elapsedTime = (int)ts.TotalMilliseconds;
  421. _ = RedisHelper.RPushAsync(queue_parse_tb_key, response);
  422. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  423. if (!string.IsNullOrEmpty(response.itemId)) TkOrderTrackingCore.SaveLinkSummary(response);
  424. if (!response.success && "nologin".Equals(response.message))
  425. {
  426. switch (response.channel)
  427. {
  428. case TkChannelEnum.tb:
  429. await Task.Run(() =>
  430. {
  431. if (alimamaPlus != null)
  432. {
  433. (bool success, string message) = alimamaPlus.RenewCookie();
  434. if (success) return;
  435. }
  436. TkPoolCore.Disabled(response.accountName, $"{response.rawContent}");
  437. }); break;
  438. }
  439. }
  440. }
  441. catch (Exception ex)
  442. {
  443. _ = new LoggerLibrary("unionParse", "database_error")
  444. .Info(response.rawContent)
  445. .Info(response.Convert2Json())
  446. .Info(ex.Message, ex.StackTrace)
  447. .SaveAsync();
  448. }
  449. }
  450. public static async Task CouponLogAsync(UnionCouponDTO response, AlimamaPlus? alimamaPlus = null)
  451. {
  452. try
  453. {
  454. var ts = DateTime.Now - response.create_time;
  455. response.elapsedTime = (int)ts.TotalMilliseconds;
  456. _ = RedisHelper.RPushAsync(queue_coupon_key, response);
  457. saveCouponCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  458. if (!response.success && "nologin".Equals(response.message))
  459. {
  460. switch (response.channel)
  461. {
  462. case TkChannelEnum.tb:
  463. await Task.Run(() =>
  464. {
  465. if (alimamaPlus != null)
  466. {
  467. (bool success, string message) = alimamaPlus.RenewCookie();
  468. if (success) return;
  469. }
  470. TkPoolCore.Disabled(response.accountName, $"{response.rawContent}");
  471. }); break;
  472. }
  473. }
  474. }
  475. catch (Exception ex)
  476. {
  477. _ = new LoggerLibrary("unionCoupon", "database_error")
  478. .Info(response.rawContent)
  479. .Info(response.Convert2Json())
  480. .Info(ex.Message, ex.StackTrace)
  481. .SaveAsync();
  482. }
  483. }
  484. public static async Task ParseLogAsync(JdDataDTO response)
  485. {
  486. try
  487. {
  488. var ts = DateTime.Now - response.create_time;
  489. response.elapsedTime = (int)ts.TotalMilliseconds;
  490. _ = RedisHelper.RPushAsync(queue_parse_jd_key, response);
  491. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  492. }
  493. catch (Exception ex) { }
  494. }
  495. public static async Task ParseLogAsync(DyDataDTO response)
  496. {
  497. try
  498. {
  499. var ts = DateTime.Now - response.create_time;
  500. response.elapsedTime = (int)ts.TotalMilliseconds;
  501. _ = RedisHelper.RPushAsync(queue_parse_dy_key, response);
  502. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  503. }
  504. catch (Exception ex) { }
  505. }
  506. public static async Task ParseLogAsync(ToolParseDataDTO response)
  507. {
  508. try
  509. {
  510. var ts = DateTime.Now - response.create_time;
  511. response.elapsedTime = (int)ts.TotalMilliseconds;
  512. _ = RedisHelper.RPushAsync(queue_parse_tool_key, response);
  513. saveParseCache(response.channel.ToString(), 0, "tool", response.success, response.message, response.reason);
  514. }
  515. catch (Exception ex) { }
  516. }
  517. private static void saveCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  518. {
  519. saveAccountCache("all", success, message, reason);
  520. saveAccountCache($"{channel}", success, message, reason);
  521. saveAccountCache($"{accountName}", success, message, reason);
  522. if (accountId != 0)
  523. {
  524. //todo 放着跑两天,要将读取的地方改成读取accountid
  525. saveAccountCache($"{channel}_{accountId}", success, message, reason);
  526. }
  527. }
  528. private static void saveAccountCache(string accountName, bool success, string message, string reason)
  529. {
  530. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMM}");
  531. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMdd}");
  532. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  533. string result = success ? "success" : "fail";
  534. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  535. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  536. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  537. if (!string.IsNullOrEmpty(message))
  538. {
  539. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  540. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  541. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  542. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  543. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  544. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  545. }
  546. if (!string.IsNullOrEmpty(reason))
  547. {
  548. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  549. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  550. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  551. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  552. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  553. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  554. }
  555. }
  556. private static void saveParseCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  557. {
  558. saveParseAccountCache("all", success, message, reason);
  559. saveParseAccountCache($"{channel}", success, message, reason);
  560. saveParseAccountCache($"{accountName}", success, message, reason);
  561. if (accountId != 0)
  562. {
  563. //todo 放着跑两天,要将读取的地方改成读取accountid
  564. saveParseAccountCache($"{channel}_{accountId}", success, message, reason);
  565. }
  566. }
  567. private static void saveParseAccountCache(string accountName, bool success, string message, string reason)
  568. {
  569. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMM}");
  570. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  571. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  572. string result = success ? "success" : "fail";
  573. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  574. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  575. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  576. if (!string.IsNullOrEmpty(message))
  577. {
  578. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  579. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  580. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  581. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  582. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  583. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  584. }
  585. if (!string.IsNullOrEmpty(reason))
  586. {
  587. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  588. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  589. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  590. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  591. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  592. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  593. }
  594. }
  595. private static void saveCouponCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  596. {
  597. saveAccountCouponCache("all", success, message, reason);
  598. saveAccountCouponCache($"{channel}", success, message, reason);
  599. if (accountId != 0)
  600. {
  601. //todo 放着跑两天,要将读取的地方改成读取accountid
  602. saveAccountCouponCache($"{channel}_{accountId}", success, message, reason);
  603. }
  604. }
  605. private static void saveAccountCouponCache(string accountName, bool success, string message, string reason)
  606. {
  607. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMM}");
  608. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  609. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  610. string result = success ? "success" : "fail";
  611. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  612. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  613. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  614. if (!string.IsNullOrEmpty(message))
  615. {
  616. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  617. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  618. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  619. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  620. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  621. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  622. }
  623. if (!string.IsNullOrEmpty(reason))
  624. {
  625. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  626. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  627. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  628. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  629. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  630. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  631. }
  632. }
  633. public static int GetTotal(string keyname, bool all_node = true)
  634. {
  635. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  636. {
  637. if (!node.is_public_api) return 0;
  638. #if DEBUG
  639. switch (node.name)
  640. {
  641. case "bj":
  642. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  643. break;
  644. case "gz":
  645. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  646. break;
  647. }
  648. #endif
  649. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  650. return redis.Get<int>(keyname);
  651. });
  652. return result.Sum();
  653. }
  654. public static string[] GetTotalKeys(string keyname, bool all_node = true)
  655. {
  656. var result = EndPointCore.ProcessEndPointNodes<string[]>(node =>
  657. {
  658. if (!node.is_public_api) return [];
  659. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  660. string[] message_keys = redis.SMembers(keyname);
  661. return message_keys;
  662. });
  663. string[] message_keys = [];
  664. foreach (var arr in result)
  665. {
  666. message_keys = message_keys.Union(arr).ToArray();
  667. }
  668. return message_keys;
  669. }
  670. //if (item.total_count > 0) continue;
  671. //item.total_count = RedisHelper.Get<int>($":total:all:{item.report_date:yyyyMMdd}");
  672. //if (item.total_count == 0) continue;
  673. //item.success_count = RedisHelper.Get<int>($":total:all:success:{item.report_date:yyyyMMdd}");
  674. //item.abandon_count = RedisHelper.Get<int>($":total:all:放弃转链:{item.report_date:yyyyMMdd}");
  675. }
  676. }