package user import ( "git.sxidc.com/go-framework/baize/convenient/entity_crud" "git.sxidc.com/go-framework/baize/framework/binding" "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" ) // Simple Bind参数 type Simple struct { // schema Schema string // AES加密用到的Key AESKey string } func (simple *Simple) Bind(binder *binding.Binder) { entity_crud.BindSimple(binder, &entity_crud.Simple[Info]{ Entity: &Entity{}, Schema: simple.Schema, CreateJsonBody: &CreateUserJsonBody{}, DeleteQueryParams: &DeleteUserQueryParams{}, UpdateJsonBody: &UpdateUserJsonBody{}, QueryQueryParams: &GetUsersQueryParams{}, GetByIDQueryParams: &GetUserQueryParams{}, }, entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{ Before: func(e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error { userEntity, err := domain.ToConcrete[*Entity](e) if err != nil { return err } encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey) if err != nil { return err } userEntity.Password = encryptedPassword if strutils.IsStringEmpty(userEntity.Name) { userEntity.Name = "匿名-" + strutils.SimpleUUID()[0:8] } encryptedPhone, err := encoding.AESEncrypt(userEntity.Phone, simple.AESKey) if err != nil { return err } userEntity.Phone = encryptedPhone encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey) if err != nil { return err } 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 }, }), entity_crud.WithUpdateCallbacks(&entity_crud.UpdateCallbacks{ Before: func(e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error { userEntity, err := domain.ToConcrete[*Entity](e) if err != nil { return err } if strutils.IsStringNotEmpty(userEntity.Password) { encryptedPassword, err := encoding.AESEncrypt(userEntity.Password, simple.AESKey) if err != nil { return err } userEntity.Password = encryptedPassword } if strutils.IsStringEmpty(userEntity.Name) { userEntity.Name = "匿名-" + strutils.SimpleUUID()[0:8] } if strutils.IsStringNotEmpty(userEntity.Phone) { encryptedPhone, err := encoding.AESEncrypt(userEntity.Phone, simple.AESKey) if err != nil { return err } userEntity.Phone = encryptedPhone } if strutils.IsStringNotEmpty(userEntity.Email) { encryptedEmail, err := encoding.AESEncrypt(userEntity.Email, simple.AESKey) if err != nil { return err } 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 }, }), entity_crud.WithQueryConditionFieldCallback[Info](func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error) { if value == nil { return true, nil } switch fieldName { case FieldUserName: userName, ok := value.(string) if !ok { return false, errors.New("用户名字段类型不是string") } if strutils.IsStringNotEmpty(userName) { conditions.Like(ColumnUserName, "%"+userName+"%") } return true, nil case FieldName: name, ok := value.(string) if !ok { return false, errors.New("姓名字段类型不是string") } if strutils.IsStringNotEmpty(name) { conditions.Like(ColumnName, "%"+name+"%") } return true, nil case FieldPhone: phone, ok := value.(string) if !ok { return false, errors.New("手机号字段类型不是string") } if strutils.IsStringNotEmpty(phone) { encryptedPassword, err := encoding.AESEncrypt(phone, simple.AESKey) if err != nil { return false, err } conditions.Equal(ColumnPassword, encryptedPassword) } return true, nil case FieldEmail: email, ok := value.(string) if !ok { return false, errors.New("邮箱字段类型不是string") } if strutils.IsStringNotEmpty(email) { encryptedPassword, err := encoding.AESEncrypt(email, simple.AESKey) if err != nil { return false, err } conditions.Equal(ColumnPassword, encryptedPassword) } return true, nil default: return false, nil } })) }