| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using System.Text.Json;
- using System.Threading.Tasks;
- using CSRedis;
- using dodohold.core;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using molilian.core;
- namespace molilian.api.Controllers
- {
- [ApiController]
- [MyAuthorize("admin")]
- [Route("api/[controller]/[action]")]
- public class TracksAdminController : ControllerBase
- {
- readonly IAuthorizationProvider provider = new AdminProvider();
- protected IHttpContextAccessor _accessor;
- public TracksAdminController(IHttpContextAccessor accessor)
- {
- _accessor = accessor;
- }
- [HttpPost]
- public ActionResult list([FromBody] JsonElement form)
- {
- var token = provider.Get(_accessor.HttpContext);
- int page = form.Read("current", 1);
- int size = form.Read("pageSize", 10);
- bool getTotal = form.Read("getTotal", true);
- string sort = form.Read("sort", "id");
- string order = form.Read("order", "DESC");
- string keyword = form.Read("keyword", string.Empty);
- string filter = string.Empty;
- if (!string.IsNullOrEmpty(keyword))
- {
- filter += " AND (event_type LIKE @keyword OR platform LIKE @keyword OR scene LIKE @keyword OR unique_id LIKE @keyword)";
- keyword = $"%{keyword}%";
- }
- filter = filter.StringTrimStart(" AND ");
- order = "descending".Equals(order, System.StringComparison.OrdinalIgnoreCase) ? "DESC" :
- "ascending".Equals(order, System.StringComparison.OrdinalIgnoreCase) ? "ASC" : order;
- string orderBy = string.IsNullOrEmpty(sort) ? "id DESC" : $"{sort} {order}";
- var result = new DBContext.Table("track_links")
- .Where(filter, new { keyword })
- .Page(size, page)
- .Order(orderBy)
- .PageList<dynamic>(getTotal);
- return new APIResult(new { data = result });
- }
- [HttpGet]
- public ActionResult info([FromQuery] int id)
- {
- var token = provider.Get(_accessor.HttpContext);
- var item = new DBContext.Table("track_links").Get<TrackLinkDTO>("id=@id", new { id });
- return new APIResult(new { data = item });
- }
- [HttpPost]
- public ActionResult report([FromBody] JsonElement form)
- {
- var token = provider.Get(_accessor.HttpContext);
- int page = form.Read("current", 1);
- int size = form.Read("pageSize", 10);
- bool getTotal = form.Read("getTotal", true);
- int linkId = form.Read("track_link_id", 0);
- string eventType = form.Read("event_type", string.Empty);
- string platform = form.Read("platform", string.Empty);
- string scene = form.Read("scene", string.Empty);
- string uniqueId = form.Read("unique_id", string.Empty);
- DateTime? start = form.Read<DateTime?>("start", null);
- DateTime? end = form.Read<DateTime?>("end", null);
- string filter = string.Empty;
- if (linkId > 0) filter += " AND track_link_id=@linkId";
- if (!string.IsNullOrWhiteSpace(eventType)) filter += " AND event_type=@eventType";
- if (!string.IsNullOrWhiteSpace(platform)) filter += " AND platform=@platform";
- if (!string.IsNullOrWhiteSpace(scene)) filter += " AND scene=@scene";
- if (!string.IsNullOrWhiteSpace(uniqueId)) filter += " AND unique_id=@uniqueId";
- if (start.HasValue) filter += " AND report_date>=@start";
- if (end.HasValue) filter += " AND report_date<=@end";
- filter = filter.StringTrimStart(" AND ");
- string orderBy = "report_date DESC";
- var result = new DBContext.Table("track_daily_report")
- .Where(filter, new { linkId, eventType, platform, scene, uniqueId, start, end })
- .Page(size, page)
- .Order(orderBy)
- .PageList<TrackDailyReportDTO>(getTotal);
- return new APIResult(new { data = result });
- }
- [HttpPost]
- public async Task<ActionResult> create([FromBody] TrackLinkDTO data)
- {
- var token = provider.Get(_accessor.HttpContext);
- var link = await TracksCore.CreateLinkAsync(data.event_type, data.typename, data.scene, data.unique_id, data.description, true);
- bool success = link != null && link.id > 0;
- return new APIResult(new
- {
- data = link,
- success,
- msg = success ? "创建成功" : "创建失败"
- });
- }
- [HttpPost]
- public ActionResult delete([FromBody] JsonElement form)
- {
- var token = provider.Get(_accessor.HttpContext);
- int id = form.Read<int>("id");
- using var conn = DBContext.GetOpenConnection();
- var item = new DBContext.Table(conn, "track_links").Get<TrackLinkDTO>("id=@id", new { id });
- if (item == null) return new APIResult(new { data = new { success = false, msg = "记录不存在" } });
- var result = new DBContext.Table("track_links").Delete("id=@id", new { id });
- bool success = result > 0;
- if (success)
- {
- string eventType = (item.event_type ?? string.Empty).Trim().ToLowerInvariant();
- string platform = (item.platform ?? string.Empty).Trim();
- string scene = string.IsNullOrWhiteSpace(item.scene) ? string.Empty : item.scene.Trim();
- string uniqueId = string.IsNullOrWhiteSpace(item.unique_id) ? string.Empty : item.unique_id.Trim();
- string cacheKey = TracksCore.GetLinkCacheKey(eventType, platform, scene, uniqueId);
- RedisHelper.Del(cacheKey);
- }
- return new APIResult(new
- {
- data = new { success, msg = success ? "删除成功" : "删除失败" },
- });
- }
- }
- }
|