904118851 2 mesi fa
parent
commit
18bd1a8ec0
2 ha cambiato i file con 103 aggiunte e 0 eliminazioni
  1. 2 0
      service/backmgr/init.lua
  2. 101 0
      service/backmgr/log.lua

+ 2 - 0
service/backmgr/init.lua

@@ -47,6 +47,7 @@ local material_platform = require "material_platform"
 local data_manager = require "data_manager"
 local statistics_platform = require "statistics_platform"
 local review_user = require "review_user"
+local log = require "log"
 local status_200 = 200
 local CMD = {
     
@@ -92,6 +93,7 @@ CMD["material_platform"] = material_platform;
 CMD["data_manager"] = data_manager;
 CMD["statistics_platform"] = statistics_platform;
 CMD["review_user"] = review_user;
+CMD["log"] = log;
 function run(target,fun,msg_body,fd)
     if target~=nil and fun~=nil and target[fun]~=nil then
         local isok,data,total = target[fun](msg_body)

+ 101 - 0
service/backmgr/log.lua

@@ -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