TracksAdminController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 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. string eventType = form.Read("event_type", string.Empty);
  64. string platform = form.Read("platform", string.Empty);
  65. string scene = form.Read("scene", string.Empty);
  66. string uniqueId = form.Read("unique_id", string.Empty);
  67. DateTime? start = form.Read<DateTime?>("start", null);
  68. DateTime? end = form.Read<DateTime?>("end", null);
  69. string filter = string.Empty;
  70. if (linkId > 0) filter += " AND track_link_id=@linkId";
  71. if (!string.IsNullOrWhiteSpace(eventType)) filter += " AND event_type=@eventType";
  72. if (!string.IsNullOrWhiteSpace(platform)) filter += " AND platform=@platform";
  73. if (!string.IsNullOrWhiteSpace(scene)) filter += " AND scene=@scene";
  74. if (!string.IsNullOrWhiteSpace(uniqueId)) filter += " AND unique_id=@uniqueId";
  75. if (start.HasValue) filter += " AND report_date>=@start";
  76. if (end.HasValue) filter += " AND report_date<=@end";
  77. filter = filter.StringTrimStart(" AND ");
  78. string orderBy = "report_date DESC";
  79. var result = new DBContext.Table("track_daily_report")
  80. .Where(filter, new { linkId, eventType, platform, scene, uniqueId, start, end })
  81. .Page(size, page)
  82. .Order(orderBy)
  83. .PageList<TrackDailyReportDTO>(getTotal);
  84. return new APIResult(new { data = result });
  85. }
  86. [HttpPost]
  87. public async Task<ActionResult> create([FromBody] TrackLinkDTO data)
  88. {
  89. var token = provider.Get(_accessor.HttpContext);
  90. var link = await TracksCore.CreateLinkAsync(data.event_type, data.typename, data.scene, data.unique_id, data.description, true);
  91. bool success = link != null && link.id > 0;
  92. return new APIResult(new
  93. {
  94. data = link,
  95. success,
  96. msg = success ? "创建成功" : "创建失败"
  97. });
  98. }
  99. [HttpPost]
  100. public ActionResult delete([FromBody] JsonElement form)
  101. {
  102. var token = provider.Get(_accessor.HttpContext);
  103. int id = form.Read<int>("id");
  104. using var conn = DBContext.GetOpenConnection();
  105. var item = new DBContext.Table(conn, "track_links").Get<TrackLinkDTO>("id=@id", new { id });
  106. if (item == null) return new APIResult(new { data = new { success = false, msg = "记录不存在" } });
  107. var result = new DBContext.Table("track_links").Delete("id=@id", new { id });
  108. bool success = result > 0;
  109. if (success)
  110. {
  111. string eventType = (item.event_type ?? string.Empty).Trim().ToLowerInvariant();
  112. string platform = (item.platform ?? string.Empty).Trim();
  113. string scene = string.IsNullOrWhiteSpace(item.scene) ? string.Empty : item.scene.Trim();
  114. string uniqueId = string.IsNullOrWhiteSpace(item.unique_id) ? string.Empty : item.unique_id.Trim();
  115. string cacheKey = TracksCore.GetLinkCacheKey(eventType, platform, scene, uniqueId);
  116. RedisHelper.Del(cacheKey);
  117. }
  118. return new APIResult(new
  119. {
  120. data = new { success, msg = success ? "删除成功" : "删除失败" },
  121. });
  122. }
  123. }
  124. }