AuthPlatform/src/dao/user.lua

96 lines
2.4 KiB
Lua
Raw Normal View History

---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by admin.
--- DateTime: 2025/10/25 16:36
--- 数据表模型文件
local helpers = require("share.helpers")
--引用使用的库文件
local model = require("share.model")
--创建一个数据表相关的模型
local userModel = model:new('sys_user')
local _M = {}
--判断用户是否存在
local function isExistUser(id)
--根据用户id进行验证用户是否存在
local code, res = userModel:find(id)
if code ~= 0 then
return false
end
local num = 0
if res ~= nil then
num = table.getn(res)
end
--用户不存在返回错误
if num <= 0 then
return false
end
return true
end
-- 查询数据表中的所有用户信息
function _M.getSystemUsers(pageNum, pageSize)
return userModel:paginate(pageNum, pageSize)
end
--根据用户id获取用户信息
function _M.getSystemUser(id)
return userModel:find(id)
end
--增加用户信息到数据表
function _M.addSystemUser(jsonData)
if jsonData == nil or jsonData == "" then
return 0x000001, nil
end
--解析json中的键和数据值
local userName = jsonData['username']
local phone = jsonData['phone']
local email = jsonData['email']
--根据用户、手机号、邮箱进行验证用户是否存在
local code, res = userModel:where("username", "=", userName):orwhere("phone", "=", phone):orwhere("email", "=", email):get()
if code ~= 0 then
return 0x000001,res
end
local num = 0
if res ~= nil then
num = table.getn(res)
end
--用户存在时返回用户已经存在
if num > 0 then
return 0x01000C,nil
end
--键值为id产生uuid数据值增加到json中
jsonData.id = helpers.getUuid()
-- 创建一个用户
return userModel:create(jsonData)
end
--删除用户信息到数据表
function _M:deleteSystemUser(id)
--根据用户id进行验证用户是否存在
local ok = isExistUser(id)
--用户不存在则返回
if ok == false then
return 0x000001,nil
end
return userModel:delete(id)
end
--更新用户信息到数据表
function _M:updateSystemUser(id, jsonData)
--根据用户id进行验证用户是否存在
local ok = isExistUser(id)
--用户不存在则返回
if ok == false then
return 0x000001,nil
end
--对数据内容进行更新
return userModel:where('id', '=', id):update(jsonData)
end
return _M