TaskController.cs 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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 deeplink_parse_logs_fail_{table_suffix} LIKE deeplink_parse_logs_fail_template;";
  412. DBContext.Execute(sql, null);
  413. sql = $"CREATE TABLE IF NOT EXISTS pdd_parse_logs_{table_suffix} LIKE pdd_parse_logs;";
  414. DBContext.Execute(sql, null);
  415. sql = $"CREATE TABLE IF NOT EXISTS tool_parse_logs_{table_suffix} LIKE tool_parse_logs_template;";
  416. DBContext.Execute(sql, null);
  417. sql = $"CREATE TABLE IF NOT EXISTS tk_dp_logs_{table_suffix} LIKE tk_dp_logs;";
  418. DBContext.Execute(sql, null);
  419. sql = $"CREATE TABLE IF NOT EXISTS pdd_dp_logs_{table_suffix} LIKE pdd_dp_logs;";
  420. DBContext.Execute(sql, null);
  421. sql = $"CREATE TABLE IF NOT EXISTS jd_dp_logs_{table_suffix} LIKE jd_dp_logs;";
  422. DBContext.Execute(sql, null);
  423. }
  424. else
  425. {
  426. string table_suffix = DateTime.Now.AddDays(i).ToString("yyyyMMdd");
  427. string sql = $"CREATE TABLE IF NOT EXISTS tk_cps_logs_{table_suffix} LIKE tk_cps_logs;";
  428. DBContext.Execute(sql, null);
  429. sql = $"CREATE TABLE IF NOT EXISTS tk_activity_logs_{table_suffix} LIKE tk_activity_logs;";
  430. DBContext.Execute(sql, null);
  431. }
  432. }
  433. TkLogCore.save_dailys_log = true;
  434. RedisHelper.Set("turn:save_dailys_log", 1, 86400);
  435. }
  436. catch (Exception ex)
  437. {
  438. string message = $"【创建数据标错误】\n{ex.Message}\n{ex.StackTrace}";
  439. NotifyCore.Notify(new NifyMessage
  440. {
  441. message = message,
  442. priority = NifyMessagePriority.high,
  443. tags = ["red_circle"]
  444. });
  445. }
  446. return new APIResult(new { success = true, message = "ok" });
  447. }
  448. //[HttpGet]
  449. //public async Task<ActionResult> ApiCallStatistics(int intervalDay = 1)
  450. //{
  451. // try
  452. // {
  453. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.tb);
  454. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.tb, "parse_");
  455. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.tb, "coupon_");
  456. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.wemeet, "parse_");
  457. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.bdpan, "parse_");
  458. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.dy, "parse_");
  459. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.jd, "parse_");
  460. // }
  461. // catch (Exception ex)
  462. // {
  463. // string message = $"【报表接口异常】ApiCallStatistics\n{ex.Message}\n{ex.StackTrace}";
  464. // NotifyCore.Notify(new NifyMessage
  465. // {
  466. // message = message,
  467. // priority = NifyMessagePriority.high,
  468. // tags = ["red_circle"]
  469. // });
  470. // }
  471. // return new APIResult(new { success = true, message = "ok" });
  472. //}
  473. [HttpGet]
  474. public async Task<ActionResult> GetDrawBalance()
  475. {
  476. var list = await TkPoolCore.ListAsync();
  477. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  478. string message = string.Empty;
  479. foreach (var account in list)
  480. {
  481. if (!account.enable_sync_order) continue;
  482. try
  483. {
  484. var alimama = new AlimamaPlus(account);
  485. alimama.GetDrawBalance();
  486. }
  487. catch (Exception ex)
  488. {
  489. message = $"【报表接口异常】GetDrawBalance\n{ex.Message}\n{ex.StackTrace}";
  490. NotifyCore.Notify(new NifyMessage
  491. {
  492. message = message,
  493. priority = NifyMessagePriority.high,
  494. tags = ["red_circle"]
  495. });
  496. continue;
  497. }
  498. }
  499. return new APIResult(new { success = true, message = "ok" });
  500. }
  501. [HttpGet]
  502. public async Task<ActionResult> LastFastReport(int days = 0)
  503. {
  504. string lockKey = "lock:LastFastReport";
  505. if (!RedisHelper.Set(lockKey, "1", 300))
  506. {
  507. string message = "【LastFastReport】任务正在执行中,跳过本次调用";
  508. _ = new LoggerLibrary("task_lock", "skip")
  509. .Info(message)
  510. .SaveAsync();
  511. return new APIResult(new { success = false, message = "任务正在执行中" });
  512. }
  513. var startTimestamp = DateTime.Now;
  514. try
  515. {
  516. string message = string.Empty;
  517. var list = await TkPoolCore.ListAsync();
  518. foreach (var account in list)
  519. {
  520. #if DEBUG
  521. if (account.id != 113) continue;
  522. #endif
  523. if (!account.enable_sync_order) continue;
  524. try
  525. {
  526. var alimama = new AlimamaPlus(account);
  527. await alimama.SaveReportOverview(days);
  528. }
  529. catch (Exception ex)
  530. {
  531. message = $"【报表接口异常】LastFastReport\n{ex.Message}\n{ex.StackTrace}";
  532. NotifyCore.Notify(new NifyMessage
  533. {
  534. message = message,
  535. priority = NifyMessagePriority.high,
  536. tags = ["red_circle"]
  537. });
  538. continue;
  539. }
  540. }
  541. await EndPointCore.NotifyReload(true);
  542. return new APIResult(new { success = true, message = "ok" });
  543. }
  544. catch (Exception ex)
  545. {
  546. string errorMessage = $"【LastFastReport】执行异常: {ex.Message}";
  547. _ = new LoggerLibrary("task_error", "LastFastReport")
  548. .Info(errorMessage, ex.StackTrace)
  549. .SaveAsync();
  550. NotifyCore.Notify(new NifyMessage
  551. {
  552. message = errorMessage,
  553. priority = NifyMessagePriority.high,
  554. tags = ["red_circle"]
  555. });
  556. return new APIResult(new { success = false, message = errorMessage });
  557. }
  558. finally
  559. {
  560. var executionTime = DateTime.Now - startTimestamp;
  561. string logMessage = $"【LastFastReport】执行完成,耗时: {executionTime.TotalSeconds:F2}秒";
  562. _ = new LoggerLibrary("task_performance", "LastFastReport")
  563. .Info(logMessage)
  564. .SaveAsync();
  565. RedisHelper.Del(lockKey);
  566. }
  567. }
  568. [HttpGet]
  569. public async Task<ActionResult> FastReport(string ignore = "")
  570. {
  571. string lockKey = "lock:FastReport";
  572. if (!RedisHelper.Set(lockKey, "1", 300))
  573. {
  574. string message = "【FastReport】任务正在执行中,跳过本次调用";
  575. _ = new LoggerLibrary("task_lock", "skip")
  576. .Info(message)
  577. .SaveAsync();
  578. return new APIResult(new { success = false, message = "任务正在执行中" });
  579. }
  580. var startTimestamp = DateTime.Now;
  581. try
  582. {
  583. string message = string.Empty;
  584. var list = await TkPoolCore.ListAsync();
  585. if (!ignore.Contains("tb") && list != null)
  586. {
  587. foreach (var account in list)
  588. {
  589. #if DEBUG
  590. if (account.id != 113) continue;
  591. #endif
  592. try
  593. {
  594. account.current_daily_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.tb, account.id, DateTime.Now.ToString("yyyyMMdd"));
  595. account.current_hourly_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.tb, account.id, DateTime.Now.ToString("yyyyMMddHH"));
  596. new DBContext.Table("tk_pool")
  597. .Add("current_hourly_calls", account.current_hourly_calls)
  598. .Add("current_daily_calls", account.current_daily_calls)
  599. .Where("id=@id", new { account.id })
  600. .Update();
  601. }
  602. catch (Exception ex)
  603. {
  604. message = $"【TB计数异常】SaveCalls\n{ex.Message}\n{ex.StackTrace}";
  605. NotifyCore.Notify(new NifyMessage
  606. {
  607. message = message,
  608. priority = NifyMessagePriority.high,
  609. tags = ["red_circle"]
  610. });
  611. continue;
  612. }
  613. if (!account.enable_sync_order) continue;
  614. try
  615. {
  616. var alimama = new AlimamaPlus(account);
  617. await alimama.SaveReportOverview();
  618. }
  619. catch (Exception ex)
  620. {
  621. message = $"【报表接口异常】FastReport\n{ex.Message}\n{ex.StackTrace}";
  622. NotifyCore.Notify(new NifyMessage
  623. {
  624. message = message,
  625. priority = NifyMessagePriority.high,
  626. tags = ["red_circle"]
  627. });
  628. continue;
  629. }
  630. }
  631. }
  632. string filter = "";
  633. #if DEBUG
  634. filter = "id IN (44)";
  635. #endif
  636. var jd_list = new DBContext.Table("jd_pool").Where(filter, null).Select<JdPoolDTO>();
  637. if (!ignore.Contains("jd") && jd_list != null)
  638. {
  639. bool any_jd_changed = false;
  640. foreach (var account in jd_list)
  641. {
  642. bool changed = false;
  643. try
  644. {
  645. account.current_daily_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.jd, account.id, DateTime.Now.ToString("yyyyMMdd"));
  646. account.current_hourly_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.jd, account.id, DateTime.Now.ToString("yyyyMMddHH"));
  647. changed = true;
  648. any_jd_changed = true;
  649. }
  650. catch (Exception ex)
  651. {
  652. message = $"【JD计数异常】SaveCalls\n{ex.Message}\n{ex.StackTrace}";
  653. NotifyCore.Notify(new NifyMessage
  654. {
  655. message = message,
  656. priority = NifyMessagePriority.high,
  657. tags = ["red_circle"]
  658. });
  659. continue;
  660. }
  661. if (!account.status) continue;
  662. string sleepLockKey = $"lock:exception:sleep:jd_{account.id}";
  663. if (!string.IsNullOrEmpty(RedisHelper.Get(sleepLockKey))) continue;
  664. if (changed) JdPoolCore.Update(account);
  665. }
  666. if (any_jd_changed) JdPoolCore.Refresh();
  667. }
  668. var pdd_list = new DBContext.Table("pdd_pool").Where(filter, null).Select<PddPoolDTO>();
  669. if (!ignore.Contains("pdd") && pdd_list != null)
  670. {
  671. bool any_pdd_changed = false;
  672. foreach (var account in pdd_list)
  673. {
  674. bool changed = false;
  675. try
  676. {
  677. account.current_daily_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.pdd, account.id, DateTime.Now.ToString("yyyyMMdd"));
  678. account.current_hourly_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.pdd, account.id, DateTime.Now.ToString("yyyyMMddHH"));
  679. changed = true;
  680. any_pdd_changed = true;
  681. }
  682. catch (Exception ex)
  683. {
  684. message = $"【Pdd计数异常】SaveCalls\n{ex.Message}\n{ex.StackTrace}";
  685. NotifyCore.Notify(new NifyMessage
  686. {
  687. message = message,
  688. priority = NifyMessagePriority.high,
  689. tags = ["red_circle"]
  690. });
  691. continue;
  692. }
  693. if (changed) PddPoolCore.Update(account);
  694. }
  695. if (any_pdd_changed) PddPoolCore.Refresh();
  696. }
  697. var cps_list = new DBContext.Table("cps_links")
  698. .Where("status=@status", new { status = 1 })
  699. .Select<CpsLinksDTO>();
  700. if (!ignore.Contains("cps") && cps_list != null)
  701. {
  702. bool any_cps_changed = false;
  703. foreach (var link in cps_list)
  704. {
  705. bool changed = false;
  706. try
  707. {
  708. link.current_daily_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.cps, link.id, DateTime.Now.ToString("yyyyMMdd"));
  709. link.current_hourly_calls = await RiskControlCore.GetAllNodesCallsAsync(TkChannelEnum.cps, link.id, DateTime.Now.ToString("yyyyMMddHH"));
  710. changed = true;
  711. any_cps_changed = true;
  712. }
  713. catch (Exception ex)
  714. {
  715. message = $"【Cps计数异常】SaveCalls\n{ex.Message}\n{ex.StackTrace}";
  716. NotifyCore.Notify(new NifyMessage
  717. {
  718. message = message,
  719. priority = NifyMessagePriority.high,
  720. tags = ["red_circle"]
  721. });
  722. continue;
  723. }
  724. if (changed) CpsPoolCore.Update(link);
  725. }
  726. if (any_cps_changed) CpsPoolCore.Refresh();
  727. }
  728. await EndPointCore.NotifyReload(true);
  729. return new APIResult(new { success = true, message = "ok" });
  730. }
  731. catch (Exception ex)
  732. {
  733. string errorMessage = $"【FastReport】执行异常: {ex.Message}";
  734. _ = new LoggerLibrary("task_error", "FastReport")
  735. .Info(errorMessage, ex.StackTrace)
  736. .SaveAsync();
  737. NotifyCore.Notify(new NifyMessage
  738. {
  739. message = errorMessage,
  740. priority = NifyMessagePriority.high,
  741. tags = ["red_circle"]
  742. });
  743. return new APIResult(new { success = false, message = errorMessage });
  744. }
  745. finally
  746. {
  747. var executionTime = DateTime.Now - startTimestamp;
  748. string logMessage = $"【FastReport】执行完成,耗时: {executionTime.TotalSeconds:F2}秒";
  749. _ = new LoggerLibrary("task_performance", "FastReport")
  750. .Info(logMessage)
  751. .SaveAsync();
  752. RedisHelper.Del(lockKey);
  753. }
  754. }
  755. [HttpGet]
  756. public async Task<ActionResult> jdQuerySpreadEffectData(string ignore = "")
  757. {
  758. string message = string.Empty;
  759. string filter = "";
  760. #if DEBUG
  761. filter = "id IN (37, 15)";
  762. #endif
  763. var jd_list = new DBContext.Table("jd_pool").Where(filter, null).Select<JdPoolDTO>();
  764. if (!ignore.Contains("jd") && jd_list != null)
  765. {
  766. bool any_jd_changed = false;
  767. foreach (var account in jd_list)
  768. {
  769. bool changed = false;
  770. if (!account.status) continue;
  771. if (!account.enable_sync_report) continue;
  772. string lockKey = $"lock:exception:sleep:jd_{account.id}";
  773. if (!string.IsNullOrEmpty(RedisHelper.Get(lockKey))) continue;
  774. if (!string.IsNullOrEmpty(account.union_cookies))
  775. {
  776. try
  777. {
  778. var plus = new JdUnionPlus(account);
  779. var data = await plus.queryTodaySpreadEffectData();
  780. account.today_clickNum = data.clickNum;
  781. account.today_cosFee = data.cosFee;
  782. account.today_cosPrice = data.cosPrice;
  783. account.today_finishCosFee = data.finishCosFee;
  784. account.today_finishCosPrice = data.finishCosPrice;
  785. account.today_finishOrderNum = data.finishOrderNum;
  786. account.today_orderNum = data.orderNum;
  787. changed = true;
  788. any_jd_changed = true;
  789. }
  790. catch (Exception ex)
  791. {
  792. if (ex.Message.Contains("no login"))
  793. {
  794. account.status = false;
  795. _ = JdPoolCore.DisabledAsync(account.id, account.name, ex.Message);
  796. }
  797. else
  798. {
  799. RedisHelper.Set(lockKey, 1, 3600);
  800. }
  801. message = $"【JD报表接口异常】{account.id}:{account.name}\tqueryTodaySpreadEffectData\n{ex.Message}\n{ex.StackTrace}";
  802. NotifyCore.Notify(message);
  803. continue;
  804. }
  805. }
  806. if (changed) JdPoolCore.Update(account);
  807. }
  808. if (any_jd_changed) JdPoolCore.Refresh();
  809. }
  810. return new APIResult(new { success = true, message = "ok" });
  811. }
  812. [HttpGet]
  813. public async Task<ActionResult> SaveReport()
  814. {
  815. string message = string.Empty;
  816. var list = await TkPoolCore.ListAsync();
  817. if (list != null)
  818. {
  819. foreach (var account in list)
  820. {
  821. if (!account.enable_sync_order) continue;
  822. #if DEBUG
  823. if (account.id != 5) continue;
  824. #endif
  825. if (account.is_hide) continue;
  826. try
  827. {
  828. var alimama = new AlimamaPlus(account);
  829. alimama.SaveReportHourtrend(DateTime.Now.AddDays(-1));
  830. alimama.SaveReportHourtrend(DateTime.Now);
  831. if (DateTime.Now.Hour >= 12 && DateTime.Now.Hour < 16)
  832. {
  833. DateTime date = DateTime.Now.AddDays(-1);
  834. await alimama.FixReport(date);
  835. }
  836. }
  837. catch (Exception ex)
  838. {
  839. message = $"【报表接口异常】SaveReport\n{ex.Message}\n{ex.StackTrace}";
  840. NotifyCore.Notify(new NifyMessage
  841. {
  842. message = message,
  843. priority = NifyMessagePriority.high,
  844. tags = ["red_circle"]
  845. });
  846. continue;
  847. }
  848. }
  849. }
  850. return new APIResult(new { success = true, message = "ok" });
  851. }
  852. [HttpGet]
  853. public async Task<ActionResult> JdSaveReport(int id = 0)
  854. {
  855. string message = string.Empty;
  856. string filter = string.Empty;
  857. if (id > 0) filter = "id=@id";
  858. var jd_list = new DBContext.Table("jd_pool").Where(filter, new { id }).Select<JdPoolDTO>();
  859. if (jd_list != null)
  860. {
  861. DateTime startDate = DateTime.Now.AddDays(-89);
  862. DateTime endDate = DateTime.Now.AddDays(-1);
  863. foreach (var account in jd_list)
  864. {
  865. if (!account.status) continue;
  866. #if DEBUG
  867. startDate = DateTime.Parse("2024-07-31");
  868. endDate = DateTime.Parse("2024-07-31");
  869. if (account.id != 15) continue;
  870. #endif
  871. if (account.is_hide) continue;
  872. if (id == 0 && !account.enable_sync_report) continue;
  873. if (!string.IsNullOrEmpty(account.union_cookies))
  874. {
  875. try
  876. {
  877. var plus = new JdUnionPlus(account);
  878. _ = await plus.querySpreadEffectData(startDate, endDate);
  879. _ = await plus.queryEstimateCommList();
  880. }
  881. catch (Exception ex)
  882. {
  883. if (ex.Message.Contains("no login"))
  884. {
  885. _ = JdPoolCore.DisabledAsync(account.id, account.name, ex.Message);
  886. }
  887. message = $"【JD报表接口异常】{account.id}:{account.name}\tquerySpreadEffectData\n{ex.Message}\n{ex.StackTrace}";
  888. NotifyCore.Notify(message);
  889. continue;
  890. }
  891. }
  892. }
  893. }
  894. return new APIResult(new { success = true, message = "ok" });
  895. }
  896. [HttpGet]
  897. public async Task<ActionResult> PddSaveReport(int days = -1)
  898. {
  899. string message = string.Empty;
  900. var pdd_list = new DBContext.Table("pdd_pool").Where("cookie_status=1", new { }).Select<PddPoolDTO>();
  901. if (pdd_list != null)
  902. {
  903. DateTime startDate = DateTime.Now.AddDays(days);
  904. DateTime endDate = DateTime.Now;
  905. foreach (var account in pdd_list)
  906. {
  907. if (account.is_hide) continue;
  908. if (!account.enable_sync_revenue) continue;
  909. if (!string.IsNullOrEmpty(account.cookies))
  910. {
  911. try
  912. {
  913. var plus = new PddUnionPlus(account);
  914. await plus.queryEstimateRevenueList(startDate, endDate);
  915. }
  916. catch (Exception ex)
  917. {
  918. if (ex.Message.Contains("43001:会话已过期"))
  919. {
  920. PddPoolCore.CookieDisabled(account.id);
  921. }
  922. message = $"【PDD报表接口异常】{account.id}:{account.name}\tqueryEstimateRevenueList\n{ex.Message}\n{ex.StackTrace}";
  923. NotifyCore.Notify(new NifyMessage
  924. {
  925. message = message,
  926. priority = NifyMessagePriority.high,
  927. tags = ["red_circle"]
  928. });
  929. continue;
  930. }
  931. }
  932. }
  933. }
  934. //更新前一天的pdd统计表
  935. await AlimamaPlus.ApiCallStatisticsAsync(1, 1, TkChannelEnum.pdd);
  936. return new APIResult(new { success = true, message = "ok" });
  937. }
  938. [HttpGet]
  939. public async Task<ActionResult> FixReport(int id, string dt)
  940. {
  941. if (!DateTime.TryParse(dt, out DateTime date))
  942. {
  943. return new APIResult(new { success = false, message = "日期错误", });
  944. }
  945. var list = await TkPoolCore.ListAsync();
  946. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  947. string message = string.Empty;
  948. foreach (var account in list)
  949. {
  950. if (id != account.id) continue;
  951. if (account.is_hide) continue;
  952. try
  953. {
  954. var alimama = new AlimamaPlus(account);
  955. while (date.Date < DateTime.Now.Date)
  956. {
  957. await alimama.FixReport(date);
  958. date = date.AddDays(1);
  959. }
  960. }
  961. catch (Exception ex)
  962. {
  963. message = $"【报表接口异常】SaveReport\n{ex.Message}\n{ex.StackTrace}";
  964. NotifyCore.Notify(new NifyMessage
  965. {
  966. message = message,
  967. priority = NifyMessagePriority.high,
  968. tags = ["red_circle"]
  969. });
  970. continue;
  971. }
  972. }
  973. return new APIResult(new { success = true, message = "ok" });
  974. }
  975. [HttpGet]
  976. public ActionResult RenewCookie(string name)
  977. {
  978. var account = new DBContext.Table("tk_pool").Get<TkPoolDTO>("name=@name", new { name });
  979. if (account == null) return new APIResult(new { success = false, message = "没有有效账号", });
  980. var alimama = new AlimamaPlus(account);
  981. (bool success, string message) = alimama.RenewCookie();
  982. return new APIResult(new { success, message });
  983. }
  984. [HttpGet]
  985. public ActionResult Reload()
  986. {
  987. RiskControlCore.Refresh();
  988. TkConfigCore.Refresh();
  989. EndPointCore.Refresh();
  990. ProxyNodesCore.Refresh();
  991. TkPoolCore.Refresh();
  992. OtherApiPoolCore.Refresh();
  993. TkEndpointCore.Refresh();
  994. TkEndpointManager.Refresh();
  995. ApiAccountCore.Refresh();
  996. DataokeCore.Refresh();
  997. JdPoolCore.Refresh();
  998. PddPoolCore.Refresh();
  999. PangolinPoolCore.Refresh();
  1000. ReduPoolCore.Refresh();
  1001. //ElePoolCore.Refresh();
  1002. //MeituanPoolCore.Refresh();
  1003. CpsPoolCore.Refresh();
  1004. DeeplinkParseRuleCore.Refresh();
  1005. OverrideRuleCore.Refresh();
  1006. return new APIResult(new
  1007. {
  1008. success = true,
  1009. message = "ok",
  1010. });
  1011. }
  1012. [HttpGet]
  1013. public ActionResult ReloadAccount()
  1014. {
  1015. RiskControlCore.Refresh();
  1016. EndPointCore.Refresh();
  1017. ProxyNodesCore.Refresh();
  1018. TkPoolCore.Refresh();
  1019. OtherApiPoolCore.Refresh();
  1020. JdPoolCore.Refresh();
  1021. PddPoolCore.Refresh();
  1022. //ElePoolCore.Refresh();
  1023. //MeituanPoolCore.Refresh();
  1024. CpsPoolCore.Refresh();
  1025. OverrideRuleCore.Refresh();
  1026. return new APIResult(new
  1027. {
  1028. success = true,
  1029. message = "ok",
  1030. });
  1031. }
  1032. [HttpGet]
  1033. public async Task<ActionResult> CheckDeepUrl(int accountid)
  1034. {
  1035. var postContent = "88✔7Fi43dJwWpV£ https://m.tb.cn/h.g92hfR4JLgn7yvG MF7997 我分享给你了一个超赞的内容,快来看看吧";
  1036. var channel = "tb";
  1037. var ip = "127.0.0.1";
  1038. var oaid = "test-oaid";
  1039. bool success = false;
  1040. string message;
  1041. //============================== 放弃转链-没有匹配账号 ==============================
  1042. TkPoolDTO? account = await TkPoolCore.GetOneAsync(accountid);
  1043. if (account == null)
  1044. {
  1045. return new APIResult(new
  1046. {
  1047. success = false,
  1048. message = "没有匹配账号",
  1049. });
  1050. }
  1051. var alimama = new AlimamaPlus(account);
  1052. TkDataDTO result = new TkDataDTO();
  1053. string url = AlimamaPlus.GetLink(postContent);
  1054. if (string.IsNullOrEmpty(url))
  1055. {
  1056. return new APIResult(new
  1057. {
  1058. success = false,
  1059. message = "无效URL",
  1060. });
  1061. }
  1062. WebProxy proxy = ProxyNodesCore.RandomOne(account.nodeName);
  1063. (success, string content) = await alimama.GetDesiredUrlAsync(proxy, url);
  1064. if (content.Contains("霸下通用 web 页面-验证码"))
  1065. {
  1066. message = "霸下验证码";
  1067. }
  1068. else
  1069. {
  1070. message = content;
  1071. }
  1072. return new APIResult(new
  1073. {
  1074. success = true,
  1075. message,
  1076. });
  1077. }
  1078. }
  1079. }