TaskController.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. namespace molilian.api.Controllers
  12. {
  13. [ApiController]
  14. [Route("[controller]_70160bd632/[action]")]
  15. public class TaskController : ControllerBase
  16. {
  17. protected IHttpContextAccessor _accessor;
  18. public TaskController(IHttpContextAccessor accessor)
  19. {
  20. _accessor = accessor;
  21. }
  22. [HttpGet]
  23. public ActionResult BatchInsertLogDB(int limit = 100)
  24. {
  25. int total = TkLogCore.BatchInsertLogDB(limit);
  26. return new APIResult(new { success = true, message = "ok", limit, total });
  27. }
  28. [HttpGet]
  29. public async Task<ActionResult> GetSettleBills(int sleep = 2000)
  30. {
  31. var list = TkPoolCore.List();
  32. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  33. string message = string.Empty;
  34. int total = 0;
  35. foreach (var account in list)
  36. {
  37. if (!account.enable_sync_order) continue;
  38. #if DEBUG
  39. if (13 != account.id) continue;
  40. #endif
  41. try
  42. {
  43. var alimama = new AlimamaPlus(account);
  44. total += alimama.GetSettleBills(sleep);
  45. }
  46. catch (Exception ex)
  47. {
  48. message = $"【收益接口异常】\t{account.name}\n{ex.Message}\n{ex.StackTrace}";
  49. NotifyCore.Notify(new NifyMessage
  50. {
  51. message = message,
  52. priority = NifyMessagePriority.high,
  53. tags = ["red_circle"]
  54. });
  55. continue;
  56. }
  57. }
  58. return new APIResult(new { success = true, message = "ok", total });
  59. }
  60. [HttpGet]
  61. public async Task<ActionResult> GetHistoryOrders(int id, int sleep, DateTime startTime, DateTime endTime)
  62. {
  63. var list = TkPoolCore.List();
  64. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  65. if (id == 0) return new APIResult(new { success = false, message = "没有有效账号", });
  66. string message = string.Empty;
  67. int total = 0;
  68. foreach (var account in list)
  69. {
  70. if (id != account.id) continue;
  71. try
  72. {
  73. //DateTime endTime = DateTime.Now;
  74. //DateTime startTime = endTime.AddDays(-90);
  75. if (account.id == 3) endTime = DateTime.Parse("2024-04-07");
  76. var alimama = new AlimamaPlus(account);
  77. total += alimama.GetHistoryOrders(sleep, startTime, endTime);
  78. }
  79. catch (Exception ex)
  80. {
  81. message = $"【历史订单接口异常】[{account.id}]{account.company}\n{ex.Message}\n{ex.StackTrace}";
  82. NotifyCore.Notify(new NifyMessage
  83. {
  84. message = message,
  85. priority = NifyMessagePriority.high,
  86. tags = ["red_circle"]
  87. });
  88. continue;
  89. }
  90. }
  91. return new APIResult(new { success = true, message = "ok", total });
  92. }
  93. [HttpGet]
  94. public async Task<ActionResult> GetIncyOrders(int sleep = 2000)
  95. {
  96. var list = TkPoolCore.List();
  97. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  98. string message = string.Empty;
  99. int total = 0;
  100. foreach (var account in list)
  101. {
  102. #if DEBUG
  103. if (account.id != 17) continue;
  104. #endif
  105. if (!account.enable_sync_order) continue;
  106. try
  107. {
  108. var alimama = new AlimamaPlus(account);
  109. total += alimama.GetIncyOrders(sleep);
  110. }
  111. catch (Exception ex)
  112. {
  113. message = $"【新增订单接口异常】[{account.id}]{account.company}\n{ex.Message}\n{ex.StackTrace}";
  114. NotifyCore.Notify(new NifyMessage
  115. {
  116. message = message,
  117. priority = NifyMessagePriority.high,
  118. tags = ["red_circle"]
  119. });
  120. continue;
  121. }
  122. }
  123. return new APIResult(new { success = true, message = "ok", total });
  124. }
  125. [HttpGet]
  126. public async Task<ActionResult> GetHistoryRefundOrders(int id, int sleep, DateTime startTime, DateTime endTime)
  127. {
  128. var list = TkPoolCore.List();
  129. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  130. if (id == 0) return new APIResult(new { success = false, message = "没有有效账号", });
  131. string message = string.Empty;
  132. int total = 0;
  133. foreach (var account in list)
  134. {
  135. if (id != account.id) continue;
  136. try
  137. {
  138. //DateTime endTime = DateTime.Now;
  139. //DateTime startTime = endTime.AddDays(-90);
  140. if (account.id == 3) endTime = DateTime.Parse("2024-04-07");
  141. var alimama = new AlimamaPlus(account);
  142. total += alimama.GetHistoryRefundOrders(sleep, startTime, endTime);
  143. }
  144. catch (Exception ex)
  145. {
  146. message = $"【维权订单接口异常】[{account.id}]{account.company}\n{ex.Message}\n{ex.StackTrace}";
  147. NotifyCore.Notify(new NifyMessage
  148. {
  149. message = message,
  150. priority = NifyMessagePriority.high,
  151. tags = ["red_circle"]
  152. });
  153. continue;
  154. }
  155. }
  156. return new APIResult(new { success = true, message = "ok", total });
  157. }
  158. [HttpGet]
  159. public async Task<ActionResult> GetIncyRefundOrders(int sleep = 2000)
  160. {
  161. var list = TkPoolCore.List();
  162. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  163. string message = string.Empty;
  164. int total = 0;
  165. foreach (var account in list)
  166. {
  167. #if DEBUG
  168. if (account.id != 17) continue;
  169. #endif
  170. if (!account.enable_sync_order) continue;
  171. try
  172. {
  173. var alimama = new AlimamaPlus(account);
  174. total += alimama.GetIncyRefundOrders(sleep);
  175. }
  176. catch (Exception ex)
  177. {
  178. message = $"【维权订单接口异常】[{account.id}]{account.company}\n{ex.Message}\n{ex.StackTrace}";
  179. NotifyCore.Notify(new NifyMessage
  180. {
  181. message = message,
  182. priority = NifyMessagePriority.high,
  183. tags = ["red_circle"]
  184. });
  185. continue;
  186. }
  187. }
  188. return new APIResult(new { success = true, message = "ok", total });
  189. }
  190. [HttpGet]
  191. public async Task<ActionResult> GetViolationuWarning()
  192. {
  193. var list = TkPoolCore.List();
  194. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  195. string message = string.Empty;
  196. foreach (var account in list)
  197. {
  198. try
  199. {
  200. var alimama = new AlimamaPlus(account);
  201. alimama.GetViolationuWarning();
  202. }
  203. catch (Exception ex)
  204. {
  205. message = $"【报表接口异常】FastReport\n{ex.Message}\n{ex.StackTrace}";
  206. NotifyCore.Notify(new NifyMessage
  207. {
  208. message = message,
  209. priority = NifyMessagePriority.high,
  210. tags = ["red_circle"]
  211. });
  212. continue;
  213. }
  214. }
  215. return new APIResult(new { success = true, message = "ok" });
  216. }
  217. [HttpGet]
  218. public async Task<ActionResult> DailyLogs(int intervalDay = 1)
  219. {
  220. try
  221. {
  222. //本节点统计
  223. AlimamaPlus.NodeDailyLogs(intervalDay, "parse_", "tb");
  224. AlimamaPlus.NodeDailyLogs(intervalDay, "coupon_", "tb");
  225. AlimamaPlus.NodeDailyLogs(intervalDay); //第三方老的统计
  226. if (CenterHub.IsCenter)
  227. {
  228. JdUnionPlus.DailyLogs(intervalDay);
  229. ToolParsePlus.DailyLogs(intervalDay);
  230. //中心服务器统计
  231. AlimamaPlus.AllDailyLogs(intervalDay, "parse_", "tb");
  232. AlimamaPlus.AllDailyLogs(intervalDay, "coupon_", "tb");
  233. AlimamaPlus.AllDailyLogs(intervalDay);
  234. }
  235. }
  236. catch (Exception ex)
  237. {
  238. string message = $"【报表接口异常】DailyLogs\n{ex.Message}\n{ex.StackTrace}";
  239. NotifyCore.Notify(new NifyMessage
  240. {
  241. message = message,
  242. priority = NifyMessagePriority.high,
  243. tags = ["red_circle"]
  244. });
  245. }
  246. if (intervalDay > 0)
  247. {
  248. try
  249. {
  250. AlimamaPlus.ApiCallStatistics(1, intervalDay, TkChannelEnum.tb);
  251. AlimamaPlus.ApiCallStatistics(2, intervalDay, TkChannelEnum.tb);
  252. }
  253. catch (Exception ex)
  254. {
  255. string message = $"【API报表更新异常】ApiCallStatistics\n{ex.Message}\n{ex.StackTrace}";
  256. NotifyCore.Notify(new NifyMessage
  257. {
  258. message = message,
  259. priority = NifyMessagePriority.high,
  260. tags = ["red_circle"]
  261. });
  262. }
  263. }
  264. return new APIResult(new { success = true, message = "ok" });
  265. }
  266. [HttpGet]
  267. public async Task<ActionResult> DailyTable(int days = 7)
  268. {
  269. try
  270. {
  271. for (int i = 0; i < days; i++)
  272. {
  273. string table_suffix = DateTime.Now.AddDays(i).ToString("yyyyMMdd");
  274. string sql = $"CREATE TABLE IF NOT EXISTS tk_parse_logs_{table_suffix} LIKE tk_parse_logs;";
  275. DBContext.Execute(sql, null);
  276. sql = $"CREATE TABLE IF NOT EXISTS jd_parse_logs_{table_suffix} LIKE jd_parse_logs;";
  277. DBContext.Execute(sql, null);
  278. }
  279. TkLogCore.save_dailys_log = true;
  280. RedisHelper.Set("turn:save_dailys_log", 1, 86400);
  281. }
  282. catch (Exception ex)
  283. {
  284. string message = $"【创建数据标错误】\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. }
  292. return new APIResult(new { success = true, message = "ok" });
  293. }
  294. [HttpGet]
  295. public async Task<ActionResult> ApiCallStatistics(int intervalDay = 1)
  296. {
  297. try
  298. {
  299. AlimamaPlus.ApiCallStatistics(1, intervalDay, TkChannelEnum.tb);
  300. AlimamaPlus.ApiCallStatistics(2, intervalDay, TkChannelEnum.tb);
  301. }
  302. catch (Exception ex)
  303. {
  304. string message = $"【API报表更新异常】ApiCallStatistics\n{ex.Message}\n{ex.StackTrace}";
  305. NotifyCore.Notify(new NifyMessage
  306. {
  307. message = message,
  308. priority = NifyMessagePriority.high,
  309. tags = ["red_circle"]
  310. });
  311. }
  312. return new APIResult(new { success = true, message = "ok" });
  313. }
  314. //[HttpGet]
  315. //public async Task<ActionResult> ApiCallStatistics(int intervalDay = 1)
  316. //{
  317. // try
  318. // {
  319. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.tb);
  320. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.tb, "parse_");
  321. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.tb, "coupon_");
  322. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.wemeet, "parse_");
  323. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.bdpan, "parse_");
  324. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.dy, "parse_");
  325. // AlimamaPlus.ApiCallStatistics(intervalDay, TkChannelEnum.jd, "parse_");
  326. // }
  327. // catch (Exception ex)
  328. // {
  329. // string message = $"【报表接口异常】ApiCallStatistics\n{ex.Message}\n{ex.StackTrace}";
  330. // NotifyCore.Notify(new NifyMessage
  331. // {
  332. // message = message,
  333. // priority = NifyMessagePriority.high,
  334. // tags = ["red_circle"]
  335. // });
  336. // }
  337. // return new APIResult(new { success = true, message = "ok" });
  338. //}
  339. [HttpGet]
  340. public async Task<ActionResult> GetDrawBalance()
  341. {
  342. var list = TkPoolCore.List();
  343. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  344. string message = string.Empty;
  345. foreach (var account in list)
  346. {
  347. if (!account.enable_sync_order) continue;
  348. try
  349. {
  350. var alimama = new AlimamaPlus(account);
  351. alimama.GetDrawBalance();
  352. }
  353. catch (Exception ex)
  354. {
  355. message = $"【报表接口异常】GetDrawBalance\n{ex.Message}\n{ex.StackTrace}";
  356. NotifyCore.Notify(new NifyMessage
  357. {
  358. message = message,
  359. priority = NifyMessagePriority.high,
  360. tags = ["red_circle"]
  361. });
  362. continue;
  363. }
  364. }
  365. return new APIResult(new { success = true, message = "ok" });
  366. }
  367. [HttpGet]
  368. public async Task<ActionResult> FastReport()
  369. {
  370. string message = string.Empty;
  371. var list = TkPoolCore.List();
  372. if (list != null)
  373. {
  374. foreach (var account in list)
  375. {
  376. if (!account.enable_sync_order) continue;
  377. try
  378. {
  379. var alimama = new AlimamaPlus(account);
  380. alimama.SaveReportOverview();
  381. }
  382. catch (Exception ex)
  383. {
  384. message = $"【报表接口异常】FastReport\n{ex.Message}\n{ex.StackTrace}";
  385. NotifyCore.Notify(new NifyMessage
  386. {
  387. message = message,
  388. priority = NifyMessagePriority.high,
  389. tags = ["red_circle"]
  390. });
  391. continue;
  392. }
  393. }
  394. }
  395. string filter = "";
  396. #if DEBUG
  397. filter = "id IN (7,8)";
  398. #endif
  399. var jd_list = new DBContext.Table("jd_pool").Where(filter, null).Select<JdPoolDTO>();
  400. if (jd_list != null)
  401. {
  402. bool any_jd_changed = false;
  403. foreach (var account in jd_list)
  404. {
  405. bool changed = false;
  406. try
  407. {
  408. account.current_daily_calls = JdPoolCore.SaveCalls(account.id, DateTime.Now.ToString("yyyyMMdd"));
  409. account.current_hourly_calls = JdPoolCore.SaveCalls(account.id, DateTime.Now.ToString("yyyyMMddHH"));
  410. changed = true;
  411. any_jd_changed = true;
  412. }
  413. catch (Exception ex)
  414. {
  415. message = $"【JD计数异常】SaveCalls\n{ex.Message}\n{ex.StackTrace}";
  416. NotifyCore.Notify(new NifyMessage
  417. {
  418. message = message,
  419. priority = NifyMessagePriority.high,
  420. tags = ["red_circle"]
  421. });
  422. continue;
  423. }
  424. if (!account.status) continue;
  425. if (!string.IsNullOrEmpty(account.cookies))
  426. {
  427. try
  428. {
  429. var plus = new JdUnionPlus(account);
  430. var data = await plus.queryTodaySpreadEffectData();
  431. account.today_clickNum = data.clickNum;
  432. account.today_cosFee = data.cosFee;
  433. account.today_cosPrice = data.cosPrice;
  434. account.today_finishCosFee = data.finishCosFee;
  435. account.today_finishCosPrice = data.finishCosPrice;
  436. account.today_finishOrderNum = data.finishOrderNum;
  437. account.today_orderNum = data.orderNum;
  438. changed = true;
  439. any_jd_changed = true;
  440. }
  441. catch (Exception ex)
  442. {
  443. if (ex.Message.Contains("no login"))
  444. {
  445. account.status = false;
  446. JdPoolCore.Disabled(account.id, account.name, ex.Message);
  447. }
  448. message = $"【JD报表接口异常】{account.id}:{account.name}\tqueryTodaySpreadEffectData\n{account.Convert2Json()}\n{ex.Message}\n{ex.StackTrace}";
  449. NotifyCore.Notify(new NifyMessage
  450. {
  451. message = message,
  452. priority = NifyMessagePriority.high,
  453. tags = ["red_circle"]
  454. });
  455. continue;
  456. }
  457. }
  458. if (changed) JdPoolCore.Update(account);
  459. }
  460. if (any_jd_changed) JdPoolCore.Refresh();
  461. }
  462. await EndPointCore.NotifyReload(true);
  463. return new APIResult(new { success = true, message = "ok" });
  464. }
  465. [HttpGet]
  466. public async Task<ActionResult> SaveReport()
  467. {
  468. string message = string.Empty;
  469. var list = TkPoolCore.List();
  470. if (list != null)
  471. {
  472. foreach (var account in list)
  473. {
  474. if (!account.enable_sync_order) continue;
  475. #if DEBUG
  476. if (account.id != 5) continue;
  477. #endif
  478. try
  479. {
  480. var alimama = new AlimamaPlus(account);
  481. alimama.SaveReportHourtrend(DateTime.Now.AddDays(-1));
  482. alimama.SaveReportHourtrend(DateTime.Now);
  483. if (DateTime.Now.Hour >= 12 && DateTime.Now.Hour < 16)
  484. {
  485. DateTime date = DateTime.Now.AddDays(-1);
  486. alimama.FixReport(date);
  487. }
  488. }
  489. catch (Exception ex)
  490. {
  491. message = $"【报表接口异常】SaveReport\n{ex.Message}\n{ex.StackTrace}";
  492. NotifyCore.Notify(new NifyMessage
  493. {
  494. message = message,
  495. priority = NifyMessagePriority.high,
  496. tags = ["red_circle"]
  497. });
  498. continue;
  499. }
  500. }
  501. }
  502. return new APIResult(new { success = true, message = "ok" });
  503. }
  504. [HttpGet]
  505. public async Task<ActionResult> JdSaveReport()
  506. {
  507. string message = string.Empty;
  508. var jd_list = new DBContext.Table("jd_pool").Where("", null).Select<JdPoolDTO>();
  509. if (jd_list != null)
  510. {
  511. foreach (var account in jd_list)
  512. {
  513. if (!account.status) continue;
  514. #if DEBUG
  515. if (account.id != 10) continue;
  516. #endif
  517. if (!string.IsNullOrEmpty(account.cookies))
  518. {
  519. try
  520. {
  521. var plus = new JdUnionPlus(account);
  522. _ = await plus.querySpreadEffectData(DateTime.Now.AddDays(-1));
  523. _ = await plus.queryEstimateCommList();
  524. }
  525. catch (Exception ex)
  526. {
  527. if (ex.Message.Contains("no login"))
  528. {
  529. JdPoolCore.Disabled(account.id, account.name, ex.Message);
  530. }
  531. message = $"【JD报表接口异常】{account.id}:{account.name}\tquerySpreadEffectData\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. }
  542. }
  543. return new APIResult(new { success = true, message = "ok" });
  544. }
  545. [HttpGet]
  546. public async Task<ActionResult> FixReport(int id, string dt)
  547. {
  548. if (!DateTime.TryParse(dt, out DateTime date))
  549. {
  550. return new APIResult(new { success = false, message = "日期错误", });
  551. }
  552. var list = TkPoolCore.List();
  553. if (list == null) return new APIResult(new { success = false, message = "没有有效账号", });
  554. string message = string.Empty;
  555. foreach (var account in list)
  556. {
  557. if (id != account.id) continue;
  558. try
  559. {
  560. var alimama = new AlimamaPlus(account);
  561. while (date.Date < DateTime.Now.Date)
  562. {
  563. alimama.FixReport(date);
  564. date = date.AddDays(1);
  565. }
  566. }
  567. catch (Exception ex)
  568. {
  569. message = $"【报表接口异常】SaveReport\n{ex.Message}\n{ex.StackTrace}";
  570. NotifyCore.Notify(new NifyMessage
  571. {
  572. message = message,
  573. priority = NifyMessagePriority.high,
  574. tags = ["red_circle"]
  575. });
  576. continue;
  577. }
  578. }
  579. return new APIResult(new { success = true, message = "ok" });
  580. }
  581. [HttpGet]
  582. public ActionResult RenewCookie(string name)
  583. {
  584. var account = new DBContext.Table("tk_pool").Get<TkPoolDTO>("name=@name", new { name });
  585. if (account == null) return new APIResult(new { success = false, message = "没有有效账号", });
  586. var alimama = new AlimamaPlus(account);
  587. (bool success, string message) = alimama.RenewCookie();
  588. return new APIResult(new { success, message });
  589. }
  590. [HttpGet]
  591. public ActionResult Reload()
  592. {
  593. TkConfigCore.Refresh();
  594. EndPointCore.Refresh();
  595. ProxyNodesCore.Refresh();
  596. TkPoolCore.Refresh();
  597. VeapiPoolCore.Refresh();
  598. ApiAccountCore.Refresh();
  599. DataokeCore.Refresh();
  600. JdPoolCore.Refresh();
  601. PddPoolCore.Refresh();
  602. PangolinPoolCore.Refresh();
  603. return new APIResult(new
  604. {
  605. success = true,
  606. message = "ok",
  607. });
  608. }
  609. [HttpGet]
  610. public ActionResult ReloadAccount()
  611. {
  612. EndPointCore.Refresh();
  613. ProxyNodesCore.Refresh();
  614. TkPoolCore.Refresh();
  615. VeapiPoolCore.Refresh();
  616. JdPoolCore.Refresh();
  617. return new APIResult(new
  618. {
  619. success = true,
  620. message = "ok",
  621. });
  622. }
  623. }
  624. }