package entity_crud import ( "git.sxidc.com/go-framework/baize/framework/binding" "git.sxidc.com/go-framework/baize/framework/core/api/request" "git.sxidc.com/go-framework/baize/framework/core/api/response" "git.sxidc.com/go-framework/baize/framework/core/domain" "git.sxidc.com/go-framework/baize/framework/core/domain/entity" ) // Simple 实体CRUD的Bind参数 // I 为查询相关接口返回的Info类型 type Simple[I any] struct { // 使用的领域实体,注意是Entity类型 Entity entity.Entity // 数据库Schema Schema string // 创建使用的请求参数 CreateJsonBody request.Params // 删除使用的请求参数,注意是WithID类型 DeleteQueryParams request.IDRequestParam // 更新使用的请求参数,注意是WithID类型 UpdateJsonBody request.IDRequestParam // 查询使用的请求参数,注意是Query类型 QueryQueryParams request.QueryRequestParams // 根据ID查询使用的请求参数,注意是WithID类型 GetByIDQueryParams request.IDRequestParam // 可选配置项,通过WithXXX配置 globalOptions *GlobalOptions createOptions *CreateOptions deleteOptions *DeleteOptions updateOptions *UpdateOptions queryOptions *QueryOptions[I] getByIDOptions *GetByIDOptions[I] } func (simple *Simple[I]) bind(binder *binding.Binder) { globalOptions := simple.globalOptions createOptions := simple.createOptions deleteOptions := simple.deleteOptions updateOptions := simple.updateOptions queryOptions := simple.queryOptions getByIDOptions := simple.getByIDOptions tableName := domain.TableName(simple.Schema, simple.Entity) domainPath := domain.RelativeDomainPath(simple.Entity) // 创建 if !createOptions.disable { createMiddlewares := append(globalOptions.middlewares, createOptions.middlewares...) binding.PostBind[string](binder, &binding.SimpleBindItem[string]{ Path: domainPath + "/create", SendResponseFunc: response.SendIDResponse, RequestParams: simple.CreateJsonBody, Objects: []domain.Object{simple.Entity}, ServiceFunc: Create(tableName, createOptions.needCreateUserID, createOptions.callbacks, createOptions.needTx), }, createMiddlewares...) } // 删除 if !deleteOptions.disable { deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...) binding.DeleteBind(binder, &binding.SimpleBindItem[any]{ Path: domainPath + "/delete", SendResponseFunc: response.SendMsgResponse, RequestParams: simple.DeleteQueryParams, Objects: []domain.Object{simple.Entity}, ServiceFunc: Delete(tableName, deleteOptions.callbacks, deleteOptions.needTx), }, deleteMiddlewares...) } // 修改 if !updateOptions.disable { updateMiddlewares := append(globalOptions.middlewares, updateOptions.middlewares...) binding.PutBind(binder, &binding.SimpleBindItem[any]{ Path: domainPath + "/update", SendResponseFunc: response.SendMsgResponse, RequestParams: simple.UpdateJsonBody, Objects: []domain.Object{simple.Entity}, ServiceFunc: Update(tableName, updateOptions.needLastUpdateUserID, updateOptions.callbacks, updateOptions.needTx), }, updateMiddlewares...) } // 查询 if !queryOptions.disable { queryMiddlewares := append(globalOptions.middlewares, queryOptions.middlewares...) binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{ Path: domainPath + "/query", SendResponseFunc: response.SendInfosResponse[I], RequestParams: simple.QueryQueryParams, Objects: []domain.Object{simple.Entity}, ServiceFunc: Query[I](tableName, queryOptions.orderBy, queryOptions.callbacks, queryOptions.conditionFieldCallback, queryOptions.formCustomConditionFunc), }, queryMiddlewares...) } // 通过ID获取 if !getByIDOptions.disable { getByIDMiddlewares := append(globalOptions.middlewares, getByIDOptions.middlewares...) binding.GetBind(binder, &binding.SimpleBindItem[I]{ Path: domainPath + "/get", SendResponseFunc: response.SendInfoResponse[I], RequestParams: simple.GetByIDQueryParams, Objects: []domain.Object{simple.Entity}, ServiceFunc: GetByID[I](tableName, getByIDOptions.callbacks), }, getByIDMiddlewares...) } } func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) { globalOptions := new(GlobalOptions) createOptions := new(CreateOptions) deleteOptions := new(DeleteOptions) updateOptions := new(UpdateOptions) queryOptions := new(QueryOptions[I]) getByIDOptions := new(GetByIDOptions[I]) for _, opt := range opts { switch o := opt.(type) { case GlobalOption: o(globalOptions) case CreateOption: o(createOptions) case DeleteOption: o(deleteOptions) case UpdateOption: o(updateOptions) case QueryOption[I]: o(queryOptions) case GetByIDOption[I]: o(getByIDOptions) default: continue } } simple.globalOptions = globalOptions simple.createOptions = createOptions simple.deleteOptions = deleteOptions simple.updateOptions = updateOptions simple.queryOptions = queryOptions simple.getByIDOptions = getByIDOptions simple.bind(binder) } type GlobalOption func(options *GlobalOptions) type CreateOption func(options *CreateOptions) type DeleteOption func(options *DeleteOptions) type UpdateOption func(options *UpdateOptions) type QueryOption[I any] func(options *QueryOptions[I]) type GetByIDOption[I any] func(options *GetByIDOptions[I]) type GlobalOptions struct { middlewares []binding.Middleware } type CreateOptions struct { // 关闭创建 disable bool // 创建是否使用事务 needTx bool // 创建需要创建用户ID needCreateUserID bool // 创建回调 callbacks *CreateCallbacks // 创建中间件 middlewares []binding.Middleware } type DeleteOptions struct { // 关闭删除 disable bool // 删除是否使用事务 needTx bool // 删除回调 callbacks *DeleteCallbacks // 删除中间件 middlewares []binding.Middleware } type UpdateOptions struct { // 关闭更新 disable bool // 更新是否使用事务 needTx bool // 更新需要最新更新用户ID needLastUpdateUserID bool // 更新回调 callbacks *UpdateCallbacks // 更新中间件 middlewares []binding.Middleware } type QueryOptions[I any] struct { // 关闭查询 disable bool // 查询条件构造回调 conditionFieldCallback ConditionFieldCallback // 自定义添加构造函数 formCustomConditionFunc FormCustomConditionFunc // 查询回调 callbacks *QueryCallbacks[I] // 查询中间件 middlewares []binding.Middleware // OrderBy orderBy string } type GetByIDOptions[I any] struct { // 关闭根据ID查询 disable bool // 根据ID查询回调 callbacks *GetByIDCallbacks[I] // 根据ID查询中间件 middlewares []binding.Middleware } func WithGlobalMiddlewares(middlewares ...binding.Middleware) GlobalOption { return func(options *GlobalOptions) { options.middlewares = middlewares } } func WithDisableCreate() CreateOption { return func(options *CreateOptions) { options.disable = true } } func WithCreateTx() CreateOption { return func(options *CreateOptions) { options.needTx = true } } func WithCreateUserID() CreateOption { return func(options *CreateOptions) { options.needCreateUserID = true } } func WithCreateCallbacks(callbacks *CreateCallbacks) CreateOption { return func(options *CreateOptions) { options.callbacks = callbacks } } func WithCreateMiddlewares(middlewares ...binding.Middleware) CreateOption { return func(options *CreateOptions) { options.middlewares = middlewares } } func WithDisableDelete() DeleteOption { return func(options *DeleteOptions) { options.disable = true } } func WithDeleteTx() DeleteOption { return func(options *DeleteOptions) { options.needTx = true } } func WithDeleteCallbacks(callbacks *DeleteCallbacks) DeleteOption { return func(options *DeleteOptions) { options.callbacks = callbacks } } func WithDeleteMiddlewares(middlewares ...binding.Middleware) DeleteOption { return func(options *DeleteOptions) { options.middlewares = middlewares } } func WithDisableUpdate() UpdateOption { return func(options *UpdateOptions) { options.disable = true } } func WithUpdateTx() UpdateOption { return func(options *UpdateOptions) { options.needTx = true } } func WithLastUpdateUserID() UpdateOption { return func(options *UpdateOptions) { options.needLastUpdateUserID = true } } func WithUpdateCallbacks(callbacks *UpdateCallbacks) UpdateOption { return func(options *UpdateOptions) { options.callbacks = callbacks } } func WithUpdateMiddlewares(middlewares ...binding.Middleware) UpdateOption { return func(options *UpdateOptions) { options.middlewares = middlewares } } func WithDisableQuery[I any]() QueryOption[I] { return func(options *QueryOptions[I]) { options.disable = true } } func WithQueryConditionFieldCallback[I any](callback ConditionFieldCallback) QueryOption[I] { return func(options *QueryOptions[I]) { options.conditionFieldCallback = callback } } func WithQueryFormCustomConditionFunc[I any](formCustomConditionFunc FormCustomConditionFunc) QueryOption[I] { return func(options *QueryOptions[I]) { options.formCustomConditionFunc = formCustomConditionFunc } } func WithQueryCallbacks[I any](callbacks *QueryCallbacks[I]) QueryOption[I] { return func(options *QueryOptions[I]) { options.callbacks = callbacks } } func WithQueryMiddlewares[I any](middlewares ...binding.Middleware) QueryOption[I] { return func(options *QueryOptions[I]) { options.middlewares = middlewares } } func WithQueryOrderBy[I any](orderBy string) QueryOption[I] { return func(options *QueryOptions[I]) { options.orderBy = orderBy } } func WithDisableGetByID[I any]() GetByIDOption[I] { return func(options *GetByIDOptions[I]) { options.disable = true } } func WithGetByIDCallbacks[I any](callbacks *GetByIDCallbacks[I]) GetByIDOption[I] { return func(options *GetByIDOptions[I]) { options.callbacks = callbacks } } func WithGetByIDMiddlewares[I any](middlewares ...binding.Middleware) GetByIDOption[I] { return func(options *GetByIDOptions[I]) { options.middlewares = middlewares } }