yjp 1 سال پیش
والد
کامیت
65f0db35aa

+ 2 - 25
convenient/domain/auth/permission/api.go

@@ -3,10 +3,6 @@ package permission
 import (
 	"git.sxidc.com/go-framework/baize/convenient/entity_crud"
 	"git.sxidc.com/go-framework/baize/framework/binding"
-	"git.sxidc.com/go-framework/baize/framework/core/api"
-	"git.sxidc.com/go-framework/baize/framework/core/domain"
-	"git.sxidc.com/go-framework/baize/framework/core/domain/entity"
-	"git.sxidc.com/go-framework/baize/framework/core/infrastructure"
 )
 
 // Simple Bind参数
@@ -28,25 +24,6 @@ func (simple *Simple) Bind(binder *binding.Binder) {
 		QueryQueryParams:   &GetPermissionsQueryParams{},
 		GetByIDQueryParams: &GetPermissionQueryParams{},
 	}, entity_crud.WithGlobalMiddlewares(simple.AuthMiddleware),
-		entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
-			Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
-				userInfo := c.GetUserInfo()
-				err := domain.SetField(e, entity.FieldCreateUserID, userInfo.GetID())
-				if err != nil {
-					return nil, err
-				}
-
-				return nil, err
-			},
-		}), entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
-			Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
-				userInfo := c.GetUserInfo()
-				err := domain.SetField(e, entity.FieldLastUpdateUserID, userInfo.GetID())
-				if err != nil {
-					return nil, err
-				}
-
-				return nil, err
-			},
-		}))
+		entity_crud.WithCreateUserID(),
+		entity_crud.WithLastUpdateUserID())
 }

+ 2 - 25
convenient/domain/auth/permission_group/api.go

@@ -3,10 +3,6 @@ package permission_group
 import (
 	"git.sxidc.com/go-framework/baize/convenient/entity_crud"
 	"git.sxidc.com/go-framework/baize/framework/binding"
-	"git.sxidc.com/go-framework/baize/framework/core/api"
-	"git.sxidc.com/go-framework/baize/framework/core/domain"
-	"git.sxidc.com/go-framework/baize/framework/core/domain/entity"
-	"git.sxidc.com/go-framework/baize/framework/core/infrastructure"
 )
 
 // Simple Bind参数
@@ -28,25 +24,6 @@ func (simple *Simple) Bind(binder *binding.Binder) {
 		QueryQueryParams:   &GetPermissionGroupsQueryParams{},
 		GetByIDQueryParams: &GetPermissionGroupQueryParams{},
 	}, entity_crud.WithGlobalMiddlewares(simple.AuthMiddleware),
-		entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
-			Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
-				userInfo := c.GetUserInfo()
-				err := domain.SetField(e, entity.FieldCreateUserID, userInfo.GetID())
-				if err != nil {
-					return nil, err
-				}
-
-				return nil, err
-			},
-		}), entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
-			Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
-				userInfo := c.GetUserInfo()
-				err := domain.SetField(e, entity.FieldLastUpdateUserID, userInfo.GetID())
-				if err != nil {
-					return nil, err
-				}
-
-				return nil, err
-			},
-		}))
+		entity_crud.WithCreateUserID(),
+		entity_crud.WithLastUpdateUserID())
 }

+ 2 - 25
convenient/domain/auth/role/api.go

@@ -3,10 +3,6 @@ package role
 import (
 	"git.sxidc.com/go-framework/baize/convenient/entity_crud"
 	"git.sxidc.com/go-framework/baize/framework/binding"
-	"git.sxidc.com/go-framework/baize/framework/core/api"
-	"git.sxidc.com/go-framework/baize/framework/core/domain"
-	"git.sxidc.com/go-framework/baize/framework/core/domain/entity"
-	"git.sxidc.com/go-framework/baize/framework/core/infrastructure"
 )
 
 // Simple Bind参数
