OverrideRulesController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. var now = DateTime.Now;
  56. foreach (var item in result.List)
  57. {
  58. await OverrideRuleCore.LoadStatisticsAsync(item, now);
  59. }
  60. return new APIResult(new { data = result });
  61. }
  62. [HttpPost]
  63. public async Task<ActionResult> update([FromBody] OverrideRuleDTO data)
  64. {
  65. var clientIp = _accessor.HttpContext.GetUserIp();
  66. var token = provider.Get(_accessor.HttpContext);
  67. using var conn = DBContext.GetOpenConnection();
  68. if (data.id == 0) return new APIResult(new { data = new { success = false, msg = "更新失败,请检查输入参数" } });
  69. var item = new DBContext.Table(conn, "tk_override_rules").Get<OverrideRuleDTO>("id=@id", new { data.id });
  70. if (item == null) return new APIResult(new { data = new { success = false, msg = "更新失败,记录不存在" } });
  71. data.last_time = DateTime.Now;
  72. var result = OverrideRuleCore.Update(data, conn);
  73. bool success = result > 0;
  74. if (success)
  75. {
  76. string desc = ObjectComparer.PrintCompareToString(item, data).Trim();
  77. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"tk_override_rules:{data.id}", item.Convert2Json(), desc);
  78. }
  79. return new APIResult(new
  80. {
  81. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  82. });
  83. }
  84. [HttpPost]
  85. public async Task<ActionResult> create([FromBody] OverrideRuleDTO data)
  86. {
  87. var clientIp = _accessor.HttpContext.GetUserIp();
  88. var token = provider.Get(_accessor.HttpContext);
  89. data.create_time = DateTime.Now;
  90. data.last_time = DateTime.Now;
  91. using var conn = DBContext.GetOpenConnection();
  92. var result = OverrideRuleCore.Create(data, conn);
  93. bool success = result > 0;
  94. if (success)
  95. {
  96. OperationLogCore.LogOperation(token.AccessKey, clientIp, $"tk_override_rules:{result}", string.Empty, data.Convert2Json());
  97. }
  98. return new APIResult(new
  99. {
  100. data = new { success, msg = success ? "更新成功" : "更新失败,请检查输入信息" },
  101. });
  102. }
  103. }
  104. }