service.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package entity_crud
  2. import (
  3. "git.sxidc.com/go-framework/baize/api"
  4. "git.sxidc.com/go-framework/baize/binding"
  5. "git.sxidc.com/go-framework/baize/binding/request"
  6. "git.sxidc.com/go-framework/baize/binding/response"
  7. "git.sxidc.com/go-framework/baize/domain"
  8. "git.sxidc.com/go-framework/baize/infrastructure"
  9. "git.sxidc.com/go-framework/baize/infrastructure/database"
  10. "git.sxidc.com/go-framework/baize/infrastructure/database/sql"
  11. "git.sxidc.com/go-framework/baize/tag/sql/sql_mapping"
  12. "git.sxidc.com/go-tools/utils/strutils"
  13. "git.sxidc.com/service-supports/fserr"
  14. "reflect"
  15. )
  16. func CommonEntityCreate(tableName string, dbExecutor database.Executor, callbacks *Callbacks[string]) binding.ServiceFunc[string] {
  17. return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
  18. e, ok := objects[0].(domain.Entity)
  19. if !ok {
  20. return "", fserr.New("需要传递领域对象应该为实体")
  21. }
  22. err := e.GenerateID()
  23. if err != nil {
  24. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, "")
  25. }
  26. err = callbackBeforeDBOperate(callbacks, e, dbExecutor)
  27. if err != nil {
  28. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, "")
  29. }
  30. err = database.InsertEntity(dbExecutor, tableName, e)
  31. if err != nil {
  32. if database.IsErrorDBRecordHasExist(err) {
  33. err = fserr.New(e.DomainCNName() + "已存在")
  34. }
  35. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, "")
  36. }
  37. err = callbackAfterDBOperate(callbacks, e, dbExecutor)
  38. if err != nil {
  39. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, "")
  40. }
  41. return callbackOnSuccessReturn(callbacks, e, dbExecutor, e.GetID())
  42. }
  43. }
  44. func CommonEntityDelete(tableName string, dbExecutor database.Executor, callbacks *Callbacks[any]) binding.ServiceFunc[any] {
  45. return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  46. e, ok := objects[0].(domain.Entity)
  47. if !ok {
  48. return nil, fserr.New("需要传递领域对象应该为实体")
  49. }
  50. if strutils.IsStringEmpty(e.GetID()) {
  51. err := fserr.New("领域实体ID为空")
  52. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  53. }
  54. exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  55. TableName: tableName,
  56. Conditions: sql.NewConditions().Equal(e.IDColumnName(), e.GetID()),
  57. })
  58. if err != nil {
  59. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  60. }
  61. if !exist {
  62. err := fserr.New(e.DomainCNName() + "不存在")
  63. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  64. }
  65. err = callbackBeforeDBOperate(callbacks, e, dbExecutor)
  66. if err != nil {
  67. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  68. }
  69. err = database.DeleteEntity(dbExecutor, tableName, e)
  70. if err != nil {
  71. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  72. }
  73. err = callbackAfterDBOperate(callbacks, e, dbExecutor)
  74. if err != nil {
  75. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  76. }
  77. return callbackOnSuccessReturn(callbacks, e, dbExecutor, nil)
  78. }
  79. }
  80. func CommonEntityUpdate(tableName string, dbExecutor database.Executor, callbacks *Callbacks[any]) binding.ServiceFunc[any] {
  81. return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  82. e, ok := objects[0].(domain.Entity)
  83. if !ok {
  84. return nil, fserr.New("需要传递领域对象应该为实体")
  85. }
  86. if strutils.IsStringEmpty(e.GetID()) {
  87. err := fserr.New("领域实体ID为空")
  88. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  89. }
  90. exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  91. TableName: tableName,
  92. Conditions: sql.NewConditions().Equal(e.IDColumnName(), e.GetID()),
  93. })
  94. if err != nil {
  95. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  96. }
  97. if !exist {
  98. err := fserr.New(e.DomainCNName() + "不存在")
  99. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  100. }
  101. err = callbackBeforeDBOperate(callbacks, e, dbExecutor)
  102. if err != nil {
  103. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  104. }
  105. err = database.UpdateEntity(dbExecutor, tableName, e)
  106. if err != nil {
  107. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  108. }
  109. err = callbackAfterDBOperate(callbacks, e, dbExecutor)
  110. if err != nil {
  111. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  112. }
  113. return callbackOnSuccessReturn(callbacks, e, dbExecutor, nil)
  114. }
  115. }
  116. func CommonEntityQuery[O any](tableName string, dbExecutor database.Executor, callbacks *Callbacks[response.InfosData[O]], conditionFieldCallback *domain.ConditionFieldCallback) binding.ServiceFunc[response.InfosData[O]] {
  117. return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[O], error) {
  118. queryDTO, ok := dto.(request.Query)
  119. if !ok {
  120. return response.InfosData[O]{}, fserr.New("DTO不是Query")
  121. }
  122. e, ok := objects[0].(domain.Entity)
  123. if !ok {
  124. return response.InfosData[O]{}, fserr.New("需要传递领域对象应该为实体")
  125. }
  126. conditions := sql.NewConditions()
  127. fields, err := sql_mapping.DefaultUsage(e)
  128. if err != nil {
  129. return response.InfosData[O]{}, err
  130. }
  131. for _, field := range fields {
  132. hasDeal := false
  133. if conditionFieldCallback != nil {
  134. hasDeal = conditionFieldCallback(conditions, field.FieldName, field.ColumnName, field.Value)
  135. }
  136. if !hasDeal {
  137. fieldValue := reflect.ValueOf(field.Value)
  138. if !fieldValue.IsZero() {
  139. conditions.Equal(field.ColumnName, field.Value)
  140. }
  141. }
  142. }
  143. err = callbackBeforeDBOperate(callbacks, e, dbExecutor)
  144. if err != nil {
  145. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, response.InfosData[O]{})
  146. }
  147. results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  148. TableName: tableName,
  149. Conditions: conditions,
  150. PageNo: queryDTO.GetPageNo(),
  151. PageSize: queryDTO.GetPageSize(),
  152. })
  153. if err != nil {
  154. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, response.InfosData[O]{})
  155. }
  156. err = callbackAfterDBOperate(callbacks, e, dbExecutor)
  157. if err != nil {
  158. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, response.InfosData[O]{})
  159. }
  160. infos := make([]O, 0)
  161. err = sql.ParseSqlResult(results, &infos)
  162. if err != nil {
  163. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, response.InfosData[O]{})
  164. }
  165. output := response.InfosData[O]{
  166. Infos: infos,
  167. TotalCount: totalCount,
  168. PageNo: queryDTO.GetPageNo(),
  169. }
  170. return callbackOnSuccessReturn(callbacks, e, dbExecutor, output)
  171. }
  172. }
  173. func CommonEntityQueryByID[O any](tableName string, dbExecutor database.Executor, callbacks *Callbacks[O]) binding.ServiceFunc[O] {
  174. return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (O, error) {
  175. var outputZero O
  176. outputZeroValue := reflect.Zero(reflect.TypeOf(outputZero))
  177. if outputZeroValue.Kind() == reflect.Pointer {
  178. outputZeroValue.Set(reflect.New(outputZeroValue.Type().Elem()))
  179. }
  180. e, ok := objects[0].(domain.Entity)
  181. if !ok {
  182. return outputZero, fserr.New("需要传递领域对象应该为实体")
  183. }
  184. if strutils.IsStringEmpty(e.GetID()) {
  185. err := fserr.New("领域实体ID为空")
  186. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, outputZero)
  187. }
  188. err := callbackBeforeDBOperate(callbacks, e, dbExecutor)
  189. if err != nil {
  190. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, outputZero)
  191. }
  192. result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  193. TableName: tableName,
  194. Conditions: sql.NewConditions().Equal(e.IDColumnName(), e.GetID()),
  195. })
  196. if err != nil {
  197. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, outputZero)
  198. }
  199. err = callbackAfterDBOperate(callbacks, e, dbExecutor)
  200. if err != nil {
  201. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, outputZero)
  202. }
  203. var info O
  204. var infoPointer any
  205. infoPointer = &info
  206. if outputZeroValue.Kind() == reflect.Pointer {
  207. infoPointer = info
  208. }
  209. err = sql.ParseSqlResult(result, infoPointer)
  210. if err != nil {
  211. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, outputZero)
  212. }
  213. return callbackOnSuccessReturn(callbacks, e, dbExecutor, info)
  214. }
  215. }
  216. func CommonEntityCreateTx(tableName string, dbExecutor database.Executor, callbacks *Callbacks[string]) binding.ServiceFunc[string] {
  217. return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
  218. e, ok := objects[0].(domain.Entity)
  219. if !ok {
  220. return "", fserr.New("需要传递领域对象应该为实体")
  221. }
  222. err := e.GenerateID()
  223. if err != nil {
  224. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, "")
  225. }
  226. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  227. err = callbackBeforeDBOperate(callbacks, e, tx)
  228. if err != nil {
  229. return err
  230. }
  231. err = database.InsertEntity(tx, tableName, e)
  232. if err != nil {
  233. if database.IsErrorDBRecordHasExist(err) {
  234. err = fserr.New(e.DomainCNName() + "已存在")
  235. }
  236. return err
  237. }
  238. err = callbackAfterDBOperate(callbacks, e, tx)
  239. if err != nil {
  240. return err
  241. }
  242. return nil
  243. })
  244. if err != nil {
  245. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, "")
  246. }
  247. return callbackOnSuccessReturn(callbacks, e, dbExecutor, e.GetID())
  248. }
  249. }
  250. func CommonEntityDeleteTx(tableName string, dbExecutor database.Executor, callbacks *Callbacks[any]) binding.ServiceFunc[any] {
  251. return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  252. e, ok := objects[0].(domain.Entity)
  253. if !ok {
  254. return nil, fserr.New("需要传递领域对象应该为实体")
  255. }
  256. if strutils.IsStringEmpty(e.GetID()) {
  257. err := fserr.New("领域实体ID为空")
  258. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  259. }
  260. exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  261. TableName: tableName,
  262. Conditions: sql.NewConditions().Equal(e.IDColumnName(), e.GetID()),
  263. })
  264. if err != nil {
  265. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  266. }
  267. if !exist {
  268. err := fserr.New(e.DomainCNName() + "不存在")
  269. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  270. }
  271. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  272. err = callbackBeforeDBOperate(callbacks, e, tx)
  273. if err != nil {
  274. return err
  275. }
  276. err = database.DeleteEntity(tx, tableName, e)
  277. if err != nil {
  278. return err
  279. }
  280. err = callbackAfterDBOperate(callbacks, e, tx)
  281. if err != nil {
  282. return err
  283. }
  284. return nil
  285. })
  286. if err != nil {
  287. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  288. }
  289. return callbackOnSuccessReturn(callbacks, e, dbExecutor, nil)
  290. }
  291. }
  292. func CommonEntityUpdateTx(tableName string, dbExecutor database.Executor, callbacks *Callbacks[any]) binding.ServiceFunc[any] {
  293. return func(c *api.Context, dto request.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  294. e, ok := objects[0].(domain.Entity)
  295. if !ok {
  296. return nil, fserr.New("需要传递领域对象应该为实体")
  297. }
  298. if strutils.IsStringEmpty(e.GetID()) {
  299. err := fserr.New("领域实体ID为空")
  300. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  301. }
  302. exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  303. TableName: tableName,
  304. Conditions: sql.NewConditions().Equal(e.IDColumnName(), e.GetID()),
  305. })
  306. if err != nil {
  307. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  308. }
  309. if !exist {
  310. err := fserr.New(e.DomainCNName() + "不存在")
  311. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  312. }
  313. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  314. err = callbackBeforeDBOperate(callbacks, e, tx)
  315. if err != nil {
  316. return err
  317. }
  318. err = database.UpdateEntity(tx, tableName, e)
  319. if err != nil {
  320. return err
  321. }
  322. err = callbackAfterDBOperate(callbacks, e, tx)
  323. if err != nil {
  324. return err
  325. }
  326. return nil
  327. })
  328. if err != nil {
  329. return callbackOnErrorReturn(callbacks, e, err, dbExecutor, nil)
  330. }
  331. return callbackOnSuccessReturn(callbacks, e, dbExecutor, nil)
  332. }
  333. }