yjp vor 5 Tagen
Ursprung
Commit
2f7a9b2365

+ 9 - 8
convenient/entity_crud/service.go

@@ -1,6 +1,9 @@
 package entity_crud
 
 import (
+	"reflect"
+	"strings"
+
 	"git.sxidc.com/go-framework/baize/framework/binding"
 	"git.sxidc.com/go-framework/baize/framework/core/api"
 	"git.sxidc.com/go-framework/baize/framework/core/api/request"
@@ -13,8 +16,6 @@ import (
 	"git.sxidc.com/go-framework/baize/framework/core/tag/sql/sql_mapping"
 	"git.sxidc.com/go-tools/utils/reflectutils"
 	"github.com/pkg/errors"
-	"reflect"
-	"strings"
 )
 
 func Create(tableName string, needCreateUserID bool, callbacks *CreateCallbacks, needTx bool) binding.ServiceFunc[string] {
@@ -117,7 +118,7 @@ func Create(tableName string, needCreateUserID bool, callbacks *CreateCallbacks,
 	}
 }
 
-func Delete(tableName string, callbacks *DeleteCallbacks, needTx bool) binding.ServiceFunc[any] {
+func Delete(tableName string, callbacks *DeleteCallbacks, needTx bool, scopeFields ...string) 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 {
@@ -148,7 +149,7 @@ func Delete(tableName string, callbacks *DeleteCallbacks, needTx bool) binding.S
 					return err
 				}
 
-				err = database.DeleteEntity(tx, tableName, e)
+				err = database.DeleteEntityWithScope(tx, tableName, e, scopeFields...)
 				if err != nil {
 					return err
 				}
@@ -169,7 +170,7 @@ func Delete(tableName string, callbacks *DeleteCallbacks, needTx bool) binding.S
 				return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
 			}
 
-			err = database.DeleteEntity(dbExecutor, tableName, e)
+			err = database.DeleteEntityWithScope(dbExecutor, tableName, e, scopeFields...)
 			if err != nil {
 				return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
 			}
@@ -184,7 +185,7 @@ func Delete(tableName string, callbacks *DeleteCallbacks, needTx bool) binding.S
 	}
 }
 
