TaobaoController.cs 21 KB

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