2025-10-23 11:14:19 +08:00
|
|
|
|
---
|
|
|
|
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
|
|
|
|
--- Created by .
|
2025-10-28 10:33:35 +08:00
|
|
|
|
--- DateTime: 2025/9/25 08:25
|
|
|
|
|
|
--- 业务逻辑 对账户数据表进行数据表业务处理
|
2025-10-24 11:11:57 +08:00
|
|
|
|
local validator = require("util.validator")
|
2025-10-29 17:29:17 +08:00
|
|
|
|
local helpers = require("share.helpers")
|
2025-10-25 17:33:31 +08:00
|
|
|
|
local account = require("model.account")
|
2025-10-23 11:14:19 +08:00
|
|
|
|
|
|
|
|
|
|
local _M = {}
|
|
|
|
|
|
|
2025-10-29 17:29:17 +08:00
|
|
|
|
local dao = require("service.system.account")
|
|
|
|
|
|
local resp = require("util.response")
|
|
|
|
|
|
|
|
|
|
|
|
--获取所有账户信息
|
|
|
|
|
|
function _M.get_allaccount()
|
|
|
|
|
|
local code,ret = dao.getAllAccount()
|
|
|
|
|
|
local result = resp:json(code, ret)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据账户id获取账户信息
|
|
|
|
|
|
function _M.get_account(m)
|
|
|
|
|
|
local id = m.id
|
|
|
|
|
|
local code,ret = dao.getAccount(id)
|
|
|
|
|
|
local result = resp:json(code, ret)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据账户id获取账户信息
|
|
|
|
|
|
function _M.add_account()
|
|
|
|
|
|
--获取请求头并进行校验
|
|
|
|
|
|
if validator.checkReqHeader() == false then
|
|
|
|
|
|
local result = resp:json(0x000001)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
|
|
|
|
|
--读取请求体的数据
|
|
|
|
|
|
ngx.req.read_body()
|
|
|
|
|
|
--获取请求数据
|
|
|
|
|
|
local body_data = ngx.req.get_body_data()
|
|
|
|
|
|
--判断请求体数据是否为空
|
|
|
|
|
|
if body_data == nil then
|
|
|
|
|
|
local result = resp:json(0x000001)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
|
|
|
|
|
--ngx.say(body_data)
|
|
|
|
|
|
local code, ret = dao.addAccount(body_data)
|
|
|
|
|
|
local result = resp:json(code, ret)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据账户id删除账户信息
|
|
|
|
|
|
function _M.delete_account(m)
|
|
|
|
|
|
local id = m.id
|
|
|
|
|
|
local code, ret = dao.deleteAccount(id)
|
|
|
|
|
|
local result = resp:json(code, ret)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--根据账户id删除账户信息
|
|
|
|
|
|
function _M.update_account(m)
|
|
|
|
|
|
local id = m.id
|
|
|
|
|
|
--读取请求体的数据
|
|
|
|
|
|
ngx.req.read_body()
|
|
|
|
|
|
--获取请求数据
|
|
|
|
|
|
local body_data = ngx.req.get_body_data()
|
|
|
|
|
|
--判断请求体数据是否为空
|
|
|
|
|
|
if body_data == nil then
|
|
|
|
|
|
local result = resp:json(0x000001)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
return
|
|
|
|
|
|
end
|
|
|
|
|
|
local code, ret = dao.updateAccount(id, body_data)
|
|
|
|
|
|
local result = resp:json(code, ret)
|
|
|
|
|
|
resp:send(result)
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-28 10:33:35 +08:00
|
|
|
|
-- 查询数据表中的所有账户信息
|
2025-10-24 10:44:12 +08:00
|
|
|
|
function _M.getAllAccount()
|
2025-10-25 17:33:31 +08:00
|
|
|
|
return account:all()
|
2025-10-23 11:14:19 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-28 10:33:35 +08:00
|
|
|
|
--根据账户id获取账户信息
|
2025-10-24 10:44:12 +08:00
|
|
|
|
function _M.getAccount(id)
|
2025-10-25 17:33:31 +08:00
|
|
|
|
return account.find(id)
|
2025-10-23 11:14:19 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-28 10:33:35 +08:00
|
|
|
|
--增加账户信息到数据表
|
2025-10-23 11:14:19 +08:00
|
|
|
|
function _M.addAccount(jsonData)
|
|
|
|
|
|
--验证数据的正确性,错误时返回
|
2025-10-24 11:11:57 +08:00
|
|
|
|
local success, result = validator.checkJson(jsonData)
|
2025-10-23 11:14:19 +08:00
|
|
|
|
if success == false then
|
2025-10-25 17:33:31 +08:00
|
|
|
|
return 0x000001, result
|
2025-10-23 11:14:19 +08:00
|
|
|
|
end
|
|
|
|
|
|
--解析json中的键和数据值
|
2025-10-24 14:58:03 +08:00
|
|
|
|
local name = ""
|
2025-10-23 11:14:19 +08:00
|
|
|
|
for key, value in pairs(result) do
|
2025-10-24 14:58:03 +08:00
|
|
|
|
if key == "name" then name = value end
|
2025-10-23 11:14:19 +08:00
|
|
|
|
end
|
2025-10-28 10:33:35 +08:00
|
|
|
|
--根据账户进行验证是否存在
|
2025-10-25 17:33:31 +08:00
|
|
|
|
local code, res = account:where("name", "=", name):get()
|
|
|
|
|
|
if code ~= 0 then
|
|
|
|
|
|
return 0x000001, res
|
2025-10-23 11:14:19 +08:00
|
|
|
|
end
|
|
|
|
|
|
local num = 0
|
|
|
|
|
|
for _, row in ipairs(res) do
|
|
|
|
|
|
for key, value in pairs(row) do
|
2025-10-25 17:33:31 +08:00
|
|
|
|
num = num + 1
|
2025-10-23 11:14:19 +08:00
|
|
|
|
end
|
|
|
|
|
|
end
|
2025-10-28 10:33:35 +08:00
|
|
|
|
--账户存在时返回账户已经存在
|
2025-10-28 16:57:06 +08:00
|
|
|
|
if num > 0 then
|
2025-10-25 17:33:31 +08:00
|
|
|
|
return 0x01000C, nil
|
2025-10-23 11:14:19 +08:00
|
|
|
|
end
|
2025-10-27 17:17:58 +08:00
|
|
|
|
--键值为id产生uuid数据值,增加到json中
|
|
|
|
|
|
result.id = helpers.getUuid()
|
|
|
|
|
|
local ret = helpers.convert_json(result)
|
2025-10-28 10:33:35 +08:00
|
|
|
|
-- 创建一个账户
|
2025-10-27 17:17:58 +08:00
|
|
|
|
return account:create('{'..ret..'}')
|
2025-10-23 11:14:19 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-28 10:33:35 +08:00
|
|
|
|
--删除账户信息到数据表
|
2025-10-24 10:44:12 +08:00
|
|
|
|
function _M.deleteAccount(id)
|
2025-10-25 17:33:31 +08:00
|
|
|
|
return account:delete(id)
|
2025-10-24 10:44:12 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2025-10-28 10:33:35 +08:00
|
|
|
|
--更新账户信息到数据表
|
2025-10-24 10:44:12 +08:00
|
|
|
|
function _M.updateAccount(id, jsonData)
|
2025-10-28 10:33:35 +08:00
|
|
|
|
--根据账户id进行验证账户是否存在
|
2025-10-25 17:33:31 +08:00
|
|
|
|
local code, res = account:find(id)
|
|
|
|
|
|
if code ~= 0 then
|
|
|
|
|
|
return 0x000001, res
|
2025-10-24 10:44:12 +08:00
|
|
|
|
end
|
|
|
|
|
|
local num = 0
|
|
|
|
|
|
for _, row in ipairs(res) do
|
|
|
|
|
|
for key, value in pairs(row) do
|
2025-10-25 17:33:31 +08:00
|
|
|
|
num = num + 1
|
2025-10-24 10:44:12 +08:00
|
|
|
|
end
|
|
|
|
|
|
end
|
2025-10-28 10:33:35 +08:00
|
|
|
|
--账户不存在返回错误
|
2025-10-24 10:44:12 +08:00
|
|
|
|
if num <= 0 then
|
2025-10-25 17:33:31 +08:00
|
|
|
|
return 0x01000C, nil
|
2025-10-24 10:44:12 +08:00
|
|
|
|
end
|
|
|
|
|
|
--验证数据的正确性,错误时返回
|
2025-10-24 11:43:02 +08:00
|
|
|
|
local success, result = validator.checkJson(jsonData)
|
2025-10-24 10:44:12 +08:00
|
|
|
|
if success == false then
|
2025-10-25 17:33:31 +08:00
|
|
|
|
return 0x000001, result
|
2025-10-24 10:44:12 +08:00
|
|
|
|
end
|
2025-10-25 17:33:31 +08:00
|
|
|
|
--对数据内容进行更新
|
|
|
|
|
|
return account:where('id', '=', id):update(jsonData)
|
2025-10-23 11:14:19 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return _M
|