|
@@ -0,0 +1,101 @@
|
|
|
+--日志
|
|
|
+local M = {}
|
|
|
+local mysqldbx = require "mysqldbx"
|
|
|
+local tools = require "tools"
|
|
|
+local skynet = require "skynet"
|
|
|
+local cjson = require "cjson"
|
|
|
+
|
|
|
+
|
|
|
+function M.add_column( column_name, column_type)
|
|
|
+ local sql = string.format("ALTER TABLE `%s` ADD COLUMN `%s` %s",
|
|
|
+ 'log', column_name, column_type)
|
|
|
+ mysqldbx.query(sql)
|
|
|
+ return true, {}
|
|
|
+end
|
|
|
+
|
|
|
+function M.add_log(msg_body)
|
|
|
+ local isok ,key = tools.checkData({"opt_type","user_id","content"},msg_body)
|
|
|
+ if not isok then
|
|
|
+ return false,string.format("缺少字段: %s.", key)
|
|
|
+ end
|
|
|
+ local sql
|
|
|
+ sql = string.format("INSERT INTO `log` (opt_type,user_id,content) VALUES (%d,%d,'%s')",msg_body.opt_type,msg_body.user_id,cjson.encode(msg_body.content))
|
|
|
+ mysqldbx.query(sql)
|
|
|
+ return true, {}
|
|
|
+end
|
|
|
+
|
|
|
+
|
|
|
+function M.diy_add_log(msg_body)
|
|
|
+ local table_name = 'log'
|
|
|
+ local data = msg_body.data
|
|
|
+ -- 构建SQL语句
|
|
|
+ local fields = {}
|
|
|
+ local values = {}
|
|
|
+ local placeholders = {}
|
|
|
+ local updates = {}
|
|
|
+
|
|
|
+ -- 处理数据
|
|
|
+ for field, value in pairs(data) do
|
|
|
+ table.insert(fields, "`" .. field .. "`")
|
|
|
+
|
|
|
+ if value == "CURRENT_TIMESTAMP" or value == "NOW()" then
|
|
|
+ table.insert(placeholders, value)
|
|
|
+ else
|
|
|
+ table.insert(placeholders, "?")
|
|
|
+ table.insert(values, value)
|
|
|
+ end
|
|
|
+
|
|
|
+ end
|
|
|
+
|
|
|
+ local sql = "INSERT "
|
|
|
+ sql = sql .. "INTO `" .. table_name .. "` (" .. table.concat(fields, ", ") .. ") "
|
|
|
+ sql = sql .. "VALUES (" .. table.concat(placeholders, ", ") .. ")"
|
|
|
+ mysqldbx.query(sql)
|
|
|
+ return true, {}
|
|
|
+
|
|
|
+end
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+function M.search(msg_body)
|
|
|
+ local isok ,key = tools.checkData({"opt_type","user_id","page_size", "page_number","start_create_day","end_create_day"},msg_body)
|
|
|
+ if not isok then
|
|
|
+ return false,string.format("缺少字段: %s.", key)
|
|
|
+ end
|
|
|
+ local page_size = msg_body.page_size
|
|
|
+ local page_number = msg_body.page_number
|
|
|
+ local offset = (page_number - 1) * page_size
|
|
|
+
|
|
|
+
|
|
|
+ local opt_type_param = ""
|
|
|
+ if msg_body.opt_type~="" then
|
|
|
+ opt_type_param = string.format(" AND opt_type = %d ",msg_body.opt_type)
|
|
|
+ end
|
|
|
+
|
|
|
+ local user_id_param = ""
|
|
|
+ if msg_body.user_id~="" then
|
|
|
+ user_id_param = string.format(" AND user_id = %d ",msg_body.user_id)
|
|
|
+ end
|
|
|
+
|
|
|
+ local create_day_param = ""
|
|
|
+ if msg_body.start_create_day~="" and msg_body.end_create_day~="" then
|
|
|
+ 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) .. ")) "
|
|
|
+ end
|
|
|
+
|
|
|
+
|
|
|
+ local param = opt_type_param..user_id_param..create_day_param
|
|
|
+
|
|
|
+ local sql = "SELECT * FROM log WHERE 1=1 "..param.."ORDER BY id DESC"..string.format(" LIMIT %d OFFSET %d ",page_size, offset)
|
|
|
+
|
|
|
+ skynet.error("sql:",sql)
|
|
|
+ -- 执行查询
|
|
|
+ local res = mysqldbx.query(sql)
|
|
|
+ -- 使用 COUNT(*) 优化总数查询
|
|
|
+ sql = "SELECT COUNT(*) AS total FROM log WHERE 1=1 "..param
|
|
|
+
|
|
|
+ local total = mysqldbx.query(sql)
|
|
|
+
|
|
|
+ return true, res, total[1].total
|
|
|
+end
|
|
|
+
|
|
|
+return M
|