TkLogCore.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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 (CenterHub.IsCenter)
  38. {
  39. if (node.is_coupon_api) return 0;
  40. }
  41. else
  42. {
  43. if (!node.is_coupon_api) return 0;
  44. }
  45. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  46. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  47. return BatchInsertLogDB(limit, redis);
  48. });
  49. return result.Sum();
  50. }
  51. private static int save_tk_log(TkDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  52. {
  53. return new DBContext.Table(connection, tablename)
  54. .Add("end_point", data.end_point)
  55. .Add("channel", (int)data.channel)
  56. .Add("accountId", data.accountId)
  57. .Add("accountName", data.accountName)
  58. .Add("rawContent", data.rawContent)
  59. .Add("rawContent2", data.rawContent2)
  60. .Add("success", data.success)
  61. .Add("message", data.message)
  62. .Add("reason", data.reason)
  63. .Add("content", data.content)
  64. .Add("couponAmount", data.couponAmount)
  65. .Add("itemId", data.itemId)
  66. .Add("itemName", data.itemName)
  67. .Add("pic", data.pic)
  68. .Add("promotionPrice", data.promotionPrice)
  69. .Add("taoToken", data.taoToken)
  70. .Add("shortLinkurl", data.shortLinkurl)
  71. .Add("deeplink_url", data.deeplink_url)
  72. .Add("num_iid", data.num_iid)
  73. .Add("elapsedTime", data.elapsedTime)
  74. .Add("subCode", data.subCode)
  75. .Add("ip", data.ip)
  76. .Add("oaid", data.oaid)
  77. .Add("create_time", data.create_time)
  78. .Create(DBContext.InsertType.NORMAL, transaction);
  79. }
  80. private static int save_tk_parse_logs(TkDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  81. {
  82. return new DBContext.Table(connection, tablename)
  83. .Add("end_point", data.end_point)
  84. .Add("channel", (int)data.channel)
  85. .Add("linkType", (int)data.link_type)
  86. .Add("accountId", data.accountId)
  87. .Add("accountName", data.accountName)
  88. .Add("rawContent", data.rawContent)
  89. .Add("success", data.success)
  90. .Add("message", data.message)
  91. .Add("reason", data.reason)
  92. .Add("content", data.content)
  93. .Add("itemId", data.itemId)
  94. .Add("itemName", data.itemName)
  95. .Add("pic", data.pic)
  96. .Add("couponAmount", data.couponAmount)
  97. .Add("promotionPrice", data.promotionPrice)
  98. .Add("taoToken", data.taoToken)
  99. .Add("shortLinkurl", data.shortLinkurl)
  100. .Add("deeplink_url", data.deeplink_url)
  101. .Add("num_iid", data.num_iid)
  102. .Add("elapsedTime", data.elapsedTime)
  103. .Add("elapsedTime2", data.elapsedTime2)
  104. .Add("elapsedTime3", data.elapsedTime3)
  105. .Add("subCode", data.subCode)
  106. .Add("ip", data.ip)
  107. .Add("oaid", data.oaid)
  108. .Add("create_time", data.create_time)
  109. .Create(DBContext.InsertType.NORMAL, transaction);
  110. }
  111. private static int save_jd_parse_logs(JdDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  112. {
  113. return new DBContext.Table(connection, tablename)
  114. .Add("end_point", data.end_point)
  115. .Add("channel", (int)data.channel)
  116. .Add("accountId", data.accountId)
  117. .Add("accountName", data.accountName)
  118. .Add("rawContent", data.rawContent)
  119. .Add("success", data.success)
  120. .Add("message", data.message)
  121. .Add("reason", data.reason)
  122. .Add("content", data.content)
  123. .Add("itemId", data.itemId)
  124. .Add("itemName", data.itemName)
  125. .Add("pic", data.pic)
  126. .Add("couponAmount", data.couponAmount)
  127. .Add("promotionPrice", data.promotionPrice)
  128. .Add("taoToken", data.taoToken)
  129. .Add("shortLinkurl", data.shortLinkurl)
  130. .Add("deeplink_url", data.deeplink_url)
  131. .Add("elapsedTime", data.elapsedTime)
  132. .Add("subCode", data.subCode)
  133. .Add("ip", data.ip)
  134. .Add("oaid", data.oaid)
  135. .Add("create_time", data.create_time)
  136. .Create(DBContext.InsertType.NORMAL, transaction);
  137. }
  138. public static int BatchInsertLogDB(int limit, CSRedisClient redis)
  139. {
  140. int total = 0;
  141. using var connection = DBContext.GetOpenConnection();
  142. connection.Open();
  143. using var transaction = connection.BeginTransaction();
  144. string cacheKey = ":lock_key:start_comparison_tk";
  145. bool start_comparison_tk = RedisHelper.Get<int>(cacheKey) > 0;
  146. try
  147. {
  148. for (int i = 0; i < limit; i++)
  149. {
  150. var data = redis.LPop<TkDataDTO>(queue_tb_key);
  151. if (data == null) break;
  152. if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  153. data.ip.Contains("127.0.0"))
  154. {
  155. save_tk_log(data, "tk_logs_test", connection, transaction);
  156. }
  157. else
  158. {
  159. data.id = save_tk_log(data, "tk_logs", connection, transaction);
  160. if (data.elapsedTime > 1000)
  161. {
  162. save_tk_log(data, "tk_logs_test", connection, transaction);
  163. }
  164. if (data.success)
  165. {
  166. save_tk_log(data, "tk_success_logs", connection, transaction);
  167. }
  168. if (!string.IsNullOrEmpty(data.itemId)) TkOrderTrackingCore.SaveLinkSummary(data);
  169. }
  170. total++;
  171. }
  172. for (int i = 0; i < limit; i++)
  173. {
  174. var data = redis.LPop<JdDataDTO>(queue_jd_key);
  175. if (data == null) break;
  176. new DBContext.Table(connection, "tk_logs")
  177. .Add("end_point", data.end_point)
  178. .Add("channel", (int)data.channel)
  179. .Add("accountId", data.accountId)
  180. .Add("accountName", data.accountName)
  181. .Add("rawContent", data.rawContent)
  182. .Add("rawContent2", data.rawContent2)
  183. .Add("success", data.success)
  184. .Add("message", data.message)
  185. .Add("reason", data.reason)
  186. .Add("shortLinkurl", data.shortLinkurl)
  187. .Add("deeplink_url", data.deeplink_url)
  188. .Add("elapsedTime", data.elapsedTime)
  189. .Add("ip", data.ip)
  190. .Add("oaid", data.oaid)
  191. .Add("create_time", data.create_time)
  192. .Create(DBContext.InsertType.NORMAL, transaction);
  193. total++;
  194. }
  195. for (int i = 0; i < limit; i++)
  196. {
  197. var data = redis.LPop<TkDataDTO>(queue_parse_tb_key);
  198. if (data == null) break;
  199. if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  200. data.ip.Contains("127.0.0"))
  201. {
  202. save_tk_parse_logs(data, "tk_parse_logs_test", connection, transaction);
  203. }
  204. else
  205. {
  206. data.id = save_tk_parse_logs(data, "tk_parse_logs", connection, transaction);
  207. if (data.elapsedTime > 1000)
  208. {
  209. save_tk_parse_logs(data, "tk_parse_logs_test", connection, transaction);
  210. }
  211. if (data.success)
  212. {
  213. save_tk_parse_logs(data, "tk_success_parse_logs", connection, transaction);
  214. }
  215. if (!string.IsNullOrEmpty(data.itemId)) TkOrderTrackingCore.SaveLinkSummary(data);
  216. }
  217. if (data.reason.Contains("初步筛选2") && !data.rawContent.Contains("I:/kWqN5t623Hx"))
  218. {
  219. save_tk_parse_logs(data, "tk_parse_logs_test2", connection, transaction);
  220. }
  221. if (data.reason.Contains("初步筛选1.5"))
  222. {
  223. save_tk_parse_logs(data, "tk_parse_logs_multi_token", connection, transaction);
  224. }
  225. if (data.reason.Contains("霸下验证码"))
  226. {
  227. save_tk_parse_logs(data, "tk_parse_logs_captcha", connection, transaction);
  228. }
  229. total++;
  230. }
  231. for (int i = 0; i < limit; i++)
  232. {
  233. var data = redis.LPop<JdDataDTO>(queue_parse_jd_key);
  234. if (data == null) break;
  235. if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  236. data.ip.Contains("127.0.0"))
  237. {
  238. save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
  239. }
  240. else
  241. {
  242. save_jd_parse_logs(data, "jd_parse_logs", connection, transaction);
  243. if (data.elapsedTime > 1000)
  244. {
  245. save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
  246. }
  247. if (data.success)
  248. {
  249. save_jd_parse_logs(data, "jd_parse_logs_success", connection, transaction);
  250. }
  251. }
  252. total++;
  253. }
  254. for (int i = 0; i < limit; i++)
  255. {
  256. var data = redis.LPop<DyDataDTO>(queue_parse_dy_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("subCode", data.subCode)
  278. .Add("ip", data.ip)
  279. .Add("oaid", data.oaid)
  280. .Add("create_time", data.create_time)
  281. .Create(DBContext.InsertType.NORMAL, transaction);
  282. total++;
  283. }
  284. for (int i = 0; i < limit; i++)
  285. {
  286. var data = redis.LPop<ToolParseDataDTO>(queue_parse_tool_key);
  287. if (data == null) break;
  288. new DBContext.Table(connection, "tool_parse_logs")
  289. .Add("end_point", data.end_point)
  290. .Add("channel", (int)data.channel)
  291. .Add("rawContent", data.rawContent)
  292. .Add("success", data.success)
  293. .Add("message", data.message)
  294. .Add("reason", data.reason)
  295. .Add("content", data.content)
  296. .Add("taoToken", data.taoToken)
  297. .Add("shortLinkurl", data.shortLinkurl)
  298. .Add("deeplink_url", data.deeplink_url)
  299. .Add("elapsedTime", data.elapsedTime)
  300. .Add("ip", data.ip)
  301. .Add("oaid", data.oaid)
  302. .Add("create_time", data.create_time)
  303. .Create(DBContext.InsertType.NORMAL, transaction);
  304. total++;
  305. }
  306. for (int i = 0; i < limit; i++)
  307. {
  308. var data = redis.LPop<UnionCouponDTO>(queue_coupon_key);
  309. if (data == null) break;
  310. if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  311. data.ip.Contains("127.0.0"))
  312. {
  313. var test_data = data.Convert2Json().Convert2Object<TestUnionCouponDTO>();
  314. connection.Insert(test_data);
  315. }
  316. else
  317. {
  318. connection.Insert(data);
  319. if (data.success)
  320. {
  321. var success_data = data.Convert2Json().Convert2Object<SuccessUnionCouponDTO>();
  322. connection.Insert(success_data);
  323. }
  324. }
  325. total++;
  326. }
  327. for (int i = 0; i < limit; i++)
  328. {
  329. var data = redis.LPop<PromotionQueryDTO>(promotion_img_key);
  330. if (data == null) break;
  331. connection.Insert(data);
  332. total++;
  333. }
  334. transaction.Commit();
  335. }
  336. catch (Exception ex)
  337. {
  338. transaction.Rollback();
  339. _ = new LoggerLibrary("database_error", "parse_log")
  340. .Info(ex.Message, ex.StackTrace)
  341. .SaveAsync();
  342. NotifyCore.Notify(new NifyMessage
  343. {
  344. message = $"【写入日志异常】\n{ex.Message}\n{ex.StackTrace}",
  345. priority = NifyMessagePriority.high,
  346. tags = ["red_circle"]
  347. });
  348. }
  349. finally
  350. {
  351. connection.Close();
  352. }
  353. return total;
  354. }
  355. public static async Task PromotionImgLogAsync(PromotionQueryDTO response)
  356. {
  357. try
  358. {
  359. var ts = DateTime.Now - response.create_time;
  360. response.elapsedTime = (int)ts.TotalMilliseconds;
  361. _ = RedisHelper.RPushAsync(promotion_img_key, response);
  362. //using var connection = DBContext.GetOpenConnection();
  363. //connection.Insert(response);
  364. savePromotionCache(response.accountId, response.accountName, response.success, response.message, response.reason);
  365. }
  366. catch (Exception ex) { }
  367. }
  368. public static async Task LogAsync(JdDataDTO response)
  369. {
  370. try
  371. {
  372. var ts = DateTime.Now - response.create_time;
  373. response.elapsedTime = (int)ts.TotalMilliseconds;
  374. _ = RedisHelper.RPushAsync(queue_jd_key, response);
  375. if (!response.ip.Contains("127.0.0"))
  376. {
  377. saveCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  378. }
  379. }
  380. catch (Exception ex) { }
  381. }
  382. public static async Task LogAsync(TkDataDTO response, AlimamaPlus? alimamaPlus = null)
  383. {
  384. try
  385. {
  386. var ts = DateTime.Now - response.create_time;
  387. response.elapsedTime = (int)ts.TotalMilliseconds;
  388. _ = RedisHelper.RPushAsync(queue_tb_key, response);
  389. if (!response.ip.Contains("127.0.0"))
  390. {
  391. saveCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  392. }
  393. if (!response.success && "nologin".Equals(response.message))
  394. {
  395. switch (response.channel)
  396. {
  397. case TkChannelEnum.tb:
  398. await Task.Run(() =>
  399. {
  400. if (alimamaPlus != null)
  401. {
  402. (bool success, string message) = alimamaPlus.RenewCookie();
  403. if (success) return;
  404. }
  405. TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  406. });
  407. break;
  408. }
  409. }
  410. if ("没有匹配账号".Equals(response.reason))
  411. {
  412. TkPoolCore.AccountExhausted();
  413. }
  414. }
  415. catch (Exception ex)
  416. {
  417. _ = new LoggerLibrary("unionParse", "database_error")
  418. .Info(response.rawContent, response.rawContent2)
  419. .Info(response.Convert2Json())
  420. .Info(ex.Message, ex.StackTrace)
  421. .SaveAsync();
  422. }
  423. }
  424. public static async Task ParseLogAsync(TkDataDTO response, AlimamaPlus? alimamaPlus = null)
  425. {
  426. #if DEBUG
  427. //return;
  428. #endif
  429. try
  430. {
  431. var ts = DateTime.Now - response.create_time;
  432. response.elapsedTime = (int)ts.TotalMilliseconds;
  433. _ = RedisHelper.RPushAsync(queue_parse_tb_key, response);
  434. if (response.success)
  435. {
  436. _ = saveUnionCouponParseCacheAsync(response);
  437. }
  438. if (!response.ip.Contains("127.0.0"))
  439. {
  440. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  441. }
  442. if (!string.IsNullOrEmpty(response.itemId)) TkOrderTrackingCore.SaveLinkSummary(response);
  443. if (!response.success && "nologin".Equals(response.message))
  444. {
  445. switch (response.channel)
  446. {
  447. case TkChannelEnum.tb:
  448. await Task.Run(() =>
  449. {
  450. if (alimamaPlus != null)
  451. {
  452. (bool success, string message) = alimamaPlus.RenewCookie();
  453. if (success) return;
  454. }
  455. TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}");
  456. }); break;
  457. }
  458. }
  459. }
  460. catch (Exception ex)
  461. {
  462. _ = new LoggerLibrary("unionParse", "database_error")
  463. .Info(response.rawContent)
  464. .Info(response.Convert2Json())
  465. .Info(ex.Message, ex.StackTrace)
  466. .SaveAsync();
  467. }
  468. }
  469. public static async Task CouponLogAsync(UnionCouponDTO response, AlimamaPlus? alimamaPlus = null)
  470. {
  471. try
  472. {
  473. var ts = DateTime.Now - response.create_time;
  474. response.elapsedTime = (int)ts.TotalMilliseconds;
  475. _ = RedisHelper.RPushAsync(queue_coupon_key, response);
  476. if (!response.ip.Contains("127.0.0"))
  477. {
  478. saveCouponCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  479. }
  480. if (!response.success && "nologin".Equals(response.message))
  481. {
  482. switch (response.channel)
  483. {
  484. case TkChannelEnum.tb:
  485. await Task.Run(() =>
  486. {
  487. if (alimamaPlus != null)
  488. {
  489. (bool success, string message) = alimamaPlus.RenewCookie();
  490. if (success) return;
  491. }
  492. TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}");
  493. }); break;
  494. }
  495. }
  496. }
  497. catch (Exception ex)
  498. {
  499. _ = new LoggerLibrary("unionCoupon", "database_error")
  500. .Info(response.rawContent)
  501. .Info(response.Convert2Json())
  502. .Info(ex.Message, ex.StackTrace)
  503. .SaveAsync();
  504. }
  505. }
  506. public static async Task ParseLogAsync(JdDataDTO response)
  507. {
  508. try
  509. {
  510. var ts = DateTime.Now - response.create_time;
  511. response.elapsedTime = (int)ts.TotalMilliseconds;
  512. _ = RedisHelper.RPushAsync(queue_parse_jd_key, response);
  513. if (!response.ip.Contains("127.0.0"))
  514. {
  515. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  516. }
  517. if (response.success || response.message.Equals("转链失败"))
  518. {
  519. JdPoolCore.CallsIncrBy(response.accountId);
  520. }
  521. //CallsIncrBy
  522. if (response.success) saveClientRequestTotal(response.channel, response.ip, response.oaid);
  523. if (!response.success && ("nologin".Equals(response.reason) ||
  524. "方法不存在".Equals(response.reason) ||
  525. "未登录".Equals(response.reason)))
  526. {
  527. await Task.Run(() =>
  528. {
  529. JdPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  530. });
  531. //switch (response.channel)
  532. //{
  533. // case TkChannelEnum.tb:
  534. // //await Task.Run(() =>
  535. // //{
  536. // // if (alimamaPlus != null)
  537. // // {
  538. // // (bool success, string message) = alimamaPlus.RenewCookie();
  539. // // if (success) return;
  540. // // }
  541. // // TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  542. // //});
  543. // break;
  544. //}
  545. }
  546. if ("没有匹配账号".Equals(response.reason))
  547. {
  548. JdPoolCore.AccountExhausted();
  549. }
  550. }
  551. catch (Exception ex) { }
  552. }
  553. public static async Task ParseLogAsync(DyDataDTO response)
  554. {
  555. try
  556. {
  557. var ts = DateTime.Now - response.create_time;
  558. response.elapsedTime = (int)ts.TotalMilliseconds;
  559. _ = RedisHelper.RPushAsync(queue_parse_dy_key, response);
  560. if (!response.ip.Contains("127.0.0"))
  561. {
  562. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  563. }
  564. }
  565. catch (Exception ex) { }
  566. }
  567. public static async Task ParseLogAsync(ToolParseDataDTO response)
  568. {
  569. try
  570. {
  571. var ts = DateTime.Now - response.create_time;
  572. response.elapsedTime = (int)ts.TotalMilliseconds;
  573. _ = RedisHelper.RPushAsync(queue_parse_tool_key, response);
  574. if (!response.ip.Contains("127.0.0"))
  575. {
  576. saveParseCache(response.channel.ToString(), 0, "tool", response.success, response.message, response.reason);
  577. }
  578. }
  579. catch (Exception ex) { }
  580. }
  581. private static void saveCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  582. {
  583. saveAccountCache("all", success, message, reason);
  584. saveAccountCache($"{channel}", success, message, reason);
  585. saveAccountCache($"{accountName}", success, message, reason);
  586. if (accountId != 0)
  587. {
  588. //todo 放着跑两天,要将读取的地方改成读取accountid
  589. saveAccountCache($"{channel}_{accountId}", success, message, reason);
  590. }
  591. }
  592. private static void saveAccountCache(string accountName, bool success, string message, string reason)
  593. {
  594. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMM}");
  595. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMdd}");
  596. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  597. string result = success ? "success" : "fail";
  598. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  599. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  600. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  601. if (!string.IsNullOrEmpty(message))
  602. {
  603. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  604. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  605. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  606. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  607. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  608. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  609. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  610. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  611. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  612. }
  613. if (!string.IsNullOrEmpty(reason))
  614. {
  615. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  616. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  617. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  618. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  619. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  620. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  621. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  622. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  623. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  624. }
  625. }
  626. private static async Task saveUnionCouponParseCacheAsync(TkDataDTO data)
  627. {
  628. string cacheKey = $":cache:parse:{data.ip}_{data.oaid}_{data.itemId}";
  629. await EndPointCore.ProcessEndPointNodesAsync(node =>
  630. {
  631. if (!node.is_coupon_api) return Task.CompletedTask;
  632. if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  633. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  634. redis.Set(cacheKey, 1, 2 * 86400);
  635. return Task.CompletedTask;
  636. });
  637. }
  638. private static void saveClientRequestTotal(TkChannelEnum channel, string ip, string oaid)
  639. {
  640. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  641. RedisHelper.IncrBy(cacheKey);
  642. RedisHelper.Expire(cacheKey, 86400);
  643. if (!string.IsNullOrEmpty(oaid))
  644. {
  645. cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  646. RedisHelper.IncrBy(cacheKey);
  647. RedisHelper.Expire(cacheKey, 86400);
  648. }
  649. }
  650. public static int getClientRequestTotalByOAID(TkChannelEnum channel, string oaid)
  651. {
  652. if (string.IsNullOrEmpty(oaid)) return 0;
  653. string cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  654. return RedisHelper.Get<int>(cacheKey);
  655. }
  656. public static int getClientRequestTotalByIp(TkChannelEnum channel, string ip)
  657. {
  658. if (string.IsNullOrEmpty(ip)) return 0;
  659. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  660. return RedisHelper.Get<int>(cacheKey);
  661. }
  662. private static void saveParseCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  663. {
  664. saveParseAccountCache("all", success, message, reason);
  665. saveParseAccountCache($"{channel}", success, message, reason);
  666. saveParseAccountCache($"{accountName}", success, message, reason);
  667. if (accountId != 0)
  668. {
  669. //todo 放着跑两天,要将读取的地方改成读取accountid
  670. saveParseAccountCache($"{channel}_{accountId}", success, message, reason);
  671. }
  672. }
  673. private static void saveParseAccountCache(string accountName, bool success, string message, string reason)
  674. {
  675. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMM}");
  676. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  677. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  678. string result = success ? "success" : "fail";
  679. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  680. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  681. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  682. if (!string.IsNullOrEmpty(message))
  683. {
  684. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  685. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  686. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  687. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  688. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  689. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  690. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  691. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  692. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  693. }
  694. if (!string.IsNullOrEmpty(reason))
  695. {
  696. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  697. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  698. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  699. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  700. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  701. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  702. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  703. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  704. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  705. }
  706. }
  707. private static void savePromotionCache(int accountId, string accountName, bool success, string message, string reason)
  708. {
  709. savePromotionAccountCache("all", success, message, reason);
  710. savePromotionAccountCache($"{accountId}", success, message, reason);
  711. }
  712. private static void savePromotionAccountCache(string accountName, bool success, string message, string reason)
  713. {
  714. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMM}");
  715. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  716. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  717. string result = success ? "success" : "fail";
  718. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  719. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  720. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  721. if (!string.IsNullOrEmpty(message))
  722. {
  723. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  724. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  725. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  726. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  727. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  728. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  729. RedisHelper.IncrBy($":promotion_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  730. RedisHelper.IncrBy($":promotion_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  731. RedisHelper.IncrBy($":promotion_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  732. }
  733. if (!string.IsNullOrEmpty(reason))
  734. {
  735. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  736. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  737. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  738. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  739. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  740. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  741. RedisHelper.IncrBy($":promotion_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  742. RedisHelper.IncrBy($":promotion_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  743. RedisHelper.IncrBy($":promotion_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  744. }
  745. }
  746. private static void saveCouponCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  747. {
  748. saveAccountCouponCache("all", success, message, reason);
  749. saveAccountCouponCache($"{channel}", success, message, reason);
  750. if (accountId != 0)
  751. {
  752. saveAccountCouponCache($"{channel}_{accountId}", success, message, reason);
  753. }
  754. }
  755. private static void saveAccountCouponCache(string accountName, bool success, string message, string reason)
  756. {
  757. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMM}");
  758. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  759. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  760. string result = success ? "success" : "fail";
  761. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  762. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  763. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  764. if (!string.IsNullOrEmpty(message))
  765. {
  766. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  767. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  768. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  769. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  770. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  771. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  772. RedisHelper.IncrBy($":coupon_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  773. RedisHelper.IncrBy($":coupon_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  774. RedisHelper.IncrBy($":coupon_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  775. }
  776. if (!string.IsNullOrEmpty(reason))
  777. {
  778. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  779. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  780. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  781. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  782. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  783. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  784. RedisHelper.IncrBy($":coupon_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  785. RedisHelper.IncrBy($":coupon_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  786. RedisHelper.IncrBy($":coupon_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  787. }
  788. }
  789. public static int GetTotal(string keyname, bool all_node = true)
  790. {
  791. //:coupon_total:tb:20240706
  792. //:coupon_total:tb:success:20240706
  793. //:coupon_total:tb:放弃转链:20240706
  794. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  795. {
  796. if (!node.is_public_api) return 0;
  797. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  798. if (!all_node)
  799. {
  800. if (CenterHub.IsCenter)
  801. {
  802. if (node.is_coupon_api) { return 0; }
  803. }
  804. else
  805. {
  806. if (!node.is_coupon_api) { return 0; }
  807. }
  808. }
  809. #if DEBUG
  810. switch (node.name)
  811. {
  812. case "bj":
  813. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  814. break;
  815. case "gz":
  816. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  817. break;
  818. case "coupon1":
  819. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  820. break;
  821. default: return 0;
  822. }
  823. #endif
  824. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  825. int count = redis.Get<int>(keyname);
  826. return count;
  827. });
  828. return result.Sum();
  829. }
  830. public static string[] GetTotalKeys(string keyname, bool all_node = true)
  831. {
  832. var result = EndPointCore.ProcessEndPointNodes<string[]>(node =>
  833. {
  834. if (!node.is_public_api) return [];
  835. if (string.IsNullOrEmpty(node.redis_server)) return [];
  836. if (!all_node)
  837. {
  838. if (CenterHub.IsCenter)
  839. {
  840. if (node.is_coupon_api) { return []; }
  841. }
  842. else
  843. {
  844. if (!node.is_coupon_api) { return []; }
  845. }
  846. }
  847. #if DEBUG
  848. switch (node.name)
  849. {
  850. case "bj":
  851. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  852. break;
  853. case "gz":
  854. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  855. break;
  856. case "coupon1":
  857. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  858. break;
  859. default: return [];
  860. }
  861. #endif
  862. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  863. string[] message_keys = redis.SMembers(keyname);
  864. return message_keys;
  865. });
  866. string[] message_keys = [];
  867. foreach (var arr in result)
  868. {
  869. message_keys = message_keys.Union(arr).ToArray();
  870. }
  871. return message_keys;
  872. }
  873. //if (item.total_count > 0) continue;
  874. //item.total_count = RedisHelper.Get<int>($":total:all:{item.report_date:yyyyMMdd}");
  875. //if (item.total_count == 0) continue;
  876. //item.success_count = RedisHelper.Get<int>($":total:all:success:{item.report_date:yyyyMMdd}");
  877. //item.abandon_count = RedisHelper.Get<int>($":total:all:放弃转链:{item.report_date:yyyyMMdd}");
  878. }
  879. }