|
|
@@ -0,0 +1,407 @@
|
|
|
+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/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] {
|
|
|
+ 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 = callbackBeforeDBOperate(callbacks, e, dbExecutor)
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, "")
|
|
|
+ }
|
|
|
+
|
|
|
+ err = database.InsertEntity(dbExecutor, tableName, e)
|
|
|
+ if err != nil {
|
|
|
+ if database.IsErrorDBRecordHasExist(err) {
|
|
|
+ err = fserr.New(e.DomainCNName() + "已存在")
|
|
|
+ }
|
|
|
+
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, "")
|
|
|
+ }
|
|
|
+
|
|
|
+ err = callbackAfterDBOperate(callbacks, e, dbExecutor)
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, "")
|
|
|
+ }
|
|
|
+
|
|
|
+ return callbackOnSuccessReturn(callbacks, e, dbExecutor, e.GetID())
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func CommonEntityDelete(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 = callbackBeforeDBOperate(callbacks, e, dbExecutor)
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = database.DeleteEntity(dbExecutor, tableName, e)
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = callbackAfterDBOperate(callbacks, e, dbExecutor)
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ return callbackOnSuccessReturn(callbacks, e, dbExecutor, nil)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func CommonEntityUpdate(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 = callbackBeforeDBOperate(callbacks, e, dbExecutor)
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = database.UpdateEntity(dbExecutor, tableName, e)
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = callbackAfterDBOperate(callbacks, e, dbExecutor)
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ return callbackOnSuccessReturn(callbacks, e, dbExecutor, nil)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+type ConditionFieldCallback func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool)
|
|
|
+
|
|
|
+func CommonEntityQuery[O any](tableName string, dbExecutor database.Executor, callbacks *Callbacks[response.InfosData[O]], conditionFieldCallback 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)
|
|
|
+ }
|
|
|
+
|
|
|
+ if !hasDeal {
|
|
|
+ fieldValue := reflect.ValueOf(field.Value)
|
|
|
+ if !fieldValue.IsZero() {
|
|
|
+ conditions.Equal(field.ColumnName, field.Value)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ err = callbackBeforeDBOperate(callbacks, e, dbExecutor)
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(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 callbackOnErrorReturn(callbacks, e, err, dbExecutor, response.InfosData[O]{})
|
|
|
+ }
|
|
|
+
|
|
|
+ err = callbackAfterDBOperate(callbacks, e, dbExecutor)
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, response.InfosData[O]{})
|
|
|
+ }
|
|
|
+
|
|
|
+ infos := make([]O, 0)
|
|
|
+ err = sql.ParseSqlResult(results, &infos)
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, response.InfosData[O]{})
|
|
|
+ }
|
|
|
+
|
|
|
+ output := response.InfosData[O]{
|
|
|
+ Infos: infos,
|
|
|
+ TotalCount: totalCount,
|
|
|
+ PageNo: queryDTO.GetPageNo(),
|
|
|
+ }
|
|
|
+
|
|
|
+ return callbackOnSuccessReturn(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 callbackOnErrorReturn(callbacks, e, err, dbExecutor, outputZero)
|
|
|
+ }
|
|
|
+
|
|
|
+ err := callbackBeforeDBOperate(callbacks, e, dbExecutor)
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, outputZero)
|
|
|
+ }
|
|
|
+
|
|
|
+ result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
|
|
|
+ TableName: tableName,
|
|
|
+ Conditions: sql.NewConditions().Equal(e.IDColumnName(), e.GetID()),
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(callbacks, e, err, dbExecutor, outputZero)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = callbackAfterDBOperate(callbacks, e, dbExecutor)
|
|
|
+ if err != nil {
|
|
|
+ return callbackOnErrorReturn(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 callbackOnErrorReturn(callbacks, e, err, dbExecutor, outputZero)
|
|
|
+ }
|
|
|
+
|
|
|
+ return callbackOnSuccessReturn(callbacks, e, dbExecutor, info)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+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)
|
|
|
+ }
|
|
|
+}
|