92 lines
2.5 KiB
Lua
92 lines
2.5 KiB
Lua
---
|
||
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
||
--- Created by .
|
||
--- DateTime: 2025/9/25 08:19
|
||
--- 业务逻辑 对用户数据表进行数据表业务处理
|
||
local validator = require("util.validator")
|
||
local helpers = require("util.helpers")
|
||
local user = require("model.user")
|
||
|
||
local _M = {}
|
||
|
||
-- 查询数据表中的所有用户信息
|
||
function _M.getAllUser()
|
||
return user:all()
|
||
end
|
||
|
||
--根据用户id获取用户信息
|
||
function _M.getUser(id)
|
||
return user:find(id)
|
||
end
|
||
|
||
--增加用户信息到数据表
|
||
function _M.addUser(jsonData)
|
||
--验证数据的正确性,错误时返回
|
||
local success, result = validator.checkJson(jsonData)
|
||
if success == false then
|
||
return 0x000001,result
|
||
end
|
||
--解析json中的键和数据值
|
||
local name, phone, email
|
||
for key, value in pairs(result) do
|
||
if key == "username" then name = value end
|
||
if key == "phone" then phone = value end
|
||
if key == "email" then email = value end
|
||
end
|
||
|
||
--根据用户、手机号、邮箱进行验证用户是否存在
|
||
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 = num + 1
|
||
end
|
||
end
|
||
--用户存在时返回用户已经存在
|
||
if num <= 0 then
|
||
return 0x01000C,nil
|
||
end
|
||
|
||
--键值为id产生uuid数据值,增加到json中
|
||
result.id = helpers.getUuid()
|
||
local ret = helpers.convert_json(result)
|
||
-- 创建一个用户
|
||
return user:create('{'..ret..'}')
|
||
end
|
||
|
||
--删除用户信息到数据表
|
||
function _M.deleteUser(id)
|
||
return user:delete(id)
|
||
end
|
||
|
||
--更新用户信息到数据表
|
||
function _M.updateUser(id, jsonData)
|
||
--根据用户id进行验证用户是否存在
|
||
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 = num + 1
|
||
end
|
||
end
|
||
--用户不存在返回错误
|
||
if num <= 0 then
|
||
return 0x01000C,nil
|
||
end
|
||
--验证数据的正确性,错误时返回
|
||
local success, result = validator.checkJson(jsonData)
|
||
if success == false then
|
||
return 0x000001,result
|
||
end
|
||
--对数据内容进行更新
|
||
return user:where('id', '=', id):update(jsonData)
|
||
end
|
||
|
||
return _M
|