simple.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package entity_crud
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/binding"
  4. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  5. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  6. "git.sxidc.com/go-framework/baize/framework/core/domain"
  7. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  8. )
  9. // Simple 实体CRUD的Bind参数
  10. // I 为查询相关接口返回的Info类型
  11. type Simple[I any] struct {
  12. // 使用的领域实体,注意是Entity类型
  13. Entity entity.Entity
  14. // 数据库Schema
  15. Schema string
  16. // 创建使用的请求参数
  17. CreateJsonBody request.Params
  18. // 删除使用的请求参数,注意是WithID类型
  19. DeleteQueryParams request.IDRequestParam
  20. // 更新使用的请求参数,注意是WithID类型
  21. UpdateJsonBody request.IDRequestParam
  22. // 查询使用的请求参数,注意是Query类型
  23. QueryQueryParams request.QueryRequestParams
  24. // 根据ID查询使用的请求参数,注意是WithID类型
  25. GetByIDQueryParams request.IDRequestParam
  26. // 可选配置项,通过WithXXX配置
  27. createOptions *CreateOptions
  28. deleteOptions *DeleteOptions
  29. updateOptions *UpdateOptions
  30. queryOptions *QueryOptions[I]
  31. getByIDOptions *GetByIDOptions[I]
  32. }
  33. func (simple *Simple[I]) bind(binder *binding.Binder, middlewares ...binding.Middleware) {
  34. createOptions := simple.createOptions
  35. deleteOptions := simple.deleteOptions
  36. updateOptions := simple.updateOptions
  37. queryOptions := simple.queryOptions
  38. getByIDOptions := simple.getByIDOptions
  39. tableName := domain.TableName(simple.Schema, simple.Entity)
  40. domainPath := domain.RelativeDomainPath(simple.Entity)
  41. // 创建
  42. if !createOptions.disable {
  43. createMiddlewares := append(middlewares, createOptions.middlewares...)
  44. if !createOptions.needTx {
  45. binding.PostBind[string](binder, &binding.SimpleBindItem[string]{
  46. Path: domainPath + "/create",
  47. SendResponseFunc: response.SendIDResponse,
  48. RequestParams: simple.CreateJsonBody,
  49. Objects: []domain.Object{simple.Entity},
  50. ServiceFunc: Create(tableName, createOptions.callbacks),
  51. }, createMiddlewares...)
  52. } else {
  53. binding.PostBind(binder, &binding.SimpleBindItem[string]{
  54. Path: domainPath + "/create",
  55. SendResponseFunc: response.SendIDResponse,
  56. RequestParams: simple.CreateJsonBody,
  57. Objects: []domain.Object{simple.Entity},
  58. ServiceFunc: CreateTx(tableName, createOptions.callbacks),
  59. }, createMiddlewares...)
  60. }
  61. }
  62. // 删除
  63. if !deleteOptions.disable {
  64. deleteMiddlewares := append(middlewares, deleteOptions.middlewares...)
  65. if !deleteOptions.needTx {
  66. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  67. Path: domainPath + "/delete",
  68. SendResponseFunc: response.SendMsgResponse,
  69. RequestParams: simple.DeleteQueryParams,
  70. Objects: []domain.Object{simple.Entity},
  71. ServiceFunc: Delete(tableName, deleteOptions.callbacks),
  72. }, deleteMiddlewares...)
  73. } else {
  74. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  75. Path: domainPath + "/delete",
  76. SendResponseFunc: response.SendMsgResponse,
  77. RequestParams: simple.DeleteQueryParams,
  78. Objects: []domain.Object{simple.Entity},
  79. ServiceFunc: DeleteTx(tableName, deleteOptions.callbacks),
  80. }, deleteMiddlewares...)
  81. }
  82. }
  83. // 修改
  84. if !updateOptions.disable {
  85. updateMiddlewares := append(middlewares, updateOptions.middlewares...)
  86. if !updateOptions.needTx {
  87. binding.PutBind(binder, &binding.SimpleBindItem[any]{
  88. Path: domainPath + "/update",
  89. SendResponseFunc: response.SendMsgResponse,
  90. RequestParams: simple.UpdateJsonBody,
  91. Objects: []domain.Object{simple.Entity},
  92. ServiceFunc: Update(tableName, updateOptions.callbacks),
  93. }, updateMiddlewares...)
  94. } else {
  95. binding.PutBind(binder, &binding.SimpleBindItem[any]{
  96. Path: domainPath + "/update",
  97. SendResponseFunc: response.SendMsgResponse,
  98. RequestParams: simple.UpdateJsonBody,
  99. Objects: []domain.Object{simple.Entity},
  100. ServiceFunc: UpdateTx(tableName, updateOptions.callbacks),
  101. }, updateMiddlewares...)
  102. }
  103. }
  104. // 查询
  105. if !queryOptions.disable {
  106. queryMiddlewares := append(middlewares, queryOptions.middlewares...)
  107. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{
  108. Path: domainPath + "/query",
  109. SendResponseFunc: response.SendInfosResponse[I],
  110. RequestParams: simple.QueryQueryParams,
  111. Objects: []domain.Object{simple.Entity},
  112. ServiceFunc: Query[I](tableName, queryOptions.callbacks, queryOptions.conditionFieldCallback),
  113. }, queryMiddlewares...)
  114. }
  115. // 通过ID获取
  116. if !getByIDOptions.disable {
  117. getByIDMiddlewares := append(middlewares, getByIDOptions.middlewares...)
  118. binding.GetBind(binder, &binding.SimpleBindItem[I]{
  119. Path: domainPath + "/get",
  120. SendResponseFunc: response.SendInfoResponse[I],
  121. RequestParams: simple.GetByIDQueryParams,
  122. Objects: []domain.Object{simple.Entity},
  123. ServiceFunc: GetByID[I](tableName, getByIDOptions.callbacks),
  124. }, getByIDMiddlewares...)
  125. }
  126. }
  127. func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) {
  128. createOptions := new(CreateOptions)
  129. deleteOptions := new(DeleteOptions)
  130. updateOptions := new(UpdateOptions)
  131. queryOptions := new(QueryOptions[I])
  132. getByIDOptions := new(GetByIDOptions[I])
  133. for _, opt := range opts {
  134. switch o := opt.(type) {
  135. case CreateOption:
  136. o(createOptions)
  137. case DeleteOption:
  138. o(deleteOptions)
  139. case UpdateOption:
  140. o(updateOptions)
  141. case QueryOption[I]:
  142. o(queryOptions)
  143. case GetByIDOption[I]:
  144. o(getByIDOptions)
  145. default:
  146. continue
  147. }
  148. }
  149. simple.createOptions = createOptions
  150. simple.deleteOptions = deleteOptions
  151. simple.updateOptions = updateOptions
  152. simple.queryOptions = queryOptions
  153. simple.getByIDOptions = getByIDOptions
  154. simple.bind(binder)
  155. }
  156. type CreateOption func(options *CreateOptions)
  157. type DeleteOption func(options *DeleteOptions)
  158. type UpdateOption func(options *UpdateOptions)
  159. type QueryOption[I any] func(options *QueryOptions[I])
  160. type GetByIDOption[I any] func(options *GetByIDOptions[I])
  161. type CreateOptions struct {
  162. // 关闭创建
  163. disable bool
  164. // 创建是否使用事务
  165. needTx bool
  166. // 创建回调
  167. callbacks *CreateCallbacks
  168. // 创建中间件
  169. middlewares []binding.Middleware
  170. }
  171. type DeleteOptions struct {
  172. // 关闭删除
  173. disable bool
  174. // 删除是否使用事务
  175. needTx bool
  176. // 删除回调
  177. callbacks *DeleteCallbacks
  178. // 删除中间件
  179. middlewares []binding.Middleware
  180. }
  181. type UpdateOptions struct {
  182. // 关闭更新
  183. disable bool
  184. // 更新是否使用事务
  185. needTx bool
  186. // 更新回调
  187. callbacks *UpdateCallbacks
  188. // 更新中间件
  189. middlewares []binding.Middleware
  190. }
  191. type QueryOptions[I any] struct {
  192. // 关闭查询
  193. disable bool
  194. // 查询条件构造回调
  195. conditionFieldCallback ConditionFieldCallback
  196. // 查询回调
  197. callbacks *QueryCallbacks[I]
  198. // 查询中间件
  199. middlewares []binding.Middleware
  200. }
  201. type GetByIDOptions[I any] struct {
  202. // 关闭根据ID查询
  203. disable bool
  204. // 根据ID查询回调
  205. callbacks *GetByIDCallbacks[I]
  206. // 根据ID查询中间件
  207. middlewares []binding.Middleware
  208. }
  209. func WithDisableCreate() CreateOption {
  210. return func(options *CreateOptions) {
  211. options.disable = true
  212. }
  213. }
  214. func WithCreateTx() CreateOption {
  215. return func(options *CreateOptions) {
  216. options.needTx = true
  217. }
  218. }
  219. func WithCreateCallbacks(callbacks *CreateCallbacks) CreateOption {
  220. return func(options *CreateOptions) {
  221. options.callbacks = callbacks
  222. }
  223. }
  224. func WithCreateMiddlewares(middlewares ...binding.Middleware) CreateOption {
  225. return func(options *CreateOptions) {
  226. options.middlewares = middlewares
  227. }
  228. }
  229. func WithDisableDelete() DeleteOption {
  230. return func(options *DeleteOptions) {
  231. options.disable = true
  232. }
  233. }
  234. func WithDeleteTx() DeleteOption {
  235. return func(options *DeleteOptions) {
  236. options.needTx = true
  237. }
  238. }
  239. func WithDeleteCallbacks(callbacks *DeleteCallbacks) DeleteOption {
  240. return func(options *DeleteOptions) {
  241. options.callbacks = callbacks
  242. }
  243. }
  244. func WithDeleteMiddlewares(middlewares ...binding.Middleware) DeleteOption {
  245. return func(options *DeleteOptions) {
  246. options.middlewares = middlewares
  247. }
  248. }
  249. func WithDisableUpdate() UpdateOption {
  250. return func(options *UpdateOptions) {
  251. options.disable = true
  252. }
  253. }
  254. func WithUpdateTx() UpdateOption {
  255. return func(options *UpdateOptions) {
  256. options.needTx = true
  257. }
  258. }
  259. func WithUpdateCallbacks(callbacks *UpdateCallbacks) UpdateOption {
  260. return func(options *UpdateOptions) {
  261. options.callbacks = callbacks
  262. }
  263. }
  264. func WithUpdateMiddlewares(middlewares ...binding.Middleware) UpdateOption {
  265. return func(options *UpdateOptions) {
  266. options.middlewares = middlewares
  267. }
  268. }
  269. func WithDisableQuery[I any]() QueryOption[I] {
  270. return func(options *QueryOptions[I]) {
  271. options.disable = true
  272. }
  273. }
  274. func WithQueryConditionFieldCallback[I any](callback ConditionFieldCallback) QueryOption[I] {
  275. return func(options *QueryOptions[I]) {
  276. options.conditionFieldCallback = callback
  277. }
  278. }
  279. func WithQueryCallbacks[I any](callbacks *QueryCallbacks[I]) QueryOption[I] {
  280. return func(options *QueryOptions[I]) {
  281. options.callbacks = callbacks
  282. }
  283. }
  284. func WithQueryMiddlewares[I any](middlewares ...binding.Middleware) QueryOption[I] {
  285. return func(options *QueryOptions[I]) {
  286. options.middlewares = middlewares
  287. }
  288. }
  289. func WithDisableGetByID[I any]() GetByIDOption[I] {
  290. return func(options *GetByIDOptions[I]) {
  291. options.disable = true
  292. }
  293. }
  294. func WithGetByIDCallbacks[I any](callbacks *GetByIDCallbacks[I]) GetByIDOption[I] {
  295. return func(options *GetByIDOptions[I]) {
  296. options.callbacks = callbacks
  297. }
  298. }
  299. func WithGetByIDMiddlewares[I any](middlewares ...binding.Middleware) GetByIDOption[I] {
  300. return func(options *GetByIDOptions[I]) {
  301. options.middlewares = middlewares
  302. }
  303. }