log.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. --日志
  2. local M = {}
  3. local mysqldbx = require "mysqldbx"
  4. local tools = require "tools"
  5. local skynet = require "skynet"
  6. local cjson = require "cjson"
  7. function M.add_column( msg_body)
  8. local isok ,key = tools.checkData({"column_name","column_type","default_value"},msg_body)
  9. if not isok then
  10. return false,string.format("缺少字段: %s.", key)
  11. end
  12. local default_param
  13. if type(msg_body.default_value) == "string" then
  14. default_param = string.format(" DEFAULT '%s' ",msg_body.default_value)
  15. elseif type(msg_body.default_value) == "number" then
  16. default_param = string.format(" DEFAULT %d ",msg_body.default_value)
  17. end
  18. local sql = string.format("ALTER TABLE `%s` ADD COLUMN `%s` %s %s",
  19. 'log', msg_body.column_name, msg_body.column_type,default_param)
  20. skynet.error("sql:",sql)
  21. mysqldbx.query(sql)
  22. return true, {}
  23. end
  24. function M.add_log(msg_body)
  25. local isok ,key = tools.checkData({"opt_type","user_id","content"},msg_body)
  26. if not isok then
  27. return false,string.format("缺少字段: %s.", key)
  28. end
  29. local current_time = os.date("%Y-%m-%d %H:%M:%S")
  30. local sql
  31. sql = string.format("INSERT INTO `log` (opt_type,user_id,content,create_day) VALUES (%d,%d,'%s','%s')",msg_body.opt_type,msg_body.user_id,cjson.encode(msg_body.content),current_time)
  32. skynet.error("sql:",sql)
  33. tools.dump(mysqldbx.query(sql))
  34. return true, {}
  35. end
  36. function M.diy_add_log(msg_body)
  37. local isok ,key = tools.checkData({"data"},msg_body)
  38. if not isok then
  39. return false,string.format("缺少字段: %s.", key)
  40. end
  41. local current_time = os.date("%Y-%m-%d %H:%M:%S")
  42. local table_name = 'log'
  43. local data = msg_body.data
  44. local fields, values_str = {}, {}
  45. -- 改用 table.index 判断替代 contains
  46. local sql_functions = {
  47. ["CURRENT_TIMESTAMP"] = true,
  48. ["NOW()"] = true,
  49. ["UUID()"] = true
  50. }
  51. table.insert(fields, "`" .. "create_day" .. "`")
  52. table.insert(values_str, "'" .. current_time .. "'" )
  53. for field, value in pairs(data) do
  54. -- 过滤字段名中的非法字符
  55. field = field:gsub("[%s`'\"]", "")
  56. table.insert(fields, "`" .. field .. "`")
  57. -- 检查是否为 SQL 函数
  58. if sql_functions[value] then
  59. table.insert(values_str, value)
  60. else
  61. -- 处理普通值
  62. if type(value) == "number" then
  63. table.insert(values_str, tostring(value))
  64. elseif type(value) == "string" then
  65. table.insert(values_str, "'" .. value:gsub("'", "''") .. "'") -- 转义单引号
  66. elseif value == nil then
  67. table.insert(values_str, "NULL")
  68. elseif type(value) == "table" then
  69. table.insert(values_str, "'"..cjson.encode(value).."'")
  70. else
  71. error("不支持的数据类型: " .. type(value))
  72. end
  73. end
  74. end
  75. local sql = "INSERT INTO `" .. table_name .. "` (" .. table.concat(fields, ", ") .. ") " ..
  76. "VALUES (" .. table.concat(values_str, ", ") .. ")"
  77. skynet.error("sql:",sql)
  78. mysqldbx.query(sql)
  79. return true, {}
  80. end
  81. function M.search(msg_body)
  82. local isok ,key = tools.checkData({"data","opt_type","user_id","page_size", "page_number","start_create_day","end_create_day"},msg_body)
  83. if not isok then
  84. return false,string.format("缺少字段: %s.", key)
  85. end
  86. local page_size = msg_body.page_size
  87. local page_number = msg_body.page_number
  88. local offset = (page_number - 1) * page_size
  89. local opt_type_param = ""
  90. if msg_body.opt_type~="" then
  91. opt_type_param = string.format(" AND opt_type = %d ",msg_body.opt_type)
  92. end
  93. local user_id_param = ""
  94. if msg_body.user_id~="" then
  95. user_id_param = string.format(" AND user_id = %d ",msg_body.user_id)
  96. end
  97. local create_day_param = ""
  98. if msg_body.start_create_day~="" and msg_body.end_create_day~="" then
  99. create_day_param = " AND DATE(create_time) >= DATE(FROM_UNIXTIME(" .. (msg_body.start_create_day / 1000) .. ")) AND DATE(create_time) <= DATE(FROM_UNIXTIME(" .. (msg_body.end_create_day / 1000) .. ")) "
  100. end
  101. local diy_param = ""
  102. if msg_body.data~="" then
  103. local data = msg_body.data
  104. local fields, values_str = {}, {}
  105. for field, value in pairs(data) do
  106. -- 过滤字段名中的非法字符
  107. field = field:gsub("[%s`'\"]", "")
  108. table.insert(fields, "`" .. field .. "`")
  109. local cur_param = ""
  110. -- 处理普通值
  111. if type(value) == "number" then
  112. -- table.insert(values_str, tostring(value))
  113. cur_param = " AND ".."`" .. field .. "`".." = "..value
  114. elseif type(value) == "string" then
  115. cur_param = " AND ".."`" .. field .. "`".." = ".."'" .. value:gsub("'", "''") .. "'"
  116. -- table.insert(values_str, "'" .. value:gsub("'", "''") .. "'") -- 转义单引号
  117. else
  118. error("不支持的数据类型: " .. type(value))
  119. end
  120. diy_param = diy_param..cur_param
  121. end
  122. end
  123. local param = diy_param..opt_type_param..user_id_param..create_day_param
  124. local sql = "SELECT * FROM log WHERE 1=1 "..param.."ORDER BY id DESC"..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
  125. skynet.error("sql:",sql)
  126. -- 执行查询
  127. local res = mysqldbx.query(sql)
  128. -- 使用 COUNT(*) 优化总数查询
  129. sql = "SELECT COUNT(*) AS total FROM log WHERE 1=1 "..param
  130. local total = mysqldbx.query(sql)
  131. return true, res, total[1].total
  132. end
  133. return M