service_callbacks.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. package entity_crud
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  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/entity"
  7. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  8. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  9. "git.sxidc.com/go-tools/utils/reflectutils"
  10. )
  11. type CreateCallbacks struct {
  12. Prepare func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error)
  13. Before func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
  14. After func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
  15. OnSuccessReturn func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, id string) (string, error)
  16. OnErrorReturn func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, err error, i *infrastructure.Infrastructure) (string, error)
  17. }
  18. func callbackPrepareCreate(callbacks *CreateCallbacks, c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
  19. if callbacks == nil {
  20. return make(map[string]any), nil
  21. }
  22. if callbacks.Prepare == nil {
  23. return make(map[string]any), nil
  24. }
  25. return callbacks.Prepare(c, params, e, i)
  26. }
  27. func callbackBeforeCreate(callbacks *CreateCallbacks, c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  28. if callbacks == nil {
  29. return nil
  30. }
  31. if callbacks.Before == nil {
  32. return nil
  33. }
  34. return callbacks.Before(c, params, e, prepared, i, tx)
  35. }
  36. func callbackAfterCreate(callbacks *CreateCallbacks, c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, dbExecutor database.Executor) error {
  37. if callbacks == nil {
  38. return nil
  39. }
  40. if callbacks.After == nil {
  41. return nil
  42. }
  43. return callbacks.After(c, params, e, prepared, i, dbExecutor)
  44. }
  45. func callbackOnCreateSuccessReturn(callbacks *CreateCallbacks, c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, id string) (string, error) {
  46. if callbacks == nil {
  47. return id, nil
  48. }
  49. if callbacks.OnSuccessReturn == nil {
  50. return id, nil
  51. }
  52. return callbacks.OnSuccessReturn(c, params, e, prepared, i, id)
  53. }
  54. func callbackOnCreateErrorReturn(callbacks *CreateCallbacks, c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, err error, i *infrastructure.Infrastructure) (string, error) {
  55. if callbacks == nil {
  56. return "", err
  57. }
  58. if callbacks.OnErrorReturn == nil {
  59. return "", err
  60. }
  61. return callbacks.OnErrorReturn(c, params, e, prepared, err, i)
  62. }
  63. type DeleteCallbacks struct {
  64. Prepare func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error)
  65. Before func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
  66. After func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
  67. OnSuccessReturn func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure) error
  68. OnErrorReturn func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, err error, i *infrastructure.Infrastructure) error
  69. }
  70. func callbackPrepareDelete(callbacks *DeleteCallbacks, c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
  71. if callbacks == nil {
  72. return make(map[string]any), nil
  73. }
  74. if callbacks.Prepare == nil {
  75. return make(map[string]any), nil
  76. }
  77. return callbacks.Prepare(c, params, e, i)
  78. }
  79. func callbackBeforeDelete(callbacks *DeleteCallbacks, c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  80. if callbacks == nil {
  81. return nil
  82. }
  83. if callbacks.Before == nil {
  84. return nil
  85. }
  86. return callbacks.Before(c, params, e, prepared, i, tx)
  87. }
  88. func callbackAfterDelete(callbacks *DeleteCallbacks, c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, dbExecutor database.Executor) error {
  89. if callbacks == nil {
  90. return nil
  91. }
  92. if callbacks.After == nil {
  93. return nil
  94. }
  95. return callbacks.After(c, params, e, prepared, i, dbExecutor)
  96. }
  97. func callbackOnDeleteSuccessReturn(callbacks *DeleteCallbacks, c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure) error {
  98. if callbacks == nil {
  99. return nil
  100. }
  101. if callbacks.OnSuccessReturn == nil {
  102. return nil
  103. }
  104. return callbacks.OnSuccessReturn(c, params, e, prepared, i)
  105. }
  106. func callbackOnDeleteErrorReturn(callbacks *DeleteCallbacks, c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, err error, i *infrastructure.Infrastructure) error {
  107. if callbacks == nil {
  108. return err
  109. }
  110. if callbacks.OnErrorReturn == nil {
  111. return err
  112. }
  113. return callbacks.OnErrorReturn(c, params, e, prepared, err, i)
  114. }
  115. type UpdateCallbacks struct {
  116. Prepare func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error)
  117. Before func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
  118. After func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error
  119. OnSuccessReturn func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure) error
  120. OnErrorReturn func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, err error, i *infrastructure.Infrastructure) error
  121. }
  122. func callbackPrepareUpdate(callbacks *UpdateCallbacks, c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
  123. if callbacks == nil {
  124. return make(map[string]any), nil
  125. }
  126. if callbacks.Prepare == nil {
  127. return make(map[string]any), nil
  128. }
  129. return callbacks.Prepare(c, params, e, i)
  130. }
  131. func callbackBeforeUpdate(callbacks *UpdateCallbacks, c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  132. if callbacks == nil {
  133. return nil
  134. }
  135. if callbacks.Before == nil {
  136. return nil
  137. }
  138. return callbacks.Before(c, params, e, prepared, i, tx)
  139. }
  140. func callbackAfterUpdate(callbacks *UpdateCallbacks, c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, dbExecutor database.Executor) error {
  141. if callbacks == nil {
  142. return nil
  143. }
  144. if callbacks.After == nil {
  145. return nil
  146. }
  147. return callbacks.After(c, params, e, prepared, i, dbExecutor)
  148. }
  149. func callbackOnUpdateSuccessReturn(callbacks *UpdateCallbacks, c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure) error {
  150. if callbacks == nil {
  151. return nil
  152. }
  153. if callbacks.OnSuccessReturn == nil {
  154. return nil
  155. }
  156. return callbacks.OnSuccessReturn(c, params, e, prepared, i)
  157. }
  158. func callbackOnUpdateErrorReturn(callbacks *UpdateCallbacks, c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, err error, i *infrastructure.Infrastructure) error {
  159. if callbacks == nil {
  160. return err
  161. }
  162. if callbacks.OnErrorReturn == nil {
  163. return err
  164. }
  165. return callbacks.OnErrorReturn(c, params, e, prepared, err, i)
  166. }
  167. type QueryCallbacks[O any] struct {
  168. Before func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) error
  169. After func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) error
  170. OnSuccessReturn func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[O]) (response.InfosData[O], error)
  171. OnErrorReturn func(c *api.Context, params request.Params, e entity.Entity, err error, i *infrastructure.Infrastructure) (response.InfosData[O], error)
  172. }
  173. func callbackBeforeQuery[O any](callbacks *QueryCallbacks[O], c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) error {
  174. if callbacks == nil {
  175. return nil
  176. }
  177. if callbacks.Before == nil {
  178. return nil
  179. }
  180. return callbacks.Before(c, params, e, i)
  181. }
  182. func callbackAfterQuery[O any](callbacks *QueryCallbacks[O], c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) error {
  183. if callbacks == nil {
  184. return nil
  185. }
  186. if callbacks.After == nil {
  187. return nil
  188. }
  189. return callbacks.After(c, params, e, i)
  190. }
  191. func callbackOnQuerySuccessReturn[O any](callbacks *QueryCallbacks[O], c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output response.InfosData[O]) (response.InfosData[O], error) {
  192. if callbacks == nil {
  193. return output, nil
  194. }
  195. if callbacks.OnSuccessReturn == nil {
  196. return output, nil
  197. }
  198. return callbacks.OnSuccessReturn(c, params, e, i, output)
  199. }
  200. func callbackOnQueryErrorReturn[O any](callbacks *QueryCallbacks[O], c *api.Context, params request.Params, e entity.Entity, err error, i *infrastructure.Infrastructure) (response.InfosData[O], error) {
  201. if callbacks == nil {
  202. return response.InfosData[O]{
  203. Infos: make([]O, 0),
  204. }, err
  205. }
  206. if callbacks.OnErrorReturn == nil {
  207. return response.InfosData[O]{
  208. Infos: make([]O, 0),
  209. }, err
  210. }
  211. return callbacks.OnErrorReturn(c, params, e, err, i)
  212. }
  213. type GetByIDCallbacks[O any] struct {
  214. Before func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) error
  215. After func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) error
  216. OnSuccessReturn func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output O) (O, error)
  217. OnErrorReturn func(c *api.Context, params request.Params, e entity.Entity, err error, i *infrastructure.Infrastructure) (O, error)
  218. }
  219. func callbackBeforeGetByID[O any](callbacks *GetByIDCallbacks[O], c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) error {
  220. if callbacks == nil {
  221. return nil
  222. }
  223. if callbacks.Before == nil {
  224. return nil
  225. }
  226. return callbacks.Before(c, params, e, i)
  227. }
  228. func callbackAfterGetByID[O any](callbacks *GetByIDCallbacks[O], c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) error {
  229. if callbacks == nil {
  230. return nil
  231. }
  232. if callbacks.After == nil {
  233. return nil
  234. }
  235. return callbacks.After(c, params, e, i)
  236. }
  237. func callbackOnGetByIDSuccessReturn[O any](callbacks *GetByIDCallbacks[O], c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure, output O) (O, error) {
  238. if callbacks == nil {
  239. return output, nil
  240. }
  241. if callbacks.OnSuccessReturn == nil {
  242. return output, nil
  243. }
  244. return callbacks.OnSuccessReturn(c, params, e, i, output)
  245. }
  246. func callbackOnGetByIDErrorReturn[O any](callbacks *GetByIDCallbacks[O], c *api.Context, params request.Params, e entity.Entity, err error, i *infrastructure.Infrastructure) (O, error) {
  247. if callbacks == nil {
  248. return reflectutils.Zero[O](), err
  249. }
  250. if callbacks.OnErrorReturn == nil {
  251. return reflectutils.Zero[O](), err
  252. }
  253. return callbacks.OnErrorReturn(c, params, e, err, i)
  254. }