TkLogCore.cs 37 KB

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