This commit is contained in:
oneao committed 2026-04-17 17:25:15 +08:00
1 parent e893ef2e48
commit b6415c0d65
25 files changed
+1495 -112

No files matched your search

@@ -6,6 +6,11 @@ import (
"strings"
)
var auditExcludeTables = map[string]bool{
"user": true,
"user_oauth": true,
}
func validateData(table string, keyField string, data map[string]any) (any, error) {
if table == "" {
return nil, fmt.Errorf("表名不能为空")
+21
View File
@@ -1,10 +1,12 @@
package db
import (
"allapp-go/internal/middleware"
"context"
"fmt"
"sort"
"strings"
"time"
)
// 构建 INSERT SQL(单条)
@@ -43,6 +45,25 @@ func (c *Client) Insert(
return err
}
if !auditExcludeTables[table] {
now := time.Now()
delete(data, "create_time")
delete(data, "update_time")
delete(data, "create_by")
delete(data, "update_by")
data["create_time"] = now
data["update_time"] = now
userID, flag := middleware.GetUserID(ctx)
if flag {
data["create_by"] = userID
data["update_by"] = userID
}
}
sql, args := buildInsertSQL(table, data)
_, err := c.exec().Exec(ctx, sql, args...)
+27 -21
View File
@@ -1,9 +1,12 @@
package db
import (
"allapp-go/pkg/logger"
"context"
"fmt"
"strings"
"go.uber.org/zap"
)
func (c *Client) LoadData(
@@ -15,33 +18,32 @@ func (c *Client) LoadData(
args []any,
) ([]map[string]any, error) {
var where string
// WHERE 构造
if searchCondition != "" && len(searchColumns) > 0 {
var conditions []string
for _, col := range searchColumns {
conditions = append(conditions, fmt.Sprintf("%s LIKE ?", col))
}
where = " WHERE (" + strings.Join(conditions, " OR ") + ")"
// 自动加 %
for i := range args {
if s, ok := args[i].(string); ok {
args[i] = "%" + s + "%"
}
}
selectCols := "*"
if len(searchColumns) > 0 {
selectCols = strings.Join(searchColumns, ", ")
}
// ORDER BY
var order string
where := ""
if searchCondition != "" {
where = " WHERE " + searchCondition
}
order := ""
if orderBy != "" {
order = " ORDER BY " + orderBy
}
sql := fmt.Sprintf("SELECT * FROM %s%s%s", viewName, where, order)
sql := fmt.Sprintf("SELECT %s FROM %s%s%s",
selectCols,
viewName,
where,
order,
)
logger.FromCtx(ctx).Info("LoadData",
zap.String("sql", sql),
zap.Any("args", args),
)
return c.query(ctx, sql, args...)
}
@@ -51,6 +53,10 @@ func (c *Client) LoadDataBySQL(
sql string,
args []any,
) ([]map[string]any, error) {
logger.FromCtx(ctx).Info("LoadDataBySQL",
zap.String("sql", sql),
zap.Any("args", args),
)
return c.query(ctx, sql, args...)
}
+17
View File
@@ -1,10 +1,12 @@
package db
import (
"allapp-go/internal/middleware"
"context"
"fmt"
"sort"
"strings"
"time"
)
func (c *Client) Update(
@@ -18,6 +20,21 @@ func (c *Client) Update(
return err
}
if !auditExcludeTables[table] {
now := time.Now()
delete(data, "update_time")
delete(data, "update_by")
data["update_time"] = now
userID, flag := middleware.GetUserID(ctx)
if flag {
data["update_by"] = userID
}
}
var (
setClauses []string
args []any