TaobaoController.cs 36 KB

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