-func Update(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallbacks, needTx bool) binding.ServiceFunc[any] {
+func Update(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallbacks, needTx bool, scopeFields ...string) 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 {
@@ -236,7 +237,7 @@ func Update(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallba
 					return err
 				}
 
-				err = database.UpdateEntity(tx, tableName, e)
+				err = database.UpdateEntityWithScope(tx, tableName, e, scopeFields...)
 				if err != nil {
 					return err
 				}
@@ -257,7 +258,7 @@ func Update(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallba
 				return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
 			}
 
-			err = database.UpdateEntity(dbExecutor, tableName, e)
+			err = database.UpdateEntityWithScope(dbExecutor, tableName, e, scopeFields...)
 			if err != nil {
 				return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
 			}

+ 7 - 0
convenient/entity_crud/simple.go

@@ -165,6 +165,7 @@ type GetByIDOption[I any] func(options *GetByIDOptions[I])
 
 type GlobalOptions struct {
 	middlewares []binding.Middleware
+	scopeFields []string
 }
 
 type CreateOptions struct {
@@ -265,6 +266,12 @@ func WithGlobalMiddlewares(middlewares ...binding.Middleware) GlobalOption {
 	}
 }
 
+func WithGlobalScopeFields(scopeFields ...string) GlobalOption {
+	return func(options *GlobalOptions) {
+		options.scopeFields = scopeFields
+	}
+}
+
 func WithDisableCreate() CreateOption {
 	return func(options *CreateOptions) {
 		options.disable = true

+ 16 - 0
framework/core/infrastructure/database/clause/condition.go

@@ -15,11 +15,27 @@ func NewConditions() *Conditions {
 	}
 }
 
+func (conditions *Conditions) AddConditions(cond *Conditions) *Conditions {
+	if conditions.err != nil || cond.err != nil {
+		return conditions
+	}
+
+	conditions.queries = append(conditions.queries, cond.queries...)
+	conditions.args = append(conditions.args, cond.args...)
+
+	return conditions
+}
+
 func (conditions *Conditions) AddCondition(query string, args ...any) *Conditions {
+	if conditions.err != nil {
+		return conditions
+	}
+
 	conditions.queries = append(conditions.queries, query)
 	for _, arg := range args {
 		conditions.args = append(conditions.args, []any{arg})
 	}
+
 	return conditions
 }
 

+ 76 - 8
framework/core/infrastructure/database/database.go

@@ -1,15 +1,16 @@
 package database
 
 import (
+	"reflect"
+	"strings"
+	"time"
+
 	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/operations"
 	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
 	"git.sxidc.com/go-framework/baize/framework/core/tag/sql/sql_mapping"
 	"git.sxidc.com/go-tools/utils/reflectutils"
 	"git.sxidc.com/go-tools/utils/strutils"
 	"github.com/pkg/errors"
-	"reflect"
-	"strings"
-	"time"
 )
 
 // Executor 数据库基础设施接口
@@ -243,6 +244,22 @@ func formInsertTableRow(fields []sql_mapping.Field, createTime time.Time) *sql.T
 // 返回值:
 // - 错误
 func DeleteEntity(executor Executor, tableName string, e any) error {
+	return deleteEntityCommon(executor, tableName, e)
+}
+
+// DeleteEntityWithScope 通过结构删除数据,可以添加范围条件,如租户ID
+// 参数:
+// - executor: 数据库基础设施接口
+// - tableName: 表名
+// - e: 结构,结构字段需要使用sqlmapping标注
+// - scopeFields: 范围字段名
+// 返回值:
+// - 错误
+func DeleteEntityWithScope(executor Executor, tableName string, e any, scopeFields ...string) error {
+	return deleteEntityCommon(executor, tableName, e, scopeFields...)
+}
+
+func deleteEntityCommon(executor Executor, tableName string, e any, scopeFields ...string) error {
 	if executor == nil {
 		return errors.New("没有传递执行器")
 	}
@@ -267,12 +284,19 @@ func DeleteEntity(executor Executor, tableName string, e any) error {
 
 	conditions := sql.NewConditions()
 	for _, field := range fields {
-		// 不是键,字段跳过
-		if !field.IsKey {
-			continue
+		if scopeFields != nil && len(scopeFields) != 0 {
+			for _, scopeField := range scopeFields {
+				if scopeField != field.FieldName {
+					continue
+				}
+
+				conditions.Equal(field.ColumnName, field.Value)
+			}
 		}
 
-		conditions.Equal(field.ColumnName, field.Value)
+		if field.IsKey {
+			conditions.Equal(field.ColumnName, field.Value)
+		}
 	}
 
 	executeParams := sql.DeleteExecuteParams{
@@ -301,7 +325,24 @@ func DeleteEntity(executor Executor, tableName string, e any) error {
 // 返回值:
 // - 错误
 func UpdateEntity(executor Executor, tableName string, e any) error {
-	_, err := UpdateEntityWithRowsAffected(executor, tableName, e)
+	_, err := updateEntityCommon(executor, tableName, e)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+// UpdateEntityWithScope 通过结构更新数据,可以添加范围条件,如租户ID
+// 参数:
+// - executor: 数据库基础设施接口
+// - tableName: 表名
+// - e: 结构,结构字段需要使用sqlmapping标注
+// - scopeFields: 范围字段名
+// 返回值:
+// - 错误
+func UpdateEntityWithScope(executor Executor, tableName string, e any, scopeFields ...string) error {
+	_, err := updateEntityCommon(executor, tableName, e, scopeFields...)
 	if err != nil {
 		return err
 	}
@@ -318,6 +359,23 @@ func UpdateEntity(executor Executor, tableName string, e any) error {
 // - 影响行数
 // - 错误
 func UpdateEntityWithRowsAffected(executor Executor, tableName string, e any) (int64, error) {
+	return updateEntityCommon(executor, tableName, e)
+}
+
+// UpdateEntityWithRowsAffectedAndScope 通过结构更新数据,可以添加范围条件,如租户ID,返回影响行数
+// 参数:
+// - executor: 数据库基础设施接口
+// - tableName: 表名
+// - e: 结构,结构字段需要使用sqlmapping标注
+// - scopeFields: 范围字段名
+// 返回值:
+// - 影响行数
+// - 错误
+func UpdateEntityWithRowsAffectedAndScope(executor Executor, tableName string, e any, scopeFields ...string) (int64, error) {
+	return updateEntityCommon(executor, tableName, e, scopeFields...)
+}
+
+func updateEntityCommon(executor Executor, tableName string, e any, scopeFields ...string) (int64, error) {
 	if executor == nil {
 		return 0, errors.New("没有传递执行器")
 	}
@@ -345,6 +403,16 @@ func UpdateEntityWithRowsAffected(executor Executor, tableName string, e any) (i
 	conditions := sql.NewConditions()
 
 	for _, field := range fields {
+		if scopeFields != nil && len(scopeFields) != 0 {
+			for _, scopeField := range scopeFields {
+				if scopeField != field.FieldName {
+					continue
+				}
+
+				conditions.Equal(field.ColumnName, field.Value)
+			}
+		}
+
 		// 不是键字段
 		// 不是更新时间字段
 		// 不更新的字段或者字段为零值且不能清空,跳过

+ 15 - 0
framework/core/infrastructure/database/sql/conditions.go

@@ -17,6 +17,16 @@ func NewConditions() *Conditions {
 	}
 }
 
+func (conditions *Conditions) AddConditions(cond *Conditions) *Conditions {
+	if conditions.err != nil || cond.err != nil {
+		return conditions
+	}
+
+	conditions.queries = append(conditions.queries, cond.queries...)
+	conditions.args = append(conditions.args, cond.args...)
+	return conditions
+}
+
 // AddCondition 添加条件
 // 参数:
 // - query: 条件语句,如: name LIKE ? AND age = ?
@@ -24,10 +34,15 @@ func NewConditions() *Conditions {
 // 返回值:
 // - 数据库条件
 func (conditions *Conditions) AddCondition(query string, args ...any) *Conditions {
+	if conditions.err != nil {
+		return conditions
+	}
+
 	conditions.queries = append(conditions.queries, query)
 	for _, arg := range args {
 		conditions.args = append(conditions.args, []any{arg})
 	}
+
 	return conditions
 }