2025-10-22 08:46:41 +08:00
|
|
|
|
---
|
|
|
|
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
|
|
|
|
--- Created by .
|
|
|
|
|
|
--- DateTime: 2025/9/25 08:19
|
|
|
|
|
|
--- 业务逻辑 对用户数据表进行数据表业务处理
|
|
|
|
|
|
local cjson = require('cjson')
|
|
|
|
|
|
local pgmoon = require('pgmoon')
|
|
|
|
|
|
local dbconf = require("config.database")
|
|
|
|
|
|
local status = require("config.status")
|
|
|
|
|
|
local snowflake = require("util.snowflake")
|
|
|
|
|
|
|
|
|
|
|
|
local _M = {}
|
|
|
|
|
|
|
|
|
|
|
|
--获取一个数据库操作连接
|
|
|
|
|
|
local function get_con(cfg)
|
|
|
|
|
|
local code = 0
|
|
|
|
|
|
-- 创建一个新的连接
|
|
|
|
|
|
local conn = pgmoon.new(cfg);
|
|
|
|
|
|
---- 连接到数据库
|
|
|
|
|
|
local ok, err = conn:connect()
|
|
|
|
|
|
if not ok then
|
|
|
|
|
|
print("Connection failed: " .. err)
|
|
|
|
|
|
code = 0x000002
|
|
|
|
|
|
end
|
|
|
|
|
|
--ngx.say("Connection success")
|
|
|
|
|
|
return code,conn
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function getUuid()
|
|
|
|
|
|
local workerId = 0 -- 假设当前机器的ID是1,范围在[0, 31]之间
|
|
|
|
|
|
local datacenterId = 0 -- 数据中心ID,范围在[0, 31]之间
|
|
|
|
|
|
local snow = snowflake.new(workerId, datacenterId)
|
|
|
|
|
|
local id = snow:generateUniqueId()-- 生成ID
|
|
|
|
|
|
--print("Generated ID:", snow.int64_to_string(id))
|
|
|
|
|
|
return snow.int64_to_string(id)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- 查询数据表中的所有用户信息
|
|
|
|
|
|
function _M.getAllUser()
|
|
|
|
|
|
--组装sql语句
|
|
|
|
|
|
local sql = "select * from \"T_Users\""
|
|
|
|
|
|
--获取数据库连接
|
|
|
|
|
|
local code,conn = get_con(dbconf.postgres)
|
|
|
|
|
|
--设置数据库的编码格式
|
|
|
|
|
|
--conn:exec("SET NAMES UTF8")
|
|
|
|
|
|
--执行数据库操作
|
|
|
|
|
|
local res = conn:query(sql)
|
|
|
|
|
|
if not res then
|
|
|
|
|
|
print("get all users Query failed: "..sql)
|
|
|
|
|
|
return 0x000003,res
|
|
|
|
|
|
end
|
|
|
|
|
|
--整理数据库结果返回值
|
|
|
|
|
|
--for _, row in ipairs(res) do
|
|
|
|
|
|
-- for key, value in pairs(row) do
|
|
|
|
|
|
-- ngx.say(key .. ":" .. tostring(value))
|
|
|
|
|
|
-- end
|
|
|
|
|
|
--end
|
|
|
|
|
|
--关闭数据库
|
|
|
|
|
|
conn:disconnect()
|
|
|
|
|
|
return code,res
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据用户id获取用户信息
|
|
|
|
|
|
function _M.getUser(id)
|
|
|
|
|
|
--组装sql语句
|
|
|
|
|
|
local sql = "select * from \"T_Users\" where id="..id
|
|
|
|
|
|
--获取数据库连接
|
|
|
|
|
|
local code,conn = get_con(dbconf.postgres)
|
|
|
|
|
|
--设置数据库的编码格式
|
|
|
|
|
|
--conn:exec("SET NAMES UTF8")
|
|
|
|
|
|
--执行数据库操作
|
|
|
|
|
|
local res = conn:query(sql)
|
|
|
|
|
|
if not res then
|
|
|
|
|
|
print("Query failed: "..sql)
|
|
|
|
|
|
return 0x000003,res
|
|
|
|
|
|
end
|
|
|
|
|
|
--关闭数据库
|
|
|
|
|
|
conn:disconnect()
|
|
|
|
|
|
return code,res
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--增加用户信息到数据表
|
|
|
|
|
|
function _M.addUser(jsonData)
|
|
|
|
|
|
--ngx.say(jsonData)
|
|
|
|
|
|
local success, result = pcall(function()
|
|
|
|
|
|
return cjson.decode(jsonData)
|
|
|
|
|
|
end)
|
|
|
|
|
|
local res = nil
|
|
|
|
|
|
if success == false then
|
|
|
|
|
|
return 0x000001,res
|
|
|
|
|
|
end
|
|
|
|
|
|
--解析json中的键和数据值
|
|
|
|
|
|
local keys = ""
|
|
|
|
|
|
local values = ""
|
|
|
|
|
|
for key, value in pairs(result) do
|
|
|
|
|
|
keys = keys..key
|
|
|
|
|
|
local val = type(value)
|
|
|
|
|
|
if val == "string" then
|
|
|
|
|
|
values = values.."\'"..value.."\'"
|
|
|
|
|
|
else
|
|
|
|
|
|
values = values..value
|
|
|
|
|
|
end
|
|
|
|
|
|
keys = keys..","
|
|
|
|
|
|
values = values..","
|
|
|
|
|
|
end
|
|
|
|
|
|
--去掉组装最后一位逗号(,)
|
|
|
|
|
|
--local newKeys = keys:sub(1, #keys -1)
|
|
|
|
|
|
--local newValues = values:sub(1, #values -1)
|
|
|
|
|
|
--自己增加对应的uuid数据值
|
|
|
|
|
|
local newKeys = keys.."uuid"
|
|
|
|
|
|
local uuid = getUuid()
|
|
|
|
|
|
local newValues = values.."\'"..uuid.."\'"
|
|
|
|
|
|
--组装sql语句
|
|
|
|
|
|
local sql = string.format("insert into \"T_Users\"(%s)values(%s)", newKeys, newValues)
|
|
|
|
|
|
--ngx.say(sql)
|
|
|
|
|
|
--获取数据库连接
|
|
|
|
|
|
local code,conn = get_con(dbconf.postgres)
|
|
|
|
|
|
--执行数据库操作
|
|
|
|
|
|
res = conn:query(sql)
|
|
|
|
|
|
if not res then
|
|
|
|
|
|
print("adduser sql failed: "..sql)
|
|
|
|
|
|
return 0x000003,res
|
|
|
|
|
|
end
|
|
|
|
|
|
--关闭数据库
|
|
|
|
|
|
conn:disconnect()
|
|
|
|
|
|
return code,res
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--增加用户信息到数据表
|
|
|
|
|
|
function _M.delete_user(id)
|
|
|
|
|
|
--组装sql语句
|
|
|
|
|
|
local sql = "delete from \"T_Users\" where id="..id
|
|
|
|
|
|
--获取数据库连接
|
|
|
|
|
|
local code,conn = get_con(dbconf.postgres)
|
|
|
|
|
|
--执行数据库操作
|
|
|
|
|
|
local res = conn:query(sql)
|
|
|
|
|
|
if not res then
|
|
|
|
|
|
print("delete exec sql failed: "..sql)
|
|
|
|
|
|
return 0x000003,res
|
|
|
|
|
|
end
|
|
|
|
|
|
--关闭数据库
|
|
|
|
|
|
conn:disconnect()
|
|
|
|
|
|
return code,res
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return _M
|