TaobaoController.cs 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Text.Json;
  5. using System.Net;
  6. using static QRCoder.PayloadGenerator;
  7. using MySqlX.XDevAPI;
  8. using OfficeOpenXml.FormulaParsing.LexicalAnalysis;
  9. using StackExchange.Redis;
  10. namespace molilian.api.Controllers
  11. {
  12. [ApiController]
  13. [MyAuthorize("admin")]
  14. [Route("api/[controller]/[action]")]
  15. [Route("coupon_api/[controller]/[action]")]
  16. public class TaobaoController : ControllerBase
  17. {
  18. readonly IAuthorizationProvider provider = new AdminProvider();
  19. protected IHttpContextAccessor _accessor;
  20. public TaobaoController(IHttpContextAccessor accessor)
  21. {
  22. _accessor = accessor;
  23. }
  24. [HttpPost]
  25. public async Task<ActionResult> updateCookies([FromBody] JsonElement form)
  26. {
  27. var clientIp = _accessor.HttpContext.GetUserIp();
  28. var token = provider.Get(_accessor.HttpContext);
  29. string user_agent = _accessor.HttpContext.Request.UserAgent();
  30. string cookie = form.Read<string>("cookie", string.Empty);
  31. var accountId = await TkPoolCore.UpdateCookies(cookie, user_agent);
  32. bool success = accountId > 0;
  33. if (success)
  34. {
  35. //string desc = ObjectComparer.PrintCompareToString(old, form).Trim();
  36. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"tk_pool:{accountId}:cookie", string.Empty, cookie);
  37. }
  38. return new APIResult(new
  39. {
  40. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入的cookie" },
  41. });
  42. }
  43. [HttpPost]
  44. public async Task<ActionResult> update([FromBody] JsonElement form)
  45. {
  46. var clientIp = _accessor.HttpContext.GetUserIp();
  47. var token = provider.Get(_accessor.HttpContext);
  48. int id = form.Read<int>("id", 0);
  49. string name = form.Read<string>("name", string.Empty);
  50. string val = form.Read<string>("val", string.Empty);
  51. if (id == 0)
  52. {
  53. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入的cookie" } });
  54. }
  55. int result;
  56. switch (name)
  57. {
  58. case "enable_fake_click":
  59. case "enable_order_summary_cache":
  60. case "enable_parse":
  61. case "enable_coupon":
  62. case "enable_sync_order":
  63. case "enable_promotionQuery":
  64. case "enable_deep_url":
  65. case "status":
  66. bool bVal = val.Equals("True");
  67. result = new DBContext.Table("tk_pool")
  68. .Add(name, bVal)
  69. .Add("last_time", DateTime.Now)
  70. .Where("id=@id", new { id })
  71. .Update();
  72. break;
  73. case "check_status":
  74. //检查状态
  75. var arr = EndPointCore.NotifyCheckDeepUrl(id);
  76. string suspended_endpoint = string.Empty;
  77. var log = new LoggerLibrary("check_suspended_endpoint");
  78. foreach ((string node, string message) in arr)
  79. {
  80. log.Info(node, message);
  81. if (message.Contains("霸下验证码"))
  82. {
  83. suspended_endpoint += $"{node},";
  84. }
  85. }
  86. result = new DBContext.Table("tk_pool")
  87. .Add("suspended_endpoint", suspended_endpoint)
  88. .Add("last_time", DateTime.Now)
  89. .Where("id=@id", new { id })
  90. .Update();
  91. log.SaveAsync();
  92. break;
  93. case "time_range":
  94. case "qps_limit":
  95. int.TryParse(val, out int iVal);
  96. if ("qps_limit".Equals(name) && iVal < 0) iVal = 0;
  97. result = new DBContext.Table("tk_pool")
  98. .Add(name, iVal)
  99. .Add("last_time", DateTime.Now)
  100. .Where("id=@id", new { id })
  101. .Update();
  102. break;
  103. default:
  104. return new APIResult(new { data = new { success = false, msg = "更新失败,未授权操作" } });
  105. }
  106. bool success = result > 0;
  107. if (success)
  108. {
  109. //string desc = ObjectComparer.PrintCompareToString(old, form).Trim();
  110. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"tk_pool:{id}:{name}", string.Empty, val);
  111. await EndPointCore.NotifyReload(true);
  112. }
  113. return new APIResult(new
  114. {
  115. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  116. });
  117. }
  118. /// <summary>
  119. /// 账号列表
  120. /// </summary>
  121. /// <param name="form"></param>
  122. /// <returns></returns>
  123. [HttpPost]
  124. public async Task<ActionResult> list([FromBody] JsonElement form)
  125. {
  126. var token = provider.Get(_accessor.HttpContext);
  127. int page = form.Read("current", 1);
  128. int size = form.Read("pageSize", 10);
  129. string keyword = form.Read("name", string.Empty);
  130. bool getTotal = form.Read("getTotal", true);
  131. string _status = form.Read("status", string.Empty);
  132. int status = _status switch
  133. {
  134. "online" => 1,
  135. "offline" => 0,
  136. _ => -1,
  137. };
  138. var lastTime = form.PathReadArray<string>("lastTime[]");
  139. string sort = form.Read<string>("sort");
  140. string order = form.Read<string>("order");
  141. bool show_hide = form.Read("show_hide", false);
  142. string filter = string.Empty;
  143. if (!show_hide) filter = "is_hide=0";
  144. if (!string.IsNullOrEmpty(keyword))
  145. {
  146. filter += $" AND (`name` LIKE @keyword OR `description` LIKE @keyword)";
  147. keyword = $"%{keyword}%";
  148. }
  149. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  150. if (lastTime.Count == 2)
  151. {
  152. if (DateTime.TryParse(lastTime[0], out stime) && DateTime.TryParse(lastTime[1], out etime))
  153. {
  154. etime = etime.AddDays(1).AddSeconds(-1);
  155. filter += $" AND last_time BETWEEN @stime AND @etime";
  156. }
  157. }
  158. if (status != -1)
  159. {
  160. filter += $" AND status=@status";
  161. }
  162. filter = filter.StringTrimStart(" AND ");
  163. //排序
  164. string orderBy = "id DESC";
  165. if (!string.IsNullOrEmpty(order))
  166. {
  167. order = "descending".Equals(order) ? "DESC" : "ASC";
  168. orderBy = sort switch
  169. {
  170. //"num" => $"num {order}",
  171. _ => $"{sort} {order},ID DESC",
  172. };
  173. }
  174. using var conn = DBContext.GetOpenConnection();
  175. var result = new DBContext.Table(conn, "tk_pool")
  176. .Where(filter, new { keyword, status, stime, etime })
  177. .Page(size, page)
  178. .Order(orderBy)
  179. .PageList<TkPoolDTO>(getTotal);
  180. var proxyNodes = (ProxyNodesCore.AllList() ?? Array.Empty<ProxyNodesDTO>())
  181. .ToList();
  182. var aiOpenAccounts = result.List
  183. .Where(AiOpenRiskControlCore.IsApplicableAccount)
  184. .ToList();
  185. var aiOpenRiskEvents = await AiOpenRiskControlCore.GetTodayEventsAsync(
  186. aiOpenAccounts.Select(account => account.id));
  187. foreach (var item in result.List)
  188. {
  189. item.current_amt = Math.Round(RiskControlCore.GetIncomeAmt(TkChannelEnum.tb, $"{item.id}"), 2);
  190. item.cookies = string.Empty;
  191. //await RiskControlCore.SetRiskCookie($"tb:{item.id}", "TEST");
  192. item.riskCookie = RiskControlCore.GetRiskCookie($"tb:{item.id}");
  193. item.proxyStatus = proxyNodes
  194. .FirstOrDefault(proxyNode =>
  195. ProxyNodesCore.IsMatchNode(proxyNode, item.nodeName))
  196. ?.status;
  197. if (AiOpenRiskControlCore.IsApplicableAccount(item))
  198. {
  199. item.aiopen_risk = new AiOpenRiskStatusDTO
  200. {
  201. applicable = true
  202. };
  203. if (aiOpenRiskEvents.TryGetValue(item.id, out var riskEvent))
  204. {
  205. item.aiopen_risk.triggered = true;
  206. item.aiopen_risk.reason_code = riskEvent.reason_code;
  207. item.aiopen_risk.reason = riskEvent.reason;
  208. item.aiopen_risk.first_trigger_time = riskEvent.first_trigger_time;
  209. item.aiopen_risk.request_count_at_trigger = riskEvent.request_count_at_trigger;
  210. item.aiopen_risk.success_count_at_trigger = riskEvent.success_count_at_trigger;
  211. item.aiopen_risk.release_time = riskEvent.release_time;
  212. }
  213. }
  214. }
  215. return new APIResult(new { data = result });
  216. }
  217. [HttpPost]
  218. public async Task<ActionResult> report_dailys([FromBody] JsonElement form)
  219. {
  220. int page = form.Read("current", 1);
  221. int size = form.Read("pageSize", 10);
  222. bool getTotal = form.Read("getTotal", true);
  223. string sort = form.Read<string>("sort");
  224. string order = form.Read<string>("order");
  225. var report_date = form.PathReadArray<string>("query_date[]");
  226. string dailys_accountName = "all";
  227. int dailys_accountId = 0;
  228. string filter = string.Empty;
  229. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  230. if (report_date.Count == 2)
  231. {
  232. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  233. {
  234. etime = etime.AddDays(1).AddSeconds(-1);
  235. filter += $" AND A.report_date BETWEEN @stime AND @etime";
  236. }
  237. }
  238. filter = filter.StringTrimStart(" AND ");
  239. //排序
  240. string orderBy = "A.report_date DESC";
  241. if (!string.IsNullOrEmpty(order))
  242. {
  243. order = "descending".Equals(order) ? "DESC" : "ASC";
  244. orderBy = sort switch
  245. {
  246. _ => $"A.{sort} {order}",
  247. };
  248. }
  249. var result = new DBContext.Table("v_tk_report_dailys")
  250. .LeftJoin("daily_logs",
  251. "B.log_date = A.report_date AND B.channel=0 AND B.accountId=@dailys_accountId",
  252. "total_count, B.abandon_count, B.success_count, B.abandon_percentage, B.success_percentage," +
  253. "B.parse_total_count, B.parse_abandon_count, B.parse_success_count, B.parse_abandon_percentage, B.parse_success_percentage," +
  254. "B.coupon_total_count, B.coupon_abandon_count, B.coupon_success_count, B.coupon_abandon_percentage, B.coupon_success_percentage," +
  255. "B.tool_total_count, B.tool_abandon_count, B.tool_success_count, B.tool_abandon_percentage, B.tool_success_percentage")
  256. .Where(filter, new { dailys_accountId, dailys_accountName, stime, etime })
  257. .Page(size, page)
  258. .Order(orderBy)
  259. .PageList<TkReportDTO>(getTotal);
  260. foreach (var item in result.List)
  261. {
  262. if (item.report_date.Date < DateTime.Now.Date) continue;
  263. if (item.total_count == 0)
  264. {
  265. item.total_count = await TkLogCore.GetTotalAsync($":total:tb:{item.report_date:yyyyMMdd}");
  266. if (item.total_count > 0)
  267. {
  268. item.success_count = await TkLogCore.GetTotalAsync($":total:tb:success:{item.report_date:yyyyMMdd}");
  269. item.abandon_count = await TkLogCore.GetTotalAsync($":total:tb:放弃转链:{item.report_date:yyyyMMdd}");
  270. if (item.total_count > 0)
  271. {
  272. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  273. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  274. }
  275. }
  276. }
  277. if (item.parse_total_count == 0)
  278. {
  279. item.parse_total_count = await TkLogCore.GetTotalAsync($":parse_total:tb:{item.report_date:yyyyMMdd}");
  280. if (item.parse_total_count > 0)
  281. {
  282. item.parse_success_count = await TkLogCore.GetTotalAsync($":parse_total:tb:success:{item.report_date:yyyyMMdd}");
  283. item.parse_abandon_count = await TkLogCore.GetTotalAsync($":parse_total:tb:放弃转链:{item.report_date:yyyyMMdd}");
  284. if (item.parse_total_count > 0)
  285. {
  286. item.parse_success_percentage = $"{item.parse_success_count / (double)item.parse_total_count * 100:f2}%";
  287. item.parse_abandon_percentage = $"{item.parse_abandon_count / (double)item.parse_total_count * 100:f2}%";
  288. }
  289. }
  290. }
  291. if (item.coupon_total_count == 0)
  292. {
  293. item.coupon_total_count = await TkLogCore.GetTotalAsync($":coupon_total:tb:{item.report_date:yyyyMMdd}", false);
  294. if (item.coupon_total_count > 0)
  295. {
  296. item.coupon_success_count = await TkLogCore.GetTotalAsync($":coupon_total:tb:success:{item.report_date:yyyyMMdd}", false);
  297. item.coupon_abandon_count = await TkLogCore.GetTotalAsync($":coupon_total:tb:放弃转链:{item.report_date:yyyyMMdd}", false);
  298. if (item.coupon_total_count > 0)
  299. {
  300. item.coupon_success_percentage = $"{item.coupon_success_count / (double)item.coupon_total_count * 100:f2}%";
  301. item.coupon_abandon_percentage = $"{item.coupon_abandon_count / (double)item.coupon_total_count * 100:f2}%";
  302. }
  303. }
  304. }
  305. if (item.tool_total_count == 0)
  306. {
  307. item.tool_total_count = await TkLogCore.GetTotalAsync($":parse_total:tool:{item.report_date:yyyyMMdd}");
  308. if (item.tool_total_count > 0)
  309. {
  310. item.tool_success_count = await TkLogCore.GetTotalAsync($":parse_total:tool:success:{item.report_date:yyyyMMdd}");
  311. item.tool_abandon_count = await TkLogCore.GetTotalAsync($":parse_total:tool:放弃转链:{item.report_date:yyyyMMdd}");
  312. if (item.tool_total_count > 0)
  313. {
  314. item.tool_success_percentage = $"{item.tool_success_count / (double)item.tool_total_count * 100:f2}%";
  315. item.tool_abandon_percentage = $"{item.tool_abandon_count / (double)item.tool_total_count * 100:f2}%";
  316. }
  317. }
  318. }
  319. }
  320. return new APIResult(new
  321. {
  322. data = result
  323. });
  324. }
  325. [HttpPost]
  326. public async Task<ActionResult> report_hourlys([FromBody] JsonElement form)
  327. {
  328. int page = form.Read("current", 1);
  329. int size = form.Read("pageSize", 10);
  330. bool getTotal = form.Read("getTotal", true);
  331. string sort = form.Read<string>("sort");
  332. string order = form.Read<string>("order");
  333. var report_date = form.PathReadArray<string>("query_date[]");
  334. var filterHours = form.Read<int?>("filterHours", null);
  335. string dailys_accountName = "all";
  336. int dailys_accountId = 0;
  337. string filter = string.Empty;
  338. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  339. if (report_date.Count == 2)
  340. {
  341. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  342. {
  343. etime = etime.AddDays(1).AddSeconds(-1);
  344. filter += $" AND A.report_date BETWEEN @stime AND @etime";
  345. }
  346. }
  347. if (filterHours != null)
  348. {
  349. filterHours--;
  350. filter += $" AND HOUR(report_date) = @filterHours";
  351. }
  352. filter = filter.StringTrimStart(" AND ");
  353. //排序
  354. string orderBy = "A.report_date DESC";
  355. if (!string.IsNullOrEmpty(order))
  356. {
  357. order = "descending".Equals(order) ? "DESC" : "ASC";
  358. orderBy = sort switch
  359. {
  360. _ => $"A.{sort} {order}",
  361. };
  362. }
  363. var result = new DBContext.Table("v_tk_report_hourlys")
  364. .LeftJoin("daily_logs",
  365. "B.log_date = A.report_date AND B.channel=0 AND B.accountId=@dailys_accountId",
  366. "total_count, B.abandon_count, B.success_count, B.abandon_percentage, B.success_percentage," +
  367. "B.parse_total_count, B.parse_abandon_count, B.parse_success_count, B.parse_abandon_percentage, B.parse_success_percentage," +
  368. "B.coupon_total_count, B.coupon_abandon_count, B.coupon_success_count, B.coupon_abandon_percentage, B.coupon_success_percentage," +
  369. "B.tool_total_count, B.tool_abandon_count, B.tool_success_count, B.tool_abandon_percentage, B.tool_success_percentage")
  370. .Where(filter, new { dailys_accountId, dailys_accountName, filterHours, stime, etime })
  371. .Page(size, page)
  372. .Order(orderBy)
  373. .PageList<TkReportDTO>(getTotal);
  374. foreach (var item in result.List)
  375. {
  376. if (item.total_count == 0)
  377. {
  378. item.total_count = await TkLogCore.GetTotalAsync($":total:tb:{item.report_date:yyyyMMddHH}");
  379. if (item.total_count > 0)
  380. {
  381. item.success_count = await TkLogCore.GetTotalAsync($":total:tb:success:{item.report_date:yyyyMMddHH}");
  382. item.abandon_count = await TkLogCore.GetTotalAsync($":total:tb:放弃转链:{item.report_date:yyyyMMddHH}");
  383. if (item.total_count > 0)
  384. {
  385. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  386. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  387. }
  388. }
  389. }
  390. if (item.parse_total_count == 0)
  391. {
  392. item.parse_total_count = await TkLogCore.GetTotalAsync($":parse_total:tb:{item.report_date:yyyyMMddHH}");
  393. if (item.parse_total_count > 0)
  394. {
  395. item.parse_success_count = await TkLogCore.GetTotalAsync($":parse_total:tb:success:{item.report_date:yyyyMMddHH}");
  396. item.parse_abandon_count = await TkLogCore.GetTotalAsync($":parse_total:tb:放弃转链:{item.report_date:yyyyMMddHH}");
  397. if (item.parse_total_count > 0)
  398. {
  399. item.parse_success_percentage = $"{item.parse_success_count / (double)item.parse_total_count * 100:f2}%";
  400. item.parse_abandon_percentage = $"{item.parse_abandon_count / (double)item.parse_total_count * 100:f2}%";
  401. }
  402. }
  403. }
  404. if (item.coupon_total_count == 0)
  405. {
  406. item.coupon_total_count = await TkLogCore.GetTotalAsync($":coupon_total:tb:{item.report_date:yyyyMMddHH}", false);
  407. if (item.coupon_total_count > 0)
  408. {
  409. item.coupon_success_count = await TkLogCore.GetTotalAsync($":coupon_total:tb:success:{item.report_date:yyyyMMddHH}", false);
  410. item.coupon_abandon_count = await TkLogCore.GetTotalAsync($":coupon_total:tb:放弃转链:{item.report_date:yyyyMMddHH}", false);
  411. if (item.coupon_total_count > 0)
  412. {
  413. item.coupon_success_percentage = $"{item.coupon_success_count / (double)item.coupon_total_count * 100:f2}%";
  414. item.coupon_abandon_percentage = $"{item.coupon_abandon_count / (double)item.coupon_total_count * 100:f2}%";
  415. }
  416. }
  417. }
  418. if (item.tool_total_count == 0)
  419. {
  420. item.tool_total_count = await TkLogCore.GetTotalAsync($":parse_total:tool:{item.report_date:yyyyMMddHH}");
  421. if (item.tool_total_count > 0)
  422. {
  423. item.tool_success_count = await TkLogCore.GetTotalAsync($":parse_total:tool:success:{item.report_date:yyyyMMddHH}");
  424. item.tool_abandon_count = await TkLogCore.GetTotalAsync($":parse_total:tool:放弃转链:{item.report_date:yyyyMMddHH}");
  425. if (item.tool_total_count > 0)
  426. {
  427. item.tool_success_percentage = $"{item.tool_success_count / (double)item.tool_total_count * 100:f2}%";
  428. item.tool_abandon_percentage = $"{item.tool_abandon_count / (double)item.tool_total_count * 100:f2}%";
  429. }
  430. }
  431. }
  432. }
  433. return new APIResult(new
  434. {
  435. data = result
  436. });
  437. }
  438. [HttpPost]
  439. public async Task<ActionResult> report_list([FromBody] JsonElement form)
  440. {
  441. int page = form.Read("current", 1);
  442. int size = form.Read("pageSize", 10);
  443. bool getTotal = form.Read("getTotal", true);
  444. string sort = form.Read<string>("sort");
  445. string order = form.Read<string>("order");
  446. var report_date = form.PathReadArray<string>("query_date[]");
  447. string accountName = form.Read<string>("accountName");
  448. string filter = string.Empty;
  449. if (!string.IsNullOrEmpty(accountName))
  450. {
  451. filter += $" AND accountName=@accountName";
  452. }
  453. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  454. if (report_date.Count == 2)
  455. {
  456. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  457. {
  458. etime = etime.AddDays(1).AddSeconds(-1);
  459. filter += $" AND report_date BETWEEN @stime AND @etime";
  460. }
  461. }
  462. filter = filter.StringTrimStart(" AND ");
  463. //排序
  464. string orderBy = "id DESC";
  465. if (!string.IsNullOrEmpty(order))
  466. {
  467. order = "descending".Equals(order) ? "DESC" : "ASC";
  468. orderBy = sort switch
  469. {
  470. _ => $"{sort} {order}",
  471. };
  472. }
  473. var result = new DBContext.Table("tk_report")
  474. .Where(filter, new { accountName, stime, etime })
  475. .Page(size, page)
  476. .Order(orderBy)
  477. .PageList<TkReportDTO>(getTotal);
  478. var tb_accounts = await TkPoolCore.AllListAsync();
  479. var tb_hide_ids = tb_accounts.Where(e => e.is_hide).Select(e => e.id).ToList();
  480. result.List = result.List.Where(e => !tb_hide_ids.Contains(e.accountId));
  481. foreach (var item in result.List)
  482. {
  483. #if DEBUG
  484. #else
  485. if (item.report_date.Date < DateTime.Now.Date) continue;
  486. #endif
  487. //var tk = TkPoolCore.GetOne(item.accountId);
  488. //if (tk != null) item.accountName = tk.company;
  489. string redisAccountName = $"{TkChannelEnum.tb}_{item.accountId}";
  490. if (item.total_count == 0)
  491. {
  492. string totalAccountName = redisAccountName;
  493. item.total_count = await TkLogCore.GetTotalAsync($":total:{totalAccountName}:{item.report_date:yyyyMMdd}");
  494. if (item.total_count == 0 && !string.Equals(item.accountName, totalAccountName, StringComparison.Ordinal))
  495. {
  496. //todo 过度用, 0613 跑两天就可以删掉
  497. totalAccountName = item.accountName;
  498. item.total_count = await TkLogCore.GetTotalAsync($":total:{totalAccountName}:{item.report_date:yyyyMMdd}");
  499. }
  500. if (item.total_count > 0)
  501. {
  502. item.success_count = await TkLogCore.GetTotalAsync($":total:{totalAccountName}:success:{item.report_date:yyyyMMdd}");
  503. item.abandon_count = await TkLogCore.GetTotalAsync($":total:{totalAccountName}:放弃转链:{item.report_date:yyyyMMdd}");
  504. if (item.total_count > 0)
  505. {
  506. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  507. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  508. }
  509. }
  510. }
  511. string parseAccountName = redisAccountName;
  512. int parseTotalCount = await TkLogCore.GetTotalAsync($":parse_total:{parseAccountName}:{item.report_date:yyyyMMdd}");
  513. if (parseTotalCount == 0 && !string.Equals(item.accountName, parseAccountName, StringComparison.Ordinal))
  514. {
  515. parseAccountName = item.accountName;
  516. parseTotalCount = await TkLogCore.GetTotalAsync($":parse_total:{parseAccountName}:{item.report_date:yyyyMMdd}");
  517. }
  518. // Today's DB row may be an earlier snapshot. Prefer the live Redis values
  519. // whenever they exist instead of only filling an initially-zero total.
  520. if (parseTotalCount > 0)
  521. {
  522. item.parse_total_count = parseTotalCount;
  523. item.parse_success_count = await TkLogCore.GetTotalAsync($":parse_total:{parseAccountName}:success:{item.report_date:yyyyMMdd}");
  524. item.parse_abandon_count = await TkLogCore.GetTotalAsync($":parse_total:{parseAccountName}:放弃转链:{item.report_date:yyyyMMdd}");
  525. item.parse_success_percentage = $"{item.parse_success_count / (double)item.parse_total_count * 100:f2}%";
  526. item.parse_abandon_percentage = $"{item.parse_abandon_count / (double)item.parse_total_count * 100:f2}%";
  527. }
  528. if (item.coupon_total_count == 0)
  529. {
  530. item.coupon_total_count = await TkLogCore.GetTotalAsync($":coupon_total:{redisAccountName}:{item.report_date:yyyyMMdd}", false);
  531. if (item.coupon_total_count > 0)
  532. {
  533. item.coupon_success_count = await TkLogCore.GetTotalAsync($":coupon_total:{redisAccountName}:success:{item.report_date:yyyyMMdd}", false);
  534. item.coupon_abandon_count = await TkLogCore.GetTotalAsync($":coupon_total:{redisAccountName}:放弃转链:{item.report_date:yyyyMMdd}", false);
  535. if (item.coupon_total_count > 0)
  536. {
  537. item.coupon_success_percentage = $"{item.coupon_success_count / (double)item.coupon_total_count * 100:f2}%";
  538. item.coupon_abandon_percentage = $"{item.coupon_abandon_count / (double)item.coupon_total_count * 100:f2}%";
  539. }
  540. }
  541. }
  542. if (item.tool_total_count == 0)
  543. {
  544. item.tool_total_count = await TkLogCore.GetTotalAsync($":parse_total:tool:{item.report_date:yyyyMMdd}");
  545. if (item.tool_total_count > 0)
  546. {
  547. item.tool_success_count = await TkLogCore.GetTotalAsync($":parse_total:tool:success:{item.report_date:yyyyMMdd}");
  548. item.tool_abandon_count = await TkLogCore.GetTotalAsync($":parse_total:tool:放弃转链:{item.report_date:yyyyMMdd}");
  549. if (item.tool_total_count > 0)
  550. {
  551. item.tool_success_percentage = $"{item.tool_success_count / (double)item.tool_total_count * 100:f2}%";
  552. item.tool_abandon_percentage = $"{item.tool_abandon_count / (double)item.tool_total_count * 100:f2}%";
  553. }
  554. }
  555. }
  556. }
  557. return new APIResult(new { data = result });
  558. }
  559. [HttpPost]
  560. public async Task<ActionResult> report_hourtrend([FromBody] JsonElement form)
  561. {
  562. int page = form.Read("current", 1);
  563. int size = form.Read("pageSize", 10);
  564. bool getTotal = form.Read("getTotal", true);
  565. string sort = form.Read<string>("sort");
  566. string order = form.Read<string>("order");
  567. var filterHours = form.Read<int?>("filterHours", null);
  568. string accountName = form.Read<string>("accountName");
  569. var report_date = form.PathReadArray<string>("query_date[]");
  570. int accountId = form.Read("accountId", 0);
  571. string filter = string.Empty;
  572. if (!string.IsNullOrEmpty(accountName))
  573. {
  574. filter += $" AND accountName=@accountName";
  575. }
  576. if (accountId != 0)
  577. {
  578. filter += $" AND accountId=@accountId";
  579. }
  580. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  581. if (report_date.Count == 2)
  582. {
  583. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  584. {
  585. etime = etime.AddDays(1).AddSeconds(-1);
  586. if (etime > DateTime.Now) etime = DateTime.Now;
  587. filter += $" AND report_date BETWEEN @stime AND @etime";
  588. }
  589. }
  590. if (filterHours != null)
  591. {
  592. filterHours--;
  593. filter += $" AND HOUR(report_date) = @filterHours";
  594. }
  595. filter = filter.StringTrimStart(" AND ");
  596. //排序
  597. string orderBy = "id ASC";
  598. if (!string.IsNullOrEmpty(order))
  599. {
  600. order = "descending".Equals(order) ? "DESC" : "ASC";
  601. orderBy = sort switch
  602. {
  603. _ => $"{sort} {order}",
  604. };
  605. }
  606. using var conn = DBContext.GetOpenConnection();
  607. var result = new DBContext.Table(conn, "tk_report_hourtrend")
  608. .Where(filter, new { accountId, stime, etime, accountName, filterHours })
  609. .Page(size, page)
  610. .Order(orderBy)
  611. .PageList<TkReportDTO>(getTotal);
  612. foreach (var item in result.List)
  613. {
  614. #if DEBUG
  615. #else
  616. if (item.report_date.Date < DateTime.Now.Date) continue;
  617. #endif
  618. accountName = $"{TkChannelEnum.tb}_{item.accountId}";
  619. if (item.total_count == 0)
  620. {
  621. item.total_count = await TkLogCore.GetTotalAsync($":total:{accountName}:{item.report_date:yyyyMMddHH}");
  622. if (item.total_count == 0)
  623. {
  624. //todo 过度用, 0613 跑两天就可以删掉
  625. accountName = item.accountName;
  626. item.total_count = await TkLogCore.GetTotalAsync($":total:{accountName}:{item.report_date:yyyyMMddHH}");
  627. }
  628. if (item.total_count == 0) continue;
  629. item.success_count = await TkLogCore.GetTotalAsync($":total:{item.accountName}:success:{item.report_date:yyyyMMddHH}");
  630. item.abandon_count = await TkLogCore.GetTotalAsync($":total:{item.accountName}:放弃转链:{item.report_date:yyyyMMddHH}");
  631. if (item.total_count > 0)
  632. {
  633. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  634. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  635. }
  636. }
  637. if (item.parse_total_count == 0)
  638. {
  639. item.parse_total_count = await TkLogCore.GetTotalAsync($":parse_total:{accountName}:{item.report_date:yyyyMMddHH}");
  640. if (item.parse_total_count > 0)
  641. {
  642. item.parse_success_count = await TkLogCore.GetTotalAsync($":parse_total:{item.accountName}:success:{item.report_date:yyyyMMddHH}");
  643. item.parse_abandon_count = await TkLogCore.GetTotalAsync($":parse_total:{item.accountName}:放弃转链:{item.report_date:yyyyMMddHH}");
  644. if (item.parse_total_count > 0)
  645. {
  646. item.parse_success_percentage = $"{item.parse_success_count / (double)item.parse_total_count * 100:f2}%";
  647. item.parse_abandon_percentage = $"{item.parse_abandon_count / (double)item.parse_total_count * 100:f2}%";
  648. }
  649. }
  650. }
  651. if (item.coupon_total_count == 0)
  652. {
  653. item.coupon_total_count = await TkLogCore.GetTotalAsync($":coupon_total:{accountName}:{item.report_date:yyyyMMddHH}", false);
  654. if (item.coupon_total_count > 0)
  655. {
  656. item.coupon_success_count = await TkLogCore.GetTotalAsync($":coupon_total:{item.accountName}:success:{item.report_date:yyyyMMddHH}", false);
  657. item.coupon_abandon_count = await TkLogCore.GetTotalAsync($":coupon_total:{item.accountName}:放弃转链:{item.report_date:yyyyMMddHH}", false);
  658. if (item.coupon_total_count > 0)
  659. {
  660. item.coupon_success_percentage = $"{item.coupon_success_count / (double)item.coupon_total_count * 100:f2}%";
  661. item.coupon_abandon_percentage = $"{item.coupon_abandon_count / (double)item.coupon_total_count * 100:f2}%";
  662. }
  663. }
  664. }
  665. if (item.tool_total_count == 0)
  666. {
  667. item.tool_total_count = await TkLogCore.GetTotalAsync($":tool_total:{accountName}:{item.report_date:yyyyMMddHH}");
  668. if (item.tool_total_count > 0)
  669. {
  670. item.tool_success_count = await TkLogCore.GetTotalAsync($":tool_total:{item.accountName}:success:{item.report_date:yyyyMMddHH}");
  671. item.tool_abandon_count = await TkLogCore.GetTotalAsync($":tool_total:{item.accountName}:放弃转链:{item.report_date:yyyyMMddHH}");
  672. if (item.tool_total_count > 0)
  673. {
  674. item.tool_success_percentage = $"{item.tool_success_count / (double)item.tool_total_count * 100:f2}%";
  675. item.tool_abandon_percentage = $"{item.tool_abandon_count / (double)item.tool_total_count * 100:f2}%";
  676. }
  677. }
  678. }
  679. }
  680. return new APIResult(new { data = result });
  681. }
  682. [HttpPost]
  683. public async Task<ActionResult> chartData([FromBody] JsonElement form)
  684. {
  685. var result = new List<dynamic>();
  686. var reason_result = new List<dynamic>();
  687. var report_date = form.PathReadArray<string>("query_date[]");
  688. var accountName = form.Read("accountName", string.Empty);
  689. var accountId = form.Read("accountId", 0);
  690. var isHourtrend = form.Read<bool>("isLeaf", false);
  691. var total_key = form.Read("total_key", "total");
  692. DateTime stime = DateTime.MinValue;
  693. if (!DateTime.TryParse(report_date[0], out stime)) return new APIResult(new { data = result, data2 = reason_result });
  694. string _report_date = stime.ToString("yyyyMMdd");
  695. if (isHourtrend) _report_date = stime.ToString("yyyyMMddHH");
  696. if (string.IsNullOrEmpty(accountName)) accountName = "tb";
  697. if (total_key == "tool_total")
  698. {
  699. accountName = "tool";
  700. total_key = "parse_total";
  701. string cacheKey = $":{total_key}:{accountName}:{_report_date}";
  702. double total = await TkLogCore.GetTotalAsync(cacheKey);
  703. if (total > 0)
  704. {
  705. string[] keys = ["bdpan", "wemeet"];
  706. foreach (var keyname in keys)
  707. {
  708. // string[] keys = ["bdpan:success", "bdpan:fail", "wemeet:success", "wemeet:fail"];
  709. cacheKey = $":{total_key}:{keyname}:{_report_date}";
  710. total = await TkLogCore.GetTotalAsync(cacheKey);
  711. cacheKey = $":{total_key}:{keyname}:success:{_report_date}";
  712. int value = await TkLogCore.GetTotalAsync(cacheKey);
  713. if (value == 0) continue;
  714. result.Add(new { name = $"{keyname}-成功", value, increases = Math.Round(value / total * 100, 2) });
  715. cacheKey = $":{total_key}:{keyname}:fail:{_report_date}";
  716. value = await TkLogCore.GetTotalAsync(cacheKey);
  717. if (value == 0) continue;
  718. result.Add(new { name = $"{keyname}-失败", value, increases = Math.Round(value / total * 100, 2) });
  719. }
  720. cacheKey = $":{total_key}:{accountName}:reason:{_report_date}";
  721. string[] reason_keys = await TkLogCore.GetTotalKeysAsync(cacheKey);
  722. foreach (var keyname in reason_keys)
  723. {
  724. cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
  725. int value = await TkLogCore.GetTotalAsync(cacheKey);
  726. if (value == 0) continue;
  727. reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  728. }
  729. reason_result = reason_result.OrderByDescending(item => item.value).ToList();
  730. }
  731. return new APIResult(new { total, data = result, data2 = reason_result });
  732. }
  733. else
  734. {
  735. // The report row is identified by tk_pool.id. This is independent of the
  736. // riskStrategy/launchScene label (for example tbpush-220), which can be shared
  737. // by multiple accounts.
  738. string requestedAccountName = accountName;
  739. if (accountId > 0)
  740. {
  741. accountName = $"{TkChannelEnum.tb}_{accountId}";
  742. }
  743. string cacheKey = $":{total_key}:{accountName}:{_report_date}";
  744. double total = await TkLogCore.GetTotalAsync(cacheKey, false);
  745. if (total == 0 &&
  746. accountId > 0 &&
  747. !string.IsNullOrEmpty(requestedAccountName) &&
  748. !string.Equals(requestedAccountName, "all", StringComparison.OrdinalIgnoreCase) &&
  749. !string.Equals(requestedAccountName, TkChannelEnum.tb.ToString(), StringComparison.OrdinalIgnoreCase) &&
  750. !string.Equals(requestedAccountName, accountName, StringComparison.Ordinal))
  751. {
  752. cacheKey = $":{total_key}:{requestedAccountName}:{_report_date}";
  753. total = await TkLogCore.GetTotalAsync(cacheKey, false);
  754. if (total > 0)
  755. {
  756. accountName = requestedAccountName;
  757. }
  758. }
  759. if (total > 0)
  760. {
  761. string[] keys = ["success"];
  762. cacheKey = $":{total_key}:{accountName}:message:{_report_date}";
  763. string[] message_keys = await TkLogCore.GetTotalKeysAsync(cacheKey, false);
  764. var combinedKeys = keys.Union(message_keys).ToList();
  765. combinedKeys.RemoveAll(item => item == "fail");
  766. combinedKeys.RemoveAll(item => item == "OK");
  767. foreach (var keyname in combinedKeys)
  768. {
  769. cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
  770. int value = await TkLogCore.GetTotalAsync(cacheKey, false);
  771. if (value == 0) continue;
  772. result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  773. }
  774. result = result.OrderByDescending(item => item.value).ToList();
  775. cacheKey = $":{total_key}:{accountName}:reason:{_report_date}";
  776. string[] reason_keys = await TkLogCore.GetTotalKeysAsync(cacheKey, false);
  777. foreach (var keyname in reason_keys)
  778. {
  779. if ("没有优惠券".Equals(keyname)) continue;
  780. cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
  781. int value = await TkLogCore.GetTotalAsync(cacheKey, false);
  782. if (value < 50) continue;
  783. reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  784. }
  785. reason_result = reason_result.OrderByDescending(item => item.value).ToList();
  786. }
  787. return new APIResult(new { total, data = result, data2 = reason_result });
  788. }
  789. }
  790. [HttpPost]
  791. public async Task<ActionResult> chartData2([FromBody] JsonElement form)
  792. {
  793. var result = new List<dynamic>();
  794. var reason_result = new List<dynamic>();
  795. var report_date = form.PathReadArray<string>("query_date[]");
  796. var accountName = form.Read("accountName", string.Empty);
  797. var isHourtrend = form.Read<bool>("isLeaf", false);
  798. var total_key = form.Read("total_key", "total");
  799. if (string.IsNullOrEmpty(accountName)) accountName = "tb";
  800. DateTime stime = DateTime.MinValue;
  801. if (!DateTime.TryParse(report_date[0], out stime)) return new APIResult(new { data = result, data2 = reason_result });
  802. string _report_date = stime.ToString("yyyyMMdd");
  803. if (isHourtrend) _report_date = stime.ToString("yyyyMMddHH");
  804. string cacheKey = $":{total_key}:{accountName}:{_report_date}";
  805. double total = await TkLogCore.GetTotalAsync(cacheKey);
  806. if (total > 0)
  807. {
  808. string[] keys = ["success"];
  809. cacheKey = $":{total_key}:{accountName}:message:{_report_date}";
  810. string[] message_keys = await TkLogCore.GetTotalKeysAsync(cacheKey);
  811. var combinedKeys = keys.Union(message_keys).ToList();
  812. combinedKeys.RemoveAll(item => item == "fail");
  813. combinedKeys.RemoveAll(item => item == "OK");
  814. foreach (var keyname in combinedKeys)
  815. {
  816. cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
  817. int value = await TkLogCore.GetTotalAsync(cacheKey);
  818. if (value == 0) continue;
  819. result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  820. }
  821. result = result.OrderByDescending(item => item.value).ToList();
  822. cacheKey = $":{total_key}:{accountName}:reason:{_report_date}";
  823. string[] reason_keys = await TkLogCore.GetTotalKeysAsync(cacheKey);
  824. foreach (var keyname in reason_keys)
  825. {
  826. if ("没有优惠券".Equals(keyname)) continue;
  827. cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
  828. int value = await TkLogCore.GetTotalAsync(cacheKey);
  829. if (value == 0) continue;
  830. reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  831. }
  832. reason_result = reason_result.OrderByDescending(item => item.value).ToList();
  833. }
  834. return new APIResult(new { total, data = result, data2 = reason_result });
  835. }
  836. [HttpPost]
  837. public ActionResult settle_bills_total([FromBody] JsonElement form)
  838. {
  839. int page = form.Read("current", 1);
  840. int size = form.Read("pageSize", 10);
  841. bool getTotal = form.Read("getTotal", true);
  842. string sort = form.Read<string>("sort");
  843. string order = form.Read<string>("order");
  844. var belongTime = form.PathReadArray<string>("query_date[]");
  845. string filter = string.Empty;
  846. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  847. if (belongTime.Count == 2)
  848. {
  849. if (DateTime.TryParse(belongTime[0], out stime) && DateTime.TryParse(belongTime[1], out etime))
  850. {
  851. etime = etime.AddDays(1).AddSeconds(-1);
  852. filter += $" AND belongTime BETWEEN @stime AND @etime";
  853. }
  854. }
  855. filter = filter.StringTrimStart(" AND ");
  856. //排序
  857. string orderBy = "belongTime DESC";
  858. if (!string.IsNullOrEmpty(order))
  859. {
  860. order = "descending".Equals(order) ? "DESC" : "ASC";
  861. orderBy = sort switch
  862. {
  863. _ => $"{sort} {order}",
  864. };
  865. }
  866. var result = new DBContext.Table("v_tk_report_bills")
  867. .Where(filter, new { stime, etime })
  868. .Page(size, page)
  869. .Order(orderBy)
  870. .PageList<TkSettleBillDTO>(getTotal);
  871. return new APIResult(new { data = result });
  872. }
  873. [HttpPost]
  874. public ActionResult settle_bills([FromBody] JsonElement form)
  875. {
  876. int page = form.Read("current", 1);
  877. int size = form.Read("pageSize", 10);
  878. bool getTotal = form.Read("getTotal", true);
  879. string sort = form.Read<string>("sort");
  880. string order = form.Read<string>("order");
  881. string name = form.Read<string>("keyword");
  882. var belongTime = form.PathReadArray<string>("query_date[]");
  883. string filter = string.Empty;
  884. if (!string.IsNullOrEmpty(name))
  885. {
  886. filter += $" AND accountId IN (SELECT ID FROM tk_pool WHERE company=@name)";
  887. }
  888. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  889. if (belongTime.Count == 2)
  890. {
  891. if (DateTime.TryParse(belongTime[0], out stime) && DateTime.TryParse(belongTime[1], out etime))
  892. {
  893. etime = etime.AddDays(1).AddSeconds(-1);
  894. filter += $" AND belongTime BETWEEN @stime AND @etime";
  895. }
  896. }
  897. filter = filter.StringTrimStart(" AND ");
  898. //排序
  899. string orderBy = "belongTime DESC";
  900. if (!string.IsNullOrEmpty(order))
  901. {
  902. order = "descending".Equals(order) ? "DESC" : "ASC";
  903. orderBy = sort switch
  904. {
  905. _ => $"{sort} {order}",
  906. };
  907. }
  908. var result = new DBContext.Table("tk_settle_bill")
  909. .Where(filter, new { name, stime, etime })
  910. .Page(size, page)
  911. .Order(orderBy)
  912. .PageList<TkSettleBillDTO>(getTotal);
  913. //foreach (var item in result.List)
  914. //{
  915. // if (string.IsNullOrEmpty(name))
  916. // {
  917. // item.accountName = "-";
  918. // }
  919. //}
  920. return new APIResult(new { data = result });
  921. }
  922. }
  923. }