feat: 实现单卡资产分配与回收功能
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m45s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 4m45s
- 新增单卡分配/回收 API(支持 ICCID 列表、号段范围、筛选条件三种选卡方式) - 新增资产分配记录查询 API(支持多条件筛选和分页) - 新增 AssetAllocationRecord 模型、Store、Service、Handler 完整实现 - 扩展 IotCardStore 新增批量更新、号段查询、筛选查询等方法 - 修复 GORM Callback 处理 slice 类型(BatchCreate)的问题 - 新增完整的单元测试和集成测试 - 同步 OpenSpec 规范并归档 change
This commit is contained in:
@@ -2,6 +2,7 @@ package gorm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/logger"
|
||||
@@ -236,13 +237,47 @@ func RegisterDataPermissionCallback(db *gorm.DB, shopStore ShopStoreInterface) e
|
||||
func RegisterSetCreatorUpdaterCallback(db *gorm.DB) error {
|
||||
err := db.Callback().Create().Before("gorm:create").Register("set_creator_updater", func(tx *gorm.DB) {
|
||||
ctx := tx.Statement.Context
|
||||
if userID, ok := tx.Statement.Context.Value(constants.ContextKeyUserID).(uint); ok {
|
||||
if f := tx.Statement.Schema; f != nil {
|
||||
if c, ok := f.FieldsByName["Creator"]; ok {
|
||||
_ = c.Set(ctx, tx.Statement.ReflectValue, userID)
|
||||
userID, ok := tx.Statement.Context.Value(constants.ContextKeyUserID).(uint)
|
||||
if !ok || tx.Statement.Schema == nil {
|
||||
return
|
||||
}
|
||||
|
||||
creatorField, hasCreator := tx.Statement.Schema.FieldsByName["Creator"]
|
||||
updaterField, hasUpdater := tx.Statement.Schema.FieldsByName["Updater"]
|
||||
if !hasCreator && !hasUpdater {
|
||||
return
|
||||
}
|
||||
|
||||
rv := tx.Statement.ReflectValue
|
||||
switch rv.Kind() {
|
||||
case reflect.Slice, reflect.Array:
|
||||
for i := 0; i < rv.Len(); i++ {
|
||||
elem := rv.Index(i)
|
||||
if elem.Kind() == reflect.Ptr {
|
||||
elem = elem.Elem()
|
||||
}
|
||||
if u, ok := f.FieldsByName["Updater"]; ok {
|
||||
_ = u.Set(ctx, tx.Statement.ReflectValue, userID)
|
||||
if hasCreator {
|
||||
_ = creatorField.Set(ctx, elem, userID)
|
||||
}
|
||||
if hasUpdater {
|
||||
_ = updaterField.Set(ctx, elem, userID)
|
||||
}
|
||||
}
|
||||
case reflect.Struct:
|
||||
if hasCreator {
|
||||
_ = creatorField.Set(ctx, rv, userID)
|
||||
}
|
||||
if hasUpdater {
|
||||
_ = updaterField.Set(ctx, rv, userID)
|
||||
}
|
||||
case reflect.Ptr:
|
||||
elem := rv.Elem()
|
||||
if elem.Kind() == reflect.Struct {
|
||||
if hasCreator {
|
||||
_ = creatorField.Set(ctx, elem, userID)
|
||||
}
|
||||
if hasUpdater {
|
||||
_ = updaterField.Set(ctx, elem, userID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user