yjp 3 mesi fa
parent
commit
f13dacf1d6

+ 117 - 235
convenient/entity_crud/service.go

@@ -16,7 +16,7 @@ import (
 	"reflect"
 )
 
-func Create(tableName string, needCreateUserID bool, callbacks *CreateCallbacks) binding.ServiceFunc[string] {
+func Create(tableName string, needCreateUserID bool, callbacks *CreateCallbacks, needTx bool) 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 {
@@ -65,30 +65,63 @@ func Create(tableName string, needCreateUserID bool, callbacks *CreateCallbacks)
 			}
 		}
 
-		err = callbackBeforeCreate(callbacks, c, params, e, prepared, i, nil)
+		err = domain.CheckFieldsForCreate(e, e.GetFieldMap())
 		if err != nil {
-			return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i)
+			return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
 		}
 
-		err = database.InsertEntity(dbExecutor, tableName, e)
-		if err != nil {
-			if database.IsErrorDBRecordHasExist(err) {
-				err = errors.New(e.DomainCNName() + "已存在")
+		if needTx {
+			err = database.Transaction(dbExecutor, func(tx database.Executor) error {
+				err = callbackBeforeCreate(callbacks, c, params, e, prepared, i, tx)
+				if err != nil {
+					return err
+				}
+
+				err = database.InsertEntity(tx, tableName, e)
+				if err != nil {
+					if database.IsErrorDBRecordHasExist(err) {
+						err = errors.New(e.DomainCNName() + "已存在")
+					}
+
+					return err
+				}
+
+				err = callbackAfterCreate(callbacks, c, params, e, prepared, i, tx)
+				if err != nil {
+					return err
+				}
+
+				return nil
+			})
+			if err != nil {
+				return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i)
+			}
+		} else {
+			err = callbackBeforeCreate(callbacks, c, params, e, prepared, i, nil)
+			if err != nil {
+				return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i)
 			}
 
-			return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i)
-		}
+			err = database.InsertEntity(dbExecutor, tableName, e)
+			if err != nil {
+				if database.IsErrorDBRecordHasExist(err) {
+					err = errors.New(e.DomainCNName() + "已存在")
+				}
 
-		err = callbackAfterCreate(callbacks, c, params, e, prepared, i, nil)
-		if err != nil {
-			return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i)
+				return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i)
+			}
+
+			err = callbackAfterCreate(callbacks, c, params, e, prepared, i, nil)
+			if err != nil {
+				return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i)
+			}
 		}
 
 		return callbackOnCreateSuccessReturn(callbacks, c, params, e, prepared, i, e.GetID())
 	}
 }
 