@@ -28,25 +24,6 @@ func (simple *Simple) Bind(binder *binding.Binder) {
 		QueryQueryParams:   &GetRolesQueryParams{},
 		GetByIDQueryParams: &GetRoleQueryParams{},
 	}, entity_crud.WithGlobalMiddlewares(simple.AuthMiddleware),
-		entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
-			Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
-				userInfo := c.GetUserInfo()
-				err := domain.SetField(e, entity.FieldCreateUserID, userInfo.GetID())
-				if err != nil {
-					return nil, err
-				}
-
-				return nil, err
-			},
-		}), entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
-			Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
-				userInfo := c.GetUserInfo()
-				err := domain.SetField(e, entity.FieldLastUpdateUserID, userInfo.GetID())
-				if err != nil {
-					return nil, err
-				}
-
-				return nil, err
-			},
-		}))
+		entity_crud.WithCreateUserID(),
+		entity_crud.WithLastUpdateUserID())
 }

+ 36 - 4
convenient/entity_crud/service.go

@@ -16,7 +16,7 @@ import (
 	"reflect"
 )
 
-func Create(tableName string, callbacks *CreateCallbacks) binding.ServiceFunc[string] {
+func Create(tableName string, needCreateUserID bool, callbacks *CreateCallbacks) binding.ServiceFunc[string] {
 	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
 		object := objects[0]
 		if object == nil {
@@ -35,6 +35,14 @@ func Create(tableName string, callbacks *CreateCallbacks) binding.ServiceFunc[st
 			return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
 
+		if needCreateUserID {
+			userInfo := c.GetUserInfo()
+			err := domain.SetField(e, entity.FieldCreateUserID, userInfo.GetID())
+			if err != nil {
+				return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
+			}
+		}
+
 		prepared, err := callbackPrepareCreate(callbacks, c, e, i)
 		if err != nil {
 			return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
@@ -111,7 +119,7 @@ func Delete(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[an
 	}
 }
 
-func Update(tableName string, callbacks *UpdateCallbacks) binding.ServiceFunc[any] {
+func Update(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallbacks) binding.ServiceFunc[any] {
 	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
 		object := objects[0]
 		if object == nil {
@@ -138,6 +146,14 @@ func Update(tableName string, callbacks *UpdateCallbacks) binding.ServiceFunc[an
 			return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
 
+		if needLastUpdateUserID {
+			userInfo := c.GetUserInfo()
+			err := domain.SetField(e, entity.FieldLastUpdateUserID, userInfo.GetID())
+			if err != nil {
+				return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
+			}
+		}
+
 		prepared, err := callbackPrepareUpdate(callbacks, c, e, i)
 		if err != nil {
 			return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
@@ -309,7 +325,7 @@ func GetByID[O any](tableName string, callbacks *GetByIDCallbacks[O]) binding.Se
 	}
 }
 
-func CreateTx(tableName string, callbacks *CreateCallbacks) binding.ServiceFunc[string] {
+func CreateTx(tableName string, needCreateUserID bool, callbacks *CreateCallbacks) binding.ServiceFunc[string] {
 	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
 		object := objects[0]
 		if object == nil {
@@ -328,6 +344,14 @@ func CreateTx(tableName string, callbacks *CreateCallbacks) binding.ServiceFunc[
 			return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
 
+		if needCreateUserID {
+			userInfo := c.GetUserInfo()
+			err := domain.SetField(e, entity.FieldCreateUserID, userInfo.GetID())
+			if err != nil {
+				return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
+			}
+		}
+
 		prepared, err := callbackPrepareCreate(callbacks, c, e, i)
 		if err != nil {
 			return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
@@ -418,7 +442,7 @@ func DeleteTx(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[
 	}
 }
 
-func UpdateTx(tableName string, callbacks *UpdateCallbacks) binding.ServiceFunc[any] {
+func UpdateTx(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallbacks) binding.ServiceFunc[any] {
 	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
 		object := objects[0]
 		if object == nil {
@@ -444,6 +468,14 @@ func UpdateTx(tableName string, callbacks *UpdateCallbacks) binding.ServiceFunc[
 			return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), errors.New(e.DomainCNName()+"不存在"), i)
 		}
 
+		if needLastUpdateUserID {
+			userInfo := c.GetUserInfo()
+			err := domain.SetField(e, entity.FieldLastUpdateUserID, userInfo.GetID())
+			if err != nil {
+				return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
+			}
+		}
+
 		prepared, err := callbackPrepareUpdate(callbacks, c, e, i)
 		if err != nil {
 			return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), err, i)

+ 22 - 4
convenient/entity_crud/simple.go

@@ -62,7 +62,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder) {
 				SendResponseFunc: response.SendIDResponse,
 				RequestParams:    simple.CreateJsonBody,
 				Objects:          []domain.Object{simple.Entity},
-				ServiceFunc:      Create(tableName, createOptions.callbacks),
+				ServiceFunc:      Create(tableName, createOptions.needCreateUserID, createOptions.callbacks),
 			}, createMiddlewares...)
 		} else {
 			binding.PostBind(binder, &binding.SimpleBindItem[string]{
@@ -70,7 +70,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder) {
 				SendResponseFunc: response.SendIDResponse,
 				RequestParams:    simple.CreateJsonBody,
 				Objects:          []domain.Object{simple.Entity},
-				ServiceFunc:      CreateTx(tableName, createOptions.callbacks),
+				ServiceFunc:      CreateTx(tableName, createOptions.needCreateUserID, createOptions.callbacks),
 			}, createMiddlewares...)
 		}
 	}
@@ -108,7 +108,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder) {
 				SendResponseFunc: response.SendMsgResponse,
 				RequestParams:    simple.UpdateJsonBody,
 				Objects:          []domain.Object{simple.Entity},
-				ServiceFunc:      Update(tableName, updateOptions.callbacks),
+				ServiceFunc:      Update(tableName, updateOptions.needLastUpdateUserID, updateOptions.callbacks),
 			}, updateMiddlewares...)
 		} else {
 			binding.PutBind(binder, &binding.SimpleBindItem[any]{
@@ -116,7 +116,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder) {
 				SendResponseFunc: response.SendMsgResponse,
 				RequestParams:    simple.UpdateJsonBody,
 				Objects:          []domain.Object{simple.Entity},
-				ServiceFunc:      UpdateTx(tableName, updateOptions.callbacks),
+				ServiceFunc:      UpdateTx(tableName, updateOptions.needLastUpdateUserID, updateOptions.callbacks),
 			}, updateMiddlewares...)
 		}
 	}
@@ -203,6 +203,9 @@ type CreateOptions struct {
 	// 创建是否使用事务
 	needTx bool
 
+	// 创建需要创建用户ID
+	needCreateUserID bool
+
 	// 创建回调
 	callbacks *CreateCallbacks
 
@@ -231,6 +234,9 @@ type UpdateOptions struct {
 	// 更新是否使用事务
 	needTx bool
 
+	// 更新需要最新更新用户ID
+	needLastUpdateUserID bool
+
 	// 更新回调
 	callbacks *UpdateCallbacks
 
@@ -281,6 +287,12 @@ func WithCreateTx() CreateOption {
 	}
 }
 
+func WithCreateUserID() CreateOption {
+	return func(options *CreateOptions) {
+		options.needCreateUserID = true
+	}
+}
+
 func WithCreateCallbacks(callbacks *CreateCallbacks) CreateOption {
 	return func(options *CreateOptions) {
 		options.callbacks = callbacks
@@ -329,6 +341,12 @@ func WithUpdateTx() UpdateOption {
 	}
 }
 
+func WithLastUpdateUserID() UpdateOption {
+	return func(options *UpdateOptions) {
+		options.needLastUpdateUserID = true
+	}
+}
+
 func WithUpdateCallbacks(callbacks *UpdateCallbacks) UpdateOption {
 	return func(options *UpdateOptions) {
 		options.callbacks = callbacks