123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- 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
- }
- }
|