|
|
@@ -4,18 +4,15 @@ import (
|
|
|
"git.sxidc.com/go-framework/baize/api"
|
|
|
"git.sxidc.com/go-framework/baize/binding"
|
|
|
"git.sxidc.com/go-framework/baize/binding/request"
|
|
|
- "git.sxidc.com/go-framework/baize/binding/response"
|
|
|
"git.sxidc.com/go-framework/baize/domain"
|
|
|
"git.sxidc.com/go-framework/baize/infrastructure"
|
|
|
"git.sxidc.com/go-framework/baize/infrastructure/database"
|
|
|
"git.sxidc.com/go-framework/baize/infrastructure/database/sql"
|
|
|
- "git.sxidc.com/go-framework/baize/tag/sql/sql_mapping"
|
|
|
"git.sxidc.com/go-tools/utils/strutils"
|
|
|
"git.sxidc.com/service-supports/fserr"
|
|
|
- "reflect"
|
|
|
)
|
|
|
|
|
|
-func CommonEntityCreate(tableName string, dbExecutor database.Executor, callbacks *Callbacks[string]) binding.ServiceFunc[string] {
|
|
|
+func CommonEntityCreateTx(tableName string, dbExecutor database.Executor, callbacks *Callbacks[string]) binding.ServiceFunc[string] {
|
|
|
return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
|
|
|
e, ok := objects[0].(domain.Entity)
|
|
|
if !ok {
|
|
|
@@ -24,28 +21,40 @@ func CommonEntityCreate(tableName string, dbExecutor database.Executor, callback
|
|
|
|
|
|
err := e.GenerateID()
|
|
|
if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, "")
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, "")
|
|
|
}
|
|
|
|
|
|
- err = callbackBeforeDBOperate(callbacks, e, dbExecutor)
|
|
|
- if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, "")
|
|
|
- }
|
|
|
+ err = database.Transaction(dbExecutor, func(tx database.Executor) error {
|
|
|
+ err = callbackBeforeDBOperate(callbacks, e, tx)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
- err = database.InsertEntity(dbExecutor, tableName, e)
|
|
|
- if err != nil {
|
|
|
- if database.IsErrorDBRecordHasExist(err) {
|
|
|
- err = fserr.New(e.DomainCNName() + "已存在")
|
|
|
+ err = database.InsertEntity(tx, tableName, e)
|
|
|
+ if err != nil {
|
|
|
+ if database.IsErrorDBRecordHasExist(err) {
|
|
|
+ err = fserr.New(e.DomainCNName() + "已存在")
|
|
|
+ }
|
|
|
+
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ err = callbackAfterDBOperate(callbacks, e, tx)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, "")
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, "")
|
|
|
}
|
|
|
|
|
|
- return callbackOnReturn(callbacks, e, dbExecutor, e.GetID())
|
|
|
+ return callbackOnSuccessReturn(callbacks, e, dbExecutor, e.GetID())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func CommonEntityDelete(tableName string, dbExecutor database.Executor, callbacks *Callbacks[any]) binding.ServiceFunc[any] {
|
|
|
+func CommonEntityDeleteTx(tableName string, dbExecutor database.Executor, callbacks *Callbacks[any]) binding.ServiceFunc[any] {
|
|
|
return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
|
|
|
e, ok := objects[0].(domain.Entity)
|
|
|
if !ok {
|
|
|
@@ -54,7 +63,7 @@ func CommonEntityDelete(tableName string, dbExecutor database.Executor, callback
|
|
|
|
|
|
if strutils.IsStringEmpty(e.GetID()) {
|
|
|
err := fserr.New("领域实体ID为空")
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, nil)
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
}
|
|
|
|
|
|
exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
|
|
|
@@ -62,29 +71,41 @@ func CommonEntityDelete(tableName string, dbExecutor database.Executor, callback
|
|
|
Conditions: sql.NewConditions().Equal(e.IDColumnName(), e.GetID()),
|
|
|
})
|
|
|
if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, nil)
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
}
|
|
|
|
|
|
if !exist {
|
|
|
err := fserr.New(e.DomainCNName() + "不存在")
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, nil)
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
}
|
|
|
|
|
|
- err = callbackBeforeDBOperate(callbacks, e, dbExecutor)
|
|
|
- if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, nil)
|
|
|
- }
|
|
|
+ err = database.Transaction(dbExecutor, func(tx database.Executor) error {
|
|
|
+ err = callbackBeforeDBOperate(callbacks, e, tx)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ err = database.DeleteEntity(tx, tableName, e)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
- err = database.DeleteEntity(dbExecutor, tableName, e)
|
|
|
+ err = callbackAfterDBOperate(callbacks, e, tx)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ })
|
|
|
if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, nil)
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
}
|
|
|
|
|
|
- return callbackOnReturn(callbacks, e, dbExecutor, nil)
|
|
|
+ return callbackOnSuccessReturn(callbacks, e, dbExecutor, nil)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func CommonEntityUpdate(tableName string, dbExecutor database.Executor, callbacks *Callbacks[any]) binding.ServiceFunc[any] {
|
|
|
+func CommonEntityUpdateTx(tableName string, dbExecutor database.Executor, callbacks *Callbacks[any]) binding.ServiceFunc[any] {
|
|
|
return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
|
|
|
e, ok := objects[0].(domain.Entity)
|
|
|
if !ok {
|
|
|
@@ -93,7 +114,7 @@ func CommonEntityUpdate(tableName string, dbExecutor database.Executor, callback
|
|
|
|
|
|
if strutils.IsStringEmpty(e.GetID()) {
|
|
|
err := fserr.New("领域实体ID为空")
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, nil)
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
}
|
|
|
|
|
|
exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
|
|
|
@@ -101,136 +122,36 @@ func CommonEntityUpdate(tableName string, dbExecutor database.Executor, callback
|
|
|
Conditions: sql.NewConditions().Equal(e.IDColumnName(), e.GetID()),
|
|
|
})
|
|
|
if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, nil)
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
}
|
|
|
|
|
|
if !exist {
|
|
|
err := fserr.New(e.DomainCNName() + "不存在")
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, nil)
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
}
|
|
|
|
|
|
- err = callbackBeforeDBOperate(callbacks, e, dbExecutor)
|
|
|
- if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, nil)
|
|
|
- }
|
|
|
-
|
|
|
- err = database.UpdateEntity(dbExecutor, tableName, e)
|
|
|
- if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, nil)
|
|
|
- }
|
|
|
-
|
|
|
- return callbackOnReturn(callbacks, e, dbExecutor, nil)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func CommonEntityQuery[O any](tableName string, dbExecutor database.Executor, callbacks *Callbacks[response.InfosData[O]], conditionFieldCallback domain.ConditionFieldCallback) binding.ServiceFunc[response.InfosData[O]] {
|
|
|
- return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[O], error) {
|
|
|
- queryDTO, ok := dto.(request.Query)
|
|
|
- if !ok {
|
|
|
- return response.InfosData[O]{}, fserr.New("DTO不是Query")
|
|
|
- }
|
|
|
-
|
|
|
- e, ok := objects[0].(domain.Entity)
|
|
|
- if !ok {
|
|
|
- return response.InfosData[O]{}, fserr.New("需要传递领域对象应该为实体")
|
|
|
- }
|
|
|
-
|
|
|
- conditions := sql.NewConditions()
|
|
|
-
|
|
|
- fields, err := sql_mapping.DefaultUsage(e)
|
|
|
- if err != nil {
|
|
|
- return response.InfosData[O]{}, err
|
|
|
- }
|
|
|
-
|
|
|
- for _, field := range fields {
|
|
|
- hasDeal := false
|
|
|
- if conditionFieldCallback != nil {
|
|
|
- hasDeal = conditionFieldCallback(conditions, field.FieldName, field.ColumnName, field.Value)
|
|
|
+ err = database.Transaction(dbExecutor, func(tx database.Executor) error {
|
|
|
+ err = callbackBeforeDBOperate(callbacks, e, tx)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
- if !hasDeal {
|
|
|
- fieldValue := reflect.ValueOf(field.Value)
|
|
|
- if !fieldValue.IsZero() {
|
|
|
- conditions.Equal(field.ColumnName, field.Value)
|
|
|
- }
|
|
|
+ err = database.UpdateEntity(tx, tableName, e)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- err = callbackBeforeDBOperate(callbacks, e, dbExecutor)
|
|
|
- if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, response.InfosData[O]{})
|
|
|
- }
|
|
|
-
|
|
|
- results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
|
|
|
- TableName: tableName,
|
|
|
- Conditions: conditions,
|
|
|
- PageNo: queryDTO.GetPageNo(),
|
|
|
- PageSize: queryDTO.GetPageSize(),
|
|
|
- })
|
|
|
- if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, response.InfosData[O]{})
|
|
|
- }
|
|
|
-
|
|
|
- infos := make([]O, 0)
|
|
|
- err = sql.ParseSqlResult(results, &infos)
|
|
|
- if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, response.InfosData[O]{})
|
|
|
- }
|
|
|
-
|
|
|
- output := response.InfosData[O]{
|
|
|
- Infos: infos,
|
|
|
- TotalCount: totalCount,
|
|
|
- PageNo: queryDTO.GetPageNo(),
|
|
|
- }
|
|
|
-
|
|
|
- return callbackOnReturn(callbacks, e, dbExecutor, output)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-func CommonEntityQueryByID[O any](tableName string, dbExecutor database.Executor, callbacks *Callbacks[O]) binding.ServiceFunc[O] {
|
|
|
- return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (O, error) {
|
|
|
- var outputZero O
|
|
|
- outputZeroValue := reflect.Zero(reflect.TypeOf(outputZero))
|
|
|
- if outputZeroValue.Kind() == reflect.Pointer {
|
|
|
- outputZeroValue.Set(reflect.New(outputZeroValue.Type().Elem()))
|
|
|
- }
|
|
|
|
|
|
- e, ok := objects[0].(domain.Entity)
|
|
|
- if !ok {
|
|
|
- return outputZero, fserr.New("需要传递领域对象应该为实体")
|
|
|
- }
|
|
|
-
|
|
|
- if strutils.IsStringEmpty(e.GetID()) {
|
|
|
- err := fserr.New("领域实体ID为空")
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, outputZero)
|
|
|
- }
|
|
|
-
|
|
|
- err := callbackBeforeDBOperate(callbacks, e, dbExecutor)
|
|
|
- if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, outputZero)
|
|
|
- }
|
|
|
+ err = callbackAfterDBOperate(callbacks, e, tx)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
|
|
|
- result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
|
|
|
- TableName: tableName,
|
|
|
- Conditions: sql.NewConditions().Equal(e.IDColumnName(), e.GetID()),
|
|
|
+ return nil
|
|
|
})
|
|
|
if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, outputZero)
|
|
|
- }
|
|
|
-
|
|
|
- var info O
|
|
|
- var infoPointer any
|
|
|
-
|
|
|
- infoPointer = &info
|
|
|
- if outputZeroValue.Kind() == reflect.Pointer {
|
|
|
- infoPointer = info
|
|
|
- }
|
|
|
-
|
|
|
- err = sql.ParseSqlResult(result, infoPointer)
|
|
|
- if err != nil {
|
|
|
- return callbackOnError(callbacks, e, err, dbExecutor, outputZero)
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
}
|
|
|
|
|
|
- return callbackOnReturn(callbacks, e, dbExecutor, info)
|
|
|
+ return callbackOnSuccessReturn(callbacks, e, dbExecutor, nil)
|
|
|
}
|
|
|
}
|