service_callbacks.go 11 KB

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