OverrideRulesController.cs 4.2 KB

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