TaobaoController.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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. namespace molilian.api.Controllers
  10. {
  11. [ApiController]
  12. [MyAuthorize("admin")]
  13. [Route("api/[controller]/[action]")]
  14. public class TaobaoController : ControllerBase
  15. {
  16. readonly IAuthorizationProvider provider = new AdminProvider();
  17. protected IHttpContextAccessor _accessor;
  18. public TaobaoController(IHttpContextAccessor accessor)
  19. {
  20. _accessor = accessor;
  21. }
  22. [HttpPost]
  23. public async Task<ActionResult> updateCookies([FromBody] JsonElement form)
  24. {
  25. var clientIp = _accessor.HttpContext.GetUserIp();
  26. var token = provider.Get(_accessor.HttpContext);
  27. string user_agent = _accessor.HttpContext.Request.UserAgent();
  28. string cookie = form.Read<string>("cookie", string.Empty);
  29. var success = TkPoolCore.UpdateCookies(cookie, user_agent);
  30. if (success)
  31. {
  32. //string desc = ObjectComparer.PrintCompareToString(old, form).Trim();
  33. OperationLogCore.LogOperation(token.AccessKey, clientIp, "tk_pool:cookie", string.Empty, cookie);
  34. }
  35. return new APIResult(new
  36. {
  37. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入的cookie" },
  38. });
  39. }
  40. [HttpPost]
  41. public async Task<ActionResult> update([FromBody] JsonElement form)
  42. {
  43. var clientIp = _accessor.HttpContext.GetUserIp();
  44. var token = provider.Get(_accessor.HttpContext);
  45. int id = form.Read<int>("id", 0);
  46. string name = form.Read<string>("name", string.Empty);
  47. string val = form.Read<string>("val", string.Empty);
  48. if (id == 0)
  49. {
  50. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入的cookie" } });
  51. }
  52. int result;
  53. switch (name)
  54. {
  55. case "enable_fake_click":
  56. case "enable_parse":
  57. case "enable_coupon":
  58. case "enable_sync_order":
  59. case "enable_promotionQuery":
  60. case "status":
  61. bool bVal = val.Equals("True");
  62. result = new DBContext.Table("tk_pool")
  63. .Add(name, bVal)
  64. .Add("last_time", DateTime.Now)
  65. .Where("id=@id", new { id })
  66. .Update();
  67. break;
  68. default:
  69. return new APIResult(new { data = new { success = false, msg = "更新失败,未授权操作" } });
  70. }
  71. bool success = result > 0;
  72. if (success)
  73. {
  74. //string desc = ObjectComparer.PrintCompareToString(old, form).Trim();
  75. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"tk_pool:{name}", string.Empty, val);
  76. await EndPointCore.NotifyReload(true);
  77. }
  78. return new APIResult(new
  79. {
  80. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  81. });
  82. }
  83. /// <summary>
  84. /// 账号列表
  85. /// </summary>
  86. /// <param name="form"></param>
  87. /// <returns></returns>
  88. [HttpPost]
  89. public ActionResult list([FromBody] JsonElement form)
  90. {
  91. var token = provider.Get(_accessor.HttpContext);
  92. int page = form.Read("current", 1);
  93. int size = form.Read("pageSize", 10);
  94. string keyword = form.Read("name", string.Empty);
  95. bool getTotal = form.Read("getTotal", true);
  96. string _status = form.Read("status", string.Empty);
  97. int status = _status switch
  98. {
  99. "online" => 1,
  100. "offline" => 0,
  101. _ => -1,
  102. };
  103. var lastTime = form.PathReadArray<string>("lastTime[]");
  104. string sort = form.Read<string>("sort");
  105. string order = form.Read<string>("order");
  106. string filter = string.Empty;
  107. if (!string.IsNullOrEmpty(keyword))
  108. {
  109. filter += $" AND (`name` LIKE @keyword OR `description` LIKE @keyword)";
  110. keyword = $"%{keyword}%";
  111. }
  112. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  113. if (lastTime.Count == 2)
  114. {
  115. if (DateTime.TryParse(lastTime[0], out stime) && DateTime.TryParse(lastTime[1], out etime))
  116. {
  117. etime = etime.AddDays(1).AddSeconds(-1);
  118. filter += $" AND last_time BETWEEN @stime AND @etime";
  119. }
  120. }
  121. if (status != -1)
  122. {
  123. filter += $" AND status=@status";
  124. }
  125. filter = filter.StringTrimStart(" AND ");
  126. //排序
  127. string orderBy = "id DESC";
  128. if (!string.IsNullOrEmpty(order))
  129. {
  130. order = "descending".Equals(order) ? "DESC" : "ASC";
  131. orderBy = sort switch
  132. {
  133. //"num" => $"num {order}",
  134. _ => $"{sort} {order}",
  135. };
  136. }
  137. using var conn = DBContext.GetOpenConnection();
  138. var result = new DBContext.Table(conn, "tk_pool")
  139. .Where(filter, new { keyword, status, stime, etime })
  140. .Page(size, page)
  141. .Order(orderBy)
  142. .PageList<TkPoolDTO>(getTotal);
  143. foreach (var item in result.List)
  144. {
  145. item.current_amt = Math.Round(TkPoolCore.GetIncomeAmt($"{item.id}"), 2);
  146. item.cookies = string.Empty;
  147. }
  148. return new APIResult(new { data = result });
  149. }
  150. [HttpPost]
  151. public ActionResult report_dailys([FromBody] JsonElement form)
  152. {
  153. int page = form.Read("current", 1);
  154. int size = form.Read("pageSize", 10);
  155. bool getTotal = form.Read("getTotal", true);
  156. string sort = form.Read<string>("sort");
  157. string order = form.Read<string>("order");
  158. var report_date = form.PathReadArray<string>("query_date[]");
  159. string dailys_accountName = "all";
  160. int dailys_accountId = 0;
  161. string filter = string.Empty;
  162. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  163. if (report_date.Count == 2)
  164. {
  165. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  166. {
  167. etime = etime.AddDays(1).AddSeconds(-1);
  168. filter += $" AND A.report_date BETWEEN @stime AND @etime";
  169. }
  170. }
  171. filter = filter.StringTrimStart(" AND ");
  172. //排序
  173. string orderBy = "A.report_date DESC";
  174. if (!string.IsNullOrEmpty(order))
  175. {
  176. order = "descending".Equals(order) ? "DESC" : "ASC";
  177. orderBy = sort switch
  178. {
  179. _ => $"A.{sort} {order}",
  180. };
  181. }
  182. var result = new DBContext.Table("v_tk_report_dailys")
  183. .LeftJoin("daily_logs",
  184. "B.log_date = A.report_date AND B.channel=0 AND B.accountId=@dailys_accountId",
  185. "total_count, B.abandon_count, B.success_count, B.abandon_percentage, B.success_percentage," +
  186. "B.parse_total_count, B.parse_abandon_count, B.parse_success_count, B.parse_abandon_percentage, B.parse_success_percentage," +
  187. "B.coupon_total_count, B.coupon_abandon_count, B.coupon_success_count, B.coupon_abandon_percentage, B.coupon_success_percentage," +
  188. "B.tool_total_count, B.tool_abandon_count, B.tool_success_count, B.tool_abandon_percentage, B.tool_success_percentage")
  189. .Where(filter, new { dailys_accountId, dailys_accountName, stime, etime })
  190. .Page(size, page)
  191. .Order(orderBy)
  192. .PageList<TkReportDTO>(getTotal);
  193. foreach (var item in result.List)
  194. {
  195. if (item.report_date.Date < DateTime.Now.Date) continue;
  196. if (item.total_count == 0)
  197. {
  198. item.total_count = TkLogCore.GetTotal($":total:tb:{item.report_date:yyyyMMdd}");
  199. if (item.total_count > 0)
  200. {
  201. item.success_count = TkLogCore.GetTotal($":total:tb:success:{item.report_date:yyyyMMdd}");
  202. item.abandon_count = TkLogCore.GetTotal($":total:tb:放弃转链:{item.report_date:yyyyMMdd}");
  203. if (item.total_count > 0)
  204. {
  205. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  206. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  207. }
  208. }
  209. }
  210. if (item.parse_total_count == 0)
  211. {
  212. item.parse_total_count = TkLogCore.GetTotal($":parse_total:tb:{item.report_date:yyyyMMdd}");
  213. if (item.parse_total_count > 0)
  214. {
  215. item.parse_success_count = TkLogCore.GetTotal($":parse_total:tb:success:{item.report_date:yyyyMMdd}");
  216. item.parse_abandon_count = TkLogCore.GetTotal($":parse_total:tb:放弃转链:{item.report_date:yyyyMMdd}");
  217. if (item.parse_total_count > 0)
  218. {
  219. item.parse_success_percentage = $"{item.parse_success_count / (double)item.parse_total_count * 100:f2}%";
  220. item.parse_abandon_percentage = $"{item.parse_abandon_count / (double)item.parse_total_count * 100:f2}%";
  221. }
  222. }
  223. }
  224. if (item.coupon_total_count == 0)
  225. {
  226. item.coupon_total_count = TkLogCore.GetTotal($":coupon_total:tb:{item.report_date:yyyyMMdd}");
  227. if (item.coupon_total_count > 0)
  228. {
  229. item.coupon_success_count = TkLogCore.GetTotal($":coupon_total:tb:success:{item.report_date:yyyyMMdd}");
  230. item.coupon_abandon_count = TkLogCore.GetTotal($":coupon_total:tb:放弃转链:{item.report_date:yyyyMMdd}");
  231. if (item.coupon_total_count > 0)
  232. {
  233. item.coupon_success_percentage = $"{item.coupon_success_count / (double)item.coupon_total_count * 100:f2}%";
  234. item.coupon_abandon_percentage = $"{item.coupon_abandon_count / (double)item.coupon_total_count * 100:f2}%";
  235. }
  236. }
  237. }
  238. if (item.tool_total_count == 0)
  239. {
  240. item.tool_total_count = TkLogCore.GetTotal($":parse_total:tool:{item.report_date:yyyyMMdd}");
  241. if (item.tool_total_count > 0)
  242. {
  243. item.tool_success_count = TkLogCore.GetTotal($":parse_total:tool:success:{item.report_date:yyyyMMdd}");
  244. item.tool_abandon_count = TkLogCore.GetTotal($":parse_total:tool:放弃转链:{item.report_date:yyyyMMdd}");
  245. if (item.tool_total_count > 0)
  246. {
  247. item.tool_success_percentage = $"{item.tool_success_count / (double)item.tool_total_count * 100:f2}%";
  248. item.tool_abandon_percentage = $"{item.tool_abandon_count / (double)item.tool_total_count * 100:f2}%";
  249. }
  250. }
  251. }
  252. }
  253. return new APIResult(new
  254. {
  255. data = result
  256. });
  257. }
  258. [HttpPost]
  259. public ActionResult report_list([FromBody] JsonElement form)
  260. {
  261. int page = form.Read("current", 1);
  262. int size = form.Read("pageSize", 10);
  263. bool getTotal = form.Read("getTotal", true);
  264. string sort = form.Read<string>("sort");
  265. string order = form.Read<string>("order");
  266. var report_date = form.PathReadArray<string>("query_date[]");
  267. int accountId = form.Read("accountId", 0);
  268. string filter = string.Empty;
  269. if (accountId != 0)
  270. {
  271. filter += $" AND A.accountId=@accountId";
  272. }
  273. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  274. if (report_date.Count == 2)
  275. {
  276. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  277. {
  278. etime = etime.AddDays(1).AddSeconds(-1);
  279. filter += $" AND A.report_date BETWEEN @stime AND @etime";
  280. }
  281. }
  282. filter = filter.StringTrimStart(" AND ");
  283. //排序
  284. string orderBy = "A.id DESC";
  285. if (!string.IsNullOrEmpty(order))
  286. {
  287. order = "descending".Equals(order) ? "DESC" : "ASC";
  288. orderBy = sort switch
  289. {
  290. _ => $"A.{sort} {order}",
  291. };
  292. }
  293. var result = new DBContext.Table("tk_report")
  294. .LeftJoin("daily_logs",
  295. "B.log_date = A.report_date AND B.channel=0 AND B.accountId=A.accountId",
  296. "total_count, B.abandon_count, B.success_count, B.abandon_percentage, B.success_percentage," +
  297. "B.parse_total_count, B.parse_abandon_count, B.parse_success_count, B.parse_abandon_percentage, B.parse_success_percentage," +
  298. "B.coupon_total_count, B.coupon_abandon_count, B.coupon_success_count, B.coupon_abandon_percentage, B.coupon_success_percentage," +
  299. "B.tool_total_count, B.tool_abandon_count, B.tool_success_count, B.tool_abandon_percentage, B.tool_success_percentage")
  300. .Where(filter, new { accountId, stime, etime })
  301. .Page(size, page)
  302. .Order(orderBy)
  303. .PageList<TkReportDTO>(getTotal);
  304. foreach (var item in result.List)
  305. {
  306. #if DEBUG
  307. #else
  308. if (item.report_date.Date < DateTime.Now.Date) continue;
  309. #endif
  310. //var tk = TkPoolCore.GetOne(item.accountId);
  311. //if (tk != null) item.accountName = tk.company;
  312. string accountName = $"{TkChannelEnum.tb}_{item.accountId}";
  313. if (item.total_count == 0)
  314. {
  315. item.total_count = TkLogCore.GetTotal($":total:{accountName}:{item.report_date:yyyyMMdd}");
  316. if (item.total_count > 0)
  317. {
  318. //todo 过度用, 0613 跑两天就可以删掉
  319. accountName = item.accountName;
  320. item.total_count = TkLogCore.GetTotal($":total:{accountName}:{item.report_date:yyyyMMdd}");
  321. }
  322. if (item.total_count > 0)
  323. {
  324. item.success_count = TkLogCore.GetTotal($":total:{accountName}:success:{item.report_date:yyyyMMdd}");
  325. item.abandon_count = TkLogCore.GetTotal($":total:{accountName}:放弃转链:{item.report_date:yyyyMMdd}");
  326. if (item.total_count > 0)
  327. {
  328. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  329. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  330. }
  331. }
  332. }
  333. if (item.parse_total_count == 0)
  334. {
  335. item.parse_total_count = TkLogCore.GetTotal($":parse_total:{accountName}:{item.report_date:yyyyMMdd}");
  336. if (item.parse_total_count > 0)
  337. {
  338. item.parse_success_count = TkLogCore.GetTotal($":parse_total:{accountName}:success:{item.report_date:yyyyMMdd}");
  339. item.parse_abandon_count = TkLogCore.GetTotal($":parse_total:{accountName}:放弃转链:{item.report_date:yyyyMMdd}");
  340. if (item.parse_total_count > 0)
  341. {
  342. item.parse_success_percentage = $"{item.parse_success_count / (double)item.parse_total_count * 100:f2}%";
  343. item.parse_abandon_percentage = $"{item.parse_abandon_count / (double)item.parse_total_count * 100:f2}%";
  344. }
  345. }
  346. }
  347. if (item.coupon_total_count == 0)
  348. {
  349. item.coupon_total_count = TkLogCore.GetTotal($":coupon_total:{accountName}:{item.report_date:yyyyMMdd}");
  350. if (item.coupon_total_count > 0)
  351. {
  352. item.coupon_success_count = TkLogCore.GetTotal($":coupon_total:{accountName}:success:{item.report_date:yyyyMMdd}");
  353. item.coupon_abandon_count = TkLogCore.GetTotal($":coupon_total:{accountName}:放弃转链:{item.report_date:yyyyMMdd}");
  354. if (item.coupon_total_count > 0)
  355. {
  356. item.coupon_success_percentage = $"{item.coupon_success_count / (double)item.coupon_total_count * 100:f2}%";
  357. item.coupon_abandon_percentage = $"{item.coupon_abandon_count / (double)item.coupon_total_count * 100:f2}%";
  358. }
  359. }
  360. }
  361. if (item.tool_total_count == 0)
  362. {
  363. item.tool_total_count = TkLogCore.GetTotal($":parse_total:tool:{item.report_date:yyyyMMdd}");
  364. if (item.tool_total_count > 0)
  365. {
  366. item.tool_success_count = TkLogCore.GetTotal($":parse_total:tool:success:{item.report_date:yyyyMMdd}");
  367. item.tool_abandon_count = TkLogCore.GetTotal($":parse_total:tool:放弃转链:{item.report_date:yyyyMMdd}");
  368. if (item.tool_total_count > 0)
  369. {
  370. item.tool_success_percentage = $"{item.tool_success_count / (double)item.tool_total_count * 100:f2}%";
  371. item.tool_abandon_percentage = $"{item.tool_abandon_count / (double)item.tool_total_count * 100:f2}%";
  372. }
  373. }
  374. }
  375. }
  376. return new APIResult(new { data = result });
  377. }
  378. [HttpPost]
  379. public ActionResult report_hourtrend([FromBody] JsonElement form)
  380. {
  381. int page = form.Read("current", 1);
  382. int size = form.Read("pageSize", 10);
  383. bool getTotal = form.Read("getTotal", true);
  384. string sort = form.Read<string>("sort");
  385. string order = form.Read<string>("order");
  386. var report_date = form.PathReadArray<string>("query_date[]");
  387. int accountId = form.Read("accountId", 0);
  388. string filter = string.Empty;
  389. if (accountId != 0)
  390. {
  391. filter += $" AND accountId=@accountId";
  392. }
  393. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  394. if (report_date.Count == 2)
  395. {
  396. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  397. {
  398. etime = etime.AddDays(1).AddSeconds(-1);
  399. if (etime > DateTime.Now) etime = DateTime.Now;
  400. filter += $" AND report_date BETWEEN @stime AND @etime";
  401. }
  402. }
  403. filter = filter.StringTrimStart(" AND ");
  404. //排序
  405. string orderBy = "id ASC";
  406. if (!string.IsNullOrEmpty(order))
  407. {
  408. order = "descending".Equals(order) ? "DESC" : "ASC";
  409. orderBy = sort switch
  410. {
  411. _ => $"{sort} {order}",
  412. };
  413. }
  414. using var conn = DBContext.GetOpenConnection();
  415. var result = new DBContext.Table(conn, "tk_report_hourtrend")
  416. .Where(filter, new { accountId, stime, etime })
  417. .Page(size, page)
  418. .Order(orderBy)
  419. .PageList<TkReportDTO>(getTotal);
  420. foreach (var item in result.List)
  421. {
  422. #if DEBUG
  423. #else
  424. if (item.report_date.Date < DateTime.Now.Date) continue;
  425. #endif
  426. string accountName = $"{TkChannelEnum.tb}_{item.accountId}";
  427. if (item.total_count == 0)
  428. {
  429. item.total_count = TkLogCore.GetTotal($":total:{accountName}:{item.report_date:yyyyMMddHH}");
  430. if (item.total_count == 0)
  431. {
  432. //todo 过度用, 0613 跑两天就可以删掉
  433. accountName = item.accountName;
  434. item.total_count = TkLogCore.GetTotal($":total:{accountName}:{item.report_date:yyyyMMddHH}");
  435. }
  436. if (item.total_count == 0) continue;
  437. item.success_count = TkLogCore.GetTotal($":total:{item.accountName}:success:{item.report_date:yyyyMMddHH}");
  438. item.abandon_count = TkLogCore.GetTotal($":total:{item.accountName}:放弃转链:{item.report_date:yyyyMMddHH}");
  439. if (item.total_count > 0)
  440. {
  441. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  442. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  443. }
  444. }
  445. if (item.parse_total_count == 0)
  446. {
  447. item.parse_total_count = TkLogCore.GetTotal($":parse_total:{accountName}:{item.report_date:yyyyMMddHH}");
  448. if (item.parse_total_count > 0)
  449. {
  450. item.parse_success_count = TkLogCore.GetTotal($":parse_total:{item.accountName}:success:{item.report_date:yyyyMMddHH}");
  451. item.parse_abandon_count = TkLogCore.GetTotal($":parse_total:{item.accountName}:放弃转链:{item.report_date:yyyyMMddHH}");
  452. if (item.parse_total_count > 0)
  453. {
  454. item.parse_success_percentage = $"{item.parse_success_count / (double)item.parse_total_count * 100:f2}%";
  455. item.parse_abandon_percentage = $"{item.parse_abandon_count / (double)item.parse_total_count * 100:f2}%";
  456. }
  457. }
  458. }
  459. if (item.coupon_total_count == 0)
  460. {
  461. item.coupon_total_count = TkLogCore.GetTotal($":coupon_total:{accountName}:{item.report_date:yyyyMMddHH}");
  462. if (item.coupon_total_count > 0)
  463. {
  464. item.coupon_success_count = TkLogCore.GetTotal($":coupon_total:{item.accountName}:success:{item.report_date:yyyyMMddHH}");
  465. item.coupon_abandon_count = TkLogCore.GetTotal($":coupon_total:{item.accountName}:放弃转链:{item.report_date:yyyyMMddHH}");
  466. if (item.coupon_total_count > 0)
  467. {
  468. item.coupon_success_percentage = $"{item.coupon_success_count / (double)item.coupon_total_count * 100:f2}%";
  469. item.coupon_abandon_percentage = $"{item.coupon_abandon_count / (double)item.coupon_total_count * 100:f2}%";
  470. }
  471. }
  472. }
  473. if (item.tool_total_count == 0)
  474. {
  475. item.tool_total_count = TkLogCore.GetTotal($":tool_total:{accountName}:{item.report_date:yyyyMMddHH}");
  476. if (item.tool_total_count > 0)
  477. {
  478. item.tool_success_count = TkLogCore.GetTotal($":tool_total:{item.accountName}:success:{item.report_date:yyyyMMddHH}");
  479. item.tool_abandon_count = TkLogCore.GetTotal($":tool_total:{item.accountName}:放弃转链:{item.report_date:yyyyMMddHH}");
  480. if (item.tool_total_count > 0)
  481. {
  482. item.tool_success_percentage = $"{item.tool_success_count / (double)item.tool_total_count * 100:f2}%";
  483. item.tool_abandon_percentage = $"{item.tool_abandon_count / (double)item.tool_total_count * 100:f2}%";
  484. }
  485. }
  486. }
  487. }
  488. return new APIResult(new { data = result });
  489. }
  490. [HttpPost]
  491. public ActionResult chartData([FromBody] JsonElement form)
  492. {
  493. var result = new List<dynamic>();
  494. var reason_result = new List<dynamic>();
  495. var report_date = form.PathReadArray<string>("query_date[]");
  496. var accountName = form.Read("accountName", string.Empty);
  497. var isHourtrend = form.Read<bool>("isLeaf", false);
  498. var total_key = form.Read("total_key", "total");
  499. DateTime stime = DateTime.MinValue;
  500. if (!DateTime.TryParse(report_date[0], out stime)) return new APIResult(new { data = result, data2 = reason_result });
  501. string _report_date = stime.ToString("yyyyMMdd");
  502. if (isHourtrend) _report_date = stime.ToString("yyyyMMddHH");
  503. if (string.IsNullOrEmpty(accountName)) accountName = "tb";
  504. if (total_key == "tool_total")
  505. {
  506. accountName = "tool";
  507. total_key = "parse_total";
  508. string cacheKey = $":{total_key}:{accountName}:{_report_date}";
  509. double total = TkLogCore.GetTotal(cacheKey);
  510. if (total > 0)
  511. {
  512. string[] keys = ["bdpan", "wemeet"];
  513. foreach (var keyname in keys)
  514. {
  515. // string[] keys = ["bdpan:success", "bdpan:fail", "wemeet:success", "wemeet:fail"];
  516. cacheKey = $":{total_key}:{keyname}:{_report_date}";
  517. total = TkLogCore.GetTotal(cacheKey);
  518. cacheKey = $":{total_key}:{keyname}:success:{_report_date}";
  519. int value = TkLogCore.GetTotal(cacheKey);
  520. if (value == 0) continue;
  521. result.Add(new { name = $"{keyname}-成功", value, increases = Math.Round(value / total * 100, 2) });
  522. cacheKey = $":{total_key}:{keyname}:fail:{_report_date}";
  523. value = TkLogCore.GetTotal(cacheKey);
  524. if (value == 0) continue;
  525. result.Add(new { name = $"{keyname}-失败", value, increases = Math.Round(value / total * 100, 2) });
  526. }
  527. cacheKey = $":{total_key}:{accountName}:reason:{_report_date}";
  528. string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey);
  529. foreach (var keyname in reason_keys)
  530. {
  531. cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
  532. int value = TkLogCore.GetTotal(cacheKey);
  533. if (value == 0) continue;
  534. reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  535. }
  536. reason_result = reason_result.OrderByDescending(item => item.value).ToList();
  537. }
  538. return new APIResult(new { total, data = result, data2 = reason_result });
  539. }
  540. else
  541. {
  542. string cacheKey = $":{total_key}:{accountName}:{_report_date}";
  543. double total = TkLogCore.GetTotal(cacheKey);
  544. if (total > 0)
  545. {
  546. string[] keys = ["success"];
  547. cacheKey = $":{total_key}:{accountName}:message:{_report_date}";
  548. string[] message_keys = TkLogCore.GetTotalKeys(cacheKey);
  549. var combinedKeys = keys.Union(message_keys).ToList();
  550. combinedKeys.RemoveAll(item => item == "fail");
  551. foreach (var keyname in combinedKeys)
  552. {
  553. cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
  554. int value = TkLogCore.GetTotal(cacheKey);
  555. if (value == 0) continue;
  556. result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  557. }
  558. result = result.OrderByDescending(item => item.value).ToList();
  559. cacheKey = $":{total_key}:{accountName}:reason:{_report_date}";
  560. string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey);
  561. foreach (var keyname in reason_keys)
  562. {
  563. cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
  564. int value = TkLogCore.GetTotal(cacheKey);
  565. if (value < 50) continue;
  566. reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  567. }
  568. reason_result = reason_result.OrderByDescending(item => item.value).ToList();
  569. }
  570. return new APIResult(new { total, data = result, data2 = reason_result });
  571. }
  572. }
  573. [HttpPost]
  574. public ActionResult chartData2([FromBody] JsonElement form)
  575. {
  576. var result = new List<dynamic>();
  577. var reason_result = new List<dynamic>();
  578. var report_date = form.PathReadArray<string>("query_date[]");
  579. var accountName = form.Read("accountName", string.Empty);
  580. var isHourtrend = form.Read<bool>("isLeaf", false);
  581. var total_key = form.Read("total_key", "total");
  582. if (string.IsNullOrEmpty(accountName)) accountName = "tb";
  583. DateTime stime = DateTime.MinValue;
  584. if (!DateTime.TryParse(report_date[0], out stime)) return new APIResult(new { data = result, data2 = reason_result });
  585. string _report_date = stime.ToString("yyyyMMdd");
  586. if (isHourtrend) _report_date = stime.ToString("yyyyMMddHH");
  587. string cacheKey = $":{total_key}:{accountName}:{_report_date}";
  588. double total = TkLogCore.GetTotal(cacheKey);
  589. if (total > 0)
  590. {
  591. string[] keys = ["success"];
  592. cacheKey = $":{total_key}:{accountName}:message:{_report_date}";
  593. string[] message_keys = TkLogCore.GetTotalKeys(cacheKey);
  594. var combinedKeys = keys.Union(message_keys).ToList();
  595. combinedKeys.RemoveAll(item => item == "fail");
  596. foreach (var keyname in combinedKeys)
  597. {
  598. cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
  599. int value = TkLogCore.GetTotal(cacheKey);
  600. if (value == 0) continue;
  601. result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  602. }
  603. result = result.OrderByDescending(item => item.value).ToList();
  604. cacheKey = $":{total_key}:{accountName}:reason:{_report_date}";
  605. string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey);
  606. foreach (var keyname in reason_keys)
  607. {
  608. cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
  609. int value = TkLogCore.GetTotal(cacheKey);
  610. if (value == 0) continue;
  611. reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  612. }
  613. reason_result = reason_result.OrderByDescending(item => item.value).ToList();
  614. }
  615. return new APIResult(new { total, data = result, data2 = reason_result });
  616. }
  617. [HttpPost]
  618. public ActionResult settle_bills_total([FromBody] JsonElement form)
  619. {
  620. int page = form.Read("current", 1);
  621. int size = form.Read("pageSize", 10);
  622. bool getTotal = form.Read("getTotal", true);
  623. string sort = form.Read<string>("sort");
  624. string order = form.Read<string>("order");
  625. var belongTime = form.PathReadArray<string>("query_date[]");
  626. string filter = string.Empty;
  627. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  628. if (belongTime.Count == 2)
  629. {
  630. if (DateTime.TryParse(belongTime[0], out stime) && DateTime.TryParse(belongTime[1], out etime))
  631. {
  632. etime = etime.AddDays(1).AddSeconds(-1);
  633. filter += $" AND belongTime BETWEEN @stime AND @etime";
  634. }
  635. }
  636. filter = filter.StringTrimStart(" AND ");
  637. //排序
  638. string orderBy = "belongTime DESC";
  639. if (!string.IsNullOrEmpty(order))
  640. {
  641. order = "descending".Equals(order) ? "DESC" : "ASC";
  642. orderBy = sort switch
  643. {
  644. _ => $"{sort} {order}",
  645. };
  646. }
  647. var result = new DBContext.Table("v_tk_report_bills")
  648. .Where(filter, new { stime, etime })
  649. .Page(size, page)
  650. .Order(orderBy)
  651. .PageList<TkSettleBillDTO>(getTotal);
  652. return new APIResult(new { data = result });
  653. }
  654. [HttpPost]
  655. public ActionResult settle_bills([FromBody] JsonElement form)
  656. {
  657. int page = form.Read("current", 1);
  658. int size = form.Read("pageSize", 10);
  659. bool getTotal = form.Read("getTotal", true);
  660. string sort = form.Read<string>("sort");
  661. string order = form.Read<string>("order");
  662. string name = form.Read<string>("keyword");
  663. var belongTime = form.PathReadArray<string>("query_date[]");
  664. string filter = string.Empty;
  665. if (!string.IsNullOrEmpty(name))
  666. {
  667. filter += $" AND accountId IN (SELECT ID FROM tk_pool WHERE company=@name)";
  668. }
  669. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  670. if (belongTime.Count == 2)
  671. {
  672. if (DateTime.TryParse(belongTime[0], out stime) && DateTime.TryParse(belongTime[1], out etime))
  673. {
  674. etime = etime.AddDays(1).AddSeconds(-1);
  675. filter += $" AND belongTime BETWEEN @stime AND @etime";
  676. }
  677. }
  678. filter = filter.StringTrimStart(" AND ");
  679. //排序
  680. string orderBy = "belongTime DESC";
  681. if (!string.IsNullOrEmpty(order))
  682. {
  683. order = "descending".Equals(order) ? "DESC" : "ASC";
  684. orderBy = sort switch
  685. {
  686. _ => $"{sort} {order}",
  687. };
  688. }
  689. var result = new DBContext.Table("tk_settle_bill")
  690. .Where(filter, new { name, stime, etime })
  691. .Page(size, page)
  692. .Order(orderBy)
  693. .PageList<TkSettleBillDTO>(getTotal);
  694. //foreach (var item in result.List)
  695. //{
  696. // if (string.IsNullOrEmpty(name))
  697. // {
  698. // item.accountName = "-";
  699. // }
  700. //}
  701. return new APIResult(new { data = result });
  702. }
  703. }
  704. }