TaobaoController.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Text.Json;
  5. using System.Net;
  6. namespace molilian.api.Controllers
  7. {
  8. [ApiController]
  9. [MyAuthorize("admin")]
  10. [Route("api/[controller]/[action]")]
  11. public class TaobaoController : ControllerBase
  12. {
  13. readonly IAuthorizationProvider provider = new AdminProvider();
  14. protected IHttpContextAccessor _accessor;
  15. public TaobaoController(IHttpContextAccessor accessor)
  16. {
  17. _accessor = accessor;
  18. }
  19. [HttpPost]
  20. public async Task<ActionResult> updateCookies([FromBody] JsonElement form)
  21. {
  22. string cookie = form.Read<string>("cookie", string.Empty);
  23. string user_agent = _accessor.HttpContext.Request.UserAgent();
  24. var success = TkPoolCore.UpdateCookies(cookie, user_agent);
  25. return new APIResult(new
  26. {
  27. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入的cookie" },
  28. });
  29. }
  30. [HttpPost]
  31. public async Task<ActionResult> update([FromBody] JsonElement form)
  32. {
  33. int id = form.Read<int>("id", 0);
  34. string name = form.Read<string>("name", string.Empty);
  35. string val = form.Read<string>("val", string.Empty);
  36. if (id == 0)
  37. {
  38. return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入的cookie" } });
  39. }
  40. int result = 0;
  41. switch (name)
  42. {
  43. case "enable_fake_click":
  44. case "enable_parse":
  45. case "enable_coupon":
  46. case "status":
  47. bool bVal = val.Equals("True");
  48. result = new DBContext.Table("tk_pool")
  49. .Add(name, bVal)
  50. .Add("last_time", DateTime.Now)
  51. .Where("id=@id", new { id })
  52. .Update();
  53. break;
  54. default:
  55. return new APIResult(new { data = new { success = false, msg = "更新失败,未授权操作" } });
  56. }
  57. TkPoolCore.Refresh();
  58. bool success = result > 0;
  59. return new APIResult(new
  60. {
  61. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  62. });
  63. }
  64. /// <summary>
  65. /// 账号列表
  66. /// </summary>
  67. /// <param name="form"></param>
  68. /// <returns></returns>
  69. [HttpPost]
  70. public ActionResult list([FromBody] JsonElement form)
  71. {
  72. var token = provider.Get(_accessor.HttpContext);
  73. int page = form.Read("current", 1);
  74. int size = form.Read("pageSize", 10);
  75. string keyword = form.Read("name", string.Empty);
  76. bool getTotal = form.Read("getTotal", true);
  77. string _status = form.Read("status", string.Empty);
  78. int status = _status switch
  79. {
  80. "online" => 1,
  81. "offline" => 0,
  82. _ => -1,
  83. };
  84. var lastTime = form.PathReadArray<string>("lastTime[]");
  85. string sort = form.Read<string>("sort");
  86. string order = form.Read<string>("order");
  87. string filter = string.Empty;
  88. if (!string.IsNullOrEmpty(keyword))
  89. {
  90. filter += $" AND (`name` LIKE @keyword OR `description` LIKE @keyword)";
  91. keyword = $"%{keyword}%";
  92. }
  93. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  94. if (lastTime.Count == 2)
  95. {
  96. if (DateTime.TryParse(lastTime[0], out stime) && DateTime.TryParse(lastTime[1], out etime))
  97. {
  98. etime = etime.AddDays(1).AddSeconds(-1);
  99. filter += $" AND last_time BETWEEN @stime AND @etime";
  100. }
  101. }
  102. if (status != -1)
  103. {
  104. filter += $" AND status=@status";
  105. }
  106. filter = filter.StringTrimStart(" AND ");
  107. //排序
  108. string orderBy = "id DESC";
  109. if (!string.IsNullOrEmpty(order))
  110. {
  111. order = "descending".Equals(order) ? "DESC" : "ASC";
  112. orderBy = sort switch
  113. {
  114. //"num" => $"num {order}",
  115. _ => $"{sort} {order}",
  116. };
  117. }
  118. using var conn = DBContext.GetOpenConnection();
  119. var result = new DBContext.Table(conn, "tk_pool")
  120. .Where(filter, new { keyword, status, stime, etime })
  121. .Page(size, page)
  122. .Order(orderBy)
  123. .PageList<TkPoolDTO>(getTotal);
  124. foreach (var item in result.List)
  125. {
  126. item.current_amt = Math.Round(TkPoolCore.GetIncomeAmt(item.name), 2);
  127. item.cookies = string.Empty;
  128. }
  129. return new APIResult(new { data = result });
  130. }
  131. [HttpPost]
  132. public ActionResult report_dailys([FromBody] JsonElement form)
  133. {
  134. int page = form.Read("current", 1);
  135. int size = form.Read("pageSize", 10);
  136. bool getTotal = form.Read("getTotal", true);
  137. string sort = form.Read<string>("sort");
  138. string order = form.Read<string>("order");
  139. var report_date = form.PathReadArray<string>("query_date[]");
  140. string dailys_accountName = "all";
  141. int dailys_accountId = 0;
  142. string filter = string.Empty;
  143. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  144. if (report_date.Count == 2)
  145. {
  146. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  147. {
  148. etime = etime.AddDays(1).AddSeconds(-1);
  149. filter += $" AND A.report_date BETWEEN @stime AND @etime";
  150. }
  151. }
  152. filter = filter.StringTrimStart(" AND ");
  153. //排序
  154. string orderBy = "A.report_date DESC";
  155. if (!string.IsNullOrEmpty(order))
  156. {
  157. order = "descending".Equals(order) ? "DESC" : "ASC";
  158. orderBy = sort switch
  159. {
  160. _ => $"A.{sort} {order}",
  161. };
  162. }
  163. var result = new DBContext.Table("v_tk_report_dailys")
  164. .LeftJoin("daily_logs",
  165. "B.log_date = A.report_date AND B.accountId=@dailys_accountId",
  166. "total_count, B.abandon_count, B.success_count, B.abandon_percentage, B.success_percentage")
  167. .Where(filter, new { dailys_accountId, dailys_accountName, stime, etime })
  168. .Page(size, page)
  169. .Order(orderBy)
  170. .PageList<TkReportDTO>(getTotal);
  171. foreach (var item in result.List)
  172. {
  173. if (item.total_count > 0) continue;
  174. item.total_count = RedisHelper.Get<int>($":total:all:{item.report_date:yyyyMMdd}");
  175. if (item.total_count == 0) continue;
  176. item.success_count = RedisHelper.Get<int>($":total:all:success:{item.report_date:yyyyMMdd}");
  177. item.abandon_count = RedisHelper.Get<int>($":total:all:放弃转链:{item.report_date:yyyyMMdd}");
  178. #if DEBUG
  179. //if (item.report_date == DateTime.Now.Date)
  180. //{
  181. // RedisHelper.Set($":total:all:{item.report_date:yyyyMMdd}", item.total_count / 2);
  182. // RedisHelper.Set($":total:all:success:{item.report_date:yyyyMMdd}", item.total_count / 2);
  183. // RedisHelper.Set($":total:all:放弃转链:{item.report_date:yyyyMMdd}", item.abandon_count / 2);
  184. //}
  185. #endif
  186. if (item.total_count > 0)
  187. {
  188. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  189. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  190. }
  191. }
  192. return new APIResult(new { data = result });
  193. }
  194. [HttpPost]
  195. public ActionResult report_list([FromBody] JsonElement form)
  196. {
  197. int page = form.Read("current", 1);
  198. int size = form.Read("pageSize", 10);
  199. bool getTotal = form.Read("getTotal", true);
  200. string sort = form.Read<string>("sort");
  201. string order = form.Read<string>("order");
  202. var report_date = form.PathReadArray<string>("query_date[]");
  203. int accountId = form.Read("accountId", 0);
  204. string filter = string.Empty;
  205. if (accountId != 0)
  206. {
  207. filter += $" AND A.accountId=@accountId";
  208. }
  209. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  210. if (report_date.Count == 2)
  211. {
  212. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  213. {
  214. etime = etime.AddDays(1).AddSeconds(-1);
  215. filter += $" AND A.report_date BETWEEN @stime AND @etime";
  216. }
  217. }
  218. filter = filter.StringTrimStart(" AND ");
  219. //排序
  220. string orderBy = "A.id DESC";
  221. if (!string.IsNullOrEmpty(order))
  222. {
  223. order = "descending".Equals(order) ? "DESC" : "ASC";
  224. orderBy = sort switch
  225. {
  226. _ => $"A.{sort} {order}",
  227. };
  228. }
  229. var result = new DBContext.Table("tk_report")
  230. .LeftJoin("daily_logs",
  231. "B.log_date = A.report_date AND B.accountId=A.accountId",
  232. "total_count, B.abandon_count, B.success_count, B.abandon_percentage, B.success_percentage")
  233. .Where(filter, new { accountId, stime, etime })
  234. .Page(size, page)
  235. .Order(orderBy)
  236. .PageList<TkReportDTO>(getTotal);
  237. foreach (var item in result.List)
  238. {
  239. //var tk = TkPoolCore.GetOne(item.accountId);
  240. //if (tk != null) item.accountName = tk.company;
  241. if (item.total_count > 0) continue;
  242. string accountName = $"{TkChannelEnum.tb}_{item.accountId}";
  243. item.total_count = RedisHelper.Get<int>($":total:{accountName}:{item.report_date:yyyyMMdd}");
  244. if (item.total_count == 0)
  245. {
  246. //todo 过度用, 0613 跑两天就可以删掉
  247. accountName = item.accountName;
  248. item.total_count = RedisHelper.Get<int>($":total:{accountName}:{item.report_date:yyyyMMdd}");
  249. }
  250. if (item.total_count == 0) continue;
  251. item.success_count = RedisHelper.Get<int>($":total:{accountName}:success:{item.report_date:yyyyMMdd}");
  252. item.abandon_count = RedisHelper.Get<int>($":total:{accountName}:放弃转链:{item.report_date:yyyyMMdd}");
  253. if (item.total_count > 0)
  254. {
  255. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  256. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  257. }
  258. }
  259. //public int api_req_num { get; set; } = 0;
  260. //public int abandon_num { get; set; } = 0;
  261. //public int success_num { get; set; } = 0;
  262. return new APIResult(new { data = result });
  263. }
  264. [HttpPost]
  265. public ActionResult report_hourtrend([FromBody] JsonElement form)
  266. {
  267. int page = form.Read("current", 1);
  268. int size = form.Read("pageSize", 10);
  269. bool getTotal = form.Read("getTotal", true);
  270. string sort = form.Read<string>("sort");
  271. string order = form.Read<string>("order");
  272. var report_date = form.PathReadArray<string>("query_date[]");
  273. int accountId = form.Read("accountId", 0);
  274. string filter = string.Empty;
  275. if (accountId != 0)
  276. {
  277. filter += $" AND accountId=@accountId";
  278. }
  279. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  280. if (report_date.Count == 2)
  281. {
  282. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  283. {
  284. etime = etime.AddDays(1).AddSeconds(-1);
  285. if (etime > DateTime.Now) etime = DateTime.Now;
  286. filter += $" AND report_date BETWEEN @stime AND @etime";
  287. }
  288. }
  289. filter = filter.StringTrimStart(" AND ");
  290. //排序
  291. string orderBy = "id ASC";
  292. if (!string.IsNullOrEmpty(order))
  293. {
  294. order = "descending".Equals(order) ? "DESC" : "ASC";
  295. orderBy = sort switch
  296. {
  297. _ => $"{sort} {order}",
  298. };
  299. }
  300. using var conn = DBContext.GetOpenConnection();
  301. var result = new DBContext.Table(conn, "tk_report_hourtrend")
  302. .Where(filter, new { accountId, stime, etime })
  303. .Page(size, page)
  304. .Order(orderBy)
  305. .PageList<TkReportDTO>(getTotal);
  306. foreach (var item in result.List)
  307. {
  308. if (item.total_count > 0) continue;
  309. string accountName = $"{TkChannelEnum.tb}_{item.accountId}";
  310. item.total_count = RedisHelper.Get<int>($":total:{accountName}:{item.report_date:yyyyMMddHH}");
  311. if (item.total_count == 0)
  312. {
  313. //todo 过度用, 0613 跑两天就可以删掉
  314. accountName = item.accountName;
  315. item.total_count = RedisHelper.Get<int>($":total:{accountName}:{item.report_date:yyyyMMddHH}");
  316. }
  317. if (item.total_count == 0) continue;
  318. item.success_count = RedisHelper.Get<int>($":total:{item.accountName}:success:{item.report_date:yyyyMMddHH}");
  319. item.abandon_count = RedisHelper.Get<int>($":total:{item.accountName}:放弃转链:{item.report_date:yyyyMMddHH}");
  320. if (item.total_count > 0)
  321. {
  322. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  323. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  324. }
  325. }
  326. return new APIResult(new { data = result });
  327. }
  328. [HttpPost]
  329. public ActionResult chartData([FromBody] JsonElement form)
  330. {
  331. var result = new List<dynamic>();
  332. var reason_result = new List<dynamic>();
  333. var report_date = form.PathReadArray<string>("query_date[]");
  334. var accountName = form.Read("accountName", string.Empty);
  335. var isHourtrend = form.Read<bool>("isLeaf", false);
  336. if (string.IsNullOrEmpty(accountName)) accountName = "all";
  337. DateTime stime = DateTime.MinValue;
  338. if (!DateTime.TryParse(report_date[0], out stime)) return new APIResult(new { data = result, data2 = reason_result });
  339. string _report_date = stime.ToString("yyyyMMdd");
  340. if (isHourtrend) _report_date = stime.ToString("yyyyMMddHH");
  341. string cacheKey = $":total:{accountName}:{_report_date}";
  342. double total = RedisHelper.Get<int>(cacheKey);
  343. if (total > 0)
  344. {
  345. string[] keys = new string[] { "success" };
  346. cacheKey = $":total:{accountName}:message:{_report_date}";
  347. string[] message_keys = RedisHelper.SMembers(cacheKey);
  348. string[] combinedKeys = keys.Union(message_keys).ToArray();
  349. foreach (var keyname in combinedKeys)
  350. {
  351. cacheKey = $":total:{accountName}:{keyname}:{_report_date}";
  352. int value = RedisHelper.Get<int>(cacheKey);
  353. if (value == 0) continue;
  354. #if DEBUG
  355. //RedisHelper.Set(cacheKey, value / 2);
  356. #endif
  357. result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  358. }
  359. result = result.OrderByDescending(item => item.value).ToList();
  360. cacheKey = $":total:{accountName}:reason:{_report_date}";
  361. string[] reason_keys = RedisHelper.SMembers(cacheKey);
  362. foreach (var keyname in reason_keys)
  363. {
  364. cacheKey = $":total:{accountName}:{keyname}:{_report_date}";
  365. int value = RedisHelper.Get<int>(cacheKey);
  366. if (value == 0) continue;
  367. #if DEBUG
  368. //RedisHelper.Set(cacheKey, value / 2);
  369. #endif
  370. reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  371. }
  372. reason_result = reason_result.OrderByDescending(item => item.value).ToList();
  373. }
  374. return new APIResult(new { total, data = result, data2 = reason_result });
  375. }
  376. [HttpPost]
  377. public ActionResult settle_bills_total([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 belongTime = form.PathReadArray<string>("query_date[]");
  385. string filter = string.Empty;
  386. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  387. if (belongTime.Count == 2)
  388. {
  389. if (DateTime.TryParse(belongTime[0], out stime) && DateTime.TryParse(belongTime[1], out etime))
  390. {
  391. etime = etime.AddDays(1).AddSeconds(-1);
  392. filter += $" AND belongTime BETWEEN @stime AND @etime";
  393. }
  394. }
  395. filter = filter.StringTrimStart(" AND ");
  396. //排序
  397. string orderBy = "belongTime DESC";
  398. if (!string.IsNullOrEmpty(order))
  399. {
  400. order = "descending".Equals(order) ? "DESC" : "ASC";
  401. orderBy = sort switch
  402. {
  403. _ => $"{sort} {order}",
  404. };
  405. }
  406. var result = new DBContext.Table("v_tk_report_bills")
  407. .Where(filter, new { stime, etime })
  408. .Page(size, page)
  409. .Order(orderBy)
  410. .PageList<TkSettleBillDTO>(getTotal);
  411. return new APIResult(new { data = result });
  412. }
  413. [HttpPost]
  414. public ActionResult settle_bills([FromBody] JsonElement form)
  415. {
  416. int page = form.Read("current", 1);
  417. int size = form.Read("pageSize", 10);
  418. bool getTotal = form.Read("getTotal", true);
  419. string sort = form.Read<string>("sort");
  420. string order = form.Read<string>("order");
  421. string name = form.Read<string>("keyword");
  422. var belongTime = form.PathReadArray<string>("query_date[]");
  423. string filter = string.Empty;
  424. if (!string.IsNullOrEmpty(name))
  425. {
  426. filter += $" AND accountId IN (SELECT ID FROM tk_pool WHERE company=@name)";
  427. }
  428. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  429. if (belongTime.Count == 2)
  430. {
  431. if (DateTime.TryParse(belongTime[0], out stime) && DateTime.TryParse(belongTime[1], out etime))
  432. {
  433. etime = etime.AddDays(1).AddSeconds(-1);
  434. filter += $" AND belongTime BETWEEN @stime AND @etime";
  435. }
  436. }
  437. filter = filter.StringTrimStart(" AND ");
  438. //排序
  439. string orderBy = "belongTime DESC";
  440. if (!string.IsNullOrEmpty(order))
  441. {
  442. order = "descending".Equals(order) ? "DESC" : "ASC";
  443. orderBy = sort switch
  444. {
  445. _ => $"{sort} {order}",
  446. };
  447. }
  448. var result = new DBContext.Table("tk_settle_bill")
  449. .Where(filter, new { name, stime, etime })
  450. .Page(size, page)
  451. .Order(orderBy)
  452. .PageList<TkSettleBillDTO>(getTotal);
  453. //foreach (var item in result.List)
  454. //{
  455. // if (string.IsNullOrEmpty(name))
  456. // {
  457. // item.accountName = "-";
  458. // }
  459. //}
  460. return new APIResult(new { data = result });
  461. }
  462. }
  463. }