TkLogCore.cs 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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. using COSXML.Network;
  21. namespace molilian.core
  22. {
  23. public class TkLogCore
  24. {
  25. static string queue_tb_key = "queue:logs:tb";
  26. static string queue_jd_key = "queue:logs:jd";
  27. static string queue_parse_tb_key = "queue:parse_logs:tb";
  28. static string queue_parse_jd_key = "queue:parse_logs:jd";
  29. static string queue_parse_pdd_key = "queue:parse_logs:pdd";
  30. static string queue_parse_dy_key = "queue:parse_logs:dy";
  31. static string queue_parse_tool_key = "queue:parse_logs:tool";
  32. static string queue_coupon_key = "queue:coupon_logs";
  33. static string promotion_img_key = "queue:promotion:img";
  34. static string queue_cps_key = "queue:cps_logs";
  35. public static bool save_dailys_log = false;
  36. static TkLogCore()
  37. {
  38. int flag = RedisHelper.Get<int>("turn:save_dailys_log");
  39. if (flag == 1) save_dailys_log = true;
  40. }
  41. public static int BatchInsertLogDB(int limit)
  42. {
  43. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  44. {
  45. if (!node.is_public_api) return 0;
  46. if (CenterHub.IsCenter)
  47. {
  48. if (node.is_coupon_api) return 0;
  49. }
  50. else
  51. {
  52. if (!node.is_coupon_api) return 0;
  53. }
  54. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  55. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  56. return BatchInsertLogDB(limit, redis);
  57. });
  58. return result.Sum();
  59. }
  60. private static int save_tk_log(TkDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  61. {
  62. return new DBContext.Table(connection, tablename)
  63. .Add("end_point", data.end_point)
  64. .Add("channel", (int)data.channel)
  65. .Add("accountId", data.accountId)
  66. .Add("accountName", data.accountName)
  67. .Add("rawContent", data.rawContent)
  68. .Add("rawContent2", data.rawContent2)
  69. .Add("success", data.success)
  70. .Add("message", data.message)
  71. .Add("reason", data.reason)
  72. .Add("content", data.content)
  73. .Add("couponAmount", data.couponAmount)
  74. .Add("itemId", data.itemId)
  75. .Add("itemName", data.itemName)
  76. .Add("pic", data.pic)
  77. .Add("promotionPrice", data.promotionPrice)
  78. .Add("taoToken", data.taoToken)
  79. .Add("shortLinkurl", data.shortLinkurl)
  80. .Add("deeplink_url", data.deeplink_url)
  81. .Add("num_iid", data.num_iid)
  82. .Add("elapsedTime", data.elapsedTime)
  83. .Add("subCode", data.subCode)
  84. .Add("ip", data.ip)
  85. .Add("oaid", data.oaid)
  86. .Add("create_time", data.create_time)
  87. .Create(DBContext.InsertType.NORMAL, transaction);
  88. }
  89. private static int save_tk_parse_logs(TkDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  90. {
  91. return new DBContext.Table(connection, tablename)
  92. .Add("end_point", data.end_point)
  93. .Add("channel", (int)data.channel)
  94. .Add("linkType", (int)data.link_type)
  95. .Add("accountId", data.accountId)
  96. .Add("accountName", data.accountName)
  97. .Add("rawContent", data.rawContent)
  98. .Add("success", data.success)
  99. .Add("message", data.message)
  100. .Add("reason", data.reason)
  101. .Add("content", data.content)
  102. .Add("itemId", data.itemId)
  103. .Add("itemName", data.itemName)
  104. .Add("pic", data.pic)
  105. .Add("couponAmount", data.couponAmount)
  106. .Add("promotionPrice", data.promotionPrice)
  107. .Add("taoToken", data.taoToken)
  108. .Add("shortLinkurl", data.shortLinkurl)
  109. .Add("deeplink_url", data.deeplink_url)
  110. .Add("num_iid", data.num_iid)
  111. .Add("elapsedTime", data.elapsedTime)
  112. .Add("elapsedTime2", data.elapsedTime2)
  113. .Add("elapsedTime3", data.elapsedTime3)
  114. .Add("subCode", data.subCode)
  115. .Add("ip", data.ip)
  116. .Add("oaid", data.oaid)
  117. .Add("create_time", data.create_time)
  118. .Create(DBContext.InsertType.NORMAL, transaction);
  119. }
  120. private static int save_jd_parse_logs(JdDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  121. {
  122. return new DBContext.Table(connection, tablename)
  123. .Add("end_point", data.end_point)
  124. .Add("channel", (int)data.channel)
  125. .Add("accountId", data.accountId)
  126. .Add("accountName", data.accountName)
  127. .Add("rawContent", data.rawContent)
  128. .Add("success", data.success)
  129. .Add("message", data.message)
  130. .Add("reason", data.reason)
  131. .Add("content", data.content)
  132. .Add("itemId", data.itemId)
  133. .Add("itemName", data.itemName)
  134. .Add("pic", data.pic)
  135. .Add("couponAmount", data.couponAmount)
  136. .Add("promotionPrice", data.promotionPrice)
  137. .Add("taoToken", data.taoToken)
  138. .Add("shortLinkurl", data.shortLinkurl)
  139. .Add("deeplink_url", data.deeplink_url)
  140. .Add("elapsedTime", data.elapsedTime)
  141. .Add("subCode", data.subCode)
  142. .Add("ip", data.ip)
  143. .Add("oaid", data.oaid)
  144. .Add("create_time", data.create_time)
  145. .Create(DBContext.InsertType.NORMAL, transaction);
  146. }
  147. private static int save_pdd_parse_logs(PddDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  148. {
  149. return new DBContext.Table(connection, tablename)
  150. .Add("end_point", data.end_point)
  151. .Add("channel", (int)data.channel)
  152. .Add("accountId", data.accountId)
  153. .Add("accountName", data.accountName)
  154. .Add("rawContent", data.rawContent)
  155. .Add("success", data.success)
  156. .Add("message", data.message)
  157. .Add("reason", data.reason)
  158. .Add("content", data.content)
  159. .Add("itemId", data.itemId)
  160. .Add("itemName", data.itemName)
  161. .Add("pic", data.pic)
  162. .Add("couponAmount", data.couponAmount)
  163. .Add("promotionPrice", data.promotionPrice)
  164. .Add("taoToken", data.taoToken)
  165. .Add("shortLinkurl", data.shortLinkurl)
  166. .Add("deeplink_url", data.deeplink_url)
  167. .Add("elapsedTime", data.elapsedTime)
  168. .Add("subCode", data.subCode)
  169. .Add("ip", data.ip)
  170. .Add("oaid", data.oaid)
  171. .Add("create_time", data.create_time)
  172. .Create(DBContext.InsertType.NORMAL, transaction);
  173. }
  174. public static int BatchInsertLogDB(int limit, CSRedisClient redis)
  175. {
  176. int total = 0;
  177. using var connection = DBContext.GetOpenConnection();
  178. connection.Open();
  179. using var transaction = connection.BeginTransaction();
  180. string cacheKey = ":lock_key:start_comparison_tk";
  181. bool start_comparison_tk = RedisHelper.Get<int>(cacheKey) > 0;
  182. try
  183. {
  184. for (int i = 0; i < limit; i++)
  185. {
  186. var data = redis.LPop<TkDataDTO>(queue_tb_key);
  187. if (data == null) break;
  188. if ("3B191CFA4C6B48F9BA459E915B57743BEC7D424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  189. data.ip.Contains("127.0.0"))
  190. {
  191. save_tk_log(data, "tk_logs_test", connection, transaction);
  192. }
  193. else
  194. {
  195. data.id = save_tk_log(data, "tk_logs", connection, transaction);
  196. if (data.elapsedTime > 1000)
  197. {
  198. save_tk_log(data, "tk_logs_test", connection, transaction);
  199. }
  200. if (data.success)
  201. {
  202. save_tk_log(data, "tk_success_logs", connection, transaction);
  203. }
  204. if (!string.IsNullOrEmpty(data.itemId)) TkOrderTrackingCore.SaveLinkSummary(data);
  205. }
  206. total++;
  207. }
  208. for (int i = 0; i < limit; i++)
  209. {
  210. var data = redis.LPop<JdDataDTO>(queue_jd_key);
  211. if (data == null) break;
  212. new DBContext.Table(connection, "tk_logs")
  213. .Add("end_point", data.end_point)
  214. .Add("channel", (int)data.channel)
  215. .Add("accountId", data.accountId)
  216. .Add("accountName", data.accountName)
  217. .Add("rawContent", data.rawContent)
  218. .Add("rawContent2", data.rawContent2)
  219. .Add("success", data.success)
  220. .Add("message", data.message)
  221. .Add("reason", data.reason)
  222. .Add("shortLinkurl", data.shortLinkurl)
  223. .Add("deeplink_url", data.deeplink_url)
  224. .Add("elapsedTime", data.elapsedTime)
  225. .Add("ip", data.ip)
  226. .Add("oaid", data.oaid)
  227. .Add("create_time", data.create_time)
  228. .Create(DBContext.InsertType.NORMAL, transaction);
  229. total++;
  230. }
  231. for (int i = 0; i < limit; i++)
  232. {
  233. var data = redis.LPop<TkDataDTO>(queue_parse_tb_key);
  234. if (data == null) break;
  235. if ("3B191CFA4C6B48F9BA459E915B57743BEC7D424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  236. data.ip.Contains("127.0.0"))
  237. {
  238. save_tk_parse_logs(data, "tk_parse_logs_test", connection, transaction);
  239. }
  240. else
  241. {
  242. data.id = save_tk_parse_logs(data, "tk_parse_logs", connection, transaction);
  243. //每日分表
  244. if (save_dailys_log)
  245. {
  246. string daily_table = $"tk_parse_logs_{DateTime.Now:yyyyMMdd}";
  247. save_tk_parse_logs(data, daily_table, connection, transaction);
  248. }
  249. if (data.elapsedTime > 1000)
  250. {
  251. save_tk_parse_logs(data, "tk_parse_logs_test", connection, transaction);
  252. }
  253. if (data.success)
  254. {
  255. save_tk_parse_logs(data, "tk_success_parse_logs", connection, transaction);
  256. }
  257. if (!string.IsNullOrEmpty(data.itemId)) TkOrderTrackingCore.SaveLinkSummary(data);
  258. }
  259. if (data.reason.Contains("初步筛选2") && !data.rawContent.Contains("I:/kWqN5t623Hx"))
  260. {
  261. save_tk_parse_logs(data, "tk_parse_logs_test2", connection, transaction);
  262. }
  263. if (data.reason.Contains("初步筛选1.5"))
  264. {
  265. save_tk_parse_logs(data, "tk_parse_logs_multi_token", connection, transaction);
  266. }
  267. if (data.reason.Contains("霸下验证码"))
  268. {
  269. save_tk_parse_logs(data, "tk_parse_logs_captcha", connection, transaction);
  270. }
  271. total++;
  272. }
  273. for (int i = 0; i < limit; i++)
  274. {
  275. var data = redis.LPop<JdDataDTO>(queue_parse_jd_key);
  276. if (data == null) break;
  277. if ("3B191CFA4C6B48F9BA459E915B57743BEC7D424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  278. data.ip.Contains("127.0.0"))
  279. {
  280. save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
  281. }
  282. else
  283. {
  284. save_jd_parse_logs(data, "jd_parse_logs", connection, transaction);
  285. if (save_dailys_log)
  286. {
  287. string daily_table = $"jd_parse_logs_{DateTime.Now:yyyyMMdd}";
  288. save_jd_parse_logs(data, daily_table, connection, transaction);
  289. }
  290. if (data.elapsedTime > 1000)
  291. {
  292. save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
  293. }
  294. if (data.success)
  295. {
  296. save_jd_parse_logs(data, "jd_parse_logs_success", connection, transaction);
  297. }
  298. }
  299. total++;
  300. }
  301. for (int i = 0; i < limit; i++)
  302. {
  303. var data = redis.LPop<PddDataDTO>(queue_parse_pdd_key);
  304. if (data == null) break;
  305. if ("3B191CFA4C6B48F9BA459E915B57743BEC7D424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  306. data.ip.Contains("127.0.0"))
  307. {
  308. save_pdd_parse_logs(data, "pdd_parse_logs_test", connection, transaction);
  309. }
  310. else
  311. {
  312. save_pdd_parse_logs(data, "pdd_parse_logs", connection, transaction);
  313. if (data.elapsedTime > 1000)
  314. {
  315. save_pdd_parse_logs(data, "pdd_parse_logs_test", connection, transaction);
  316. }
  317. if (data.success)
  318. {
  319. save_pdd_parse_logs(data, "pdd_parse_logs_success", connection, transaction);
  320. }
  321. }
  322. total++;
  323. }
  324. for (int i = 0; i < limit; i++)
  325. {
  326. var data = redis.LPop<DyDataDTO>(queue_parse_dy_key);
  327. if (data == null) break;
  328. new DBContext.Table(connection, "tk_parse_logs")
  329. .Add("end_point", data.end_point)
  330. .Add("channel", (int)data.channel)
  331. .Add("accountId", data.accountId)
  332. .Add("accountName", data.accountName)
  333. .Add("rawContent", data.rawContent)
  334. .Add("success", data.success)
  335. .Add("message", data.message)
  336. .Add("reason", data.reason)
  337. .Add("content", data.content)
  338. .Add("itemId", data.itemId)
  339. .Add("itemName", data.itemName)
  340. .Add("pic", data.pic)
  341. .Add("couponAmount", data.couponAmount)
  342. .Add("promotionPrice", data.promotionPrice)
  343. .Add("taoToken", data.taoToken)
  344. .Add("shortLinkurl", data.shortLinkurl)
  345. .Add("deeplink_url", data.deeplink_url)
  346. .Add("elapsedTime", data.elapsedTime)
  347. .Add("subCode", data.subCode)
  348. .Add("ip", data.ip)
  349. .Add("oaid", data.oaid)
  350. .Add("create_time", data.create_time)
  351. .Create(DBContext.InsertType.NORMAL, transaction);
  352. total++;
  353. }
  354. for (int i = 0; i < limit; i++)
  355. {
  356. var data = redis.LPop<ToolParseDataDTO>(queue_parse_tool_key);
  357. if (data == null) break;
  358. if ("3B191CFA4C6B48F9BA459E915B57743BEC7D424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  359. data.ip.Contains("127.0.0"))
  360. {
  361. new DBContext.Table(connection, "tool_parse_logs_test")
  362. .Add("end_point", data.end_point)
  363. .Add("channel", (int)data.channel)
  364. .Add("rawContent", data.rawContent)
  365. .Add("success", data.success)
  366. .Add("message", data.message)
  367. .Add("reason", data.reason)
  368. .Add("content", data.content)
  369. .Add("taoToken", data.taoToken)
  370. .Add("shortLinkurl", data.shortLinkurl)
  371. .Add("deeplink_url", data.deeplink_url)
  372. .Add("elapsedTime", data.elapsedTime)
  373. .Add("ip", data.ip)
  374. .Add("oaid", data.oaid)
  375. .Add("create_time", data.create_time)
  376. .Create(DBContext.InsertType.NORMAL, transaction);
  377. }
  378. else
  379. {
  380. new DBContext.Table(connection, "tool_parse_logs")
  381. .Add("end_point", data.end_point)
  382. .Add("channel", (int)data.channel)
  383. .Add("rawContent", data.rawContent)
  384. .Add("success", data.success)
  385. .Add("message", data.message)
  386. .Add("reason", data.reason)
  387. .Add("content", data.content)
  388. .Add("taoToken", data.taoToken)
  389. .Add("shortLinkurl", data.shortLinkurl)
  390. .Add("deeplink_url", data.deeplink_url)
  391. .Add("elapsedTime", data.elapsedTime)
  392. .Add("ip", data.ip)
  393. .Add("oaid", data.oaid)
  394. .Add("create_time", data.create_time)
  395. .Create(DBContext.InsertType.NORMAL, transaction);
  396. }
  397. total++;
  398. }
  399. for (int i = 0; i < limit; i++)
  400. {
  401. var data = redis.LPop<UnionCouponDTO>(queue_coupon_key);
  402. if (data == null) break;
  403. if ("3B191CFA4C6B48F9BA459E915B57743BEC7D424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  404. data.ip.Contains("127.0.0"))
  405. {
  406. var test_data = data.Convert2Json().Convert2Object<TestUnionCouponDTO>();
  407. connection.Insert(test_data);
  408. }
  409. else
  410. {
  411. connection.Insert(data);
  412. if (data.success)
  413. {
  414. var success_data = data.Convert2Json().Convert2Object<SuccessUnionCouponDTO>();
  415. connection.Insert(success_data);
  416. }
  417. }
  418. total++;
  419. }
  420. for (int i = 0; i < limit; i++)
  421. {
  422. var data = redis.LPop<UnionCpsDTO>(queue_cps_key);
  423. if (data == null) break;
  424. if ("3B191CFA4C6B48F9BA459E915B57743BEC7D424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  425. data.ip.Contains("127.0.0"))
  426. {
  427. var test_data = data.Convert2Json().Convert2Object<TestUnionCpsDTO>();
  428. connection.Insert(test_data);
  429. }
  430. else
  431. {
  432. connection.Insert(data);
  433. if (data.success)
  434. {
  435. var success_data = data.Convert2Json().Convert2Object<SuccessUnionCpsDTO>();
  436. connection.Insert(success_data);
  437. }
  438. }
  439. total++;
  440. }
  441. for (int i = 0; i < limit; i++)
  442. {
  443. var data = redis.LPop<PromotionQueryDTO>(promotion_img_key);
  444. if (data == null) break;
  445. if (data.success)
  446. {
  447. data.similarPromotion = string.Empty;
  448. data.promotionImg = string.Empty;
  449. }
  450. connection.Insert(data);
  451. total++;
  452. }
  453. transaction.Commit();
  454. }
  455. catch (Exception ex)
  456. {
  457. transaction.Rollback();
  458. _ = new LoggerLibrary("database_error", "parse_log")
  459. .Info(ex.Message, ex.StackTrace)
  460. .SaveAsync();
  461. NotifyCore.Notify(new NifyMessage
  462. {
  463. message = $"【写入日志异常】\n{ex.Message}\n{ex.StackTrace}",
  464. priority = NifyMessagePriority.high,
  465. tags = ["red_circle"]
  466. });
  467. }
  468. finally
  469. {
  470. connection.Close();
  471. }
  472. return total;
  473. }
  474. public static async Task PromotionImgLogAsync(PromotionQueryDTO response)
  475. {
  476. try
  477. {
  478. var ts = DateTime.Now - response.create_time;
  479. response.elapsedTime = (int)ts.TotalMilliseconds;
  480. _ = RedisHelper.RPushAsync(promotion_img_key, response);
  481. //using var connection = DBContext.GetOpenConnection();
  482. //connection.Insert(response);
  483. savePromotionCache(response.accountId, response.accountName, response.success, response.message, response.reason);
  484. }
  485. catch (Exception ex) { }
  486. }
  487. public static async Task LogAsync(JdDataDTO response)
  488. {
  489. try
  490. {
  491. var ts = DateTime.Now - response.create_time;
  492. response.elapsedTime = (int)ts.TotalMilliseconds;
  493. _ = RedisHelper.RPushAsync(queue_jd_key, response);
  494. if (!response.ip.Contains("127.0.0"))
  495. {
  496. saveCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  497. }
  498. }
  499. catch (Exception ex) { }
  500. }
  501. public static async Task LogAsync(TkDataDTO response, AlimamaPlus? alimamaPlus = null)
  502. {
  503. try
  504. {
  505. var ts = DateTime.Now - response.create_time;
  506. response.elapsedTime = (int)ts.TotalMilliseconds;
  507. _ = RedisHelper.RPushAsync(queue_tb_key, response);
  508. if (!response.ip.Contains("127.0.0"))
  509. {
  510. saveCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  511. }
  512. if (!response.success && "nologin".Equals(response.message))
  513. {
  514. switch (response.channel)
  515. {
  516. case TkChannelEnum.tb:
  517. await Task.Run(() =>
  518. {
  519. if (alimamaPlus != null)
  520. {
  521. (bool success, string message) = alimamaPlus.RenewCookie();
  522. if (success) return;
  523. }
  524. TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  525. });
  526. break;
  527. }
  528. }
  529. if ("没有匹配账号".Equals(response.reason))
  530. {
  531. TkPoolCore.AccountExhausted();
  532. }
  533. }
  534. catch (Exception ex)
  535. {
  536. _ = new LoggerLibrary("unionParse", "database_error")
  537. .Info(response.rawContent, response.rawContent2)
  538. .Info(response.Convert2Json())
  539. .Info(ex.Message, ex.StackTrace)
  540. .SaveAsync();
  541. }
  542. }
  543. public static async Task ParseLogAsync(TkDataDTO response, AlimamaPlus? alimamaPlus = null)
  544. {
  545. #if DEBUG
  546. //return;
  547. #endif
  548. try
  549. {
  550. var ts = DateTime.Now - response.create_time;
  551. response.elapsedTime = (int)ts.TotalMilliseconds;
  552. _ = RedisHelper.RPushAsync(queue_parse_tb_key, response);
  553. if (response.success)
  554. {
  555. _ = saveUnionCouponParseCacheAsync(response);
  556. }
  557. if (!response.ip.Contains("127.0.0"))
  558. {
  559. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  560. }
  561. if (!string.IsNullOrEmpty(response.itemId)) TkOrderTrackingCore.SaveLinkSummary(response);
  562. if (!response.success && "nologin".Equals(response.message))
  563. {
  564. switch (response.channel)
  565. {
  566. case TkChannelEnum.tb:
  567. await Task.Run(() =>
  568. {
  569. if (alimamaPlus != null)
  570. {
  571. (bool success, string message) = alimamaPlus.RenewCookie();
  572. if (success) return;
  573. }
  574. TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}");
  575. }); break;
  576. }
  577. }
  578. if (response.subCode == TkSubCodeEnum.Captcha || "霸下验证码".Equals(response.reason))
  579. {
  580. TkPoolCore.Suspend(response.end_point, response.accountId, response.accountName, "霸下验证码");
  581. }
  582. }
  583. catch (Exception ex)
  584. {
  585. _ = new LoggerLibrary("unionParse", "database_error")
  586. .Info(response.rawContent)
  587. .Info(response.Convert2Json())
  588. .Info(ex.Message, ex.StackTrace)
  589. .SaveAsync();
  590. }
  591. }
  592. public static async Task CouponLogAsync(UnionCouponDTO response, AlimamaPlus? alimamaPlus = null)
  593. {
  594. try
  595. {
  596. var ts = DateTime.Now - response.create_time;
  597. response.elapsedTime = (int)ts.TotalMilliseconds;
  598. _ = RedisHelper.RPushAsync(queue_coupon_key, response);
  599. if (!response.ip.Contains("127.0.0"))
  600. {
  601. saveCouponCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  602. }
  603. if (!response.success && "nologin".Equals(response.message))
  604. {
  605. switch (response.channel)
  606. {
  607. case TkChannelEnum.tb:
  608. await Task.Run(() =>
  609. {
  610. if (alimamaPlus != null)
  611. {
  612. (bool success, string message) = alimamaPlus.RenewCookie();
  613. if (success) return;
  614. }
  615. TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}");
  616. }); break;
  617. }
  618. }
  619. }
  620. catch (Exception ex)
  621. {
  622. _ = new LoggerLibrary("unionCoupon", "database_error")
  623. .Info(response.rawContent)
  624. .Info(response.Convert2Json())
  625. .Info(ex.Message, ex.StackTrace)
  626. .SaveAsync();
  627. }
  628. }
  629. public static async Task CpsLogAsync(UnionCpsDTO response, AlimamaPlus? alimamaPlus = null)
  630. {
  631. try
  632. {
  633. var ts = DateTime.Now - response.create_time;
  634. response.elapsedTime = (int)ts.TotalMilliseconds;
  635. _ = RedisHelper.RPushAsync(queue_cps_key, response);
  636. if (response.success) saveClientRequestTotal(response.channel, response.ip, response.oaid);
  637. if (!response.ip.Contains("127.0.0"))
  638. {
  639. saveCpsCache(response.channel.ToString(), response.accountId, response.success, response.message, response.reason);
  640. }
  641. }
  642. catch (Exception ex)
  643. {
  644. _ = new LoggerLibrary("unionCps", "database_error")
  645. .Info(response.rawContent)
  646. .Info(response.Convert2Json())
  647. .Info(ex.Message, ex.StackTrace)
  648. .SaveAsync();
  649. }
  650. }
  651. public static async Task ParseLogAsync(JdDataDTO response)
  652. {
  653. try
  654. {
  655. var ts = DateTime.Now - response.create_time;
  656. response.elapsedTime = (int)ts.TotalMilliseconds;
  657. _ = RedisHelper.RPushAsync(queue_parse_jd_key, response);
  658. if (!response.ip.Contains("127.0.0"))
  659. {
  660. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  661. }
  662. if (response.success || response.message.Equals("转链失败"))
  663. {
  664. JdPoolCore.CallsIncrBy(response.accountId);
  665. }
  666. //CallsIncrBy
  667. if (response.success) saveClientRequestTotal(response.channel, response.ip, response.oaid);
  668. if (!response.success && ("nologin".Equals(response.reason) ||
  669. "方法不存在".Equals(response.reason) ||
  670. "未登录".Equals(response.reason)))
  671. {
  672. await Task.Run(() =>
  673. {
  674. JdPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  675. });
  676. //switch (response.channel)
  677. //{
  678. // case TkChannelEnum.tb:
  679. // //await Task.Run(() =>
  680. // //{
  681. // // if (alimamaPlus != null)
  682. // // {
  683. // // (bool success, string message) = alimamaPlus.RenewCookie();
  684. // // if (success) return;
  685. // // }
  686. // // TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  687. // //});
  688. // break;
  689. //}
  690. }
  691. if ("没有匹配账号".Equals(response.reason))
  692. {
  693. JdPoolCore.AccountExhausted();
  694. }
  695. }
  696. catch (Exception ex) { }
  697. }
  698. public static async Task ParseLogAsync(PddDataDTO response)
  699. {
  700. try
  701. {
  702. var ts = DateTime.Now - response.create_time;
  703. response.elapsedTime = (int)ts.TotalMilliseconds;
  704. _ = RedisHelper.RPushAsync(queue_parse_pdd_key, response);
  705. #if DEBUG
  706. for (int i = 0; i < 100; i++)
  707. {
  708. var data = RedisHelper.LPop<PddDataDTO>(queue_parse_pdd_key);
  709. if (data == null) break;
  710. if (data.ip.Contains("127.0.0"))
  711. {
  712. save_pdd_parse_logs(data, "pdd_parse_logs_test", null, null);
  713. }
  714. else
  715. {
  716. save_pdd_parse_logs(data, "pdd_parse_logs", null, null);
  717. if (data.elapsedTime > 1000)
  718. {
  719. save_pdd_parse_logs(data, "pdd_parse_logs_test", null, null);
  720. }
  721. if (data.success)
  722. {
  723. save_pdd_parse_logs(data, "pdd_parse_logs_success", null, null);
  724. }
  725. }
  726. }
  727. #endif
  728. if (!response.ip.Contains("127.0.0"))
  729. {
  730. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  731. }
  732. if (response.success || response.message.Equals("转链失败"))
  733. {
  734. PddPoolCore.CallsIncrBy(response.accountId);
  735. }
  736. if (response.reason.Equals("您的调用次数过高"))
  737. {
  738. PddPoolCore.TempSuspend(response.accountId);
  739. }
  740. if (response.success) saveClientRequestTotal(response.channel, response.ip, response.oaid);
  741. if (!response.success && ("nologin".Equals(response.reason) ||
  742. "方法不存在".Equals(response.reason) ||
  743. "未登录".Equals(response.reason)))
  744. {
  745. await Task.Run(() =>
  746. {
  747. PddPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  748. });
  749. }
  750. if ("没有匹配账号".Equals(response.reason))
  751. {
  752. PddPoolCore.AccountExhausted();
  753. }
  754. }
  755. catch (Exception ex) { }
  756. }
  757. public static async Task ParseLogAsync(DyDataDTO response)
  758. {
  759. try
  760. {
  761. var ts = DateTime.Now - response.create_time;
  762. response.elapsedTime = (int)ts.TotalMilliseconds;
  763. _ = RedisHelper.RPushAsync(queue_parse_dy_key, response);
  764. if (!response.ip.Contains("127.0.0"))
  765. {
  766. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  767. }
  768. }
  769. catch (Exception ex) { }
  770. }
  771. public static async Task ParseLogAsync(ToolParseDataDTO response)
  772. {
  773. try
  774. {
  775. var ts = DateTime.Now - response.create_time;
  776. response.elapsedTime = (int)ts.TotalMilliseconds;
  777. _ = RedisHelper.RPushAsync(queue_parse_tool_key, response);
  778. if (!response.ip.Contains("127.0.0"))
  779. {
  780. saveParseCache(response.channel.ToString(), 0, "tool", response.success, response.message, response.reason);
  781. }
  782. }
  783. catch (Exception ex) { }
  784. }
  785. private static void saveCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  786. {
  787. saveAccountCache("all", success, message, reason);
  788. saveAccountCache($"{channel}", success, message, reason);
  789. saveAccountCache($"{accountName}", success, message, reason);
  790. if (accountId != 0)
  791. {
  792. //todo 放着跑两天,要将读取的地方改成读取accountid
  793. saveAccountCache($"{channel}_{accountId}", success, message, reason);
  794. }
  795. }
  796. private static void saveAccountCache(string accountName, bool success, string message, string reason)
  797. {
  798. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMM}");
  799. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMdd}");
  800. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  801. string result = success ? "success" : "fail";
  802. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  803. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  804. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  805. if (!string.IsNullOrEmpty(message))
  806. {
  807. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  808. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  809. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  810. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  811. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  812. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  813. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  814. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  815. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  816. }
  817. if (!string.IsNullOrEmpty(reason))
  818. {
  819. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  820. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  821. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  822. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  823. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  824. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  825. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  826. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  827. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  828. }
  829. }
  830. private static async Task saveUnionCouponParseCacheAsync(TkDataDTO data)
  831. {
  832. string cacheKey = $":cache:parse:{data.ip}_{data.oaid}_{data.itemId}";
  833. await EndPointCore.ProcessEndPointNodesAsync(node =>
  834. {
  835. if (!node.is_coupon_api) return Task.CompletedTask;
  836. if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  837. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  838. redis.Set(cacheKey, 1, 2 * 86400);
  839. return Task.CompletedTask;
  840. });
  841. }
  842. private static void saveClientRequestTotal(TkChannelEnum channel, string ip, string oaid)
  843. {
  844. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  845. RedisHelper.IncrBy(cacheKey);
  846. RedisHelper.Expire(cacheKey, 86400);
  847. if (!string.IsNullOrEmpty(oaid))
  848. {
  849. cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  850. RedisHelper.IncrBy(cacheKey);
  851. RedisHelper.Expire(cacheKey, 86400);
  852. }
  853. }
  854. public static int getClientRequestTotalByOAID(TkChannelEnum channel, string oaid)
  855. {
  856. if (string.IsNullOrEmpty(oaid)) return 0;
  857. string cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  858. return RedisHelper.Get<int>(cacheKey);
  859. }
  860. public static int getClientRequestTotalByIp(TkChannelEnum channel, string ip)
  861. {
  862. if (string.IsNullOrEmpty(ip)) return 0;
  863. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  864. return RedisHelper.Get<int>(cacheKey);
  865. }
  866. private static void saveClientRequestTotal(CpsChannelEnum channel, string ip, string oaid)
  867. {
  868. string cacheKey = $":cache:cps_{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  869. RedisHelper.IncrBy(cacheKey);
  870. RedisHelper.Expire(cacheKey, 86400);
  871. if (!string.IsNullOrEmpty(oaid))
  872. {
  873. cacheKey = $":cache:cps_{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  874. RedisHelper.IncrBy(cacheKey);
  875. RedisHelper.Expire(cacheKey, 86400);
  876. }
  877. }
  878. public static int getClientRequestTotalByOAID(CpsChannelEnum channel, string oaid)
  879. {
  880. if (string.IsNullOrEmpty(oaid)) return 0;
  881. string cacheKey = $":cache:cps_{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  882. return RedisHelper.Get<int>(cacheKey);
  883. }
  884. public static int getClientRequestTotalByIp(CpsChannelEnum channel, string ip)
  885. {
  886. if (string.IsNullOrEmpty(ip)) return 0;
  887. string cacheKey = $":cache:cps_{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  888. return RedisHelper.Get<int>(cacheKey);
  889. }
  890. private static void saveParseCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  891. {
  892. saveParseAccountCache("all", success, message, reason);
  893. saveParseAccountCache($"{channel}", success, message, reason);
  894. saveParseAccountCache($"{accountName}", success, message, reason);
  895. if (accountId != 0)
  896. {
  897. //todo 放着跑两天,要将读取的地方改成读取accountid
  898. saveParseAccountCache($"{channel}_{accountId}", success, message, reason);
  899. }
  900. }
  901. private static void saveParseAccountCache(string accountName, bool success, string message, string reason)
  902. {
  903. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMM}");
  904. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  905. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  906. string result = success ? "success" : "fail";
  907. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  908. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  909. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  910. if (!string.IsNullOrEmpty(message))
  911. {
  912. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  913. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  914. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  915. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  916. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  917. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  918. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  919. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  920. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  921. }
  922. if (!string.IsNullOrEmpty(reason))
  923. {
  924. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  925. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  926. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  927. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  928. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  929. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  930. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  931. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  932. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  933. }
  934. }
  935. private static void savePromotionCache(int accountId, string accountName, bool success, string message, string reason)
  936. {
  937. savePromotionAccountCache("all", success, message, reason);
  938. savePromotionAccountCache($"{accountId}", success, message, reason);
  939. }
  940. private static void savePromotionAccountCache(string accountName, bool success, string message, string reason)
  941. {
  942. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMM}");
  943. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  944. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  945. string result = success ? "success" : "fail";
  946. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  947. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  948. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  949. if (!string.IsNullOrEmpty(message))
  950. {
  951. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  952. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  953. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  954. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  955. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  956. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  957. RedisHelper.IncrBy($":promotion_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  958. RedisHelper.IncrBy($":promotion_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  959. RedisHelper.IncrBy($":promotion_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  960. }
  961. if (!string.IsNullOrEmpty(reason))
  962. {
  963. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  964. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  965. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  966. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  967. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  968. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  969. RedisHelper.IncrBy($":promotion_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  970. RedisHelper.IncrBy($":promotion_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  971. RedisHelper.IncrBy($":promotion_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  972. }
  973. }
  974. private static void saveCouponCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  975. {
  976. saveAccountCouponCache("all", success, message, reason);
  977. saveAccountCouponCache($"{channel}", success, message, reason);
  978. if (accountId != 0)
  979. {
  980. saveAccountCouponCache($"{channel}_{accountId}", success, message, reason);
  981. }
  982. }
  983. private static void saveAccountCouponCache(string accountName, bool success, string message, string reason)
  984. {
  985. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMM}");
  986. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  987. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  988. string result = success ? "success" : "fail";
  989. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  990. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  991. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  992. if (!string.IsNullOrEmpty(message))
  993. {
  994. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  995. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  996. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  997. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  998. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  999. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  1000. RedisHelper.IncrBy($":coupon_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  1001. RedisHelper.IncrBy($":coupon_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  1002. RedisHelper.IncrBy($":coupon_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  1003. }
  1004. if (!string.IsNullOrEmpty(reason))
  1005. {
  1006. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  1007. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  1008. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  1009. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  1010. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  1011. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  1012. RedisHelper.IncrBy($":coupon_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  1013. RedisHelper.IncrBy($":coupon_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  1014. RedisHelper.IncrBy($":coupon_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  1015. }
  1016. }
  1017. private static void saveCpsCache(string channel, int accountId, bool success, string message, string reason)
  1018. {
  1019. saveAccountCpsCache("all", success, message, reason);
  1020. saveAccountCpsCache($"{channel}", success, message, reason);
  1021. if (accountId != 0)
  1022. {
  1023. saveAccountCpsCache($"{channel}_{accountId}", success, message, reason);
  1024. }
  1025. }
  1026. private static void saveAccountCpsCache(string flagName, bool success, string message, string reason)
  1027. {
  1028. RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMM}");
  1029. RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMMdd}");
  1030. RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMMddHH}");
  1031. string result = success ? "success" : "fail";
  1032. RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMM}");
  1033. RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMMdd}");
  1034. RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMMddHH}");
  1035. if (!string.IsNullOrEmpty(message))
  1036. {
  1037. RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMM}", message);
  1038. RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMMdd}", message);
  1039. RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMMddHH}", message);
  1040. RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMM}");
  1041. RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMMdd}");
  1042. RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMMddHH}");
  1043. RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMM}");
  1044. RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  1045. RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  1046. }
  1047. if (!string.IsNullOrEmpty(reason))
  1048. {
  1049. RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMM}", reason);
  1050. RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  1051. RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  1052. RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMM}");
  1053. RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMMdd}");
  1054. RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  1055. RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  1056. RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  1057. RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  1058. }
  1059. }
  1060. public static int GetTotal(string keyname, bool all_node = true)
  1061. {
  1062. //:coupon_total:tb:20240706
  1063. //:coupon_total:tb:success:20240706
  1064. //:coupon_total:tb:放弃转链:20240706
  1065. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  1066. {
  1067. if (!node.is_public_api) return 0;
  1068. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  1069. if (!all_node)
  1070. {
  1071. if (CenterHub.IsCenter)
  1072. {
  1073. if (node.is_coupon_api) { return 0; }
  1074. }
  1075. else
  1076. {
  1077. if (!node.is_coupon_api) { return 0; }
  1078. }
  1079. }
  1080. #if DEBUG
  1081. switch (node.name)
  1082. {
  1083. case "bj":
  1084. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  1085. break;
  1086. case "gz":
  1087. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  1088. break;
  1089. case "coupon1":
  1090. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  1091. break;
  1092. default: return 0;
  1093. }
  1094. #endif
  1095. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  1096. int count = redis.Get<int>(keyname);
  1097. return count;
  1098. });
  1099. return result.Sum();
  1100. }
  1101. public static string[] GetTotalKeys(string keyname, bool all_node = true)
  1102. {
  1103. var result = EndPointCore.ProcessEndPointNodes<string[]>(node =>
  1104. {
  1105. if (!node.is_public_api) return [];
  1106. if (string.IsNullOrEmpty(node.redis_server)) return [];
  1107. if (!all_node)
  1108. {
  1109. if (CenterHub.IsCenter)
  1110. {
  1111. if (node.is_coupon_api) { return []; }
  1112. }
  1113. else
  1114. {
  1115. if (!node.is_coupon_api) { return []; }
  1116. }
  1117. }
  1118. #if DEBUG
  1119. switch (node.name)
  1120. {
  1121. case "bj":
  1122. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  1123. break;
  1124. case "gz":
  1125. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  1126. break;
  1127. case "coupon1":
  1128. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  1129. break;
  1130. default: return [];
  1131. }
  1132. #endif
  1133. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  1134. string[] message_keys = redis.SMembers(keyname);
  1135. return message_keys;
  1136. });
  1137. string[] message_keys = [];
  1138. foreach (var arr in result)
  1139. {
  1140. message_keys = message_keys.Union(arr).ToArray();
  1141. }
  1142. return message_keys;
  1143. }
  1144. //if (item.total_count > 0) continue;
  1145. //item.total_count = RedisHelper.Get<int>($":total:all:{item.report_date:yyyyMMdd}");
  1146. //if (item.total_count == 0) continue;
  1147. //item.success_count = RedisHelper.Get<int>($":total:all:success:{item.report_date:yyyyMMdd}");
  1148. //item.abandon_count = RedisHelper.Get<int>($":total:all:放弃转链:{item.report_date:yyyyMMdd}");
  1149. }
  1150. }