TaskController.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Org.BouncyCastle.Ocsp;
  5. using System.Text.Json;
  6. using System.Runtime.InteropServices;
  7. using System.Net;
  8. using System.Threading.Channels;
  9. using TencentCloud.Cdwch.V20200915.Models;
  10. using COSXML.Network;
  11. using System.Security.Cryptography;
  12. using System.Threading;
  13. using TencentCloud.Batch.V20170312.Models;
  14. using Org.BouncyCastle.Bcpg.OpenPgp;
  15. using Microsoft.VisualBasic;
  16. using static System.Runtime.InteropServices.JavaScript.JSType;
  17. namespace molilian.api.Controllers
  18. {
  19. [ApiController]
  20. [Route("[controller]_70160bd632/[action]")]
  21. public class TaskController : ControllerBase
  22. {
  23. protected IHttpContextAccessor _accessor;
  24. public TaskController(IHttpContextAccessor accessor)
  25. {
  26. _accessor = accessor;
  27. }
  28. [HttpGet]
  29. public ActionResult NotifyNotPushed()
  30. {
  31. PushDataReportCore core = new();
  32. int count = core.NotifyNotPushed();
  33. return new APIResult(new { success = true, count });
  34. }
  35. [HttpGet]
  36. public async Task<ActionResult> FillPushReortData(DateTime date = default)
  37. {
  38. PushDataReportCore core = new();
  39. if (date == default) date = DateTime.Now.AddDays(-1);
  40. int count = await core.FillData(date.Date);
  41. return new APIResult(new { success = true, count });
  42. }
  43. [HttpGet]
  44. public async Task<ActionResult> PushReportData(int repush = 0, DateTime date = default)
  45. {
  46. PushDataReportCore core = new();
  47. if (date == default) date = DateTime.Now.AddDays(-1);
  48. int count = await core.PushReportData(date, repush == 1);
  49. return new APIResult(new { success = true, count });
  50. }
  51. [HttpGet]
  52. public async Task<ActionResult> GetRawItemIdTask(int limit = 100)
  53. {
  54. var list = await TkPoolCore.ListAsync();
  55. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  56. string message = string.Empty;
  57. int total = 0;
  58. foreach (var account in list)
  59. {
  60. if (!account.enable_sync_order) continue;
  61. int accountId = account.id;
  62. DateTime create_time = DateTime.Now.AddDays(-2);
  63. string filter = "accountId=@accountId AND create_time>@create_time AND (itemId IS NULL OR itemId = '')";
  64. var order_list = new DBContext.Table("tk_order_details")
  65. .Where(filter, new { accountId, create_time })
  66. .Order("create_time")
  67. .Limit(limit)
  68. .Select<TkOrderDetailDTO>();
  69. if (!order_list.Any()) continue;
  70. var alimama = new AlimamaPlus(account);
  71. foreach (var item in order_list)
  72. {
  73. string mktId = item.mktId;
  74. //进行订单和转链匹配
  75. (int state, string itemId) = alimama.GetRawItemId(mktId);
  76. if (state == -1) return new APIResult(new { success = false, message = "有账号掉线了", });
  77. if (string.IsNullOrEmpty(itemId)) continue;
  78. item.itemId = itemId;
  79. new DBContext.Table("tk_order_details")
  80. .Add("itemId", itemId)
  81. .Where("id=@id", new { item.id })
  82. .Update();
  83. TkOrderTrackingCore.MatchOrder(account, item);
  84. }
  85. }
  86. return new APIResult(new { success = true, message = "ok", limit, total });
  87. }
  88. [HttpGet]
  89. public async Task<ActionResult> BatchInsertLogDB(int limit = 100)
  90. {
  91. int total = await TkLogCore.BatchInsertLogDBAsync(limit);
  92. return new APIResult(new { success = true, message = "ok", limit, total });
  93. }
  94. [HttpGet]
  95. public async Task<ActionResult> GetAaliyunBlance()
  96. {
  97. AliyunPoolCore core = new AliyunPoolCore();
  98. core.GetBlances();
  99. return new APIResult(new { success = true, message = "ok" });
  100. }
  101. [HttpGet]
  102. public async Task<ActionResult> GetSettleBills(int sleep = 2000)
  103. {
  104. var list = await TkPoolCore.ListAsync();
  105. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  106. string message = string.Empty;
  107. int total = 0;
  108. foreach (var account in list)
  109. {
  110. if (!account.enable_sync_order) continue;
  111. #if DEBUG
  112. if (13 != account.id) continue;
  113. #endif
  114. if (account.is_hide) continue;
  115. try
  116. {
  117. var alimama = new AlimamaPlus(account);
  118. total += await alimama.GetSettleBillsAsync(sleep);
  119. }
  120. catch (Exception ex)
  121. {
  122. message = $"【收益接口异常】\t{account.name}\n{ex.Message}\n{ex.StackTrace}";
  123. NotifyCore.Notify(new NifyMessage
  124. {
  125. message = message,
  126. priority = NifyMessagePriority.high,
  127. tags = ["red_circle"]
  128. });
  129. continue;
  130. }
  131. }
  132. return new APIResult(new { success = true, message = "ok", total });
  133. }
  134. [HttpGet]
  135. public async Task<ActionResult> GetHistoryOrders(int id, int sleep, DateTime startTime, DateTime endTime)
  136. {
  137. var list = await TkPoolCore.ListAsync();
  138. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  139. if (id == 0) return new APIResult(new { success = false, message = "没有有效账号", });
  140. string message = string.Empty;
  141. int total = 0;
  142. foreach (var account in list)
  143. {
  144. if (id != account.id) continue;
  145. try
  146. {
  147. //DateTime endTime = DateTime.Now;
  148. //DateTime startTime = endTime.AddDays(-90);
  149. if (account.id == 3) endTime = DateTime.Parse("2024-04-07");
  150. var alimama = new AlimamaPlus(account);
  151. total = await alimama.GetHistoryOrdersAsync(sleep, startTime, endTime);
  152. }
  153. catch (Exception ex)
  154. {
  155. message = $"【历史订单接口异常】[{account.id}]{account.company}\n{ex.Message}\n{ex.StackTrace}";
  156. NotifyCore.Notify(new NifyMessage
  157. {
  158. message = message,
  159. priority = NifyMessagePriority.high,
  160. tags = ["red_circle"]
  161. });
  162. continue;
  163. }
  164. }
  165. return new APIResult(new { success = true, message = "ok", total });
  166. }
  167. [HttpGet]
  168. public async Task<ActionResult> GetIncyOrders(int sleep = 2000, int retry_days = 0)
  169. {
  170. var list = await TkPoolCore.ListAsync();
  171. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  172. string message = string.Empty;
  173. int total = 0;
  174. foreach (var account in list)
  175. {
  176. if (!account.enable_sync_order) continue;
  177. #if DEBUG
  178. if (account.id != 128) continue;
  179. #endif
  180. try
  181. {
  182. var alimama = new AlimamaPlus(account);
  183. total = await alimama.GetIncyOrdersAsync(sleep, retry_days);
  184. }
  185. catch (Exception ex)
  186. {
  187. message = $"【新增订单接口异常】[{account.id}]{account.company}\n{ex.Message}\n{ex.StackTrace}";
  188. NotifyCore.Notify(new NifyMessage
  189. {
  190. message = message,
  191. priority = NifyMessagePriority.high,
  192. tags = ["red_circle"]
  193. });
  194. continue;
  195. }
  196. }
  197. return new APIResult(new { success = true, message = "ok", total });
  198. }
  199. [HttpGet]
  200. public async Task<ActionResult> GetOrdersByAdZone(int id, long adzoneId, int sleep, DateTime startTime, DateTime endTime)
  201. {
  202. var list = await TkPoolCore.ListAsync();
  203. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  204. string message = string.Empty;
  205. int total = 0;
  206. foreach (var account in list)
  207. {
  208. #if DEBUG
  209. if (account.id != id) continue;
  210. #endif
  211. try
  212. {
  213. var alimama = new AlimamaPlus(account);
  214. total += await alimama.GetOrdersByAdZoneAsync(adzoneId, startTime, endTime, sleep);
  215. }
  216. catch (Exception ex)
  217. {
  218. message = $"【新增订单接口异常adzone】[{account.id}]{account.company}\n{ex.Message}\n{ex.StackTrace}";
  219. NotifyCore.Notify(new NifyMessage
  220. {
  221. message = message,
  222. priority = NifyMessagePriority.high,
  223. tags = ["red_circle"]
  224. });
  225. continue;
  226. }
  227. }
  228. return new APIResult(new { success = true, message = "ok", total });
  229. }
  230. [HttpGet]
  231. public async Task<ActionResult> GetHistoryRefundOrders(int id, int sleep, DateTime startTime, DateTime endTime)
  232. {
  233. var list = await TkPoolCore.ListAsync();
  234. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  235. if (id == 0) return new APIResult(new { success = false, message = "没有有效账号", });
  236. string message = string.Empty;
  237. int total = 0;
  238. foreach (var account in list)
  239. {
  240. if (id != account.id) continue;
  241. try
  242. {
  243. //DateTime endTime = DateTime.Now;
  244. //DateTime startTime = endTime.AddDays(-90);
  245. if (account.id == 3) endTime = DateTime.Parse("2024-04-07");
  246. var alimama = new AlimamaPlus(account);
  247. total += await alimama.GetHistoryRefundOrdersAsync(sleep, startTime, endTime);
  248. }
  249. catch (Exception ex)
  250. {
  251. message = $"【维权订单接口异常】[{account.id}]{account.company}\n{ex.Message}\n{ex.StackTrace}";
  252. NotifyCore.Notify(new NifyMessage
  253. {
  254. message = message,
  255. priority = NifyMessagePriority.high,
  256. tags = ["red_circle"]
  257. });
  258. continue;
  259. }
  260. }
  261. return new APIResult(new { success = true, message = "ok", total });
  262. }
  263. [HttpGet]
  264. public async Task<ActionResult> GetIncyRefundOrders(int sleep = 2000)
  265. {
  266. var list = await TkPoolCore.ListAsync();
  267. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  268. string message = string.Empty;
  269. int total = 0;
  270. foreach (var account in list)
  271. {
  272. #if DEBUG
  273. if (account.id != 17) continue;
  274. #endif
  275. if (!account.enable_sync_order) continue;
  276. try
  277. {
  278. var alimama = new AlimamaPlus(account);
  279. total += await alimama.GetIncyRefundOrdersAsync(sleep);
  280. }
  281. catch (Exception ex)
  282. {
  283. message = $"【维权订单接口异常】[{account.id}]{account.company}\n{ex.Message}\n{ex.StackTrace}";
  284. NotifyCore.Notify(new NifyMessage
  285. {
  286. message = message,
  287. priority = NifyMessagePriority.high,
  288. tags = ["red_circle"]
  289. });
  290. continue;
  291. }
  292. }
  293. return new APIResult(new { success = true, message = "ok", total });
  294. }
  295. [HttpGet]
  296. public async Task<ActionResult> GetViolationuWarning()
  297. {
  298. var list = await TkPoolCore.ListAsync();
  299. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  300. string message = string.Empty;
  301. foreach (var account in list)
  302. {
  303. try
  304. {
  305. var alimama = new AlimamaPlus(account);
  306. alimama.GetViolationuWarning();
  307. }
  308. catch (Exception ex)
  309. {
  310. message = $"【报表接口异常】GetViolationuWarning\n{ex.Message}\n{ex.StackTrace}";
  311. NotifyCore.Notify(new NifyMessage
  312. {
  313. message = message,
  314. priority = NifyMessagePriority.high,
  315. tags = ["red_circle"]
  316. });
  317. continue;
  318. }
  319. }
  320. return new APIResult(new { success = true, message = "ok" });
  321. }
  322. [HttpGet]
  323. public async Task<ActionResult> DailyLogs(int intervalDay = 1)
  324. {
  325. try
  326. {
  327. //本节点统计
  328. await AlimamaPlus.NodeDailyLogsAsync(intervalDay, "parse_", "tb");
  329. await AlimamaPlus.NodeDailyLogsAsync(intervalDay, "coupon_", "tb");
  330. await AlimamaPlus.NodeDailyLogsAsync(intervalDay); //第三方老的统计
  331. if (CenterHub.IsCenter)
  332. {
  333. await JdUnionPlus.DailyLogsAsync(intervalDay);
  334. await ToolParsePlus.DailyLogsAsync(intervalDay);
  335. //中心服务器统计
  336. await AlimamaPlus.AllDailyLogsAsync(intervalDay, "parse_", "tb");
  337. await AlimamaPlus.AllDailyLogsAsync(intervalDay, "coupon_", "tb");
  338. await KsUnionPlus.DailyLogsAsync(intervalDay);
  339. await PddUnionPlus.DailyLogsAsync(intervalDay);
  340. await AlimamaPlus.AllDailyLogsAsync(intervalDay);
  341. }
  342. }
  343. catch (Exception ex)
  344. {
  345. string message = $"【报表接口异常】DailyLogs\n{ex.Message}\n{ex.StackTrace}";
  346. NotifyCore.Notify(new NifyMessage
  347. {
  348. message = message,
  349. priority = NifyMessagePriority.high,
  350. tags = ["red_circle"]
  351. });
  352. }
  353. return new APIResult(new { success = true, message = "ok" });
  354. }
  355. [HttpGet]
  356. public async Task<ActionResult> ApiCallStatistics(int intervalDay = 1, int? chanel = null)
  357. {
  358. try
  359. {
  360. if (chanel == null)
  361. {
  362. #if DEBUG
  363. await AlimamaPlus.ApiCallStatisticsAsync(3, intervalDay, TkChannelEnum.tb);
  364. #else
  365. await AlimamaPlus.ApiCallStatisticsAsync(1, intervalDay, TkChannelEnum.tb);
  366. await AlimamaPlus.ApiCallStatisticsAsync(2, intervalDay, TkChannelEnum.tb);
  367. await AlimamaPlus.ApiCallStatisticsAsync(1, intervalDay, TkChannelEnum.jd);
  368. await AlimamaPlus.ApiCallStatisticsAsync(1, intervalDay, TkChannelEnum.pdd);
  369. #endif
  370. }
  371. else
  372. {
  373. TkChannelEnum _chanel = (TkChannelEnum)chanel;
  374. await AlimamaPlus.ApiCallStatisticsAsync(1, intervalDay, _chanel);
  375. }
  376. }
  377. catch (Exception ex)
  378. {
  379. string message = $"【API报表更新异常】ApiCallStatistics\n{ex.Message}\n{ex.StackTrace}";
  380. NotifyCore.Notify(new NifyMessage
  381. {
  382. message = message,
  383. priority = NifyMessagePriority.high,
  384. tags = ["red_circle"]
  385. });
  386. }
  387. return new APIResult(new { success = true, message = "ok" });
  388. }
  389. [HttpGet]
  390. public async Task<ActionResult> DailyTable(int days = 3)
  391. {
  392. try
  393. {
  394. for (int i = 0; i < days; i++)
  395. {
  396. if (CenterHub.IsCenter)
  397. {
  398. string table_suffix = DateTime.Now.AddDays(i).ToString("yyyyMMdd");
  399. string sql = $"CREATE TABLE IF NOT EXISTS tk_parse_logs_{table_suffix} LIKE tk_parse_logs;";
  400. DBContext.Execute(sql, null);
  401. sql = $"CREATE TABLE IF NOT EXISTS jd_parse_logs_{table_suffix} LIKE jd_parse_logs;";
  402. DBContext.Execute(sql, null);
  403. sql = $"CREATE TABLE IF NOT EXISTS ks_parse_logs_{table_suffix} LIKE ks_parse_logs;";
  404. DBContext.Execute(sql, null);
  405. sql = $"CREATE TABLE IF NOT EXISTS dy_parse_logs_{table_suffix} LIKE dy_parse_logs;";
  406. DBContext.Execute(sql, null);
  407. sql = $"CREATE TABLE IF NOT EXISTS tk_promotion_logs_{table_suffix} LIKE tk_promotion_logs;";
  408. DBContext.Execute(sql, null);
  409. sql = $"CREATE TABLE IF NOT EXISTS deeplink_parse_logs_{table_suffix} LIKE deeplink_parse_logs;";
  410. DBContext.Execute(sql, null);
  411. sql = $"CREATE TABLE IF NOT EXISTS pdd_parse_logs_{table_suffix} LIKE pdd_parse_logs;";
  412. DBContext.Execute(sql, null);
  413. sql = $"CREATE TABLE IF NOT EXISTS tool_parse_logs_{table_suffix} LIKE tool_parse_logs_template;";
  414. DBContext.Execute(sql, null);
  415. sql = $"CREATE TABLE IF NOT EXISTS tk_dp_logs_{table_suffix} LIKE tk_dp_logs;";
  416. DBContext.Execute(sql, null);
  417. sql = $"CREATE TABLE IF NOT EXISTS pdd_dp_logs_{table_suffix} LIKE pdd_dp_logs;";
  418. DBContext.Execute(sql, null);
  419. sql = $"CREATE TABLE IF NOT EXISTS jd_dp_logs_{table_suffix} LIKE jd_dp_logs;";
  420. DBContext.Execute(sql, null);
  421. }
  422. else
  423. {
  424. string table_suffix = DateTime.Now.AddDays(i).ToString("yyyyMMdd");
  425. string sql = $"CREATE TABLE IF NOT EXISTS tk_cps_logs_{table_suffix} LIKE tk_cps_logs;";
  426. DBContext.Execute(sql, null);
  427. sql = $"CREATE TABLE IF NOT EXISTS tk_activity_logs_{table_suffix} LIKE tk_activity_logs;";
  428. DBContext.Execute(sql, null);
  429. }
  430. }
  431. TkLogCore.save_dailys_log = true;
  432. RedisHelper.Set("turn:save_dailys_log", 1, 86400);
  433. }
  434. catch (Exception ex)
  435. {
  436. string message = $"【创建数据标错误】\n{ex.Message}\n{ex.StackTrace}";
  437. NotifyCore.Notify(new NifyMessage
  438. {
  439. message = message,
  440. priority = NifyMessagePriority.high,
  441. tags = ["red_circle"]
  442. });
  443. }
  444. return new APIResult(new { success = true, message = "ok" });
  445. }
  446. //[HttpGet]
  447. //public async Task<ActionResult> ApiCallStatistics(int intervalDay = 1)
  448. //{
  449. // try
  450. // {
  451. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.tb);
  452. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.tb, "parse_");
  453. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.tb, "coupon_");
  454. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.wemeet, "parse_");
  455. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.bdpan, "parse_");
  456. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.dy, "parse_");
  457. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.jd, "parse_");
  458. // }
  459. // catch (Exception ex)
  460. // {
  461. // string message = $"【报表接口异常】ApiCallStatistics\n{ex.Message}\n{ex.StackTrace}";
  462. // NotifyCore.Notify(new NifyMessage
  463. // {
  464. // message = message,
  465. // priority = NifyMessagePriority.high,
  466. // tags = ["red_circle"]
  467. // });
  468. // }
  469. // return new APIResult(new { success = true, message = "ok" });
  470. //}
  471. [HttpGet]
  472. public async Task<ActionResult> GetDrawBalance()
  473. {
  474. var list = await TkPoolCore.ListAsync();
  475. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  476. string message = string.Empty;
  477. foreach (var account in list)
  478. {
  479. if (!account.enable_sync_order) continue;
  480. try
  481. {
  482. var alimama = new AlimamaPlus(account);
  483. alimama.GetDrawBalance();
  484. }
  485. catch (Exception ex)
  486. {
  487. message = $"【报表接口异常】GetDrawBalance\n{ex.Message}\n{ex.StackTrace}";
  488. NotifyCore.Notify(new NifyMessage
  489. {
  490. message = message,
  491. priority = NifyMessagePriority.high,
  492. tags = ["red_circle"]
  493. });
  494. continue;
  495. }
  496. }
  497. return new APIResult(new { success = true, message = "ok" });
  498. }
  499. [HttpGet]
  500. public async Task<ActionResult> LastFastReport(int days = 0)
  501. {
  502. string lockKey = "lock:LastFastReport";
  503. if (!RedisHelper.Set(lockKey, "1", 300))
  504. {
  505. string message = "【LastFastReport】任务正在执行中,跳过本次调用";
  506. _ = new LoggerLibrary("task_lock", "skip")
  507. .Info(message)
  508. .SaveAsync();
  509. return new APIResult(new { success = false, message = "任务正在执行中" });
  510. }
  511. var startTimestamp = DateTime.Now;
  512. try
  513. {
  514. string message = string.Empty;
  515. var list = await TkPoolCore.ListAsync();
  516. foreach (var account in list)
  517. {
  518. #if DEBUG
  519. if (account.id != 113) continue;
  520. #endif
  521. if (!account.enable_sync_order) continue;
  522. try
  523. {
  524. var alimama = new AlimamaPlus(account);
  525. await alimama.SaveReportOverview(days);
  526. }
  527. catch (Exception ex)
  528. {
  529. message = $"【报表接口异常】LastFastReport\n{ex.Message}\n{ex.StackTrace}";
  530. NotifyCore.Notify(new NifyMessage
  531. {
  532. message = message,
  533. priority = NifyMessagePriority.high,
  534. tags = ["red_circle"]
  535. });
  536. continue;
  537. }
  538. }
  539. await EndPointCore.NotifyReload(true);
  540. return new APIResult(new { success = true, message = "ok" });
  541. }
  542. catch (Exception ex)
  543. {
  544. string errorMessage = $"【LastFastReport】执行异常: {ex.Message}";
  545. _ = new LoggerLibrary("task_error", "LastFastReport")
  546. .Info(errorMessage, ex.StackTrace)
  547. .SaveAsync();
  548. NotifyCore.Notify(new NifyMessage
  549. {
  550. message = errorMessage,
  551. priority = NifyMessagePriority.high,
  552. tags = ["red_circle"]
  553. });
  554. return new APIResult(new { success = false, message = errorMessage });
  555. }
  556. finally
  557. {
  558. var executionTime = DateTime.Now - startTimestamp;
  559. string logMessage = $"【LastFastReport】执行完成,耗时: {executionTime.TotalSeconds:F2}秒";
  560. _ = new LoggerLibrary("task_performance", "LastFastReport")
  561. .Info(logMessage)
  562. .SaveAsync();
  563. RedisHelper.Del(lockKey);
  564. }
  565. }
  566. [HttpGet]
  567. public async Task<ActionResult> FastReport(string ignore = "")
  568. {
  569. string lockKey = "lock:FastReport";
  570. if (!RedisHelper.Set(lockKey, "1", 300))
  571. {
  572. string message = "【FastReport】任务正在执行中,跳过本次调用";
  573. _ = new LoggerLibrary("task_lock", "skip")
  574. .Info(message)
  575. .SaveAsync();
  576. return new APIResult(new { success = false, message = "任务正在执行中" });
  577. }
  578. var startTimestamp = DateTime.Now;
  579. try
  580. {
  581. string message = string.Empty;
  582. var list = await TkPoolCore.ListAsync();
  583. if (!ignore.Contains("tb") && list != null)
  584. {
  585. foreach (var account in list)
  586. {
  587. #if DEBUG
  588. if (account.id != 113) continue;
  589. #endif
  590. try
  591. {
  592. account.current_daily_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.tb, account.id, DateTime.Now.ToString("yyyyMMdd"));
  593. account.current_hourly_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.tb, account.id, DateTime.Now.ToString("yyyyMMddHH"));
  594. new DBContext.Table("tk_pool")
  595. .Add("current_hourly_calls", account.current_hourly_calls)
  596. .Add("current_daily_calls", account.current_daily_calls)
  597. .Where("id=@id", new { account.id })
  598. .Update();
  599. }
  600. catch (Exception ex)
  601. {
  602. message = $"【TB计数异常】SaveCalls\n{ex.Message}\n{ex.StackTrace}";
  603. NotifyCore.Notify(new NifyMessage
  604. {
  605. message = message,
  606. priority = NifyMessagePriority.high,
  607. tags = ["red_circle"]
  608. });
  609. continue;
  610. }
  611. if (!account.enable_sync_order) continue;
  612. try
  613. {
  614. var alimama = new AlimamaPlus(account);
  615. await alimama.SaveReportOverview();
  616. }
  617. catch (Exception ex)
  618. {
  619. message = $"【报表接口异常】FastReport\n{ex.Message}\n{ex.StackTrace}";
  620. NotifyCore.Notify(new NifyMessage
  621. {
  622. message = message,
  623. priority = NifyMessagePriority.high,
  624. tags = ["red_circle"]
  625. });
  626. continue;
  627. }
  628. }
  629. }
  630. string filter = "";
  631. #if DEBUG
  632. filter = "id IN (44)";
  633. #endif
  634. var jd_list = new DBContext.Table("jd_pool").Where(filter, null).Select<JdPoolDTO>();
  635. if (!ignore.Contains("jd") && jd_list != null)
  636. {
  637. bool any_jd_changed = false;
  638. foreach (var account in jd_list)
  639. {
  640. bool changed = false;
  641. try
  642. {
  643. account.current_daily_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.jd, account.id, DateTime.Now.ToString("yyyyMMdd"));
  644. account.current_hourly_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.jd, account.id, DateTime.Now.ToString("yyyyMMddHH"));
  645. changed = true;
  646. any_jd_changed = true;
  647. }
  648. catch (Exception ex)
  649. {
  650. message = $"【JD计数异常】SaveCalls\n{ex.Message}\n{ex.StackTrace}";
  651. NotifyCore.Notify(new NifyMessage
  652. {
  653. message = message,
  654. priority = NifyMessagePriority.high,
  655. tags = ["red_circle"]
  656. });
  657. continue;
  658. }
  659. if (!account.status) continue;
  660. string sleepLockKey = $"lock:exception:sleep:jd_{account.id}";
  661. if (!string.IsNullOrEmpty(RedisHelper.Get(sleepLockKey))) continue;
  662. if (changed) JdPoolCore.Update(account);
  663. }
  664. if (any_jd_changed) JdPoolCore.Refresh();
  665. }
  666. var pdd_list = new DBContext.Table("pdd_pool").Where(filter, null).Select<PddPoolDTO>();
  667. if (!ignore.Contains("pdd") && pdd_list != null)
  668. {
  669. bool any_pdd_changed = false;
  670. foreach (var account in pdd_list)
  671. {
  672. bool changed = false;
  673. try
  674. {
  675. account.current_daily_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.pdd, account.id, DateTime.Now.ToString("yyyyMMdd"));
  676. account.current_hourly_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.pdd, account.id, DateTime.Now.ToString("yyyyMMddHH"));
  677. changed = true;
  678. any_pdd_changed = true;
  679. }
  680. catch (Exception ex)
  681. {
  682. message = $"【Pdd计数异常】SaveCalls\n{ex.Message}\n{ex.StackTrace}";
  683. NotifyCore.Notify(new NifyMessage
  684. {
  685. message = message,
  686. priority = NifyMessagePriority.high,
  687. tags = ["red_circle"]
  688. });
  689. continue;
  690. }
  691. if (changed) PddPoolCore.Update(account);
  692. }
  693. if (any_pdd_changed) PddPoolCore.Refresh();
  694. }
  695. var cps_list = new DBContext.Table("cps_links")
  696. .Where("status=@status", new { status = 1 })
  697. .Select<CpsLinksDTO>();
  698. if (!ignore.Contains("cps") && cps_list != null)
  699. {
  700. bool any_cps_changed = false;
  701. foreach (var link in cps_list)
  702. {
  703. bool changed = false;
  704. try
  705. {
  706. link.current_daily_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.cps, link.id, DateTime.Now.ToString("yyyyMMdd"));
  707. link.current_hourly_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.cps, link.id, DateTime.Now.ToString("yyyyMMddHH"));
  708. changed = true;
  709. any_cps_changed = true;
  710. }
  711. catch (Exception ex)
  712. {
  713. message = $"【Cps计数异常】SaveCalls\n{ex.Message}\n{ex.StackTrace}";
  714. NotifyCore.Notify(new NifyMessage
  715. {
  716. message = message,
  717. priority = NifyMessagePriority.high,
  718. tags = ["red_circle"]
  719. });
  720. continue;
  721. }
  722. if (changed) CpsPoolCore.Update(link);
  723. }
  724. if (any_cps_changed) CpsPoolCore.Refresh();
  725. }
  726. await EndPointCore.NotifyReload(true);
  727. return new APIResult(new { success = true, message = "ok" });
  728. }
  729. catch (Exception ex)
  730. {
  731. string errorMessage = $"【FastReport】执行异常: {ex.Message}";
  732. _ = new LoggerLibrary("task_error", "FastReport")
  733. .Info(errorMessage, ex.StackTrace)
  734. .SaveAsync();
  735. NotifyCore.Notify(new NifyMessage
  736. {
  737. message = errorMessage,
  738. priority = NifyMessagePriority.high,
  739. tags = ["red_circle"]
  740. });
  741. return new APIResult(new { success = false, message = errorMessage });
  742. }
  743. finally
  744. {
  745. var executionTime = DateTime.Now - startTimestamp;
  746. string logMessage = $"【FastReport】执行完成,耗时: {executionTime.TotalSeconds:F2}秒";
  747. _ = new LoggerLibrary("task_performance", "FastReport")
  748. .Info(logMessage)
  749. .SaveAsync();
  750. RedisHelper.Del(lockKey);
  751. }
  752. }
  753. [HttpGet]
  754. public async Task<ActionResult> jdQuerySpreadEffectData(string ignore = "")
  755. {
  756. string message = string.Empty;
  757. string filter = "";
  758. #if DEBUG
  759. filter = "id IN (37, 15)";
  760. #endif
  761. var jd_list = new DBContext.Table("jd_pool").Where(filter, null).Select<JdPoolDTO>();
  762. if (!ignore.Contains("jd") && jd_list != null)
  763. {
  764. bool any_jd_changed = false;
  765. foreach (var account in jd_list)
  766. {
  767. bool changed = false;
  768. if (!account.status) continue;
  769. if (!account.enable_sync_report) continue;
  770. string lockKey = $"lock:exception:sleep:jd_{account.id}";
  771. if (!string.IsNullOrEmpty(RedisHelper.Get(lockKey))) continue;
  772. if (!string.IsNullOrEmpty(account.union_cookies))
  773. {
  774. try
  775. {
  776. var plus = new JdUnionPlus(account);
  777. var data = await plus.queryTodaySpreadEffectData();
  778. account.today_clickNum = data.clickNum;
  779. account.today_cosFee = data.cosFee;
  780. account.today_cosPrice = data.cosPrice;
  781. account.today_finishCosFee = data.finishCosFee;
  782. account.today_finishCosPrice = data.finishCosPrice;
  783. account.today_finishOrderNum = data.finishOrderNum;
  784. account.today_orderNum = data.orderNum;
  785. changed = true;
  786. any_jd_changed = true;
  787. }
  788. catch (Exception ex)
  789. {
  790. if (ex.Message.Contains("no login"))
  791. {
  792. account.status = false;
  793. _ = JdPoolCore.DisabledAsync(account.id, account.name, ex.Message);
  794. }
  795. else
  796. {
  797. RedisHelper.Set(lockKey, 1, 3600);
  798. }
  799. message = $"【JD报表接口异常】{account.id}:{account.name}\tqueryTodaySpreadEffectData\n{ex.Message}\n{ex.StackTrace}";
  800. NotifyCore.Notify(message);
  801. continue;
  802. }
  803. }
  804. if (changed) JdPoolCore.Update(account);
  805. }
  806. if (any_jd_changed) JdPoolCore.Refresh();
  807. }
  808. return new APIResult(new { success = true, message = "ok" });
  809. }
  810. [HttpGet]
  811. public async Task<ActionResult> SaveReport()
  812. {
  813. string message = string.Empty;
  814. var list = await TkPoolCore.ListAsync();
  815. if (list != null)
  816. {
  817. foreach (var account in list)
  818. {
  819. if (!account.enable_sync_order) continue;
  820. #if DEBUG
  821. if (account.id != 5) continue;
  822. #endif
  823. if (account.is_hide) continue;
  824. try
  825. {
  826. var alimama = new AlimamaPlus(account);
  827. alimama.SaveReportHourtrend(DateTime.Now.AddDays(-1));
  828. alimama.SaveReportHourtrend(DateTime.Now);
  829. if (DateTime.Now.Hour >= 12 && DateTime.Now.Hour < 16)
  830. {
  831. DateTime date = DateTime.Now.AddDays(-1);
  832. await alimama.FixReport(date);
  833. }
  834. }
  835. catch (Exception ex)
  836. {
  837. message = $"【报表接口异常】SaveReport\n{ex.Message}\n{ex.StackTrace}";
  838. NotifyCore.Notify(new NifyMessage
  839. {
  840. message = message,
  841. priority = NifyMessagePriority.high,
  842. tags = ["red_circle"]
  843. });
  844. continue;
  845. }
  846. }
  847. }
  848. return new APIResult(new { success = true, message = "ok" });
  849. }
  850. [HttpGet]
  851. public async Task<ActionResult> JdSaveReport(int id = 0)
  852. {
  853. string message = string.Empty;
  854. string filter = string.Empty;
  855. if (id > 0) filter = "id=@id";
  856. var jd_list = new DBContext.Table("jd_pool").Where(filter, new { id }).Select<JdPoolDTO>();
  857. if (jd_list != null)
  858. {
  859. DateTime startDate = DateTime.Now.AddDays(-89);
  860. DateTime endDate = DateTime.Now.AddDays(-1);
  861. foreach (var account in jd_list)
  862. {
  863. if (!account.status) continue;
  864. #if DEBUG
  865. startDate = DateTime.Parse("2024-07-31");
  866. endDate = DateTime.Parse("2024-07-31");
  867. if (account.id != 15) continue;
  868. #endif
  869. if (account.is_hide) continue;
  870. if (id == 0 && !account.enable_sync_report) continue;
  871. if (!string.IsNullOrEmpty(account.union_cookies))
  872. {
  873. try
  874. {
  875. var plus = new JdUnionPlus(account);
  876. _ = await plus.querySpreadEffectData(startDate, endDate);
  877. _ = await plus.queryEstimateCommList();
  878. }
  879. catch (Exception ex)
  880. {
  881. if (ex.Message.Contains("no login"))
  882. {
  883. _ = JdPoolCore.DisabledAsync(account.id, account.name, ex.Message);
  884. }
  885. message = $"【JD报表接口异常】{account.id}:{account.name}\tquerySpreadEffectData\n{ex.Message}\n{ex.StackTrace}";
  886. NotifyCore.Notify(message);
  887. continue;
  888. }
  889. }
  890. }
  891. }
  892. return new APIResult(new { success = true, message = "ok" });
  893. }
  894. [HttpGet]
  895. public async Task<ActionResult> PddSaveReport()
  896. {
  897. string message = string.Empty;
  898. var pdd_list = new DBContext.Table("pdd_pool").Where("cookie_status=1", new { }).Select<PddPoolDTO>();
  899. if (pdd_list != null)
  900. {
  901. DateTime startDate = DateTime.Now.AddDays(-91);
  902. DateTime endDate = DateTime.Now.AddDays(-1);
  903. foreach (var account in pdd_list)
  904. {
  905. if (account.is_hide) continue;
  906. if (!account.enable_sync_revenue) continue;
  907. if (!string.IsNullOrEmpty(account.cookies))
  908. {
  909. try
  910. {
  911. var plus = new PddUnionPlus(account);
  912. await plus.queryEstimateRevenueList(startDate, endDate);
  913. }
  914. catch (Exception ex)
  915. {
  916. if (ex.Message.Contains("43001:会话已过期"))
  917. {
  918. PddPoolCore.CookieDisabled(account.id);
  919. }
  920. message = $"【PDD报表接口异常】{account.id}:{account.name}\tqueryEstimateRevenueList\n{ex.Message}\n{ex.StackTrace}";
  921. NotifyCore.Notify(new NifyMessage
  922. {
  923. message = message,
  924. priority = NifyMessagePriority.high,
  925. tags = ["red_circle"]
  926. });
  927. continue;
  928. }
  929. }
  930. }
  931. }
  932. //更新前一天的pdd统计表
  933. await AlimamaPlus.ApiCallStatisticsAsync(1, 1, TkChannelEnum.pdd);
  934. return new APIResult(new { success = true, message = "ok" });
  935. }
  936. [HttpGet]
  937. public async Task<ActionResult> FixReport(int id, string dt)
  938. {
  939. if (!DateTime.TryParse(dt, out DateTime date))
  940. {
  941. return new APIResult(new { success = false, message = "日期错误", });
  942. }
  943. var list = await TkPoolCore.ListAsync();
  944. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  945. string message = string.Empty;
  946. foreach (var account in list)
  947. {
  948. if (id != account.id) continue;
  949. if (account.is_hide) continue;
  950. try
  951. {
  952. var alimama = new AlimamaPlus(account);
  953. while (date.Date < DateTime.Now.Date)
  954. {
  955. await alimama.FixReport(date);
  956. date = date.AddDays(1);
  957. }
  958. }
  959. catch (Exception ex)
  960. {
  961. message = $"【报表接口异常】SaveReport\n{ex.Message}\n{ex.StackTrace}";
  962. NotifyCore.Notify(new NifyMessage
  963. {
  964. message = message,
  965. priority = NifyMessagePriority.high,
  966. tags = ["red_circle"]
  967. });
  968. continue;
  969. }
  970. }
  971. return new APIResult(new { success = true, message = "ok" });
  972. }
  973. [HttpGet]
  974. public ActionResult RenewCookie(string name)
  975. {
  976. var account = new DBContext.Table("tk_pool").Get<TkPoolDTO>("name=@name", new { name });
  977. if (account == null) return new APIResult(new { success = false, message = "没有有效账号", });
  978. var alimama = new AlimamaPlus(account);
  979. (bool success, string message) = alimama.RenewCookie();
  980. return new APIResult(new { success, message });
  981. }
  982. [HttpGet]
  983. public ActionResult Reload()
  984. {
  985. RiskControlCore.Refresh();
  986. TkConfigCore.Refresh();
  987. EndPointCore.Refresh();
  988. ProxyNodesCore.Refresh();
  989. TkPoolCore.Refresh();
  990. OtherApiPoolCore.Refresh();
  991. TkEndpointCore.Refresh();
  992. TkEndpointManager.Refresh();
  993. ApiAccountCore.Refresh();
  994. DataokeCore.Refresh();
  995. JdPoolCore.Refresh();
  996. PddPoolCore.Refresh();
  997. PangolinPoolCore.Refresh();
  998. ReduPoolCore.Refresh();
  999. //ElePoolCore.Refresh();
  1000. //MeituanPoolCore.Refresh();
  1001. CpsPoolCore.Refresh();
  1002. DeeplinkParseRuleCore.Refresh();
  1003. OverrideRuleCore.Refresh();
  1004. return new APIResult(new
  1005. {
  1006. success = true,
  1007. message = "ok",
  1008. });
  1009. }
  1010. [HttpGet]
  1011. public ActionResult ReloadAccount()
  1012. {
  1013. RiskControlCore.Refresh();
  1014. EndPointCore.Refresh();
  1015. ProxyNodesCore.Refresh();
  1016. TkPoolCore.Refresh();
  1017. OtherApiPoolCore.Refresh();
  1018. JdPoolCore.Refresh();
  1019. PddPoolCore.Refresh();
  1020. //ElePoolCore.Refresh();
  1021. //MeituanPoolCore.Refresh();
  1022. CpsPoolCore.Refresh();
  1023. OverrideRuleCore.Refresh();
  1024. return new APIResult(new
  1025. {
  1026. success = true,
  1027. message = "ok",
  1028. });
  1029. }
  1030. [HttpGet]
  1031. public async Task<ActionResult> CheckDeepUrl(int accountid)
  1032. {
  1033. var postContent = "88✔7Fi43dJwWpV£ https://m.tb.cn/h.g92hfR4JLgn7yvG MF7997 我分享给你了一个超赞的内容,快来看看吧";
  1034. var channel = "tb";
  1035. var ip = "127.0.0.1";
  1036. var oaid = "test-oaid";
  1037. bool success = false;
  1038. string message;
  1039. //============================== 放弃转链-没有匹配账号 ==============================
  1040. TkPoolDTO? account = await TkPoolCore.GetOneAsync(accountid);
  1041. if (account == null)
  1042. {
  1043. return new APIResult(new
  1044. {
  1045. success = false,
  1046. message = "没有匹配账号",
  1047. });
  1048. }
  1049. var alimama = new AlimamaPlus(account);
  1050. TkDataDTO result = new TkDataDTO();
  1051. string url = AlimamaPlus.GetLink(postContent);
  1052. if (string.IsNullOrEmpty(url))
  1053. {
  1054. return new APIResult(new
  1055. {
  1056. success = false,
  1057. message = "无效URL",
  1058. });
  1059. }
  1060. WebProxy proxy = ProxyNodesCore.RandomOne(account.nodeName);
  1061. (success, string content) = await alimama.GetDesiredUrlAsync(proxy, url);
  1062. if (content.Contains("霸下通用 web 页面-验证码"))
  1063. {
  1064. message = "霸下验证码";
  1065. }
  1066. else
  1067. {
  1068. message = content;
  1069. }
  1070. return new APIResult(new
  1071. {
  1072. success = true,
  1073. message,
  1074. });
  1075. }
  1076. }
  1077. }