TaskController.cs 49 KB

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