-func Delete(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[any] {
+func Delete(tableName string, callbacks *DeleteCallbacks, needTx bool) 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 {
@@ -112,26 +145,50 @@ func Delete(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[an
 			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
 		}
 
-		err = callbackBeforeDelete(callbacks, c, params, e, prepared, i, nil)
-		if err != nil {
-			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
-		}
+		if needTx {
+			err = database.Transaction(dbExecutor, func(tx database.Executor) error {
+				err = callbackBeforeDelete(callbacks, c, params, e, prepared, i, tx)
+				if err != nil {
+					return err
+				}
 
-		err = database.DeleteEntity(dbExecutor, tableName, e)
-		if err != nil {
-			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
-		}
+				err = database.DeleteEntity(tx, tableName, e)
+				if err != nil {
+					return err
+				}
 
-		err = callbackAfterDelete(callbacks, c, params, e, prepared, i, nil)
-		if err != nil {
-			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
+				err = callbackAfterDelete(callbacks, c, params, e, prepared, i, tx)
+				if err != nil {
+					return err
+				}
+
+				return nil
+			})
+			if err != nil {
+				return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
+			}
+		} else {
+			err = callbackBeforeDelete(callbacks, c, params, e, prepared, i, nil)
+			if err != nil {
+				return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
+			}
+
+			err = database.DeleteEntity(dbExecutor, tableName, e)
+			if err != nil {
+				return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
+			}
+
+			err = callbackAfterDelete(callbacks, c, params, e, prepared, i, nil)
+			if err != nil {
+				return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
+			}
 		}
 
 		return nil, callbackOnDeleteSuccessReturn(callbacks, c, params, e, prepared, i)
 	}
 }
 
-func Update(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallbacks) binding.ServiceFunc[any] {
+func Update(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallbacks, needTx bool) 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 {
@@ -176,19 +233,43 @@ func Update(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallba
 			return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
 		}
 
-		err = callbackBeforeUpdate(callbacks, c, params, e, prepared, i, nil)
-		if err != nil {
-			return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
-		}
+		if needTx {
+			err = database.Transaction(dbExecutor, func(tx database.Executor) error {
+				err = callbackBeforeUpdate(callbacks, c, params, e, prepared, i, tx)
+				if err != nil {
+					return err
+				}
 
-		err = database.UpdateEntity(dbExecutor, tableName, e)
-		if err != nil {
-			return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
-		}
+				err = database.UpdateEntity(tx, tableName, e)
+				if err != nil {
+					return err
+				}
 
-		err = callbackAfterUpdate(callbacks, c, params, e, prepared, i, nil)
-		if err != nil {
-			return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
+				err = callbackAfterUpdate(callbacks, c, params, e, prepared, i, tx)
+				if err != nil {
+					return err
+				}
+
+				return nil
+			})
+			if err != nil {
+				return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
+			}
+		} else {
+			err = callbackBeforeUpdate(callbacks, c, params, e, prepared, i, nil)
+			if err != nil {
+				return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
+			}
+
+			err = database.UpdateEntity(dbExecutor, tableName, e)
+			if err != nil {
+				return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
+			}
+
+			err = callbackAfterUpdate(callbacks, c, params, e, prepared, i, nil)
+			if err != nil {
+				return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
+			}
 		}
 
 		return nil, callbackOnUpdateSuccessReturn(callbacks, c, params, e, prepared, i)
@@ -354,202 +435,3 @@ func GetByID[O any](tableName string, callbacks *GetByIDCallbacks[O]) binding.Se
 		return callbackOnGetByIDSuccessReturn(callbacks, c, params, e, i, info)
 	}
 }
-
-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 {
-			return "", errors.New("领域实体为空")
-		}
-
-		dbExecutor := i.DBExecutor()
-
-		e, ok := objects[0].(entity.Entity)
-		if !ok {
-			return "", errors.New("需要传递领域对象应该为实体")
-		}
-
-		err := e.GenerateID()
-		if err != nil {
-			return callbackOnCreateErrorReturn(callbacks, c, params, 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, params, e, make(map[string]any), err, i)
-			}
-		}
-
-		prepared, err := callbackPrepareCreate(callbacks, c, params, e, i)
-		if err != nil {
-			return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
-		}
-
-		if domain.HasField(e, entity.FieldCreateUserID) && domain.HasField(e, entity.FieldLastUpdateUserID) {
-			createUserID, err := domain.Field[string](e, entity.FieldCreateUserID)
-			if err != nil {
-				return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
-			}
-
-			err = domain.SetField(e, entity.FieldLastUpdateUserID, createUserID)
-			if err != nil {
-				return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
-			}
-		}
-
-		err = domain.CheckFieldsForCreate(e, e.GetFieldMap())
-		if err != nil {
-			return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
-		}
-
-		err = database.Transaction(dbExecutor, func(tx database.Executor) error {
-			err = callbackBeforeCreate(callbacks, c, params, e, prepared, i, tx)
-			if err != nil {
-				return err
-			}
-
-			err = database.InsertEntity(tx, tableName, e)
-			if err != nil {
-				if database.IsErrorDBRecordHasExist(err) {
-					err = errors.New(e.DomainCNName() + "已存在")
-				}
-
-				return err
-			}
-
-			err = callbackAfterCreate(callbacks, c, params, e, prepared, i, tx)
-			if err != nil {
-				return err
-			}
-
-			return nil
-		})
-		if err != nil {
-			return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i)
-		}
-
-		return callbackOnCreateSuccessReturn(callbacks, c, params, e, prepared, i, e.GetID())
-	}
-}
-
-func DeleteTx(tableName string, callbacks *DeleteCallbacks) 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 {
-			return nil, errors.New("领域实体为空")
-		}
-
-		dbExecutor := i.DBExecutor()
-
-		e, ok := objects[0].(entity.Entity)
-		if !ok {
-			return nil, errors.New("需要传递领域对象应该为实体")
-		}
-
-		prepared, err := callbackPrepareDelete(callbacks, c, params, e, i)
-		if err != nil {
-			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
-		}
-
-		err = domain.CheckFieldsForDelete(e, e.GetFieldMap())
-		if err != nil {
-			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
-		}
-
-		err = database.Transaction(dbExecutor, func(tx database.Executor) error {
-			err = callbackBeforeDelete(callbacks, c, params, e, prepared, i, tx)
-			if err != nil {
-				return err
-			}
-
-			err = database.DeleteEntity(tx, tableName, e)
-			if err != nil {
-				return err
-			}
-
-			err = callbackAfterDelete(callbacks, c, params, e, prepared, i, tx)
-			if err != nil {
-				return err
-			}
-
-			return nil
-		})
-		if err != nil {
-			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
-		}
-
-		return nil, callbackOnDeleteSuccessReturn(callbacks, c, params, e, prepared, i)
-	}
-}
-
-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 {
-			return nil, errors.New("领域实体为空")
-		}
-
-		dbExecutor := i.DBExecutor()
-
-		e, ok := objects[0].(entity.Entity)
-		if !ok {
-			return nil, errors.New("需要传递领域对象应该为实体")
-		}
-
-		exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
-			TableName:  tableName,
-			Conditions: sql.NewConditions().Equal(entity.ColumnID, e.GetID()),
-		})
-		if err != nil {
-			return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
-		}
-
-		if !exist {
-			return nil, callbackOnUpdateErrorReturn(callbacks, c, params, 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, params, e, make(map[string]any), err, i)
-			}
-		}
-
-		prepared, err := callbackPrepareUpdate(callbacks, c, params, e, i)
-		if err != nil {
-			return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
-		}
-
-		err = domain.CheckFieldsForUpdate(e, e.GetFieldMap())
-		if err != nil {
-			return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
-		}
-
-		err = database.Transaction(dbExecutor, func(tx database.Executor) error {
-			err = callbackBeforeUpdate(callbacks, c, params, e, prepared, i, tx)
-			if err != nil {
-				return err
-			}
-
-			err = database.UpdateEntity(tx, tableName, e)
-			if err != nil {
-				return err
-			}
-
-			err = callbackAfterUpdate(callbacks, c, params, e, prepared, i, tx)
-			if err != nil {
-				return err
-			}
-
-			return nil
-		})
-		if err != nil {
-			return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
-		}
-
-		return nil, callbackOnUpdateSuccessReturn(callbacks, c, params, e, prepared, i)
-	}
-}

