template.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. require("fwutils.webapi")
  2. -- CREATE TABLE `fw_template` (
  3. -- `id` int NOT NULL AUTO_INCREMENT,
  4. -- `role_id` int DEFAULT NULL,
  5. -- `key` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  6. -- `value` varchar(2048) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  7. -- `enable` tinyint NOT NULL COMMENT '是否启用',
  8. -- PRIMARY KEY (`id`),
  9. -- UNIQUE KEY `role_id` (`role_id`,`key`)
  10. -- ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Fastweb-关键词模板';
  11. local M = {}
  12. function M.get_by_id(id,conn)
  13. local select = conn:select()
  14. select:table("fw_template LEFT JOIN fw_role ON fw_template.role_id = fw_role.id")
  15. select:field({
  16. "fw_template.id",
  17. "fw_template.role_id",
  18. "fw_template.`key`",
  19. "fw_template.value",
  20. "fw_template.enable",
  21. "fw_role.title as role_title",
  22. })
  23. select:where_i32("fw_template.id","=",id)
  24. select:limit(0,1)
  25. local result = select:query()
  26. if result:row_count() == 0 then
  27. return nil
  28. end
  29. local d = result:table()[1]
  30. return d
  31. end
  32. function M.add(data,conn)
  33. local insert = conn:insert()
  34. insert:table("fw_template")
  35. if data.role_id ~= nil and data.role_id ~= cjson.null then
  36. insert:set_i32("role_id",data.role_id)
  37. end
  38. if data.key ~= nil then
  39. insert:set_str("`key`",data.key)
  40. end
  41. if data.value ~= nil then
  42. insert:set_str("value",data.value)
  43. end
  44. if data.enable ~= nil then
  45. insert:set_i32("enable",data.enable)
  46. end
  47. local d = insert:exec()
  48. return d == 1
  49. end
  50. function M.update(data,conn)
  51. local update = conn:update()
  52. update:table("fw_template")
  53. if data.role_id ~= nil then
  54. update:set_i32("role_id",data.role_id)
  55. end
  56. if data.key ~= nil then
  57. update:set_str("`key`",data.key)
  58. end
  59. if data.value ~= nil then
  60. update:set_str("value",data.value)
  61. end
  62. if data.enable ~= nil then
  63. update:set_i32("enable",data.enable)
  64. end
  65. update:where_i32("id","=",data.id)
  66. local d = update:exec()
  67. return d == 1
  68. end
  69. function M.delete(id,conn)
  70. local del = conn:delete()
  71. del:table("fw_template")
  72. del:where_i32("id","=",id)
  73. local d = del:exec()
  74. return d == 1
  75. end
  76. function M.list(search,limit,conn)
  77. return query_model_ex(conn,[=[
  78. fw_template LEFT JOIN fw_role ON fw_template.role_id = fw_role.id
  79. ]=],{
  80. "fw_template.id",
  81. "fw_template.role_id",
  82. "fw_template.`key`",
  83. "fw_template.value",
  84. "fw_template.enable",
  85. "fw_role.title as role_title",
  86. "fw_role.id as role_id",
  87. },limit.start,limit.length,function(sel)
  88. if search.role_id ~= nil and search.role_id ~= -1 then
  89. sel:where_i32("fw_template.role_id","=",search.role_id)
  90. end
  91. end,function(sel_data)
  92. sel_data:orderby("fw_template.enable DESC")
  93. end)
  94. end
  95. return M