TaobaoController.cs 36 KB

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