+ 21 - 51
convenient/entity_crud/simple.go

@@ -56,69 +56,39 @@ func (simple *Simple[I]) bind(binder *binding.Binder) {
 	if !createOptions.disable {
 		createMiddlewares := append(globalOptions.middlewares, createOptions.middlewares...)
 
-		if !createOptions.needTx {
-			binding.PostBind[string](binder, &binding.SimpleBindItem[string]{
-				Path:             domainPath + "/create",
-				SendResponseFunc: response.SendIDResponse,
-				RequestParams:    simple.CreateJsonBody,
-				Objects:          []domain.Object{simple.Entity},
-				ServiceFunc:      Create(tableName, createOptions.needCreateUserID, createOptions.callbacks),
-			}, createMiddlewares...)
-		} else {
-			binding.PostBind(binder, &binding.SimpleBindItem[string]{
-				Path:             domainPath + "/create",
-				SendResponseFunc: response.SendIDResponse,
-				RequestParams:    simple.CreateJsonBody,
-				Objects:          []domain.Object{simple.Entity},
-				ServiceFunc:      CreateTx(tableName, createOptions.needCreateUserID, createOptions.callbacks),
-			}, createMiddlewares...)
-		}
+		binding.PostBind[string](binder, &binding.SimpleBindItem[string]{
+			Path:             domainPath + "/create",
+			SendResponseFunc: response.SendIDResponse,
+			RequestParams:    simple.CreateJsonBody,
+			Objects:          []domain.Object{simple.Entity},
+			ServiceFunc:      Create(tableName, createOptions.needCreateUserID, createOptions.callbacks, createOptions.needTx),
+		}, createMiddlewares...)
 	}
 
 	// 删除
 	if !deleteOptions.disable {
 		deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...)
 
-		if !deleteOptions.needTx {
-			binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
-				Path:             domainPath + "/delete",
-				SendResponseFunc: response.SendMsgResponse,
-				RequestParams:    simple.DeleteQueryParams,
-				Objects:          []domain.Object{simple.Entity},
-				ServiceFunc:      Delete(tableName, deleteOptions.callbacks),
-			}, deleteMiddlewares...)
-		} else {
-			binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
-				Path:             domainPath + "/delete",
-				SendResponseFunc: response.SendMsgResponse,
-				RequestParams:    simple.DeleteQueryParams,
-				Objects:          []domain.Object{simple.Entity},
-				ServiceFunc:      DeleteTx(tableName, deleteOptions.callbacks),
-			}, deleteMiddlewares...)
-		}
+		binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
+			Path:             domainPath + "/delete",
+			SendResponseFunc: response.SendMsgResponse,
+			RequestParams:    simple.DeleteQueryParams,
+			Objects:          []domain.Object{simple.Entity},
+			ServiceFunc:      Delete(tableName, deleteOptions.callbacks, deleteOptions.needTx),
+		}, deleteMiddlewares...)
 	}
 
 	// 修改
 	if !updateOptions.disable {
 		updateMiddlewares := append(globalOptions.middlewares, updateOptions.middlewares...)
 
-		if !updateOptions.needTx {
-			binding.PutBind(binder, &binding.SimpleBindItem[any]{
-				Path:             domainPath + "/update",
-				SendResponseFunc: response.SendMsgResponse,
-				RequestParams:    simple.UpdateJsonBody,
-				Objects:          []domain.Object{simple.Entity},
-				ServiceFunc:      Update(tableName, updateOptions.needLastUpdateUserID, updateOptions.callbacks),
-			}, updateMiddlewares...)
-		} else {
-			binding.PutBind(binder, &binding.SimpleBindItem[any]{
-				Path:             domainPath + "/update",
-				SendResponseFunc: response.SendMsgResponse,
-				RequestParams:    simple.UpdateJsonBody,
-				Objects:          []domain.Object{simple.Entity},
-				ServiceFunc:      UpdateTx(tableName, updateOptions.needLastUpdateUserID, updateOptions.callbacks),
-			}, updateMiddlewares...)
-		}
+		binding.PutBind(binder, &binding.SimpleBindItem[any]{
+			Path:             domainPath + "/update",
+			SendResponseFunc: response.SendMsgResponse,
+			RequestParams:    simple.UpdateJsonBody,
+			Objects:          []domain.Object{simple.Entity},
+			ServiceFunc:      Update(tableName, updateOptions.needLastUpdateUserID, updateOptions.callbacks, updateOptions.needTx),
+		}, updateMiddlewares...)
 	}
 
 	// 查询

