|
|
@@ -1,157 +0,0 @@
|
|
|
-package entity_crud
|
|
|
-
|
|
|
-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/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-tools/utils/strutils"
|
|
|
- "git.sxidc.com/service-supports/fserr"
|
|
|
-)
|
|
|
-
|
|
|
-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 {
|
|
|
- return "", fserr.New("需要传递领域对象应该为实体")
|
|
|
- }
|
|
|
-
|
|
|
- err := e.GenerateID()
|
|
|
- if err != nil {
|
|
|
- return callbackOnErrorReturn(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(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 nil
|
|
|
- })
|
|
|
- if err != nil {
|
|
|
- return callbackOnErrorReturn(callbacks, e, err, dbExecutor, "")
|
|
|
- }
|
|
|
-
|
|
|
- return callbackOnSuccessReturn(callbacks, e, dbExecutor, e.GetID())
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-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 {
|
|
|
- return nil, fserr.New("需要传递领域对象应该为实体")
|
|
|
- }
|
|
|
-
|
|
|
- if strutils.IsStringEmpty(e.GetID()) {
|
|
|
- err := fserr.New("领域实体ID为空")
|
|
|
- return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
- }
|
|
|
-
|
|
|
- exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
|
|
|
- TableName: tableName,
|
|
|
- Conditions: sql.NewConditions().Equal(e.IDColumnName(), e.GetID()),
|
|
|
- })
|
|
|
- if err != nil {
|
|
|
- return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
- }
|
|
|
-
|
|
|
- if !exist {
|
|
|
- err := fserr.New(e.DomainCNName() + "不存在")
|
|
|
- return callbackOnErrorReturn(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 = callbackAfterDBOperate(callbacks, e, tx)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
- })
|
|
|
- if err != nil {
|
|
|
- return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
- }
|
|
|
-
|
|
|
- return callbackOnSuccessReturn(callbacks, e, dbExecutor, nil)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-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 {
|
|
|
- return nil, fserr.New("需要传递领域对象应该为实体")
|
|
|
- }
|
|
|
-
|
|
|
- if strutils.IsStringEmpty(e.GetID()) {
|
|
|
- err := fserr.New("领域实体ID为空")
|
|
|
- return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
- }
|
|
|
-
|
|
|
- exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
|
|
|
- TableName: tableName,
|
|
|
- Conditions: sql.NewConditions().Equal(e.IDColumnName(), e.GetID()),
|
|
|
- })
|
|
|
- if err != nil {
|
|
|
- return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
- }
|
|
|
-
|
|
|
- if !exist {
|
|
|
- err := fserr.New(e.DomainCNName() + "不存在")
|
|
|
- return callbackOnErrorReturn(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.UpdateEntity(tx, tableName, e)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- err = callbackAfterDBOperate(callbacks, e, tx)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
- })
|
|
|
- if err != nil {
|
|
|
- return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
- }
|
|
|
-
|
|
|
- return callbackOnSuccessReturn(callbacks, e, dbExecutor, nil)
|
|
|
- }
|
|
|
-}
|