TracksAdminController.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System.Text.Json;
  2. using System.Threading.Tasks;
  3. using CSRedis;
  4. using dodohold.core;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using molilian.core;
  8. namespace molilian.api.Controllers
  9. {
  10. [ApiController]
  11. [MyAuthorize("admin")]
  12. [Route("api/[controller]/[action]")]
  13. public class TracksAdminController : ControllerBase
  14. {
  15. readonly IAuthorizationProvider provider = new AdminProvider();
  16. protected IHttpContextAccessor _accessor;
  17. public TracksAdminController(IHttpContextAccessor accessor)
  18. {
  19. _accessor = accessor;
  20. }
  21. [HttpPost]
  22. public ActionResult list([FromBody] JsonElement form)
  23. {
  24. var token = provider.Get(_accessor.HttpContext);
  25. int page = form.Read("current", 1);
  26. int size = form.Read("pageSize", 10);
  27. bool getTotal = form.Read("getTotal", true);
  28. string sort = form.Read("sort", "id");
  29. string order = form.Read("order", "DESC");
  30. string keyword = form.Read("keyword", string.Empty);
  31. string filter = string.Empty;
  32. if (!string.IsNullOrEmpty(keyword))
  33. {
  34. filter += " AND (event_type LIKE @keyword OR platform LIKE @keyword OR typename LIKE @keyword OR scene LIKE @keyword OR unique_id LIKE @keyword)";
  35. keyword = $"%{keyword}%";
  36. }
  37. filter = filter.StringTrimStart(" AND ");
  38. order = "descending".Equals(order, System.StringComparison.OrdinalIgnoreCase) ? "DESC" :
  39. "ascending".Equals(order, System.StringComparison.OrdinalIgnoreCase) ? "ASC" : order;
  40. string orderBy = string.IsNullOrEmpty(sort) ? "id DESC" : $"{sort} {order}";
  41. var result = new DBContext.Table("track_links")
  42. .Where(filter, new { keyword })
  43. .Page(size, page)
  44. .Order(orderBy)
  45. .PageList<dynamic>(getTotal);
  46. return new APIResult(new { data = result });
  47. }
  48. [HttpGet]
  49. public ActionResult info([FromQuery] int id)
  50. {
  51. var token = provider.Get(_accessor.HttpContext);
  52. var item = new DBContext.Table("track_links").Get<TrackLinkDTO>("id=@id", new { id });
  53. return new APIResult(new { data = item });
  54. }
  55. [HttpPost]
  56. public ActionResult report([FromBody] JsonElement form)
  57. {
  58. var token = provider.Get(_accessor.HttpContext);
  59. int page = form.Read("current", 1);
  60. int size = form.Read("pageSize", 10);
  61. bool getTotal = form.Read("getTotal", true);
  62. int linkId = form.Read("track_link_id", 0);
  63. int accountId = form.Read("account_id", -1);
  64. bool includeAccountBreakdown = form.Read("include_account_breakdown", false);
  65. bool accountBreakdownOnly = form.Read("account_breakdown_only", false);
  66. string eventType = form.Read("event_type", string.Empty);
  67. string platform = form.Read("platform", string.Empty);
  68. string typename = form.Read("typename", string.Empty);
  69. string scene = form.Read("scene", string.Empty);
  70. string uniqueId = form.Read("unique_id", string.Empty);
  71. DateTime? start = form.Read<DateTime?>("start", null);
  72. DateTime? end = form.Read<DateTime?>("end", null);
  73. string filter = string.Empty;
  74. if (linkId > 0) filter += " AND track_link_id=@linkId";
  75. if (accountBreakdownOnly) filter += " AND account_id>0";
  76. else if (accountId >= 0) filter += " AND account_id=@accountId";
  77. else if (!includeAccountBreakdown) filter += " AND account_id=0";
  78. if (!string.IsNullOrWhiteSpace(eventType)) filter += " AND event_type=@eventType";
  79. if (!string.IsNullOrWhiteSpace(platform)) filter += " AND platform=@platform";
  80. if (!string.IsNullOrWhiteSpace(typename)) filter += " AND typename=@typename";
  81. if (!string.IsNullOrWhiteSpace(scene)) filter += " AND scene=@scene";
  82. if (!string.IsNullOrWhiteSpace(uniqueId)) filter += " AND unique_id=@uniqueId";
  83. if (start.HasValue) filter += " AND report_date>=@start";
  84. if (end.HasValue) filter += " AND report_date<=@end";
  85. filter = filter.StringTrimStart(" AND ");
  86. string orderBy = "report_date DESC";
  87. var result = new DBContext.Table("track_daily_report")
  88. .Where(filter, new { linkId, accountId, eventType, platform, typename, scene, uniqueId, start, end })
  89. .Page(size, page)
  90. .Order(orderBy)
  91. .PageList<TrackDailyReportDTO>(getTotal);
  92. return new APIResult(new { data = result });
  93. }
  94. [HttpPost]
  95. public async Task<ActionResult> hourly([FromBody] JsonElement form)
  96. {
  97. var token = provider.Get(_accessor.HttpContext);
  98. int linkId = form.Read("track_link_id", 0);
  99. int accountId = form.Read("account_id", 0);
  100. string dateText = form.Read("date", string.Empty);
  101. string scene = form.Read("scene", string.Empty);
  102. DateTime date = DateTime.TryParse(dateText, out var parsed) ? parsed.Date : DateTime.Now.Date;
  103. var list = await TracksCore.GetHourlyReportAsync(linkId, date, scene, accountId);
  104. return new APIResult(new
  105. {
  106. data = new
  107. {
  108. list,
  109. count = list.Count
  110. }
  111. });
  112. }
  113. [HttpPost]
  114. public async Task<ActionResult> create([FromBody] TrackLinkDTO data)
  115. {
  116. var token = provider.Get(_accessor.HttpContext);
  117. if (string.IsNullOrWhiteSpace(data.typename)) data.typename = data.platform;
  118. if (string.IsNullOrWhiteSpace(data.platform)) data.platform = data.typename;
  119. var link = await TracksCore.CreateLinkAsync(data.event_type, data.platform, data.typename, data.scene, data.unique_id, data.description, true);
  120. bool success = link != null && link.id > 0;
  121. return new APIResult(new
  122. {
  123. data = link,
  124. success,
  125. msg = success ? "创建成功" : "创建失败"
  126. });
  127. }
  128. [HttpPost]
  129. public ActionResult delete([FromBody] JsonElement form)
  130. {
  131. var token = provider.Get(_accessor.HttpContext);
  132. int id = form.Read<int>("id");
  133. using var conn = DBContext.GetOpenConnection();
  134. var item = new DBContext.Table(conn, "track_links").Get<TrackLinkDTO>("id=@id", new { id });
  135. if (item == null) return new APIResult(new { data = new { success = false, msg = "记录不存在" } });
  136. var result = new DBContext.Table("track_links").Delete("id=@id", new { id });
  137. bool success = result > 0;
  138. if (success)
  139. {
  140. string eventType = (item.event_type ?? string.Empty).Trim().ToLowerInvariant();
  141. string platform = (item.platform ?? string.Empty).Trim();
  142. string typename = (item.typename ?? string.Empty).Trim();
  143. string scene = string.IsNullOrWhiteSpace(item.scene) ? string.Empty : item.scene.Trim();
  144. string uniqueId = string.IsNullOrWhiteSpace(item.unique_id) ? string.Empty : item.unique_id.Trim();
  145. string cacheKey = TracksCore.GetLinkCacheKey(eventType, platform, typename, scene, uniqueId);
  146. RedisHelper.Del(cacheKey);
  147. RedisHelper.Del(TracksCore.GetLinkIdCacheKey(item.id));
  148. }
  149. return new APIResult(new
  150. {
  151. data = new { success, msg = success ? "删除成功" : "删除失败" },
  152. });
  153. }
  154. }
  155. }