瀏覽代碼

完成修改

yjp 4 月之前
父節點
當前提交
c1f7734f25

+ 5 - 98
convenient/domain/auth/permission/entity.go

@@ -3,9 +3,6 @@ package permission
 import (
 	"git.sxidc.com/go-framework/baize/framework/core/domain"
 	"git.sxidc.com/go-framework/baize/framework/core/domain/entity"
-	"git.sxidc.com/go-framework/baize/framework/core/tag/check"
-	"git.sxidc.com/go-tools/utils/strutils"
-	"github.com/pkg/errors"
 )
 
 const (
@@ -31,10 +28,10 @@ var fieldMap = map[string]string{
 
 type Entity struct {
 	entity.Base
-	Name        string `sqlmapping:"column:name" sqlresult:"column:name" check:"required,lte=256"`
+	Name        string `sqlmapping:"column:name" sqlresult:"column:name" check:"required,lte=256,when=create/update"`
 	Description string `sqlmapping:"column:description;updateClear" sqlresult:"column:description"`
-	Resource    string `sqlmapping:"column:resource" sqlresult:"column:resource" check:"required"`
-	Action      string `sqlmapping:"column:action" sqlresult:"column:action" check:"required,lte=128"`
+	Resource    string `sqlmapping:"column:resource" sqlresult:"column:resource" check:"required,when=create/update"`
+	Action      string `sqlmapping:"column:action" sqlresult:"column:action" check:"required,lte=128,when=create/update"`
 
 	PermissionGroupIDs []string `sqlmapping:"-" sqlresult:"-"`
 	RoleIDs            []string `sqlmapping:"-" sqlresult:"-"`
@@ -51,96 +48,6 @@ func (e *Entity) DomainCamelName() string {
 	return "Permission"
 }
 
-func (e *Entity) ForCreate() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, e.DomainCNName(), FieldName)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, e.DomainCNName(), FieldResource)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, e.DomainCNName(), FieldAction)
-	if err != nil {
-		return err
-	}
-
-	err = entity.CheckFieldCreateUserIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	e.LastUpdateUserID = e.CreateUserID
-
-	return nil
-}
-
-func (e *Entity) ForDelete() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func (e *Entity) ForUpdate() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	err = entity.CheckFieldLastUpdateUserIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	err = e.checkUpdateFields(checkResult)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func (e *Entity) checkUpdateFields(checkResult check.Result) error {
-	if strutils.AllBlank(e.Name, e.Resource, e.Action) {
-		return errors.New(e.DomainCNName() + "没有传递需要更新的字段")
-	}
-
-	if strutils.IsStringNotEmpty(e.Name) {
-		err := domain.CheckField(checkResult, e.DomainCNName(), FieldName)
-		if err != nil {
-			return err
-		}
-	}
-
-	if strutils.IsStringNotEmpty(e.Resource) {
-		err := domain.CheckField(checkResult, e.DomainCNName(), FieldResource)
-		if err != nil {
-			return err
-		}
-	}
-
-	if strutils.IsStringNotEmpty(e.Action) {
-		err := domain.CheckField(checkResult, e.DomainCNName(), FieldAction)
-		if err != nil {
-			return err
-		}
-	}
-
-	return nil
+func (e *Entity) GetFieldMap() map[string]string {
+	return fieldMap
 }

+ 3 - 72
convenient/domain/auth/permission_group/entity.go

@@ -3,9 +3,6 @@ package permission_group
 import (
 	"git.sxidc.com/go-framework/baize/framework/core/domain"
 	"git.sxidc.com/go-framework/baize/framework/core/domain/entity"
-	"git.sxidc.com/go-framework/baize/framework/core/tag/check"
-	"git.sxidc.com/go-tools/utils/strutils"
-	"github.com/pkg/errors"
 )
 
 const (
@@ -25,7 +22,7 @@ var fieldMap = map[string]string{
 
 type Entity struct {
 	entity.Base
-	Name        string `sqlmapping:"column:name" sqlresult:"column:name" check:"required,lte=256"`
+	Name        string `sqlmapping:"column:name" sqlresult:"column:name" check:"required,lte=256,when=create/update"`
 	Description string `sqlmapping:"column:description;updateClear" sqlresult:"column:description"`
 
 	PermissionIDs []string `sqlmapping:"-" sqlresult:"-"`
@@ -42,72 +39,6 @@ func (e *Entity) DomainCamelName() string {
 	return "PermissionGroup"
 }
 
-func (e *Entity) ForCreate() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, e.DomainCNName(), FieldName)
-	if err != nil {
-		return err
-	}
-
-	err = entity.CheckFieldCreateUserIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	e.LastUpdateUserID = e.CreateUserID
-
-	return nil
-}
-
-func (e *Entity) ForDelete() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func (e *Entity) ForUpdate() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	err = entity.CheckFieldLastUpdateUserIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	err = e.checkUpdateFields(checkResult)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func (e *Entity) checkUpdateFields(checkResult check.Result) error {
-	if strutils.AllBlank(e.Name) {
-		return errors.New(e.DomainCNName() + "没有传递需要更新的字段")
-	}
-
-	if strutils.IsStringNotEmpty(e.Name) {
-		err := domain.CheckField(checkResult, e.DomainCNName(), FieldName)
-		if err != nil {
-			return err
-		}
-	}
-
-	return nil
+func (e *Entity) GetFieldMap() map[string]string {
+	return fieldMap
 }

+ 3 - 72
convenient/domain/auth/role/entity.go

@@ -3,9 +3,6 @@ package role
 import (
 	"git.sxidc.com/go-framework/baize/framework/core/domain"
 	"git.sxidc.com/go-framework/baize/framework/core/domain/entity"
-	"git.sxidc.com/go-framework/baize/framework/core/tag/check"
-	"git.sxidc.com/go-tools/utils/strutils"
-	"github.com/pkg/errors"
 )
 
 const (
@@ -25,7 +22,7 @@ var fieldMap = map[string]string{
 
 type Entity struct {
 	entity.Base
-	Name        string `sqlmapping:"column:name" sqlresult:"column:name" check:"required,lte=256"`
+	Name        string `sqlmapping:"column:name" sqlresult:"column:name" check:"required,lte=256,when=create/update"`
 	Description string `sqlmapping:"column:description;updateClear" sqlresult:"column:description"`
 
 	UserIDs       []string `sqlmapping:"-" sqlresult:"-"`
@@ -43,72 +40,6 @@ func (e *Entity) DomainCamelName() string {
 	return "Role"
 }
 
-func (e *Entity) ForCreate() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, e.DomainCNName(), FieldName)
-	if err != nil {
-		return err
-	}
-
-	err = entity.CheckFieldCreateUserIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	e.LastUpdateUserID = e.CreateUserID
-
-	return nil
-}
-
-func (e *Entity) ForDelete() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func (e *Entity) ForUpdate() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	err = entity.CheckFieldLastUpdateUserIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	err = e.checkUpdateFields(checkResult)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func (e *Entity) checkUpdateFields(checkResult check.Result) error {
-	if strutils.AllBlank(e.Name) {
-		return errors.New(e.DomainCNName() + "没有传递需要更新的字段")
-	}
-
-	if strutils.IsStringNotEmpty(e.Name) {
-		err := domain.CheckField(checkResult, e.DomainCNName(), FieldName)
-		if err != nil {
-			return err
-		}
-	}
-
-	return nil
+func (e *Entity) GetFieldMap() map[string]string {
+	return fieldMap
 }

+ 12 - 66
convenient/domain/auth/user/api.go

@@ -8,9 +8,7 @@ import (
 	"git.sxidc.com/go-framework/baize/framework/core/domain"
 	"git.sxidc.com/go-framework/baize/framework/core/domain/entity"
 	"git.sxidc.com/go-framework/baize/framework/core/infrastructure"
-	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
 	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
-	"git.sxidc.com/go-framework/baize/framework/core/tag/check"
 	"git.sxidc.com/go-tools/utils/encoding"
 	"git.sxidc.com/go-tools/utils/strutils"
 	"github.com/pkg/errors"
@@ -39,15 +37,15 @@ func (simple *Simple) Bind(binder *binding.Binder) {
 		GetByIDQueryParams: &GetUserQueryParams{},
 	}, entity_crud.WithGlobalMiddlewares(simple.AuthMiddleware),
 		entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
-			Before: func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
+			Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
 				userEntity, err := domain.ToConcrete[*Entity](e)
 				if err != nil {
-					return err
+					return nil, err
 				}
 
 				encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey)
 				if err != nil {
-					return errors.New(err.Error())
+					return nil, errors.New(err.Error())
 				}
 
 				userEntity.Password = encryptedPassword
@@ -58,53 +56,31 @@ func (simple *Simple) Bind(binder *binding.Binder) {
 
 				encryptedPhone, err := encoding.AESEncrypt(userEntity.Phone, simple.AESKey)
 				if err != nil {
-					return errors.New(err.Error())
+					return nil, errors.New(err.Error())
 				}
 
 				userEntity.Phone = encryptedPhone
 
 				encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey)
 				if err != nil {
-					return errors.New(err.Error())
+					return nil, errors.New(err.Error())
 				}
 
 				userEntity.Email = encryptedEmail
 
-				checkResult := check.Struct(userEntity, fieldMap)
-
-				err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldPassword)
-				if err != nil {
-					return err
-				}
-
-				err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldName)
-				if err != nil {
-					return err
-				}
-
-				err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldPhone)
-				if err != nil {
-					return err
-				}
-
-				err = domain.CheckField(checkResult, userEntity.DomainCNName(), FieldEmail)
-				if err != nil {
-					return err
-				}
-
-				return nil
+				return nil, nil
 			},
 		}), entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{
-			Before: func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
+			Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
 				userEntity, err := domain.ToConcrete[*Entity](e)
 				if err != nil {
-					return err
+					return nil, err
 				}
 
 				if strutils.IsStringNotEmpty(userEntity.Password) {
 					encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey)
 					if err != nil {
-						return errors.New(err.Error())
+						return nil, errors.New(err.Error())
 					}
 
 					userEntity.Password = encryptedPassword
@@ -117,7 +93,7 @@ func (simple *Simple) Bind(binder *binding.Binder) {
 				if strutils.IsStringNotEmpty(userEntity.Phone) {
 					encryptedPhone, err := encoding.AESEncrypt(userEntity.Phone, simple.AESKey)
 					if err != nil {
-						return errors.New(err.Error())
+						return nil, errors.New(err.Error())
 					}
 
 					userEntity.Phone = encryptedPhone
@@ -126,43 +102,13 @@ func (simple *Simple) Bind(binder *binding.Binder) {
 				if strutils.IsStringNotEmpty(userEntity.Email) {
 					encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey)
 					if err != nil {
-						return errors.New(err.Error())
+						return nil, errors.New(err.Error())
 					}
 
 					userEntity.Email = encryptedEmail
 				}
 
-				checkResult := check.Struct(userEntity, fieldMap)
-
-				if strutils.IsStringNotEmpty(userEntity.Password) {
-					err := domain.CheckField(checkResult, e.DomainCNName(), FieldPassword)
-					if err != nil {
-						return err
-					}
-				}
-
-				if strutils.IsStringNotEmpty(userEntity.Name) {
-					err := domain.CheckField(checkResult, e.DomainCNName(), FieldName)
-					if err != nil {
-						return err
-					}
-				}
-
-				if strutils.IsStringNotEmpty(userEntity.Phone) {
-					err := domain.CheckField(checkResult, e.DomainCNName(), FieldPhone)
-					if err != nil {
-						return err
-					}
-				}
-
-				if strutils.IsStringNotEmpty(userEntity.Email) {
-					err := domain.CheckField(checkResult, e.DomainCNName(), FieldEmail)
-					if err != nil {
-						return err
-					}
-				}
-
-				return nil
+				return nil, nil
 			},
 		}), entity_crud.WithQueryConditionFieldCallback[Info](func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error) {
 			if value == nil {

+ 7 - 57
convenient/domain/auth/user/entity.go

@@ -3,9 +3,6 @@ package user
 import (
 	"git.sxidc.com/go-framework/baize/framework/core/domain"
 	"git.sxidc.com/go-framework/baize/framework/core/domain/entity"
-	"git.sxidc.com/go-framework/baize/framework/core/tag/check"
-	"git.sxidc.com/go-tools/utils/strutils"
-	"github.com/pkg/errors"
 	"time"
 )
 
@@ -41,11 +38,11 @@ var fieldMap = map[string]string{
 
 type Entity struct {
 	entity.Base
-	UserName      string    `sqlmapping:"column:user_name;notUpdate" sqlresult:"column:user_name" check:"required,lte=256"`
-	Password      string    `sqlmapping:"column:password" sqlresult:"column:password" check:"required,lte=256"`
-	Name          string    `sqlmapping:"column:name;updateClear" sqlresult:"column:name" check:"lte=256"`
-	Phone         string    `sqlmapping:"column:phone;updateClear" sqlresult:"column:phone" check:"lte=128"`
-	Email         string    `sqlmapping:"column:email;updateClear" sqlresult:"column:email" check:"lte=512"`
+	UserName      string    `sqlmapping:"column:user_name;notUpdate" sqlresult:"column:user_name" check:"required,lte=256,when=create"`
+	Password      string    `sqlmapping:"column:password" sqlresult:"column:password" check:"required,lte=256,when=create/update"`
+	Name          string    `sqlmapping:"column:name;updateClear" sqlresult:"column:name" check:"lte=256,when=create/update"`
+	Phone         string    `sqlmapping:"column:phone;updateClear" sqlresult:"column:phone" check:"lte=128,when=create/update"`
+	Email         string    `sqlmapping:"column:email;updateClear" sqlresult:"column:email" check:"lte=512,when=create/update"`
 	Token         string    `sqlmapping:"column:token;notUpdate" sqlresult:"column:token"`
 	LastLoginTime time.Time `sqlmapping:"column:last_login_time;notUpdate" sqlresult:"column:last_login_time" check:"required,timezero"`
 
@@ -62,53 +59,6 @@ func (e *Entity) DomainCamelName() string {
 	return "User"
 }
 
-func (e *Entity) ForCreate() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, e.DomainCNName(), FieldUserName)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func (e *Entity) ForDelete() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func (e *Entity) ForUpdate() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	err = e.checkUpdateFields()
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func (e *Entity) checkUpdateFields() error {
-	if strutils.AllBlank(e.Password) && e.LastLoginTime.IsZero() {
-		return errors.New(e.DomainCNName() + "没有传递需要更新的字段")
-	}
-
-	return nil
+func (e *Entity) GetFieldMap() map[string]string {
+	return fieldMap
 }

+ 5 - 39
convenient/domain/configuration/value_object.go

@@ -3,7 +3,6 @@ package configuration
 import (
 	"git.sxidc.com/go-framework/baize/framework/core/domain"
 	"git.sxidc.com/go-framework/baize/framework/core/domain/value_object"
-	"git.sxidc.com/go-framework/baize/framework/core/tag/check"
 )
 
 const (
@@ -26,9 +25,9 @@ var fieldMap = map[string]string{
 
 type ValueObject struct {
 	value_object.Base
-	Scope string `sqlmapping:"column:scope;key;notUpdate;" sqlresult:"column:scope;" check:"required,lte=256"`
-	Group string `sqlmapping:"column:group;key;notUpdate;" sqlresult:"column:group;" check:"required,lte=256"`
-	Value string `sqlmapping:"column:value;notUpdate;" sqlresult:"column:value;" check:"required,lte=256"`
+	Scope string `sqlmapping:"column:scope;key;notUpdate;" sqlresult:"column:scope;" check:"required,lte=256,when=create/delete"`
+	Group string `sqlmapping:"column:group;key;notUpdate;" sqlresult:"column:group;" check:"required,lte=256,when=create/delete"`
+	Value string `sqlmapping:"column:value;notUpdate;" sqlresult:"column:value;" check:"required,lte=256,when=create"`
 }
 
 func (valueObject *ValueObject) DomainCNName() string {
@@ -39,39 +38,6 @@ func (valueObject *ValueObject) DomainCamelName() string {
 	return "Configuration"
 }
 
-func (valueObject *ValueObject) CheckKeyFields() error {
-	checkResult := check.Struct(valueObject, fieldMap)
-
-	err := domain.CheckField(checkResult, valueObject.DomainCNName(), FieldScope)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, valueObject.DomainCNName(), FieldGroup)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func (valueObject *ValueObject) ForCreate() error {
-	checkResult := check.Struct(valueObject, fieldMap)
-
-	err := domain.CheckField(checkResult, valueObject.DomainCNName(), FieldScope)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, valueObject.DomainCNName(), FieldGroup)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, valueObject.DomainCNName(), FieldValue)
-	if err != nil {
-		return err
-	}
-
-	return nil
+func (valueObject *ValueObject) GetFieldMap() map[string]string {
+	return fieldMap
 }

+ 4 - 0
convenient/domain/operate_log/value_object.go

@@ -54,3 +54,7 @@ func (v *ValueObject) DomainCNName() string {
 func (v *ValueObject) DomainCamelName() string {
 	return "OperateLog"
 }
+
+func (v *ValueObject) GetFieldMap() map[string]string {
+	return fieldMap
+}

+ 14 - 1
convenient/domain/query_rule/rule/api.go

@@ -33,7 +33,20 @@ func (simple *Simple) Bind(binder *binding.Binder) {
 		},
 		entity_crud.WithDisableUpdate(),
 		entity_crud.WithDisableQuery[any](),
-		entity_crud.WithDisableGetByID[any]())
+		entity_crud.WithDisableGetByID[any](),
+		entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
+			Before: func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
+				ruleEntity, err := domain.ToConcrete[*Entity](e)
+				if err != nil {
+					return err
+				}
+
+				return ruleEntity.LintRule()
+			},
+			After:           nil,
+			OnSuccessReturn: nil,
+			OnErrorReturn:   nil,
+		}))
 
 	binding.PostBind(binder, &binding.SimpleBindItem[response.InfosData[Info]]{
 		Path:             "/queryRule/query",

+ 6 - 49
convenient/domain/query_rule/rule/entity.go

@@ -4,7 +4,6 @@ import (
 	"encoding/json"
 	"git.sxidc.com/go-framework/baize/framework/core/domain"
 	"git.sxidc.com/go-framework/baize/framework/core/domain/entity"
-	"git.sxidc.com/go-framework/baize/framework/core/tag/check"
 	"github.com/pkg/errors"
 )
 
@@ -30,9 +29,9 @@ var fieldMap = map[string]string{
 
 type Entity struct {
 	entity.Base
-	Scope      string `sqlmapping:"column:scope;notUpdate;" sqlresult:"column:scope;" check:"required,lte=256"`
-	DomainName string `sqlmapping:"column:domain_name;notUpdate;" sqlresult:"column:domain_name;" check:"required,lte=256"`
-	Rule       string `sqlmapping:"column:rule;notUpdate;" sqlresult:"column:rule;" check:"required"`
+	Scope      string `sqlmapping:"column:scope;notUpdate;" sqlresult:"column:scope;" check:"required,lte=256,when=create"`
+	DomainName string `sqlmapping:"column:domain_name;notUpdate;" sqlresult:"column:domain_name;" check:"required,lte=256,when=create"`
+	Rule       string `sqlmapping:"column:rule;notUpdate;" sqlresult:"column:rule;" check:"required,when=create"`
 	Enabled    bool   `sqlresult:"column:enabled;" sqlresult:"column:enabled;"`
 }
 
@@ -44,53 +43,11 @@ func (e *Entity) DomainCamelName() string {
 	return "QueryRule"
 }
 
-func (e *Entity) ForCreate() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, e.DomainCNName(), FieldScope)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, e.DomainCNName(), FieldDomainName)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, e.DomainCNName(), FieldRule)
-	if err != nil {
-		return err
-	}
-
-	err = e.lintRule()
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func (e *Entity) ForDelete() error {
-	checkResult := check.Struct(e, fieldMap)
-
-	err := entity.CheckFieldIDResult(checkResult)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func (e *Entity) ForUpdate() error {
-	return nil
+func (e *Entity) GetFieldMap() map[string]string {
+	return fieldMap
 }
 
-func (e *Entity) lintRule() error {
+func (e *Entity) LintRule() error {
 	r := new(Rule)
 
 	err := json.Unmarshal([]byte(e.Rule), r)

+ 1 - 1
convenient/domain/sql_executor/api.go

@@ -52,7 +52,7 @@ func (simple *Simple) bind(binder *binding.Binder) {
 					ExecutorName: jsonBody.ExecutorName,
 				}
 
-				err = sqlExecuteLog.ForCreate()
+				err = domain.CheckFieldsForCreate(sqlExecuteLog, sqlExecuteLog.GetFieldMap())
 				if err != nil {
 					return err
 				}

+ 5 - 25
convenient/domain/sql_executor/value_object.go

@@ -3,7 +3,6 @@ package sql_executor
 import (
 	"git.sxidc.com/go-framework/baize/framework/core/domain"
 	"git.sxidc.com/go-framework/baize/framework/core/domain/value_object"
-	"git.sxidc.com/go-framework/baize/framework/core/tag/check"
 	"time"
 )
 
@@ -30,9 +29,9 @@ var fieldMap = map[string]string{
 
 type SqlExecuteLog struct {
 	value_object.Base
-	Sql          string    `sqlmapping:"column:sql;" sqlresult:"column:sql;" check:"required"`
-	ExecutorID   string    `sqlmapping:"column:executor_id;" sqlresult:"column:executor_id;" check:"required,len=32"`
-	ExecutorName string    `sqlmapping:"column:executor_name;" sqlresult:"column:executor_name;" check:"required,lte=256"`
+	Sql          string    `sqlmapping:"column:sql;" sqlresult:"column:sql;" check:"required,when=create"`
+	ExecutorID   string    `sqlmapping:"column:executor_id;" sqlresult:"column:executor_id;" check:"required,len=32,when=create"`
+	ExecutorName string    `sqlmapping:"column:executor_name;" sqlresult:"column:executor_name;" check:"required,lte=256,when=create"`
 	ExecutedTime time.Time `sqlmapping:"column:executed_time;" sqlresult:"column:executed_time;"`
 }
 
@@ -44,25 +43,6 @@ func (sqlExecuteLog *SqlExecuteLog) DomainCamelName() string {
 	return "SqlExecuteLog"
 }
 
-func (sqlExecuteLog *SqlExecuteLog) ForCreate() error {
-	checkResult := check.Struct(sqlExecuteLog, fieldMap)
-
-	err := domain.CheckField(checkResult, sqlExecuteLog.DomainCNName(), FieldSql)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, sqlExecuteLog.DomainCNName(), FieldExecutorID)
-	if err != nil {
-		return err
-	}
-
-	err = domain.CheckField(checkResult, sqlExecuteLog.DomainCNName(), FieldExecutorName)
-	if err != nil {
-		return err
-	}
-
-	sqlExecuteLog.ExecutedTime = time.Now().Local()
-
-	return nil
+func (sqlExecuteLog *SqlExecuteLog) GetFieldMap() map[string]string {
+	return fieldMap
 }

+ 7 - 7
convenient/entity_crud/service.go

@@ -48,7 +48,7 @@ func Create(tableName string, needCreateUserID bool, callbacks *CreateCallbacks)
 			return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
 
-		err = e.ForCreate()
+		err = domain.CheckFieldsForCreate(e, e.GetFieldMap())
 		if err != nil {
 			return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
@@ -95,7 +95,7 @@ func Delete(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[an
 			return nil, callbackOnDeleteErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
 
-		err = e.ForDelete()
+		err = domain.CheckFieldsForDelete(e, e.GetFieldMap())
 		if err != nil {
 			return nil, callbackOnDeleteErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
@@ -159,7 +159,7 @@ func Update(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallba
 			return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
 
-		err = e.ForUpdate()
+		err = domain.CheckFieldsForUpdate(e, e.GetFieldMap())
 		if err != nil {
 			return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
@@ -289,7 +289,7 @@ func GetByID[O any](tableName string, callbacks *GetByIDCallbacks[O]) binding.Se
 			return outputZero, errors.New("需要传递领域对象应该为实体")
 		}
 
-		err := entity.CheckFieldID(e)
+		err := domain.CheckField(e, entity.FieldID, e.GetFieldMap())
 		if err != nil {
 			return callbackOnGetByIDErrorReturn(callbacks, c, e, err, i)
 		}
@@ -358,7 +358,7 @@ func CreateTx(tableName string, needCreateUserID bool, callbacks *CreateCallback
 			return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
 
-		err = e.ForCreate()
+		err = domain.CheckFieldsForCreate(e, e.GetFieldMap())
 		if err != nil {
 			return callbackOnCreateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
@@ -412,7 +412,7 @@ func DeleteTx(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[
 			return nil, callbackOnDeleteErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
 
-		err = e.ForDelete()
+		err = domain.CheckFieldsForDelete(e, e.GetFieldMap())
 		if err != nil {
 			return nil, callbackOnDeleteErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
@@ -482,7 +482,7 @@ func UpdateTx(tableName string, needLastUpdateUserID bool, callbacks *UpdateCall
 			return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}
 
-		err = e.ForUpdate()
+		err = domain.CheckFieldsForUpdate(e, e.GetFieldMap())
 		if err != nil {
 			return nil, callbackOnUpdateErrorReturn(callbacks, c, e, make(map[string]any), err, i)
 		}

+ 1 - 1
convenient/relation/many2many/service.go

@@ -31,7 +31,7 @@ func Update(middleTableName string,
 		}
 
 		// 字段校验
-		err := entity.CheckFieldID(fromEntity)
+		err := domain.CheckField(fromEntity, entity.FieldID, fromEntity.GetFieldMap())
 		if err != nil {
 			return nil, err
 		}

+ 2 - 2
convenient/relation/one2many/service.go

@@ -34,7 +34,7 @@ func UpdateLeft(leftTableName string, leftDomainCNName string, leftRelationField
 		}
 
 		// 字段校验
-		err := entity.CheckFieldID(leftEntity)
+		err := domain.CheckField(leftEntity, entity.FieldID, leftEntity.GetFieldMap())
 		if err != nil {
 			return nil, err
 		}
@@ -196,7 +196,7 @@ func UpdateRight(rightTableName string, rightRelationFieldName string,
 		}
 
 		// 字段校验
-		err := entity.CheckFieldID(rightEntity)
+		err := domain.CheckField(rightEntity, entity.FieldID, rightEntity.GetFieldMap())
 		if err != nil {
 			return nil, err
 		}

+ 1 - 1
convenient/relation/one2one/service.go

@@ -33,7 +33,7 @@ func Update(fromTableName string, fromRelationFieldName string, fromRelationColu
 		}
 
 		// 字段校验
-		err := entity.CheckFieldID(fromEntity)
+		err := domain.CheckField(fromEntity, entity.FieldID, fromEntity.GetFieldMap())
 		if err != nil {
 			return nil, err
 		}

+ 1 - 1
convenient/relation/remote/service.go

@@ -31,7 +31,7 @@ func Update(middleTableName string,
 		}
 
 		// 字段校验
-		err := entity.CheckFieldID(fromEntity)
+		err := domain.CheckField(fromEntity, entity.FieldID, fromEntity.GetFieldMap())
 		if err != nil {
 			return nil, err
 		}

+ 4 - 4
convenient/value_object_crud/service.go

@@ -34,7 +34,7 @@ func Create(tableName string, callbacks *CreateCallbacks) binding.ServiceFunc[an
 			return nil, callbackOnCreateErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
 		}
 
-		err = valueObject.ForCreate()
+		err = domain.CheckFieldsForCreate(valueObject, valueObject.GetFieldMap())
 		if err != nil {
 			return nil, callbackOnCreateErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
 		}
@@ -81,7 +81,7 @@ func Delete(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[an
 			return nil, callbackOnDeleteErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
 		}
 
-		err = valueObject.CheckKeyFields()
+		err = domain.CheckFieldsForDelete(valueObject, valueObject.GetFieldMap())
 		if err != nil {
 			return nil, callbackOnDeleteErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
 		}
@@ -214,7 +214,7 @@ func CreateTx(tableName string, callbacks *CreateCallbacks) binding.ServiceFunc[
 			return nil, callbackOnCreateErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
 		}
 
-		err = valueObject.ForCreate()
+		err = domain.CheckFieldsForCreate(valueObject, valueObject.GetFieldMap())
 		if err != nil {
 			return nil, callbackOnCreateErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
 		}
@@ -268,7 +268,7 @@ func DeleteTx(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[
 			return nil, callbackOnDeleteErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
 		}
 
-		err = valueObject.CheckKeyFields()
+		err = domain.CheckFieldsForDelete(valueObject, valueObject.GetFieldMap())
 		if err != nil {
 			return nil, callbackOnDeleteErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
 		}

+ 1 - 1
framework/core/domain/entity/base.go

@@ -5,7 +5,7 @@ import (
 )
 
 type Base struct {
-	ID string `sqlmapping:"column:id;key;" sqlresult:"column:id;" check:"required,len=32"`
+	ID string `sqlmapping:"column:id;key;" sqlresult:"column:id;" check:"required,len=32,when=create/delete/update"`
 }
 
 func (e *Base) GenerateID() error {

+ 0 - 9
framework/core/domain/entity/entity.go

@@ -13,13 +13,4 @@ type Entity interface {
 
 	// GetID 获取ID
 	GetID() string
-
-	// ForCreate 创建准备方法(规则校验)
-	ForCreate() error
-
-	// ForDelete 删除准备方法(规则校验)
-	ForDelete() error
-
-	// ForUpdate 更新准备方法(规则校验)
-	ForUpdate() error
 }

+ 5 - 136
framework/core/domain/entity/field.go

@@ -1,7 +1,6 @@
 package entity
 
 import (
-	"git.sxidc.com/go-framework/baize/framework/core/tag/check"
 	"git.sxidc.com/go-tools/utils/strutils"
 	"github.com/pkg/errors"
 	"time"
@@ -65,19 +64,19 @@ const (
 
 // TenantIDField 租户ID字段
 type TenantIDField struct {
-	TenantID string `sqlmapping:"column:tenant_id;" sqlresult:"column:tenant_id;" check:"required,len=32"`
+	TenantID string `sqlmapping:"column:tenant_id;" sqlresult:"column:tenant_id;" check:"required,len=32,when=create/delete/update"`
 }
 
 // UserIDFields 用户ID相关字段
 type UserIDFields struct {
-	CreateUserID     string `sqlmapping:"column:create_user_id;" sqlresult:"column:create_user_id;" check:"required,len=32"`
-	LastUpdateUserID string `sqlmapping:"column:last_update_user_id;" sqlresult:"column:last_update_user_id;" check:"required,len=32"`
-	DeleteUserID     string `sqlmapping:"-" sqlresult:"-" check:"required,len=32"`
+	CreateUserID     string `sqlmapping:"column:create_user_id;" sqlresult:"column:create_user_id;" check:"required,len=32,when=create"`
+	LastUpdateUserID string `sqlmapping:"column:last_update_user_id;" sqlresult:"column:last_update_user_id;" check:"required,len=32,when=update"`
+	DeleteUserID     string `sqlmapping:"-" sqlresult:"-" check:"required,len=32,when=delete"`
 }
 
 // OperatorUserNameField 操作者用户名
 type OperatorUserNameField struct {
-	OperatorUserName string `sqlmapping:"-" sqlresult:"-" check:"required"`
+	OperatorUserName string `sqlmapping:"-" sqlresult:"-" check:"required,when=create/delete/update"`
 }
 
 // TimeFields 时间相关字段
@@ -86,114 +85,6 @@ type TimeFields struct {
 	LastUpdatedTime time.Time `sqlmapping:"column:last_updated_time;" sqlresult:"column:last_updated_time;"`
 }
 
-// CheckFieldID 校验领域实体的ID字段是否合规
-// 参数:
-// - entity: 领域实体
-// 返回值:
-// - 错误
-func CheckFieldID(entity Entity) error {
-	return checkField(entity, FieldID, "ID")
-}
-
-// CheckFieldTenantID 校验领域实体的租户ID字段是否合规
-// 参数:
-// - entity: 领域实体
-// 返回值:
-// - 错误
-func CheckFieldTenantID(entity Entity) error {
-	return checkField(entity, FieldTenantID, "租户ID")
-}
-
-// CheckFieldCreateUserID 校验领域实体的创建用户ID字段是否合规
-// 参数:
-// - entity: 领域实体
-// 返回值:
-// - 错误
-func CheckFieldCreateUserID(entity Entity) error {
-	return checkField(entity, FieldCreateUserID, "创建用户ID")
-}
-
-// CheckFieldLastUpdateUserID 校验领域实体的最近更新用户ID字段是否合规
-// 参数:
-// - entity: 领域实体
-// 返回值:
-// - 错误
-func CheckFieldLastUpdateUserID(entity Entity) error {
-	return checkField(entity, FieldLastUpdateUserID, "最近更新用户ID")
-}
-
-// CheckFieldDeleteUserID 校验领域实体的删除用户ID字段是否合规
-// 参数:
-// - entity: 领域实体
-// 返回值:
-// - 错误
-func CheckFieldDeleteUserID(entity Entity) error {
-	return checkField(entity, FieldDeleteUserID, "删除用户ID")
-}
-
-// CheckFieldIDResult 利用check.Struct函数返回的结果校验领域实体的ID字段是否合规
-// 参数:
-// - checkResult: check.Struct函数返回的结果
-// 返回值:
-// - 错误
-func CheckFieldIDResult(checkResult check.Result) error {
-	return checkUseResult(checkResult, FieldID, "ID")
-}
-
-// CheckFieldOperatorUserName 校验领域实体的操作者用户ID字段是否合规
-// 参数:
-// - entity: 领域实体
-// 返回值:
-// - 错误
-func CheckFieldOperatorUserName(entity Entity) error {
-	return checkField(entity, FieldOperatorUserName, "操作者用户ID")
-}
-
-// CheckFieldTenantIDResult 利用check.Struct函数返回的结果校验领域实体的租户ID字段是否合规
-// 参数:
-// - checkResult: check.Struct函数返回的结果
-// 返回值:
-// - 错误
-func CheckFieldTenantIDResult(checkResult check.Result) error {
-	return checkUseResult(checkResult, FieldTenantID, "租户ID")
-}
-
-// CheckFieldCreateUserIDResult 利用check.Struct函数返回的结果校验领域实体的创建用户ID字段是否合规
-// 参数:
-// - checkResult: check.Struct函数返回的结果
-// 返回值:
-// - 错误
-func CheckFieldCreateUserIDResult(checkResult check.Result) error {
-	return checkUseResult(checkResult, FieldCreateUserID, "创建用户ID")
-}
-
-// CheckFieldLastUpdateUserIDResult 利用check.Struct函数返回的结果校验领域实体的最近更新用户ID字段是否合规
-// 参数:
-// - checkResult: check.Struct函数返回的结果
-// 返回值:
-// - 错误
-func CheckFieldLastUpdateUserIDResult(checkResult check.Result) error {
-	return checkUseResult(checkResult, FieldLastUpdateUserID, "最近更新用户ID")
-}
-
-// CheckFieldDeleteUserIDResult 利用check.Struct函数返回的结果校验领域实体的删除用户ID字段是否合规
-// 参数:
-// - checkResult: check.Struct函数返回的结果
-// 返回值:
-// - 错误
-func CheckFieldDeleteUserIDResult(checkResult check.Result) error {
-	return checkUseResult(checkResult, FieldDeleteUserID, "删除用户ID")
-}
-
-// CheckFieldOperatorUserNameResult 利用check.Struct函数返回的结果校验领域实体的操作者用户ID字段是否合规
-// 参数:
-// - entity: 领域实体
-// 返回值:
-// - 错误
-func CheckFieldOperatorUserNameResult(checkResult check.Result) error {
-	return checkUseResult(checkResult, FieldOperatorUserName, "操作者用户ID")
-}
-
 // CheckIDTypeValue 校验ID类型的字段,如ID,租户ID,用户ID等,特点是字符串类型,不能为空且长度严格为32字节
 // 参数:
 // - domainCNName: 领域中文名,可以使用DomainCNName()方法获得
@@ -212,25 +103,3 @@ func CheckIDTypeValue(domainCNName string, fieldCNName string, id string) error
 
 	return nil
 }
-
-func checkField(entity Entity, fieldName string, fieldCNName string) error {
-	checkResult := check.Struct(entity, map[string]string{
-		fieldName: entity.DomainCNName() + fieldCNName,
-	})
-
-	err := checkResult.CheckFields(fieldName)
-	if err != nil {
-		return errors.New(entity.DomainCNName() + ": " + err.Error())
-	}
-
-	return nil
-}
-
-func checkUseResult(checkResult check.Result, domainCNName string, fieldName string) error {
-	err := checkResult.CheckFields(fieldName)
-	if err != nil {
-		return errors.New(domainCNName + ": " + err.Error())
-	}
-
-	return nil
-}

+ 153 - 13
framework/core/domain/object.go

@@ -1,6 +1,7 @@
 package domain
 
 import (
+	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
 	"git.sxidc.com/go-framework/baize/framework/core/tag/check"
 	"git.sxidc.com/go-tools/utils/reflectutils"
 	"git.sxidc.com/go-tools/utils/strutils"
@@ -10,6 +11,12 @@ import (
 	"reflect"
 )
 
+const (
+	WhenCreate = "create"
+	WhenDelete = "delete"
+	WhenUpdate = "update"
+)
+
 // Object 领域对象接口
 type Object interface {
 	// DomainCNName 返回领域的中文名称
@@ -17,6 +24,9 @@ type Object interface {
 
 	// DomainCamelName 返回领域的大写驼峰式名称
 	DomainCamelName() string
+
+	// 获取字段解释map
+	GetFieldMap() map[string]string
 }
 
 // HasField 校验领域对象中是否有某一个字段
@@ -29,33 +39,163 @@ func HasField(object Object, fieldName string) bool {
 	return hasField(object, fieldName)
 }
 
-// CheckField 校验领域对象某一字段的字段校验结果
+// CheckFieldsForCreate 为创建校验领域对象字段
+// 参数:
+// - object: 领域对象
+// - fieldMap: 字段说明map
+// 返回值:
+// - 错误
+func CheckFieldsForCreate(object Object, fieldMap map[string]string) error {
+	return CheckWhen(object, fieldMap, WhenCreate)
+}
+
+// CheckFieldsForDelete 为删除校验领域对象字段
+//
+// 参数:
+// - object: 领域对象
+// - fieldMap: 字段说明map
+// 返回值:
+// - 错误
+func CheckFieldsForDelete(object Object, fieldMap map[string]string) error {
+	return CheckWhen(object, fieldMap, WhenDelete)
+}
+
+// CheckFieldsForUpdate 为更新校验领域对象字段
+//
+// 参数:
+// - object: 领域对象
+// - fieldMap: 字段说明map
+// 返回值:
+// - 错误
+func CheckFieldsForUpdate(object Object, fieldMap map[string]string) error {
+	return CheckFieldsWhen(object, fieldMap, WhenUpdate, func(fieldName string) bool {
+		fieldValue, err := getFieldValue(object, fieldName)
+		if err != nil {
+			logger.GetInstance().Error(err)
+			return false
+		}
+
+		if !fieldValue.IsValid() {
+			return false
+		}
+
+		if fieldValue.IsZero() {
+			return false
+		}
+
+		if fieldValue.IsNil() {
+			return false
+		}
+
+		return true
+	})
+}
+
+// Check 校验领域对象
+// 参数:
+// - object: 领域对象
+// - fieldMap: 字段说明map
+// - when: 何时校验
+// 返回值:
+// - 错误
+func Check(object Object, fieldMap map[string]string) error {
+	err := check.Struct(object, fieldMap).CheckWhen("")
+	if err != nil {
+		return errors.New(object.DomainCNName() + ":\n" + err.Error())
+	}
+
+	return nil
+}
+
+// CheckField 校验领域对象字段
+// 参数:
+// - object: 领域对象
+// - fieldName: 字段名
+// - fieldMap: 字段说明map
+// 返回值:
+// - 错误
+func CheckField(object Object, fieldName string, fieldMap map[string]string) error {
+	err := check.Struct(object, fieldMap).CheckFieldWhen("", func(fieldName string) bool {
+		if fieldName == fieldName {
+			return true
+		}
+
+		return false
+	})
+	if err != nil {
+		return errors.New(object.DomainCNName() + ":\n" + err.Error())
+	}
+
+	return nil
+}
+
+// CheckFields 校验领域对象字段
+// 参数:
+// - object: 领域对象
+// - fieldMap: 字段说明map
+// - checkFunc: 检查函数,返回true检查该字段,返回false不检查该字段
+// 返回值:
+// - 错误
+func CheckFields(object Object, fieldMap map[string]string, checkFunc func(fieldName string) bool) error {
+	err := check.Struct(object, fieldMap).CheckFieldWhen("", checkFunc)
+	if err != nil {
+		return errors.New(object.DomainCNName() + ":\n" + err.Error())
+	}
+
+	return nil
+}
+
+// CheckWhen 校验领域对象
 // 参数:
-// - result: 使用check.Struct返回的字段校验结果
-// - domainCNName: 领域中文名,可以使用DomainCNName()方法的返回值
+// - object: 领域对象
+// - fieldMap: 字段说明map
+// - when: 何时校验
+// 返回值:
+// - 错误
+func CheckWhen(object Object, fieldMap map[string]string, when string) error {
+	err := check.Struct(object, fieldMap).CheckWhen(when)
+	if err != nil {
+		return errors.New(object.DomainCNName() + ":\n" + err.Error())
+	}
+
+	return nil
+}
+
+// CheckFieldWhen 校验领域对象字段
+// 参数:
+// - object: 领域对象
 // - fieldName: 字段名
+// - fieldMap: 字段说明map
+// - when: 何时校验
 // 返回值:
 // - 错误
-func CheckField(result check.Result, domainCNName string, fieldName string) error {
-	err := result.CheckFields(fieldName)
+func CheckFieldWhen(object Object, fieldName string, fieldMap map[string]string, when string) error {
+	err := check.Struct(object, fieldMap).CheckFieldWhen(when, func(fieldName string) bool {
+		if fieldName == fieldName {
+			return true
+		}
+
+		return false
+	})
 	if err != nil {
-		return errors.New(domainCNName + ": " + err.Error())
+		return errors.New(object.DomainCNName() + ":\n" + err.Error())
 	}
 
 	return nil
 }
 
-// CheckFields 校验领域对象多个字段的字段校验结果
+// CheckFieldsWhen 校验领域对象字段
 // 参数:
-// - result: 使用check.Struct返回的字段校验结果
-// - domainCNName: 领域中文名,可以使用DomainCNName()方法的返回值
-// - fieldNames: 多个字段名
+// - object: 领域对象
+// - fieldMap: 字段说明map
+// - when: 何时校验
+// - checkFunc: 检查函数,返回true检查该字段,返回false不检查该字段
 // 返回值:
 // - 错误
-func CheckFields(result check.Result, domainCNName string, fieldNames []string) error {
-	err := result.CheckFields(fieldNames...)
+func CheckFieldsWhen(object Object, fieldMap map[string]string, when string, checkFunc func(fieldName string) bool) error {
+	err := check.Struct(object, fieldMap).CheckFieldWhen(when, checkFunc)
 	if err != nil {
-		return errors.New(domainCNName + ":\n" + err.Error())
+		return errors.New(object.DomainCNName() + ":\n" + err.Error())
 	}
 
 	return nil

+ 0 - 6
framework/core/domain/value_object/value_object.go

@@ -5,10 +5,4 @@ import "git.sxidc.com/go-framework/baize/framework/core/domain"
 // ValueObject 值对象接口
 type ValueObject interface {
 	domain.Object
-
-	// CheckKeyFields 校验关键字段,关键字段值能够唯一标识一个值对象的联合主键字段
-	CheckKeyFields() error
-
-	// ForCreate 创建准备方法(规则校验)
-	ForCreate() error
 }

+ 30 - 102
framework/core/tag/check/validate.go

@@ -9,7 +9,6 @@ import (
 	"github.com/go-playground/validator/v10"
 	zhTranslations "github.com/go-playground/validator/v10/translations/zh"
 	"github.com/pkg/errors"
-	"reflect"
 	"strings"
 	"sync"
 	"time"
@@ -24,7 +23,7 @@ var validate = validator.New(validator.WithRequiredStructEnabled())
 var translator ut.Translator
 
 var whenMapMutex = sync.RWMutex{}
-var whenMap = make(map[string]map[string][]string)
+var whenMap = make(map[string][]string)
 
 func init() {
 	validate.SetTagName("check")
@@ -72,20 +71,15 @@ func registerCustomTags() {
 		whenMapMutex.Lock()
 		defer whenMapMutex.Unlock()
 
-		topStructName := reflectutils.PointerValueElem(fl.Top()).Type().String()
-		topStructMap, ok := whenMap[topStructName]
-		if !ok {
-			topStructMap = make(map[string][]string)
-			whenMap[topStructName] = topStructMap
-		}
-
 		param := strings.Trim(fl.Param(), "/")
 
 		if strutils.IsStringEmpty(param) {
 			return true
 		}
 
-		topStructMap[fl.FieldName()] = strings.Split(param, "/")
+		structName := reflectutils.PointerValueElem(fl.Top()).Type().Name()
+		structFieldName := structName + "." + fl.StructFieldName()
+		whenMap[structFieldName] = strings.Split(param, "/")
 
 		return true
 	}); err != nil {
@@ -95,29 +89,20 @@ func registerCustomTags() {
 }
 
 func Struct(obj any, fieldNameMap map[string]string) Result {
-	validateErr := validate.Struct(obj)
-
-	whenMapMutex.RLock()
-	typeName := reflectutils.PointerTypeElem(reflect.TypeOf(obj)).String()
-	whenFieldMap := whenMap[typeName]
-	whenMapMutex.RUnlock()
-
-	return newResult(validateErr, nil, fieldNameMap, whenFieldMap)
+	return newResult(validate.Struct(obj), nil, fieldNameMap)
 }
 
 type Result struct {
 	err            error
 	translatedErrs map[string]string
 	fieldNameMap   map[string]string
-	whenFieldMap   map[string][]string
 }
 
-func newResult(err error, translatedErrs map[string]string, fieldNameMap map[string]string, whenFieldMap map[string][]string) Result {
+func newResult(err error, translatedErrs map[string]string, fieldNameMap map[string]string) Result {
 	return Result{
 		err:            err,
 		translatedErrs: translatedErrs,
 		fieldNameMap:   fieldNameMap,
-		whenFieldMap:   whenFieldMap,
 	}
 }
 
@@ -125,31 +110,11 @@ func (result Result) IsError() bool {
 	return result.err != nil
 }
 
-func (result Result) CheckFields(fieldNames ...string) error {
-	if len(fieldNames) == 0 {
-		return nil
-	}
-
-	return result.checkFields("", fieldNames...)
-}
-
-func (result Result) CheckStruct() error {
-	return result.checkStruct("")
+func (result Result) CheckWhen(when string) error {
+	return result.CheckFieldWhen(when, nil)
 }
 
-func (result Result) CheckFieldsWhen(when string, fieldNames ...string) error {
-	if len(fieldNames) == 0 {
-		return nil
-	}
-
-	return result.checkFields(when, fieldNames...)
-}
-
-func (result Result) CheckStructWhen(when string) error {
-	return result.checkStruct(when)
-}
-
-func (result Result) checkFields(when string, fieldNames ...string) error {
+func (result Result) CheckFieldWhen(when string, check func(fieldName string) bool) error {
 	if result.err == nil {
 		return nil
 	}
@@ -160,74 +125,37 @@ func (result Result) checkFields(when string, fieldNames ...string) error {
 		return translatedResult.err
 	}
 
-	errMsg := strings.Builder{}
-
-	for _, fieldName := range fieldNames {
-		for errStructFieldName, translatedErr := range translatedResult.translatedErrs {
-			errStructFieldNameParts := strings.Split(errStructFieldName, ".")
-			errFieldName := errStructFieldNameParts[len(errStructFieldNameParts)-1]
-			if fieldName == errFieldName {
-				if strutils.IsStringNotEmpty(when) && result.whenFieldMap != nil && len(result.whenFieldMap) != 0 {
-					find := false
-					for _, fieldWhen := range result.whenFieldMap[fieldName] {
-						if fieldWhen == when {
-							find = true
-							break
-						}
-					}
-
-					if !find {
-						continue
-					}
-				}
+	for errStructNamespace, translatedErr := range translatedResult.translatedErrs {
+		whenMapMutex.RLock()
+		whens := whenMap[errStructNamespace]
+		whenMapMutex.RUnlock()
 
-				if translatedResult.fieldNameMap != nil {
-					fieldCNName, ok := translatedResult.fieldNameMap[fieldName]
-					if ok {
-						translatedErr = strings.ReplaceAll(translatedErr, fieldName, fieldCNName)
-					}
+		if strutils.IsStringNotEmpty(when) {
+			find := false
+			for _, fieldWhen := range whens {
+				if fieldWhen == when {
+					find = true
+					break
 				}
+			}
 
-				errMsg.WriteString(translatedErr + "\n")
+			if !find {
+				continue
 			}
 		}
-	}
 
-	if errMsg.Len() > 0 {
-		return errors.New(errMsg.String())
-	}
-
-	return nil
-}
-
-func (result Result) checkStruct(when string) error {
-	if result.err == nil {
-		return nil
-	}
-
-	translatedResult := result.translation()
-
-	if translatedResult.translatedErrs == nil || len(translatedResult.translatedErrs) == 0 {
-		return translatedResult.err
-	}
-
-	errMsg := strings.Builder{}
+		if check != nil && !check(strings.Split(errStructNamespace, ".")[1]) {
+			continue
+		}
 
-	for errStructFieldName, translatedErr := range translatedResult.translatedErrs {
 		if translatedResult.fieldNameMap != nil {
-			errStructFieldNameParts := strings.Split(errStructFieldName, ".")
-			errFieldName := errStructFieldNameParts[len(errStructFieldNameParts)-1]
-			fieldCNName, ok := translatedResult.fieldNameMap[errFieldName]
+			fieldCNName, ok := translatedResult.fieldNameMap[errStructNamespace]
 			if ok {
-				translatedErr = strings.ReplaceAll(translatedErr, errFieldName, fieldCNName)
+				translatedErr = strings.ReplaceAll(translatedErr, errStructNamespace, fieldCNName)
 			}
 		}
 
-		errMsg.WriteString(translatedErr + "\n")
-	}
-
-	if errMsg.Len() > 0 {
-		return errors.New(errMsg.String())
+		return errors.New(translatedErr)
 	}
 
 	return nil
@@ -251,9 +179,9 @@ func (result Result) translation() Result {
 
 	for _, validationError := range validationErrors {
 		if validationError.Tag() == timeNotZeroTag {
-			translatedErrors[validationError.Field()] = validationError.Field() + "使用了时间零值"
+			translatedErrors[validationError.StructNamespace()] = validationError.Field() + "使用了时间零值"
 		}
 	}
 
-	return newResult(result.err, translatedErrors, result.fieldNameMap, result.whenFieldMap)
+	return newResult(result.err, translatedErrors, result.fieldNameMap)
 }

+ 8 - 56
test/check_tag_test.go

@@ -35,97 +35,49 @@ func TestCustomCheckTag(t *testing.T) {
 	customCheckTagPointerStructZero := CustomCheckTagPointerStruct{}
 
 	err := check.Struct(customCheckTagStruct, fieldMap).
-		CheckFields("Time")
+		CheckWhen("create")
 	if err != nil {
 		t.Fatalf("%+v\n", err)
 	}
 
 	err = check.Struct(customCheckTagPointerStruct, fieldMap).
-		CheckFields("Time")
+		CheckWhen("create")
 	if err != nil {
 		t.Fatalf("%+v\n", err)
 	}
 
 	err = check.Struct(&customCheckTagStruct, fieldMap).
-		CheckFields("Time")
+		CheckWhen("delete")
 	if err != nil {
 		t.Fatalf("%+v\n", err)
 	}
 
 	err = check.Struct(&customCheckTagPointerStruct, fieldMap).
-		CheckFields("Time")
+		CheckWhen("delete")
 	if err != nil {
 		t.Fatalf("%+v\n", err)
 	}
 
 	err = check.Struct(customCheckTagStructZero, fieldMap).
-		CheckFields("Time")
+		CheckWhen("update")
 	if err == nil || !strings.Contains(err.Error(), "使用了时间零值") {
 		t.Fatalf("%+v\n", errors.New("没有检测出使用了时间零值"))
 	}
 
 	err = check.Struct(customCheckTagPointerStructZero, fieldMap).
-		CheckFields("Time")
+		CheckWhen("update")
 	if err == nil || !strings.Contains(err.Error(), "使用了时间零值") {
 		t.Fatalf("%+v\n", errors.New("没有检测出使用了时间零值"))
 	}
 
 	err = check.Struct(&customCheckTagStructZero, fieldMap).
-		CheckFields("Time")
+		CheckWhen("foo")
 	if err == nil || !strings.Contains(err.Error(), "使用了时间零值") {
 		t.Fatalf("%+v\n", errors.New("没有检测出使用了时间零值"))
 	}
 
 	err = check.Struct(&customCheckTagPointerStructZero, fieldMap).
-		CheckFields("Time")
-	if err == nil || !strings.Contains(err.Error(), "使用了时间零值") {
-		t.Fatalf("%+v\n", errors.New("没有检测出使用了时间零值"))
-	}
-
-	err = check.Struct(customCheckTagStruct, fieldMap).
-		CheckFieldsWhen("create", "Time")
-	if err != nil {
-		t.Fatalf("%+v\n", err)
-	}
-
-	err = check.Struct(customCheckTagPointerStruct, fieldMap).
-		CheckFieldsWhen("create", "Time")
-	if err != nil {
-		t.Fatalf("%+v\n", err)
-	}
-
-	err = check.Struct(&customCheckTagStruct, fieldMap).
-		CheckFieldsWhen("delete", "Time")
-	if err != nil {
-		t.Fatalf("%+v\n", err)
-	}
-
-	err = check.Struct(&customCheckTagPointerStruct, fieldMap).
-		CheckFieldsWhen("delete", "Time")
-	if err != nil {
-		t.Fatalf("%+v\n", err)
-	}
-
-	err = check.Struct(customCheckTagStructZero, fieldMap).
-		CheckFieldsWhen("update", "Time")
-	if err == nil || !strings.Contains(err.Error(), "使用了时间零值") {
-		t.Fatalf("%+v\n", errors.New("没有检测出使用了时间零值"))
-	}
-
-	err = check.Struct(customCheckTagPointerStructZero, fieldMap).
-		CheckFieldsWhen("update", "Time")
-	if err == nil || !strings.Contains(err.Error(), "使用了时间零值") {
-		t.Fatalf("%+v\n", errors.New("没有检测出使用了时间零值"))
-	}
-
-	err = check.Struct(&customCheckTagStructZero, fieldMap).
-		CheckFieldsWhen("foo", "Time")
-	if err == nil || !strings.Contains(err.Error(), "使用了时间零值") {
-		t.Fatalf("%+v\n", errors.New("没有检测出使用了时间零值"))
-	}
-
-	err = check.Struct(&customCheckTagPointerStructZero, fieldMap).
-		CheckFieldsWhen("foo", "Time")
+		CheckWhen("foo")
 	if err == nil || !strings.Contains(err.Error(), "使用了时间零值") {
 		t.Fatalf("%+v\n", errors.New("没有检测出使用了时间零值"))
 	}