157 lines
4.5 KiB
Lua
157 lines
4.5 KiB
Lua
---
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
--- Created by .
|
|
--- DateTime: 2025/9/25 08:19
|
|
--- 业务逻辑 对用户数据表进行数据表业务处理
|
|
local pgmoon = require('pgmoon')
|
|
local dbconf = require("config.database")
|
|
local status = require("config.status")
|
|
local validator = require("util.validator")
|
|
local helpers = require("util.helpers")
|
|
|
|
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
|
|
|
|
--数据库执行sql语句
|
|
local function execSQL(sql)
|
|
local res = nil
|
|
if sql == '' or sql == nil then
|
|
return 0x000003,res
|
|
end
|
|
--获取数据库连接
|
|
local code,conn = get_con(dbconf.postgres)
|
|
if code ~= 0 then
|
|
return 0x000003,res
|
|
end
|
|
--执行数据库操作
|
|
res = conn:query(sql)
|
|
if not res then
|
|
print("query sql failed: "..sql)
|
|
return 0x000003,res
|
|
end
|
|
--关闭数据库
|
|
conn:disconnect()
|
|
return code,res
|
|
end
|
|
|
|
--通过查询条件判断数据库中的数据记录
|
|
local function checkAccountExist(where)
|
|
--组装sql语句
|
|
local sql = string.format("select count(*) as count from \"tbl_account\" %s", where)
|
|
--ngx.say("check sql: "..sql)
|
|
--获取数据库连接
|
|
return execSQL(sql)
|
|
end
|
|
|
|
-- 查询数据表中的所有账号信息
|
|
function _M.getAllAccount()
|
|
--组装sql语句
|
|
local sql = "select * from \"tbl_account\""
|
|
return execSQL(sql)
|
|
end
|
|
|
|
--根据用户id获取账号信息
|
|
function _M.getAccount(id)
|
|
--组装sql语句
|
|
local sql = "select * from \"tbl_account\" where id='"..id.."'"
|
|
return execSQL(sql)
|
|
end
|
|
|
|
--增加账号信息到数据表
|
|
function _M.addAccount(jsonData)
|
|
--验证数据的正确性,错误时返回
|
|
local success, result = validator.checkJson(jsonData)
|
|
if success == false then
|
|
return 0x000001,result
|
|
end
|
|
--解析json中的键和数据值
|
|
local keys = ""
|
|
local values = ""
|
|
local name = ""
|
|
for key, value in pairs(result) do
|
|
keys = keys..key..","
|
|
values = values..((type(value) == "string") and "'"..value.."'" or value)..","
|
|
if key == "name" then name = value end
|
|
end
|
|
--根据用户进行验证用户是否存在
|
|
local where = string.format("where name='%s'", name)
|
|
local ok, res = checkAccountExist(where)
|
|
if ok ~= 0 then
|
|
return 0x000001,res
|
|
end
|
|
local num = 0
|
|
for _, row in ipairs(res) do
|
|
for key, value in pairs(row) do
|
|
num = value
|
|
end
|
|
end
|
|
print("exec result:", num)
|
|
if num > 0 then
|
|
return 0x010000,nil
|
|
end
|
|
--自己增加对应的uuid数据值
|
|
local newKeys = keys.."id"
|
|
local newValues = values.."'"..helpers.getUuid().."'"
|
|
--组装sql语句
|
|
local sql = string.format("insert into \"tbl_account\"(%s)values(%s)", newKeys, newValues)
|
|
return execSQL(sql)
|
|
end
|
|
|
|
--增加账号信息到数据表
|
|
function _M.deleteAccount(id)
|
|
--组装sql语句
|
|
local sql = "delete from \"tbl_account\" where id='"..id.."'"
|
|
return execSQL(sql)
|
|
end
|
|
|
|
--更新账号信息到数据表
|
|
function _M.updateAccount(id, jsonData)
|
|
--根据用户id进行验证用户是否存在
|
|
local where = string.format("where id='%s'", id)
|
|
local ok, res = checkAccountExist(where)
|
|
if ok ~= 0 then
|
|
return 0x000001,res
|
|
end
|
|
local num = 0
|
|
for _, row in ipairs(res) do
|
|
for key, value in pairs(row) do
|
|
num = value
|
|
end
|
|
end
|
|
print("exec result:", num)
|
|
if num <= 0 then
|
|
return 0x01000C,nil
|
|
end
|
|
--验证数据的正确性,错误时返回
|
|
local success, result = validator.checkJson(jsonData)
|
|
if success == false then
|
|
return 0x000001,result
|
|
end
|
|
--解析json中的键和数据值
|
|
local tmp = ""
|
|
for key, value in pairs(result) do
|
|
local val = (type(value) == "string") and "'"..value.."'" or value
|
|
tmp = string.format("%s=%s,", key, val)
|
|
end
|
|
local vals = tmp:sub(1, #tmp - 1)
|
|
--组装sql语句
|
|
local sql = string.format("update \"tbl_account\" set %s where id='%s'", vals, id)
|
|
return execSQL(sql)
|
|
end
|
|
|
|
return _M
|