entity_crud.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package service
  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/domain"
  6. "git.sxidc.com/go-framework/baize/infrastructure"
  7. "git.sxidc.com/go-framework/baize/infrastructure/database"
  8. "git.sxidc.com/go-tools/utils/strutils"
  9. "git.sxidc.com/service-supports/fserr"
  10. )
  11. const (
  12. DatabaseExecutorOperations = "operations"
  13. DatabaseExecutorDataService = "data_service"
  14. )
  15. func CommonEntityCreate(tableName string, databaseExecutorType string, callbacks EntityCRUDCallbacks) binding.ServiceFunc[string] {
  16. return func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
  17. e, ok := objects[0].(domain.Entity)
  18. if !ok {
  19. return "", fserr.New("需要传递领域对象应该为实体")
  20. }
  21. err := e.GenerateID()
  22. if err != nil {
  23. return "", err
  24. }
  25. if callbacks.Before != nil {
  26. err := callbacks.Before(e)
  27. if err != nil {
  28. return "", err
  29. }
  30. }
  31. err = database.InsertEntity(chooseDatabaseExecutor(databaseExecutorType, i), tableName, e)
  32. if err != nil {
  33. if callbacks.Error != nil {
  34. err := callbacks.Error(e, err)
  35. if err != nil {
  36. return "", err
  37. }
  38. }
  39. return "", err
  40. }
  41. if callbacks.Success != nil {
  42. err := callbacks.Success(e)
  43. if err != nil {
  44. return "", err
  45. }
  46. }
  47. return e.ID(), nil
  48. }
  49. }
  50. func CommonEntityDelete(tableName string, databaseExecutorType string, callbacks EntityCRUDCallbacks) binding.ServiceFunc[any] {
  51. return func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  52. e, ok := objects[0].(domain.Entity)
  53. if !ok {
  54. return nil, fserr.New("需要传递领域对象应该为实体")
  55. }
  56. if strutils.IsStringEmpty(e.ID()) {
  57. return nil, fserr.New("领域实体ID为空")
  58. }
  59. if callbacks.Before != nil {
  60. err := callbacks.Before(e)
  61. if err != nil {
  62. return nil, err
  63. }
  64. }
  65. err := database.DeleteEntity(chooseDatabaseExecutor(databaseExecutorType, i), tableName, e)
  66. if err != nil {
  67. if callbacks.Error != nil {
  68. err := callbacks.Error(e, err)
  69. if err != nil {
  70. return nil, err
  71. }
  72. }
  73. return nil, err
  74. }
  75. if callbacks.Success != nil {
  76. err := callbacks.Success(e)
  77. if err != nil {
  78. return nil, err
  79. }
  80. }
  81. return nil, nil
  82. }
  83. }
  84. func CommonEntityUpdate() binding.ServiceFunc[any] {
  85. return func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  86. }
  87. }
  88. func CommonEntityQuery() binding.ServiceFunc[binding.InfosData[any]] {
  89. return func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (binding.InfosData[any], error) {
  90. }
  91. }
  92. func CommonEntityQueryByID() binding.ServiceFunc[any] {
  93. return func(c *api.Context, dto binding.DTO, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  94. }
  95. }
  96. func chooseDatabaseExecutor(dataExecutorType string, i *infrastructure.Infrastructure) database.Executor {
  97. switch dataExecutorType {
  98. case DatabaseExecutorOperations:
  99. return i.DBOperations()
  100. case DatabaseExecutorDataService:
  101. return i.DataService()
  102. default:
  103. return i.DBOperations()
  104. }
  105. }