+ 78 - 130
convenient/value_object_crud/service.go

@@ -15,7 +15,7 @@ import (
 	"reflect"
 )
 
-func Create(tableName string, callbacks *CreateCallbacks) binding.ServiceFunc[any] {
+func Create(tableName string, callbacks *CreateCallbacks, needTx bool) 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 {
@@ -39,30 +39,58 @@ func Create(tableName string, callbacks *CreateCallbacks) binding.ServiceFunc[an
 			return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
 		}
 
-		err = callbackBeforeCreate(callbacks, c, params, valueObject, prepared, i, nil)
-		if err != nil {
-			return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
-		}
+		if needTx {
+			err = database.Transaction(dbExecutor, func(tx database.Executor) error {
+				err := callbackBeforeCreate(callbacks, c, params, valueObject, prepared, i, tx)
+				if err != nil {
+					return err
+				}
 
-		err = database.InsertEntity(dbExecutor, tableName, valueObject)
-		if err != nil {
-			if database.IsErrorDBRecordHasExist(err) {
-				err = errors.New(valueObject.DomainCNName() + "已存在")
+				err = database.InsertEntity(tx, tableName, valueObject)
+				if err != nil {
+					if database.IsErrorDBRecordHasExist(err) {
+						err = errors.New(valueObject.DomainCNName() + "已存在")
+					}
+
+					return err
+				}
+
+				err = callbackAfterCreate(callbacks, c, params, valueObject, prepared, i, tx)
+				if err != nil {
+					return err
+				}
+
+				return nil
+			})
+			if err != nil {
+				return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
+			}
+		} else {
+			err = callbackBeforeCreate(callbacks, c, params, valueObject, prepared, i, nil)
+			if err != nil {
+				return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
 			}
 
-			return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
-		}
+			err = database.InsertEntity(dbExecutor, tableName, valueObject)
+			if err != nil {
+				if database.IsErrorDBRecordHasExist(err) {
+					err = errors.New(valueObject.DomainCNName() + "已存在")
+				}
 
-		err = callbackAfterCreate(callbacks, c, params, valueObject, prepared, i, nil)
-		if err != nil {
-			return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
+				return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
+			}
+
+			err = callbackAfterCreate(callbacks, c, params, valueObject, prepared, i, nil)
+			if err != nil {
+				return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
+			}
 		}
 
 		return nil, callbackOnCreateSuccessReturn(callbacks, c, params, valueObject, prepared, i)
 	}
 }
 
-func Delete(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[any] {
+func Delete(tableName string, callbacks *DeleteCallbacks, needTx bool) 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 {
@@ -86,19 +114,43 @@ func Delete(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[an
 			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
 		}
 
-		err = callbackBeforeDelete(callbacks, c, params, valueObject, prepared, i, nil)
-		if err != nil {
-			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
-		}
+		if needTx {
+			err = database.Transaction(dbExecutor, func(tx database.Executor) error {
+				err = callbackBeforeDelete(callbacks, c, params, valueObject, prepared, i, tx)
+				if err != nil {
+					return err
+				}
 
-		err = database.DeleteEntity(dbExecutor, tableName, valueObject)
-		if err != nil {
-			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
-		}
+				err = database.DeleteEntity(tx, tableName, valueObject)
+				if err != nil {
+					return err
+				}
 
-		err = callbackAfterDelete(callbacks, c, params, valueObject, prepared, i, nil)
-		if err != nil {
-			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
+				err = callbackAfterDelete(callbacks, c, params, valueObject, prepared, i, tx)
+				if err != nil {
+					return err
+				}
+
+				return nil
+			})
+			if err != nil {
+				return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
+			}
+		} else {
+			err = callbackBeforeDelete(callbacks, c, params, valueObject, prepared, i, nil)
+			if err != nil {
+				return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
+			}
+
+			err = database.DeleteEntity(dbExecutor, tableName, valueObject)
+			if err != nil {
+				return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
+			}
+
+			err = callbackAfterDelete(callbacks, c, params, valueObject, prepared, i, nil)
+			if err != nil {
+				return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
+			}
 		}
 
 		return nil, callbackOnDeleteSuccessReturn(callbacks, c, params, valueObject, prepared, i)
@@ -211,107 +263,3 @@ func Query[O any](tableName string, orderBy string, callbacks *QueryCallbacks[O]
 		return callbackOnQuerySuccessReturn(callbacks, c, params, valueObject, i, output)
 	}
 }
-
-func CreateTx(tableName string, callbacks *CreateCallbacks) 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 {
-			return nil, errors.New("领域实体为空")
-		}
-
-		dbExecutor := i.DBExecutor()
-
-		valueObject, ok := objects[0].(value_object.ValueObject)
-		if !ok {
-			return nil, errors.New("需要传递领域对象应该为值对象")
-		}
-
-		prepared, err := callbackPrepareCreate(callbacks, c, params, valueObject, i)
-		if err != nil {
-			return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
-		}
-
-		err = domain.CheckFieldsForCreate(valueObject, valueObject.GetFieldMap())
-		if err != nil {
-			return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
-		}
-
-		err = database.Transaction(dbExecutor, func(tx database.Executor) error {
-			err := callbackBeforeCreate(callbacks, c, params, valueObject, prepared, i, tx)
-			if err != nil {
-				return err
-			}
-
-			err = database.InsertEntity(tx, tableName, valueObject)
-			if err != nil {
-				if database.IsErrorDBRecordHasExist(err) {
-					err = errors.New(valueObject.DomainCNName() + "已存在")
-				}
-
-				return err
-			}
-
-			err = callbackAfterCreate(callbacks, c, params, valueObject, prepared, i, tx)
-			if err != nil {
-				return err
-			}
-
-			return nil
-		})
-		if err != nil {
-			return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
-		}
-
-		return nil, callbackOnCreateSuccessReturn(callbacks, c, params, valueObject, prepared, i)
-	}
-}
-
-func DeleteTx(tableName string, callbacks *DeleteCallbacks) 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 {
-			return nil, errors.New("领域实体为空")
-		}
-
-		dbExecutor := i.DBExecutor()
-
-		valueObject, ok := objects[0].(value_object.ValueObject)
-		if !ok {
-			return nil, errors.New("需要传递领域对象应该为值对象")
-		}
-
-		prepared, err := callbackPrepareDelete(callbacks, c, params, valueObject, i)
-		if err != nil {
-			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
-		}
-
-		err = domain.CheckFieldsForDelete(valueObject, valueObject.GetFieldMap())
-		if err != nil {
-			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
-		}
-
-		err = database.Transaction(dbExecutor, func(tx database.Executor) error {
-			err = callbackBeforeDelete(callbacks, c, params, valueObject, prepared, i, tx)
-			if err != nil {
-				return err
-			}
-
-			err = database.DeleteEntity(tx, tableName, valueObject)
-			if err != nil {
-				return err
-			}
-
-			err = callbackAfterDelete(callbacks, c, params, valueObject, prepared, i, tx)
-			if err != nil {
-				return err
-			}
-
-			return nil
-		})
-		if err != nil {
-			return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
-		}
-
-		return nil, callbackOnDeleteSuccessReturn(callbacks, c, params, valueObject, prepared, i)
-	}
-}

+ 15 - 36
convenient/value_object_crud/simple.go

@@ -46,48 +46,27 @@ func (simple *Simple[I]) bind(binder *binding.Binder) {
 	if !createOptions.disable {
 		createMiddlewares := append(globalOptions.middlewares, createOptions.middlewares...)
 
-		if !createOptions.needTx {
-			binding.PostBind(binder, &binding.SimpleBindItem[any]{
-				Path:             domainPath + "/create",
-				SendResponseFunc: response.SendMsgResponse,
-				RequestParams:    simple.CreateJsonBody,
-				Objects:          []domain.Object{simple.ValueObject},
-				ServiceFunc:      Create(tableName, createOptions.callbacks),
-			}, createMiddlewares...)
-		} else {
-			binding.PostBind(binder, &binding.SimpleBindItem[any]{
-				Path:             domainPath + "/create",
-				SendResponseFunc: response.SendMsgResponse,
-				RequestParams:    simple.CreateJsonBody,
-				Objects:          []domain.Object{simple.ValueObject},
-				ServiceFunc:      CreateTx(tableName, createOptions.callbacks),
-			}, createMiddlewares...)
-		}
+		binding.PostBind(binder, &binding.SimpleBindItem[any]{
+			Path:             domainPath + "/create",
+			SendResponseFunc: response.SendMsgResponse,
+			RequestParams:    simple.CreateJsonBody,
+			Objects:          []domain.Object{simple.ValueObject},
+			ServiceFunc:      Create(tableName, createOptions.callbacks, createOptions.needTx),
+		}, createMiddlewares...)
 	}
 
 	// 删除
 	if !deleteOptions.disable {
 		deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...)
 
-		if !deleteOptions.needTx {
-			binding.PostBind(binder, &binding.SimpleBindItem[any]{
-				Path:                  domainPath + "/delete",
-				SendResponseFunc:      response.SendMsgResponse,
-				RequestParams:         simple.DeleteJsonBody,
-				BindRequestParamsFunc: request.BindJsonBody,
-				Objects:               []domain.Object{simple.ValueObject},
-				ServiceFunc:           Delete(tableName, deleteOptions.callbacks),
-			}, deleteMiddlewares...)
-		} else {
-			binding.PostBind(binder, &binding.SimpleBindItem[any]{
-				Path:                  domainPath + "/delete",
-				SendResponseFunc:      response.SendMsgResponse,
-				RequestParams:         simple.DeleteJsonBody,
-				BindRequestParamsFunc: request.BindJsonBody,
-				Objects:               []domain.Object{simple.ValueObject},
-				ServiceFunc:           DeleteTx(tableName, deleteOptions.callbacks),
-			}, deleteMiddlewares...)
-		}
+		binding.PostBind(binder, &binding.SimpleBindItem[any]{
+			Path:                  domainPath + "/delete",
+			SendResponseFunc:      response.SendMsgResponse,
+			RequestParams:         simple.DeleteJsonBody,
+			BindRequestParamsFunc: request.BindJsonBody,
+			Objects:               []domain.Object{simple.ValueObject},
+			ServiceFunc:           Delete(tableName, deleteOptions.callbacks, deleteOptions.needTx),
+		}, deleteMiddlewares...)
 	}
 
 	// 查询