OverrideRulesController.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Text.Json;
  5. using System.Net;
  6. using static QRCoder.PayloadGenerator;
  7. using MySqlX.XDevAPI;
  8. using OfficeOpenXml.FormulaParsing.LexicalAnalysis;
  9. using StackExchange.Redis;
  10. using YunhuiKit;
  11. namespace molilian.api.Controllers
  12. {
  13. [ApiController]
  14. [MyAuthorize("admin")]
  15. [Route("api/[controller]/[action]")]
  16. public class OverrideRulesController : ControllerBase
  17. {
  18. readonly IAuthorizationProvider provider = new AdminProvider();
  19. protected IHttpContextAccessor _accessor;
  20. public OverrideRulesController(IHttpContextAccessor accessor)
  21. {
  22. _accessor = accessor;
  23. }
  24. [HttpPost]
  25. public async Task<ActionResult> list([FromBody] JsonElement form)
  26. {
  27. var token = provider.Get(_accessor.HttpContext);
  28. int page = form.Read("current", 1);
  29. int size = form.Read("pageSize", 10);
  30. bool getTotal = form.Read("getTotal", true);
  31. string sort = form.Read<string>("sort");
  32. string order = form.Read<string>("order");
  33. string filter = string.Empty;
  34. var keyword = form.Read("keyword", string.Empty);
  35. if (!string.IsNullOrEmpty(keyword))
  36. {
  37. filter += $" AND (original_text like @keyword OR original_text like @keyword)";
  38. keyword = $"%{keyword}%";
  39. }
  40. filter = filter.StringTrimStart(" AND ");
  41. string orderBy = "sort DESC, id DESC";
  42. if (!string.IsNullOrEmpty(order))
  43. {
  44. order = "descending".Equals(order) ? "DESC" : "ASC";
  45. orderBy = sort switch
  46. {
  47. _ => $"{sort} {order}",
  48. };
  49. }
  50. var result = new DBContext.Table("tk_override_rules")
  51. .Where(filter, new { keyword })
  52. .Page(size, page)
  53. .Order(orderBy)
  54. .PageList<OverrideRuleDTO>(getTotal);
  55. foreach (var item in result.List)
  56. {
  57. string hourlyKey = $":override_rule_calls:{item.id}:{DateTime.Now:yyyyMMddHH}";
  58. string dailyKey = $":override_rule_calls:{item.id}:{DateTime.Now:yyyyMMdd}";
  59. item.current_daily_calls = await RedisKit.GetAsync<int>(hourlyKey);
  60. item.current_hourly_calls = await RedisKit.GetAsync<int>(hourlyKey);
  61. }
  62. return new APIResult(new { data = result });
  63. }
  64. [HttpPost]
  65. public async Task<ActionResult> update([FromBody] OverrideRuleDTO data)
  66. {
  67. var clientIp = _accessor.HttpContext.GetUserIp();
  68. var token = provider.Get(_accessor.HttpContext);
  69. using var conn = DBContext.GetOpenConnection();
  70. if (data.id == 0) return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入参数" } });
  71. var item = new DBContext.Table(conn, "tk_override_rules").Get<OverrideRuleDTO>("id=@id", new { data.id });
  72. if (item == null) return new APIResult(new { data = new { success = false, msg = "更新失败,记录不存在" } });
  73. data.last_time = DateTime.Now;
  74. var result = OverrideRuleCore.Update(data, conn);
  75. bool success = result > 0;
  76. if (success)
  77. {
  78. string desc = ObjectComparer.PrintCompareToString(item, data).Trim();
  79. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"tk_override_rules:{data.id}", item.Convert2Json(), desc);
  80. }
  81. return new APIResult(new
  82. {
  83. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  84. });
  85. }
  86. [HttpPost]
  87. public async Task<ActionResult> create([FromBody] OverrideRuleDTO data)
  88. {
  89. var clientIp = _accessor.HttpContext.GetUserIp();
  90. var token = provider.Get(_accessor.HttpContext);
  91. data.create_time = DateTime.Now;
  92. data.last_time = DateTime.Now;
  93. using var conn = DBContext.GetOpenConnection();
  94. var result = OverrideRuleCore.Create(data, conn);
  95. bool success = result > 0;
  96. if (success)
  97. {
  98. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"tk_override_rules:{result}", string.Empty, data.Convert2Json());
  99. }
  100. return new APIResult(new
  101. {
  102. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  103. });
  104. }
  105. }
  106. }