service.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package value_object_crud
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/binding"
  4. "git.sxidc.com/go-framework/baize/framework/core/api"
  5. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  6. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  7. "git.sxidc.com/go-framework/baize/framework/core/domain"
  8. "git.sxidc.com/go-framework/baize/framework/core/domain/value_object"
  9. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  10. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  11. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  12. "git.sxidc.com/go-framework/baize/framework/core/tag/sql/sql_mapping"
  13. "github.com/pkg/errors"
  14. "reflect"
  15. )
  16. func Create(tableName string, callbacks *CreateCallbacks, needTx bool) binding.ServiceFunc[any] {
  17. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  18. object := objects[0]
  19. if object == nil {
  20. return nil, errors.New("领域实体为空")
  21. }
  22. dbExecutor := i.DBExecutor()
  23. valueObject, ok := objects[0].(value_object.ValueObject)
  24. if !ok {
  25. return nil, errors.New("需要传递领域对象应该为值对象")
  26. }
  27. prepared, err := callbackPrepareCreate(callbacks, c, params, valueObject, i)
  28. if err != nil {
  29. return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
  30. }
  31. err = domain.CheckFieldsForCreate(valueObject, valueObject.GetFieldMap())
  32. if err != nil {
  33. return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
  34. }
  35. if needTx {
  36. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  37. err := callbackBeforeCreate(callbacks, c, params, valueObject, prepared, i, tx)
  38. if err != nil {
  39. return err
  40. }
  41. err = database.InsertEntity(tx, tableName, valueObject)
  42. if err != nil {
  43. if database.IsErrorDBRecordHasExist(err) {
  44. err = errors.New(valueObject.DomainCNName() + "已存在")
  45. }
  46. return err
  47. }
  48. err = callbackAfterCreate(callbacks, c, params, valueObject, prepared, i, tx)
  49. if err != nil {
  50. return err
  51. }
  52. return nil
  53. })
  54. if err != nil {
  55. return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
  56. }
  57. } else {
  58. err = callbackBeforeCreate(callbacks, c, params, valueObject, prepared, i, nil)
  59. if err != nil {
  60. return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
  61. }
  62. err = database.InsertEntity(dbExecutor, tableName, valueObject)
  63. if err != nil {
  64. if database.IsErrorDBRecordHasExist(err) {
  65. err = errors.New(valueObject.DomainCNName() + "已存在")
  66. }
  67. return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
  68. }
  69. err = callbackAfterCreate(callbacks, c, params, valueObject, prepared, i, nil)
  70. if err != nil {
  71. return nil, callbackOnCreateErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
  72. }
  73. }
  74. return nil, callbackOnCreateSuccessReturn(callbacks, c, params, valueObject, prepared, i)
  75. }
  76. }
  77. func Delete(tableName string, callbacks *DeleteCallbacks, needTx bool) binding.ServiceFunc[any] {
  78. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  79. object := objects[0]
  80. if object == nil {
  81. return nil, errors.New("领域实体为空")
  82. }
  83. dbExecutor := i.DBExecutor()
  84. valueObject, ok := objects[0].(value_object.ValueObject)
  85. if !ok {
  86. return nil, errors.New("需要传递领域对象应该为值对象")
  87. }
  88. prepared, err := callbackPrepareDelete(callbacks, c, params, valueObject, i)
  89. if err != nil {
  90. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
  91. }
  92. err = domain.CheckFieldsForDelete(valueObject, valueObject.GetFieldMap())
  93. if err != nil {
  94. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
  95. }
  96. if needTx {
  97. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  98. err = callbackBeforeDelete(callbacks, c, params, valueObject, prepared, i, tx)
  99. if err != nil {
  100. return err
  101. }
  102. err = database.DeleteEntity(tx, tableName, valueObject)
  103. if err != nil {
  104. return err
  105. }
  106. err = callbackAfterDelete(callbacks, c, params, valueObject, prepared, i, tx)
  107. if err != nil {
  108. return err
  109. }
  110. return nil
  111. })
  112. if err != nil {
  113. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, prepared, err, i)
  114. }
  115. } else {
  116. err = callbackBeforeDelete(callbacks, c, params, valueObject, prepared, i, nil)
  117. if err != nil {
  118. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
  119. }
  120. err = database.DeleteEntity(dbExecutor, tableName, valueObject)
  121. if err != nil {
  122. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
  123. }
  124. err = callbackAfterDelete(callbacks, c, params, valueObject, prepared, i, nil)
  125. if err != nil {
  126. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, valueObject, make(map[string]any), err, i)
  127. }
  128. }
  129. return nil, callbackOnDeleteSuccessReturn(callbacks, c, params, valueObject, prepared, i)
  130. }
  131. }
  132. type ConditionFieldCallback func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error)
  133. type CustomCondition struct {
  134. Conditions *sql.Conditions
  135. OrderBy string
  136. }
  137. type FormCustomConditionFunc func(c *api.Context, params request.Params, valueObject value_object.ValueObject) (*CustomCondition, error)
  138. func Query[O any](tableName string, orderBy string, callbacks *QueryCallbacks[O], conditionFieldCallback ConditionFieldCallback, formCustomConditionFunc FormCustomConditionFunc) binding.ServiceFunc[response.InfosData[O]] {
  139. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[O], error) {
  140. errResponse := response.InfosData[O]{
  141. Infos: make([]O, 0),
  142. }
  143. if params == nil {
  144. return errResponse, errors.New("请求参数为空")
  145. }
  146. object := objects[0]
  147. if object == nil {
  148. return errResponse, errors.New("领域实体为空")
  149. }
  150. dbExecutor := i.DBExecutor()
  151. queryParams, ok := params.(request.QueryRequestParams)
  152. if !ok {
  153. return errResponse, errors.New("请求参数不是Query接口")
  154. }
  155. valueObject, ok := objects[0].(value_object.ValueObject)
  156. if !ok {
  157. return errResponse, errors.New("需要传递领域对象应该为值对象")
  158. }
  159. conditions := sql.NewConditions()
  160. fields, err := sql_mapping.DefaultUsage(valueObject)
  161. if err != nil {
  162. return errResponse, err
  163. }
  164. if formCustomConditionFunc == nil {
  165. for _, field := range fields {
  166. hasDeal := false
  167. if conditionFieldCallback != nil {
  168. hasDeal, err = conditionFieldCallback(conditions, field.FieldName, field.ColumnName, field.Value)
  169. if err != nil {
  170. return errResponse, err
  171. }
  172. }
  173. if !hasDeal {
  174. fieldValue := reflect.ValueOf(field.Value)
  175. if !fieldValue.IsZero() {
  176. conditions.Equal(field.ColumnName, field.Value)
  177. }
  178. }
  179. }
  180. } else {
  181. customCondition, err := formCustomConditionFunc(c, queryParams, valueObject)
  182. if err != nil {
  183. return errResponse, err
  184. }
  185. conditions = customCondition.Conditions
  186. orderBy = customCondition.OrderBy
  187. }
  188. err = callbackBeforeQuery(callbacks, c, params, valueObject, i)
  189. if err != nil {
  190. return callbackOnQueryErrorReturn(callbacks, c, params, valueObject, err, i)
  191. }
  192. results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  193. TableName: tableName,
  194. Conditions: conditions,
  195. OrderBy: orderBy,
  196. PageNo: queryParams.GetPageNo(),
  197. PageSize: queryParams.GetPageSize(),
  198. })
  199. if err != nil {
  200. return callbackOnQueryErrorReturn(callbacks, c, params, valueObject, err, i)
  201. }
  202. err = callbackAfterQuery(callbacks, c, params, valueObject, i)
  203. if err != nil {
  204. return callbackOnQueryErrorReturn(callbacks, c, params, valueObject, err, i)
  205. }
  206. infos := make([]O, 0)
  207. err = sql.ParseSqlResult(results, &infos)
  208. if err != nil {
  209. return callbackOnQueryErrorReturn(callbacks, c, params, valueObject, err, i)
  210. }
  211. output := response.InfosData[O]{
  212. Infos: infos,
  213. TotalCount: totalCount,
  214. PageNo: queryParams.GetPageNo(),
  215. }
  216. return callbackOnQuerySuccessReturn(callbacks, c, params, valueObject, i, output)
  217. }
  218. }