对用户和账号进行模型文件修改,并相关相关业务逻辑代码实现外部接口的查询、增加、删除和修改等操作
This commit is contained in:
parent
a786ad1c6f
commit
df3a0f6adf
12
src/model/account.lua
Normal file
12
src/model/account.lua
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
||||
--- Created by admin.
|
||||
--- DateTime: 2025/10/25 16:36
|
||||
--- 数据表模型文件
|
||||
|
||||
--引用使用的库文件
|
||||
local Model = require("util.model")
|
||||
|
||||
--创建一个数据表相关的模型
|
||||
local Account = Model:new('tbl_account')
|
||||
return Account
|
||||
|
|
@ -1,5 +1,12 @@
|
|||
---
|
||||
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
||||
--- Created by admin.
|
||||
--- DateTime: 2025/10/25 16:36
|
||||
--- 数据表模型文件
|
||||
|
||||
--引用使用的库文件
|
||||
local Model = require("util.model")
|
||||
|
||||
--创建一个数据表相关的模型
|
||||
local User = Model:new('tbl_user')
|
||||
|
||||
return User
|
||||
|
|
@ -3,72 +3,20 @@
|
|||
--- 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 account = require("model.account")
|
||||
|
||||
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)
|
||||
return account:all()
|
||||
end
|
||||
|
||||
--根据用户id获取账号信息
|
||||
--根据账号id获取账号信息
|
||||
function _M.getAccount(id)
|
||||
--组装sql语句
|
||||
local sql = "select * from \"tbl_account\" where id='"..id.."'"
|
||||
return execSQL(sql)
|
||||
return account.find(id)
|
||||
end
|
||||
|
||||
--增加账号信息到数据表
|
||||
|
|
@ -76,83 +24,65 @@ function _M.addAccount(jsonData)
|
|||
--验证数据的正确性,错误时返回
|
||||
local success, result = validator.checkJson(jsonData)
|
||||
if success == false then
|
||||
return 0x000001,result
|
||||
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
|
||||
--根据账号进行验证是否存在
|
||||
local code, res = account:where("name", "=", name):get()
|
||||
if code ~= 0 then
|
||||
return 0x000001, res
|
||||
end
|
||||
local num = 0
|
||||
for _, row in ipairs(res) do
|
||||
for key, value in pairs(row) do
|
||||
num = value
|
||||
num = num + 1
|
||||
end
|
||||
end
|
||||
print("exec result:", num)
|
||||
if num > 0 then
|
||||
return 0x010000,nil
|
||||
--账号存在时返回账号已经存在
|
||||
if num <= 0 then
|
||||
return 0x01000C, 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)
|
||||
local newKeys = "id"
|
||||
local newValues = "'"..helpers.getUuid().."'"
|
||||
|
||||
-- 创建一个账号
|
||||
return account:create(jsonData)
|
||||
end
|
||||
|
||||
--增加账号信息到数据表
|
||||
function _M.deleteAccount(id)
|
||||
--组装sql语句
|
||||
local sql = "delete from \"tbl_account\" where id='"..id.."'"
|
||||
return execSQL(sql)
|
||||
return account:delete(id)
|
||||
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
|
||||
--根据账号id进行验证账号是否存在
|
||||
local code, res = account:find(id)
|
||||
if code ~= 0 then
|
||||
return 0x000001, res
|
||||
end
|
||||
--获取数据表中的数据记录值
|
||||
local num = 0
|
||||
for _, row in ipairs(res) do
|
||||
for key, value in pairs(row) do
|
||||
num = value
|
||||
num = num + 1
|
||||
end
|
||||
end
|
||||
--账号不存在返回错误信息
|
||||
print("exec result:", num)
|
||||
--账号不存在返回错误
|
||||
if num <= 0 then
|
||||
return 0x01000C,nil
|
||||
return 0x01000C, nil
|
||||
end
|
||||
--验证数据的正确性,错误时返回
|
||||
local success, result = validator.checkJson(jsonData)
|
||||
if success == false then
|
||||
return 0x000001,result
|
||||
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)
|
||||
--对数据内容进行更新
|
||||
return account:where('id', '=', id):update(jsonData)
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
|||
|
|
@ -3,72 +3,20 @@
|
|||
--- 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 user = require("model.user")
|
||||
|
||||
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 checkUserExist(where)
|
||||
--组装sql语句
|
||||
local sql = string.format("select count(*) as count from \"tbl_user\" %s", where)
|
||||
print("check sql: "..sql)
|
||||
--获取数据库连接
|
||||
return execSQL(sql)
|
||||
end
|
||||
|
||||
-- 查询数据表中的所有用户信息
|
||||
function _M.getAllUser()
|
||||
--组装sql语句
|
||||
local sql = "select * from \"tbl_user\""
|
||||
return execSQL(sql)
|
||||
return user:all()
|
||||
end
|
||||
|
||||
--根据用户id获取用户信息
|
||||
function _M.getUser(id)
|
||||
--组装sql语句
|
||||
local sql = "select * from \"tbl_user\" where id='"..id.."'"
|
||||
return execSQL(sql)
|
||||
return user:find(id)
|
||||
end
|
||||
|
||||
--增加用户信息到数据表
|
||||
|
|
@ -79,62 +27,56 @@ function _M.addUser(jsonData)
|
|||
return 0x000001,result
|
||||
end
|
||||
--解析json中的键和数据值
|
||||
local keys = ""
|
||||
local values = ""
|
||||
local username, phone, email
|
||||
local name, phone, email
|
||||
for key, value in pairs(result) do
|
||||
keys = keys..key..","
|
||||
values = values..((type(value) == "string") and "'"..value.."'" or value)..","
|
||||
if key == "username" then username = value end
|
||||
if key == "username" then name = value end
|
||||
if key == "phone" then phone = value end
|
||||
if key == "email" then email = value end
|
||||
end
|
||||
|
||||
--根据用户、手机号、邮箱进行验证用户是否存在
|
||||
local where = string.format("where username='%s' or phone='%s' or email='%s'", username, phone, email)
|
||||
local ok, res = checkUserExist(where)
|
||||
if ok ~= 0 then
|
||||
local code, res = user:where("name", "=", name).where("phone", "=", phone).where("email", "=", phone):get()
|
||||
if code ~= 0 then
|
||||
return 0x000001,res
|
||||
end
|
||||
local num = 0
|
||||
for _, row in ipairs(res) do
|
||||
for key, value in pairs(row) do
|
||||
num = value
|
||||
num = num + 1
|
||||
end
|
||||
end
|
||||
print("exec result:", num)
|
||||
if num > 0 then
|
||||
return 0x010000,nil
|
||||
--用户存在时返回用户已经存在
|
||||
if num <= 0 then
|
||||
return 0x01000C,nil
|
||||
end
|
||||
--自己增加对应的uuid数据值
|
||||
local newKeys = keys.."id"
|
||||
local newValues = values.."'"..helpers.getUuid().."'"
|
||||
--组装sql语句
|
||||
local sql = string.format("insert into \"tbl_user\"(%s)values(%s)", newKeys, newValues)
|
||||
return execSQL(sql)
|
||||
|
||||
--产生uuid数据值,增加到json中
|
||||
local newKeys = "id"
|
||||
local newValues = "'"..helpers.getUuid().."'"
|
||||
|
||||
-- 创建一个用户
|
||||
return user:create(jsonData)
|
||||
end
|
||||
|
||||
--增加用户信息到数据表
|
||||
function _M.deleteUser(id)
|
||||
--组装sql语句
|
||||
local sql = "delete from \"tbl_user\" where id='"..id.."'"
|
||||
return execSQL(sql)
|
||||
return user:delete(id)
|
||||
end
|
||||
|
||||
--更新用户信息到数据表
|
||||
function _M.updateUser(id, jsonData)
|
||||
--根据用户id进行验证用户是否存在
|
||||
local where = string.format("where id='%s'", id)
|
||||
local ok, res = checkUserExist(where)
|
||||
if ok ~= 0 then
|
||||
local code, res = user:find(id)
|
||||
if code ~= 0 then
|
||||
return 0x000001,res
|
||||
end
|
||||
local num = 0
|
||||
for _, row in ipairs(res) do
|
||||
for key, value in pairs(row) do
|
||||
num = value
|
||||
num = num + 1
|
||||
end
|
||||
end
|
||||
print("exec result:", num)
|
||||
--用户不存在返回错误
|
||||
if num <= 0 then
|
||||
return 0x01000C,nil
|
||||
end
|
||||
|
|
@ -143,16 +85,8 @@ function _M.updateUser(id, 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_user\" set %s where id='%s'", vals, id)
|
||||
return execSQL(sql)
|
||||
--对数据内容进行更新
|
||||
return user:where('id', '=', id):update(jsonData)
|
||||
end
|
||||
|
||||
return _M
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user