|
@@ -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)
|
|
|
- }
|
|
|
-}
|