TaobaoController.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. int dailys_accountId = 0;
  140. string filter = string.Empty;
  141. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  142. if (report_date.Count == 2)
  143. {
  144. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  145. {
  146. etime = etime.AddDays(1).AddSeconds(-1);
  147. filter += $" AND A.report_date BETWEEN @stime AND @etime";
  148. }
  149. }
  150. filter = filter.StringTrimStart(" AND ");
  151. //排序
  152. string orderBy = "A.report_date DESC";
  153. if (!string.IsNullOrEmpty(order))
  154. {
  155. order = "descending".Equals(order) ? "DESC" : "ASC";
  156. orderBy = sort switch
  157. {
  158. _ => $"A.{sort} {order}",
  159. };
  160. }
  161. var result = new DBContext.Table("v_tk_report_dailys")
  162. .LeftJoin("daily_logs",
  163. "B.log_date = A.report_date AND B.accountId=@dailys_accountId",
  164. "total_count, B.abandon_count, B.success_count, B.abandon_percentage, B.success_percentage")
  165. .Where(filter, new { dailys_accountId, dailys_accountName, stime, etime })
  166. .Page(size, page)
  167. .Order(orderBy)
  168. .PageList<TkReportDTO>(getTotal);
  169. foreach (var item in result.List)
  170. {
  171. if (item.total_count > 0) continue;
  172. item.total_count = RedisHelper.Get<int>($":total:all:{item.report_date:yyyyMMdd}");
  173. if (item.total_count == 0) continue;
  174. item.success_count = RedisHelper.Get<int>($":total:all:success:{item.report_date:yyyyMMdd}");
  175. item.abandon_count = RedisHelper.Get<int>($":total:all:放弃转链:{item.report_date:yyyyMMdd}");
  176. if (item.total_count > 0)
  177. {
  178. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  179. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  180. }
  181. }
  182. return new APIResult(new { data = result });
  183. }
  184. [HttpPost]
  185. public ActionResult report_list([FromBody] JsonElement form)
  186. {
  187. int page = form.Read("current", 1);
  188. int size = form.Read("pageSize", 10);
  189. bool getTotal = form.Read("getTotal", true);
  190. string sort = form.Read<string>("sort");
  191. string order = form.Read<string>("order");
  192. var report_date = form.PathReadArray<string>("query_date[]");
  193. int accountId = form.Read("accountId", 0);
  194. string filter = string.Empty;
  195. if (accountId != 0)
  196. {
  197. filter += $" AND A.accountId=@accountId";
  198. }
  199. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  200. if (report_date.Count == 2)
  201. {
  202. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  203. {
  204. etime = etime.AddDays(1).AddSeconds(-1);
  205. filter += $" AND A.report_date BETWEEN @stime AND @etime";
  206. }
  207. }
  208. filter = filter.StringTrimStart(" AND ");
  209. //排序
  210. string orderBy = "A.id DESC";
  211. if (!string.IsNullOrEmpty(order))
  212. {
  213. order = "descending".Equals(order) ? "DESC" : "ASC";
  214. orderBy = sort switch
  215. {
  216. _ => $"A.{sort} {order}",
  217. };
  218. }
  219. var result = new DBContext.Table("tk_report")
  220. .LeftJoin("daily_logs",
  221. "B.log_date = A.report_date AND B.accountId=A.accountId",
  222. "total_count, B.abandon_count, B.success_count, B.abandon_percentage, B.success_percentage")
  223. .Where(filter, new { accountId, stime, etime })
  224. .Page(size, page)
  225. .Order(orderBy)
  226. .PageList<TkReportDTO>(getTotal);
  227. foreach (var item in result.List)
  228. {
  229. //var tk = TkPoolCore.GetOne(item.accountId);
  230. //if (tk != null) item.accountName = tk.company;
  231. if (item.total_count > 0) continue;
  232. item.total_count = RedisHelper.Get<int>($":total:{item.accountName}:{item.report_date:yyyyMMdd}");
  233. if (item.total_count == 0) continue;
  234. item.success_count = RedisHelper.Get<int>($":total:{item.accountName}:success:{item.report_date:yyyyMMdd}");
  235. item.abandon_count = RedisHelper.Get<int>($":total:{item.accountName}:放弃转链:{item.report_date:yyyyMMdd}");
  236. if (item.total_count > 0)
  237. {
  238. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  239. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  240. }
  241. }
  242. //public int api_req_num { get; set; } = 0;
  243. //public int abandon_num { get; set; } = 0;
  244. //public int success_num { get; set; } = 0;
  245. return new APIResult(new { data = result });
  246. }
  247. [HttpPost]
  248. public ActionResult report_hourtrend([FromBody] JsonElement form)
  249. {
  250. int page = form.Read("current", 1);
  251. int size = form.Read("pageSize", 10);
  252. bool getTotal = form.Read("getTotal", true);
  253. string sort = form.Read<string>("sort");
  254. string order = form.Read<string>("order");
  255. var report_date = form.PathReadArray<string>("query_date[]");
  256. int accountId = form.Read("accountId", 0);
  257. string filter = string.Empty;
  258. if (accountId != 0)
  259. {
  260. filter += $" AND accountId=@accountId";
  261. }
  262. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  263. if (report_date.Count == 2)
  264. {
  265. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  266. {
  267. etime = etime.AddDays(1).AddSeconds(-1);
  268. if (etime > DateTime.Now) etime = DateTime.Now;
  269. filter += $" AND report_date BETWEEN @stime AND @etime";
  270. }
  271. }
  272. filter = filter.StringTrimStart(" AND ");
  273. //排序
  274. string orderBy = "id ASC";
  275. if (!string.IsNullOrEmpty(order))
  276. {
  277. order = "descending".Equals(order) ? "DESC" : "ASC";
  278. orderBy = sort switch
  279. {
  280. _ => $"{sort} {order}",
  281. };
  282. }
  283. using var conn = DBContext.GetOpenConnection();
  284. var result = new DBContext.Table(conn, "tk_report_hourtrend")
  285. .Where(filter, new { accountId, stime, etime })
  286. .Page(size, page)
  287. .Order(orderBy)
  288. .PageList<TkReportDTO>(getTotal);
  289. foreach (var item in result.List)
  290. {
  291. if (item.total_count > 0) continue;
  292. item.total_count = RedisHelper.Get<int>($":total:{item.accountName}:{item.report_date:yyyyMMddHH}");
  293. if (item.total_count == 0) continue;
  294. item.success_count = RedisHelper.Get<int>($":total:{item.accountName}:success:{item.report_date:yyyyMMddHH}");
  295. item.abandon_count = RedisHelper.Get<int>($":total:{item.accountName}:放弃转链:{item.report_date:yyyyMMddHH}");
  296. if (item.total_count > 0)
  297. {
  298. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  299. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  300. }
  301. }
  302. return new APIResult(new { data = result });
  303. }
  304. [HttpPost]
  305. public ActionResult chartData([FromBody] JsonElement form)
  306. {
  307. var result = new List<dynamic>();
  308. var reason_result = new List<dynamic>();
  309. var report_date = form.PathReadArray<string>("query_date[]");
  310. var accountName = form.Read("accountName", string.Empty);
  311. var isHourtrend = form.Read<bool>("isLeaf", false);
  312. if (string.IsNullOrEmpty(accountName)) accountName = "all";
  313. DateTime stime = DateTime.MinValue;
  314. if (!DateTime.TryParse(report_date[0], out stime)) return new APIResult(new { data = result, data2 = reason_result });
  315. string _report_date = stime.ToString("yyyyMMdd");
  316. if (isHourtrend) _report_date = stime.ToString("yyyyMMddHH");
  317. string cacheKey = $":total:{accountName}:{_report_date}";
  318. double total = RedisHelper.Get<int>(cacheKey);
  319. if (total > 0)
  320. {
  321. string[] keys = new string[] { "success" };
  322. cacheKey = $":total:{accountName}:message:{_report_date}";
  323. string[] message_keys = RedisHelper.SMembers(cacheKey);
  324. string[] combinedKeys = keys.Union(message_keys).ToArray();
  325. foreach (var keyname in combinedKeys)
  326. {
  327. cacheKey = $":total:{accountName}:{keyname}:{_report_date}";
  328. int value = RedisHelper.Get<int>(cacheKey);
  329. if (value == 0) continue;
  330. result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  331. }
  332. result = result.OrderByDescending(item => item.value).ToList();
  333. cacheKey = $":total:{accountName}:reason:{_report_date}";
  334. string[] reason_keys = RedisHelper.SMembers(cacheKey);
  335. foreach (var keyname in reason_keys)
  336. {
  337. cacheKey = $":total:{accountName}:{keyname}:{_report_date}";
  338. int value = RedisHelper.Get<int>(cacheKey);
  339. if (value == 0) continue;
  340. reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  341. }
  342. reason_result = reason_result.OrderByDescending(item => item.value).ToList();
  343. }
  344. return new APIResult(new { total, data = result, data2 = reason_result });
  345. //RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMM}");
  346. //RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMdd}");
  347. //RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  348. //string result = success ? "success" : "fail";
  349. //RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  350. //RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  351. //RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  352. //if (!string.IsNullOrEmpty(message))
  353. //{
  354. // RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  355. // RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  356. // RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  357. // RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  358. // RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  359. //}
  360. //if (!string.IsNullOrEmpty(reason))
  361. //{
  362. // RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  363. // RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  364. // RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  365. // RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  366. // RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  367. //}
  368. }
  369. [HttpPost]
  370. public ActionResult settle_bills_total([FromBody] JsonElement form)
  371. {
  372. int page = form.Read("current", 1);
  373. int size = form.Read("pageSize", 10);
  374. bool getTotal = form.Read("getTotal", true);
  375. string sort = form.Read<string>("sort");
  376. string order = form.Read<string>("order");
  377. var belongTime = form.PathReadArray<string>("query_date[]");
  378. string filter = string.Empty;
  379. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  380. if (belongTime.Count == 2)
  381. {
  382. if (DateTime.TryParse(belongTime[0], out stime) && DateTime.TryParse(belongTime[1], out etime))
  383. {
  384. etime = etime.AddDays(1).AddSeconds(-1);
  385. filter += $" AND belongTime BETWEEN @stime AND @etime";
  386. }
  387. }
  388. filter = filter.StringTrimStart(" AND ");
  389. //排序
  390. string orderBy = "belongTime DESC";
  391. if (!string.IsNullOrEmpty(order))
  392. {
  393. order = "descending".Equals(order) ? "DESC" : "ASC";
  394. orderBy = sort switch
  395. {
  396. _ => $"{sort} {order}",
  397. };
  398. }
  399. var result = new DBContext.Table("v_tk_report_bills")
  400. .Where(filter, new { stime, etime })
  401. .Page(size, page)
  402. .Order(orderBy)
  403. .PageList<TkSettleBillDTO>(getTotal);
  404. return new APIResult(new { data = result });
  405. }
  406. [HttpPost]
  407. public ActionResult settle_bills([FromBody] JsonElement form)
  408. {
  409. int page = form.Read("current", 1);
  410. int size = form.Read("pageSize", 10);
  411. bool getTotal = form.Read("getTotal", true);
  412. string sort = form.Read<string>("sort");
  413. string order = form.Read<string>("order");
  414. string name = form.Read<string>("keyword");
  415. var belongTime = form.PathReadArray<string>("query_date[]");
  416. string filter = string.Empty;
  417. if (!string.IsNullOrEmpty(name))
  418. {
  419. filter += $" AND accountId IN (SELECT ID FROM tk_pool WHERE company=@name)";
  420. }
  421. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  422. if (belongTime.Count == 2)
  423. {
  424. if (DateTime.TryParse(belongTime[0], out stime) && DateTime.TryParse(belongTime[1], out etime))
  425. {
  426. etime = etime.AddDays(1).AddSeconds(-1);
  427. filter += $" AND belongTime BETWEEN @stime AND @etime";
  428. }
  429. }
  430. filter = filter.StringTrimStart(" AND ");
  431. //排序
  432. string orderBy = "belongTime DESC";
  433. if (!string.IsNullOrEmpty(order))
  434. {
  435. order = "descending".Equals(order) ? "DESC" : "ASC";
  436. orderBy = sort switch
  437. {
  438. _ => $"{sort} {order}",
  439. };
  440. }
  441. var result = new DBContext.Table("tk_settle_bill")
  442. .Where(filter, new { name, stime, etime })
  443. .Page(size, page)
  444. .Order(orderBy)
  445. .PageList<TkSettleBillDTO>(getTotal);
  446. //foreach (var item in result.List)
  447. //{
  448. // if (string.IsNullOrEmpty(name))
  449. // {
  450. // item.accountName = "-";
  451. // }
  452. //}
  453. return new APIResult(new { data = result });
  454. }
  455. }
  456. }