增加helpers文件用于提取公共的函数,对使用的公共函数进行封装处理,使用统一调用
This commit is contained in:
parent
8b69d78c3b
commit
5f8257e91d
|
|
@ -3,12 +3,11 @@
|
|||
--- Created by .
|
||||
--- DateTime: 2025/9/25 08:19
|
||||
--- 业务逻辑 对用户数据表进行数据表业务处理
|
||||
local cjson = require('cjson')
|
||||
local pgmoon = require('pgmoon')
|
||||
local dbconf = require("config.database")
|
||||
local status = require("config.status")
|
||||
local snowflake = require("util.snowflake")
|
||||
local validator = require("util.validator")
|
||||
local helpers = require("util.helpers")
|
||||
|
||||
local _M = {}
|
||||
|
||||
|
|
@ -27,15 +26,6 @@ local function get_con(cfg)
|
|||
return code,conn
|
||||
end
|
||||
|
||||
local function getUuid()
|
||||
local workerId = 0 -- 假设当前机器的ID是1,范围在[0, 31]之间
|
||||
local datacenterId = 0 -- 数据中心ID,范围在[0, 31]之间
|
||||
local snow = snowflake.new(workerId, datacenterId)
|
||||
local id = snow:generateUniqueId()-- 生成ID
|
||||
--print("Generated ID:", snow.int64_to_string(id))
|
||||
return snow.int64_to_string(id)
|
||||
end
|
||||
|
||||
--数据库执行sql语句
|
||||
local function execSQL(sql)
|
||||
local res = nil
|
||||
|
|
@ -117,7 +107,7 @@ function _M.addAccount(jsonData)
|
|||
end
|
||||
--自己增加对应的uuid数据值
|
||||
local newKeys = keys.."id"
|
||||
local newValues = values.."'"..getUuid().."'"
|
||||
local newValues = values.."'"..helpers.getUuid().."'"
|
||||
--组装sql语句
|
||||
local sql = string.format("insert into \"tbl_account\"(%s)values(%s)", newKeys, newValues)
|
||||
return execSQL(sql)
|
||||
|
|
@ -149,7 +139,7 @@ function _M.updateAccount(id, jsonData)
|
|||
return 0x01000C,nil
|
||||
end
|
||||
--验证数据的正确性,错误时返回
|
||||
local success, result = checkJson(jsonData)
|
||||
local success, result = validator.checkJson(jsonData)
|
||||
if success == false then
|
||||
return 0x000001,result
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@
|
|||
--- Created by .
|
||||
--- DateTime: 2025/9/25 08:19
|
||||
--- 业务逻辑 对用户数据表进行数据表业务处理
|
||||
local cjson = require('cjson')
|
||||
local pgmoon = require('pgmoon')
|
||||
local dbconf = require("config.database")
|
||||
local status = require("config.status")
|
||||
local snowflake = require("util.snowflake")
|
||||
local validator = require("util.validator")
|
||||
local helpers = require("util.helpers")
|
||||
|
||||
local _M = {}
|
||||
|
||||
|
|
@ -26,15 +26,6 @@ local function get_con(cfg)
|
|||
return code,conn
|
||||
end
|
||||
|
||||
local function getUuid()
|
||||
local workerId = 0 -- 假设当前机器的ID是1,范围在[0, 31]之间
|
||||
local datacenterId = 0 -- 数据中心ID,范围在[0, 31]之间
|
||||
local snow = snowflake.new(workerId, datacenterId)
|
||||
local id = snow:generateUniqueId()-- 生成ID
|
||||
--print("Generated ID:", snow.int64_to_string(id))
|
||||
return snow.int64_to_string(id)
|
||||
end
|
||||
|
||||
--数据库执行sql语句
|
||||
local function execSQL(sql)
|
||||
local res = nil
|
||||
|
|
@ -57,18 +48,6 @@ local function execSQL(sql)
|
|||
return code,res
|
||||
end
|
||||
|
||||
--校验json数据的正确性,并返回json解析后的数据
|
||||
local function checkJson(jsonData)
|
||||
local success, result = pcall(function()
|
||||
return cjson.decode(jsonData)
|
||||
end)
|
||||
if success == true then
|
||||
return true, result
|
||||
end
|
||||
local res = nil
|
||||
return false,res
|
||||
end
|
||||
|
||||
--通过查询条件判断数据库中的数据记录
|
||||
local function checkUserExist(where)
|
||||
--组装sql语句
|
||||
|
|
@ -128,7 +107,7 @@ function _M.addUser(jsonData)
|
|||
end
|
||||
--自己增加对应的uuid数据值
|
||||
local newKeys = keys.."id"
|
||||
local newValues = values.."'"..getUuid().."'"
|
||||
local newValues = values.."'"..helpers.getUuid().."'"
|
||||
--组装sql语句
|
||||
local sql = string.format("insert into \"tbl_user\"(%s)values(%s)", newKeys, newValues)
|
||||
return execSQL(sql)
|
||||
|
|
@ -160,7 +139,7 @@ function _M.updateUser(id, jsonData)
|
|||
return 0x01000C,nil
|
||||
end
|
||||
--验证数据的正确性,错误时返回
|
||||
local success, result = checkJson(jsonData)
|
||||
local success, result = validator.checkJson(jsonData)
|
||||
if success == false then
|
||||
return 0x000001,result
|
||||
end
|
||||
|
|
|
|||
19
src/util/helpers.lua
Normal file
19
src/util/helpers.lua
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
---
|
||||
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
||||
--- Created by admin.
|
||||
--- DateTime: 2025/10/24 11:36
|
||||
---
|
||||
local snowflake = require("util.snowflake")
|
||||
|
||||
local _M = {}
|
||||
|
||||
function _M:getUuid()
|
||||
local workerId = 0 -- 假设当前机器的ID是1,范围在[0, 31]之间
|
||||
local datacenterId = 0 -- 数据中心ID,范围在[0, 31]之间
|
||||
local snow = snowflake.new(workerId, datacenterId)
|
||||
local id = snow:generateUniqueId()-- 生成ID
|
||||
--print("Generated ID:", snow.int64_to_string(id))
|
||||
return snow.int64_to_string(id)
|
||||
end
|
||||
|
||||
return _M
|
||||
Loading…
Reference in New Issue
Block a user