simple.go 11 KB

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