TaobaoController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Text.Json;
  5. namespace molilian.api.Controllers
  6. {
  7. [ApiController]
  8. [MyAuthorize("admin")]
  9. [Route("api/[controller]/[action]")]
  10. public class TaobaoController : ControllerBase
  11. {
  12. readonly IAuthorizationProvider provider = new AdminProvider();
  13. protected IHttpContextAccessor _accessor;
  14. public TaobaoController(IHttpContextAccessor accessor)
  15. {
  16. _accessor = accessor;
  17. }
  18. [HttpPost]
  19. public async Task<ActionResult> updateCookies([FromBody] JsonElement form)
  20. {
  21. string cookie = form.Read<string>("cookie", string.Empty);
  22. string user_agent = _accessor.HttpContext.Request.UserAgent();
  23. var success = TkPoolCore.UpdateCookies(cookie, user_agent);
  24. return new APIResult(new
  25. {
  26. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入的cookie" },
  27. });
  28. }
  29. [HttpPost]
  30. public ActionResult list([FromBody] JsonElement form)
  31. {
  32. var token = provider.Get(_accessor.HttpContext);
  33. int page = form.Read("current", 1);
  34. int size = form.Read("pageSize", 10);
  35. string keyword = form.Read("name", string.Empty);
  36. bool getTotal = form.Read("getTotal", true);
  37. string _status = form.Read("status", string.Empty);
  38. int status = _status switch
  39. {
  40. "online" => 1,
  41. "offline" => 0,
  42. _ => -1,
  43. };
  44. var lastTime = form.PathReadArray<string>("lastTime[]");
  45. string sort = form.Read<string>("sort");
  46. string order = form.Read<string>("order");
  47. string filter = string.Empty;
  48. if (!string.IsNullOrEmpty(keyword))
  49. {
  50. filter += $" AND (`name` LIKE @keyword OR `description` LIKE @keyword)";
  51. keyword = $"%{keyword}%";
  52. }
  53. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  54. if (lastTime.Count == 2)
  55. {
  56. if (DateTime.TryParse(lastTime[0], out stime) && DateTime.TryParse(lastTime[1], out etime))
  57. {
  58. etime = etime.AddDays(1).AddSeconds(-1);
  59. filter += $" AND last_time BETWEEN @stime AND @etime";
  60. }
  61. }
  62. if (status != -1)
  63. {
  64. filter += $" AND status=@status";
  65. }
  66. filter = filter.StringTrimStart(" AND ");
  67. //排序
  68. string orderBy = "id DESC";
  69. if (!string.IsNullOrEmpty(order))
  70. {
  71. order = "descending".Equals(order) ? "DESC" : "ASC";
  72. orderBy = sort switch
  73. {
  74. //"num" => $"num {order}",
  75. _ => $"{sort} {order}",
  76. };
  77. }
  78. using var conn = DBContext.GetOpenConnection();
  79. var result = new DBContext.Table(conn, "tk_pool")
  80. .Where(filter, new { keyword, status, stime, etime })
  81. .Page(size, page)
  82. .Order(orderBy)
  83. .PageList<TkPoolDTO>(getTotal);
  84. foreach (var item in result.List)
  85. {
  86. item.current_amt = Math.Round(TkPoolCore.GetIncomeAmt(item.name), 2);
  87. }
  88. return new APIResult(new { data = result });
  89. }
  90. [HttpPost]
  91. public ActionResult report_dailys([FromBody] JsonElement form)
  92. {
  93. int page = form.Read("current", 1);
  94. int size = form.Read("pageSize", 10);
  95. bool getTotal = form.Read("getTotal", true);
  96. string sort = form.Read<string>("sort");
  97. string order = form.Read<string>("order");
  98. var report_date = form.PathReadArray<string>("query_date[]");
  99. var accountName = form.Read("accountName", string.Empty);
  100. string dailys_accountName = "all";
  101. string filter = string.Empty;
  102. if (!string.IsNullOrEmpty(accountName))
  103. {
  104. filter += $" AND A.accountName=@accountName";
  105. }
  106. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  107. if (report_date.Count == 2)
  108. {
  109. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  110. {
  111. etime = etime.AddDays(1).AddSeconds(-1);
  112. filter += $" AND A.report_date BETWEEN @stime AND @etime";
  113. }
  114. }
  115. filter = filter.StringTrimStart(" AND ");
  116. //排序
  117. string orderBy = "A.report_date DESC";
  118. if (!string.IsNullOrEmpty(order))
  119. {
  120. order = "descending".Equals(order) ? "DESC" : "ASC";
  121. orderBy = sort switch
  122. {
  123. _ => $"A.{sort} {order}",
  124. };
  125. }
  126. var result = new DBContext.Table("v_tk_report_dailys")
  127. .LeftJoin("daily_logs",
  128. "B.log_date = A.report_date AND B.accountName=@dailys_accountName",
  129. "total_count, B.abandon_count, B.success_count, B.abandon_percentage, B.success_percentage")
  130. .Where(filter, new { accountName, dailys_accountName, stime, etime })
  131. .Page(size, page)
  132. .Order(orderBy)
  133. .PageList<TkReportDTO>(getTotal);
  134. foreach (var item in result.List)
  135. {
  136. if (item.total_count > 0) continue;
  137. item.total_count = RedisHelper.Get<int>($":total:all:{item.report_date:yyyyMMdd}");
  138. if (item.total_count == 0) continue;
  139. item.success_count = RedisHelper.Get<int>($":total:all:success:{item.report_date:yyyyMMdd}");
  140. item.abandon_count = RedisHelper.Get<int>($":total:all:放弃转链:{item.report_date:yyyyMMdd}");
  141. if (item.total_count > 0)
  142. {
  143. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  144. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  145. }
  146. }
  147. return new APIResult(new { data = result });
  148. }
  149. [HttpPost]
  150. public ActionResult report_list([FromBody] JsonElement form)
  151. {
  152. int page = form.Read("current", 1);
  153. int size = form.Read("pageSize", 10);
  154. bool getTotal = form.Read("getTotal", true);
  155. string sort = form.Read<string>("sort");
  156. string order = form.Read<string>("order");
  157. var report_date = form.PathReadArray<string>("query_date[]");
  158. var accountName = form.Read("accountName", string.Empty);
  159. string filter = string.Empty;
  160. if (!string.IsNullOrEmpty(accountName))
  161. {
  162. filter += $" AND A.accountName=@accountName";
  163. }
  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.id 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("tk_report")
  185. .LeftJoin("daily_logs",
  186. "B.log_date = A.report_date AND B.accountName=A.accountName",
  187. "total_count, B.abandon_count, B.success_count, B.abandon_percentage, B.success_percentage")
  188. .Where(filter, new { accountName, stime, etime })
  189. .Page(size, page)
  190. .Order(orderBy)
  191. .PageList<TkReportDTO>(getTotal);
  192. foreach (var item in result.List)
  193. {
  194. if (item.total_count > 0) continue;
  195. item.total_count = RedisHelper.Get<int>($":total:{item.accountName}:{item.report_date:yyyyMMdd}");
  196. if (item.total_count == 0) continue;
  197. item.success_count = RedisHelper.Get<int>($":total:{item.accountName}:success:{item.report_date:yyyyMMdd}");
  198. item.abandon_count = RedisHelper.Get<int>($":total:{item.accountName}:放弃转链:{item.report_date:yyyyMMdd}");
  199. if (item.total_count > 0)
  200. {
  201. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  202. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  203. }
  204. }
  205. //public int api_req_num { get; set; } = 0;
  206. //public int abandon_num { get; set; } = 0;
  207. //public int success_num { get; set; } = 0;
  208. return new APIResult(new { data = result });
  209. }
  210. [HttpPost]
  211. public ActionResult report_hourtrend([FromBody] JsonElement form)
  212. {
  213. int page = form.Read("current", 1);
  214. int size = form.Read("pageSize", 10);
  215. bool getTotal = form.Read("getTotal", true);
  216. string sort = form.Read<string>("sort");
  217. string order = form.Read<string>("order");
  218. var report_date = form.PathReadArray<string>("query_date[]");
  219. var accountName = form.Read("accountName", string.Empty);
  220. string filter = string.Empty;
  221. if (!string.IsNullOrEmpty(accountName))
  222. {
  223. filter += $" AND accountName=@accountName";
  224. }
  225. DateTime stime = DateTime.MinValue, etime = DateTime.MinValue;
  226. if (report_date.Count == 2)
  227. {
  228. if (DateTime.TryParse(report_date[0], out stime) && DateTime.TryParse(report_date[1], out etime))
  229. {
  230. etime = etime.AddDays(1).AddSeconds(-1);
  231. if (etime > DateTime.Now) etime = DateTime.Now;
  232. filter += $" AND report_date BETWEEN @stime AND @etime";
  233. }
  234. }
  235. filter = filter.StringTrimStart(" AND ");
  236. //排序
  237. string orderBy = "id ASC";
  238. if (!string.IsNullOrEmpty(order))
  239. {
  240. order = "descending".Equals(order) ? "DESC" : "ASC";
  241. orderBy = sort switch
  242. {
  243. _ => $"{sort} {order}",
  244. };
  245. }
  246. using var conn = DBContext.GetOpenConnection();
  247. var result = new DBContext.Table(conn, "tk_report_hourtrend")
  248. .Where(filter, new { accountName, stime, etime })
  249. .Page(size, page)
  250. .Order(orderBy)
  251. .PageList<TkReportDTO>(getTotal);
  252. foreach (var item in result.List)
  253. {
  254. if (item.total_count > 0) continue;
  255. item.total_count = RedisHelper.Get<int>($":total:{item.accountName}:{item.report_date:yyyyMMddHH}");
  256. if (item.total_count == 0) continue;
  257. item.success_count = RedisHelper.Get<int>($":total:{item.accountName}:success:{item.report_date:yyyyMMddHH}");
  258. item.abandon_count = RedisHelper.Get<int>($":total:{item.accountName}:放弃转链:{item.report_date:yyyyMMddHH}");
  259. if (item.total_count > 0)
  260. {
  261. item.success_percentage = $"{item.success_count / (double)item.total_count * 100:f2}%";
  262. item.abandon_percentage = $"{item.abandon_count / (double)item.total_count * 100:f2}%";
  263. }
  264. }
  265. return new APIResult(new { data = result });
  266. }
  267. [HttpPost]
  268. public ActionResult chartData([FromBody] JsonElement form)
  269. {
  270. var result = new List<dynamic>();
  271. var reason_result = new List<dynamic>();
  272. var report_date = form.PathReadArray<string>("query_date[]");
  273. var accountName = form.Read("accountName", string.Empty);
  274. var isHourtrend = form.Read<bool>("isLeaf", false);
  275. if (string.IsNullOrEmpty(accountName)) accountName = "all";
  276. DateTime stime = DateTime.MinValue;
  277. if (!DateTime.TryParse(report_date[0], out stime)) return new APIResult(new { data = result, data2 = reason_result });
  278. string _report_date = stime.ToString("yyyyMMdd");
  279. if (isHourtrend) _report_date = stime.ToString("yyyyMMddHH");
  280. string cacheKey = $":total:{accountName}:{_report_date}";
  281. double total = RedisHelper.Get<int>(cacheKey);
  282. if (total > 0)
  283. {
  284. string[] keys = new string[] { "success" };
  285. cacheKey = $":total:{accountName}:message:{_report_date}";
  286. string[] message_keys = RedisHelper.SMembers(cacheKey);
  287. string[] combinedKeys = keys.Union(message_keys).ToArray();
  288. foreach (var keyname in combinedKeys)
  289. {
  290. cacheKey = $":total:{accountName}:{keyname}:{_report_date}";
  291. int value = RedisHelper.Get<int>(cacheKey);
  292. if (value == 0) continue;
  293. result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  294. }
  295. result = result.OrderByDescending(item => item.value).ToList();
  296. cacheKey = $":total:{accountName}:reason:{_report_date}";
  297. string[] reason_keys = RedisHelper.SMembers(cacheKey);
  298. foreach (var keyname in reason_keys)
  299. {
  300. cacheKey = $":total:{accountName}:{keyname}:{_report_date}";
  301. int value = RedisHelper.Get<int>(cacheKey);
  302. if (value == 0) continue;
  303. reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  304. }
  305. reason_result = reason_result.OrderByDescending(item => item.value).ToList();
  306. }
  307. return new APIResult(new { total, data = result, data2 = reason_result });
  308. //RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMM}");
  309. //RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMdd}");
  310. //RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  311. //string result = success ? "success" : "fail";
  312. //RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  313. //RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  314. //RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  315. //if (!string.IsNullOrEmpty(message))
  316. //{
  317. // RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  318. // RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  319. // RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  320. // RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  321. // RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  322. //}
  323. //if (!string.IsNullOrEmpty(reason))
  324. //{
  325. // RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  326. // RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  327. // RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  328. // RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  329. // RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  330. //}
  331. }
  332. }
  333. }