simple.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. globalOptions *GlobalOptions
  28. createOptions *CreateOptions
  29. deleteOptions *DeleteOptions
  30. updateOptions *UpdateOptions
  31. queryOptions *QueryOptions[I]
  32. getByIDOptions *GetByIDOptions[I]
  33. }
  34. func (simple *Simple[I]) bind(binder *binding.Binder) {
  35. globalOptions := simple.globalOptions
  36. createOptions := simple.createOptions
  37. deleteOptions := simple.deleteOptions
  38. updateOptions := simple.updateOptions
  39. queryOptions := simple.queryOptions
  40. getByIDOptions := simple.getByIDOptions
  41. tableName := domain.TableName(simple.Schema, simple.Entity)
  42. domainPath := domain.RelativeDomainPath(simple.Entity)
  43. // 创建
  44. if !createOptions.disable {
  45. createMiddlewares := append(globalOptions.middlewares, createOptions.middlewares...)
  46. binding.PostBind[string](binder, &binding.SimpleBindItem[string]{
  47. Path: domainPath + "/create",
  48. SendResponseFunc: response.SendIDResponse,
  49. RequestParams: simple.CreateJsonBody,
  50. Objects: []domain.Object{simple.Entity},
  51. ServiceFunc: Create(tableName, createOptions.needCreateUserID, createOptions.callbacks, createOptions.needTx),
  52. }, createMiddlewares...)
  53. }
  54. // 删除
  55. if !deleteOptions.disable {
  56. deleteMiddlewares := append(globalOptions.middlewares, deleteOptions.middlewares...)
  57. binding.DeleteBind(binder, &binding.SimpleBindItem[any]{
  58. Path: domainPath + "/delete",
  59. SendResponseFunc: response.SendMsgResponse,
  60. RequestParams: simple.DeleteQueryParams,
  61. Objects: []domain.Object{simple.Entity},
  62. ServiceFunc: Delete(tableName, deleteOptions.callbacks, deleteOptions.needTx),
  63. }, deleteMiddlewares...)
  64. }
  65. // 修改
  66. if !updateOptions.disable {
  67. updateMiddlewares := append(globalOptions.middlewares, updateOptions.middlewares...)
  68. binding.PutBind(binder, &binding.SimpleBindItem[any]{
  69. Path: domainPath + "/update",
  70. SendResponseFunc: response.SendMsgResponse,
  71. RequestParams: simple.UpdateJsonBody,
  72. Objects: []domain.Object{simple.Entity},
  73. ServiceFunc: Update(tableName, updateOptions.needLastUpdateUserID, updateOptions.callbacks, updateOptions.needTx),
  74. }, updateMiddlewares...)
  75. }
  76. // 查询
  77. if !queryOptions.disable {
  78. queryMiddlewares := append(globalOptions.middlewares, queryOptions.middlewares...)
  79. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{
  80. Path: domainPath + "/query",
  81. SendResponseFunc: response.SendInfosResponse[I],
  82. RequestParams: simple.QueryQueryParams,
  83. Objects: []domain.Object{simple.Entity},
  84. ServiceFunc: Query[I](tableName, queryOptions.orderBy, queryOptions.callbacks, queryOptions.conditionFieldCallback, queryOptions.formCustomConditionFunc),
  85. }, queryMiddlewares...)
  86. }
  87. // 通过ID获取
  88. if !getByIDOptions.disable {
  89. getByIDMiddlewares := append(globalOptions.middlewares, getByIDOptions.middlewares...)
  90. binding.GetBind(binder, &binding.SimpleBindItem[I]{
  91. Path: domainPath + "/get",
  92. SendResponseFunc: response.SendInfoResponse[I],
  93. RequestParams: simple.GetByIDQueryParams,
  94. Objects: []domain.Object{simple.Entity},
  95. ServiceFunc: GetByID[I](tableName, getByIDOptions.callbacks),
  96. }, getByIDMiddlewares...)
  97. }
  98. }
  99. func BindSimple[I any](binder *binding.Binder, simple *Simple[I], opts ...any) {
  100. globalOptions := new(GlobalOptions)
  101. createOptions := new(CreateOptions)
  102. deleteOptions := new(DeleteOptions)
  103. updateOptions := new(UpdateOptions)
  104. queryOptions := new(QueryOptions[I])
  105. getByIDOptions := new(GetByIDOptions[I])
  106. for _, opt := range opts {
  107. switch o := opt.(type) {
  108. case GlobalOption:
  109. o(globalOptions)
  110. case CreateOption:
  111. o(createOptions)
  112. case DeleteOption:
  113. o(deleteOptions)
  114. case UpdateOption:
  115. o(updateOptions)
  116. case QueryOption[I]:
  117. o(queryOptions)
  118. case GetByIDOption[I]:
  119. o(getByIDOptions)
  120. default:
  121. continue
  122. }
  123. }
  124. simple.globalOptions = globalOptions
  125. simple.createOptions = createOptions
  126. simple.deleteOptions = deleteOptions
  127. simple.updateOptions = updateOptions
  128. simple.queryOptions = queryOptions
  129. simple.getByIDOptions = getByIDOptions
  130. simple.bind(binder)
  131. }
  132. type GlobalOption func(options *GlobalOptions)
  133. type CreateOption func(options *CreateOptions)
  134. type DeleteOption func(options *DeleteOptions)
  135. type UpdateOption func(options *UpdateOptions)
  136. type QueryOption[I any] func(options *QueryOptions[I])
  137. type GetByIDOption[I any] func(options *GetByIDOptions[I])
  138. type GlobalOptions struct {
  139. middlewares []binding.Middleware
  140. }
  141. type CreateOptions struct {
  142. // 关闭创建
  143. disable bool
  144. // 创建是否使用事务
  145. needTx bool
  146. // 创建需要创建用户ID
  147. needCreateUserID bool
  148. // 创建回调
  149. callbacks *CreateCallbacks
  150. // 创建中间件
  151. middlewares []binding.Middleware
  152. }
  153. type DeleteOptions struct {
  154. // 关闭删除
  155. disable bool
  156. // 删除是否使用事务
  157. needTx bool
  158. // 删除回调
  159. callbacks *DeleteCallbacks
  160. // 删除中间件
  161. middlewares []binding.Middleware
  162. }
  163. type UpdateOptions struct {
  164. // 关闭更新
  165. disable bool
  166. // 更新是否使用事务
  167. needTx bool
  168. // 更新需要最新更新用户ID
  169. needLastUpdateUserID bool
  170. // 更新回调
  171. callbacks *UpdateCallbacks
  172. // 更新中间件
  173. middlewares []binding.Middleware
  174. }
  175. type QueryOptions[I any] struct {
  176. // 关闭查询
  177. disable bool
  178. // 查询条件构造回调
  179. conditionFieldCallback ConditionFieldCallback
  180. // 自定义添加构造函数
  181. formCustomConditionFunc FormCustomConditionFunc
  182. // 查询回调
  183. callbacks *QueryCallbacks[I]
  184. // 查询中间件
  185. middlewares []binding.Middleware
  186. // OrderBy
  187. orderBy string
  188. }
  189. type GetByIDOptions[I any] struct {
  190. // 关闭根据ID查询
  191. disable bool
  192. // 根据ID查询回调
  193. callbacks *GetByIDCallbacks[I]
  194. // 根据ID查询中间件
  195. middlewares []binding.Middleware
  196. }
  197. func WithGlobalMiddlewares(middlewares ...binding.Middleware) GlobalOption {
  198. return func(options *GlobalOptions) {
  199. options.middlewares = middlewares
  200. }
  201. }
  202. func WithDisableCreate() CreateOption {
  203. return func(options *CreateOptions) {
  204. options.disable = true
  205. }
  206. }
  207. func WithCreateTx() CreateOption {
  208. return func(options *CreateOptions) {
  209. options.needTx = true
  210. }
  211. }
  212. func WithCreateUserID() CreateOption {
  213. return func(options *CreateOptions) {
  214. options.needCreateUserID = true
  215. }
  216. }
  217. func WithCreateCallbacks(callbacks *CreateCallbacks) CreateOption {
  218. return func(options *CreateOptions) {
  219. options.callbacks = callbacks
  220. }
  221. }
  222. func WithCreateMiddlewares(middlewares ...binding.Middleware) CreateOption {
  223. return func(options *CreateOptions) {
  224. options.middlewares = middlewares
  225. }
  226. }
  227. func WithDisableDelete() DeleteOption {
  228. return func(options *DeleteOptions) {
  229. options.disable = true
  230. }
  231. }
  232. func WithDeleteTx() DeleteOption {
  233. return func(options *DeleteOptions) {
  234. options.needTx = true
  235. }
  236. }
  237. func WithDeleteCallbacks(callbacks *DeleteCallbacks) DeleteOption {
  238. return func(options *DeleteOptions) {
  239. options.callbacks = callbacks
  240. }
  241. }
  242. func WithDeleteMiddlewares(middlewares ...binding.Middleware) DeleteOption {
  243. return func(options *DeleteOptions) {
  244. options.middlewares = middlewares
  245. }
  246. }
  247. func WithDisableUpdate() UpdateOption {
  248. return func(options *UpdateOptions) {
  249. options.disable = true
  250. }
  251. }
  252. func WithUpdateTx() UpdateOption {
  253. return func(options *UpdateOptions) {
  254. options.needTx = true
  255. }
  256. }
  257. func WithLastUpdateUserID() UpdateOption {
  258. return func(options *UpdateOptions) {
  259. options.needLastUpdateUserID = true
  260. }
  261. }
  262. func WithUpdateCallbacks(callbacks *UpdateCallbacks) UpdateOption {
  263. return func(options *UpdateOptions) {
  264. options.callbacks = callbacks
  265. }
  266. }
  267. func WithUpdateMiddlewares(middlewares ...binding.Middleware) UpdateOption {
  268. return func(options *UpdateOptions) {
  269. options.middlewares = middlewares
  270. }
  271. }
  272. func WithDisableQuery[I any]() QueryOption[I] {
  273. return func(options *QueryOptions[I]) {
  274. options.disable = true
  275. }
  276. }
  277. func WithQueryConditionFieldCallback[I any](callback ConditionFieldCallback) QueryOption[I] {
  278. return func(options *QueryOptions[I]) {
  279. options.conditionFieldCallback = callback
  280. }
  281. }
  282. func WithQueryFormCustomConditionFunc[I any](formCustomConditionFunc FormCustomConditionFunc) QueryOption[I] {
  283. return func(options *QueryOptions[I]) {
  284. options.formCustomConditionFunc = formCustomConditionFunc
  285. }
  286. }
  287. func WithQueryCallbacks[I any](callbacks *QueryCallbacks[I]) QueryOption[I] {
  288. return func(options *QueryOptions[I]) {
  289. options.callbacks = callbacks
  290. }
  291. }
  292. func WithQueryMiddlewares[I any](middlewares ...binding.Middleware) QueryOption[I] {
  293. return func(options *QueryOptions[I]) {
  294. options.middlewares = middlewares
  295. }
  296. }
  297. func WithQueryOrderBy[I any](orderBy string) QueryOption[I] {
  298. return func(options *QueryOptions[I]) {
  299. options.orderBy = orderBy
  300. }
  301. }
  302. func WithDisableGetByID[I any]() GetByIDOption[I] {
  303. return func(options *GetByIDOptions[I]) {
  304. options.disable = true
  305. }
  306. }
  307. func WithGetByIDCallbacks[I any](callbacks *GetByIDCallbacks[I]) GetByIDOption[I] {
  308. return func(options *GetByIDOptions[I]) {
  309. options.callbacks = callbacks
  310. }
  311. }
  312. func WithGetByIDMiddlewares[I any](middlewares ...binding.Middleware) GetByIDOption[I] {
  313. return func(options *GetByIDOptions[I]) {
  314. options.middlewares = middlewares
  315. }
  316. }