service.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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) 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, valueObject, i)
  28. if err != nil {
  29. return nil, callbackOnCreateErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
  30. }
  31. err = valueObject.ForCreate()
  32. if err != nil {
  33. return nil, callbackOnCreateErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
  34. }
  35. err = callbackBeforeCreate(callbacks, c, valueObject, prepared, i, nil)
  36. if err != nil {
  37. return nil, callbackOnCreateErrorReturn(callbacks, c, valueObject, prepared, err, i)
  38. }
  39. err = database.InsertEntity(dbExecutor, tableName, valueObject)
  40. if err != nil {
  41. if database.IsErrorDBRecordHasExist(err) {
  42. err = errors.New(valueObject.DomainCNName() + "已存在")
  43. }
  44. return nil, callbackOnCreateErrorReturn(callbacks, c, valueObject, prepared, err, i)
  45. }
  46. err = callbackAfterCreate(callbacks, c, valueObject, prepared, i, nil)
  47. if err != nil {
  48. return nil, callbackOnCreateErrorReturn(callbacks, c, valueObject, prepared, err, i)
  49. }
  50. return nil, callbackOnCreateSuccessReturn(callbacks, c, valueObject, prepared, i)
  51. }
  52. }
  53. func Delete(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[any] {
  54. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  55. object := objects[0]
  56. if object == nil {
  57. return nil, errors.New("领域实体为空")
  58. }
  59. dbExecutor := i.DBExecutor()
  60. valueObject, ok := objects[0].(value_object.ValueObject)
  61. if !ok {
  62. return nil, errors.New("需要传递领域对象应该为值对象")
  63. }
  64. prepared, err := callbackPrepareDelete(callbacks, c, valueObject, i)
  65. if err != nil {
  66. return nil, callbackOnDeleteErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
  67. }
  68. err = valueObject.CheckKeyFields()
  69. if err != nil {
  70. return nil, callbackOnDeleteErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
  71. }
  72. err = callbackBeforeDelete(callbacks, c, valueObject, prepared, i, nil)
  73. if err != nil {
  74. return nil, callbackOnDeleteErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
  75. }
  76. err = database.DeleteEntity(dbExecutor, tableName, valueObject)
  77. if err != nil {
  78. return nil, callbackOnDeleteErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
  79. }
  80. err = callbackAfterDelete(callbacks, c, valueObject, prepared, i, nil)
  81. if err != nil {
  82. return nil, callbackOnDeleteErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
  83. }
  84. return nil, callbackOnDeleteSuccessReturn(callbacks, c, valueObject, prepared, i)
  85. }
  86. }
  87. type ConditionFieldCallback func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error)
  88. func Query[O any](tableName string, orderBy string, callbacks *QueryCallbacks[O], conditionFieldCallback ConditionFieldCallback) binding.ServiceFunc[response.InfosData[O]] {
  89. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[O], error) {
  90. errResponse := response.InfosData[O]{
  91. Infos: make([]O, 0),
  92. }
  93. if params == nil {
  94. return errResponse, errors.New("请求参数为空")
  95. }
  96. object := objects[0]
  97. if object == nil {
  98. return errResponse, errors.New("领域实体为空")
  99. }
  100. dbExecutor := i.DBExecutor()
  101. queryParams, ok := params.(request.QueryRequestParams)
  102. if !ok {
  103. return errResponse, errors.New("请求参数不是Query接口")
  104. }
  105. valueObject, ok := objects[0].(value_object.ValueObject)
  106. if !ok {
  107. return errResponse, errors.New("需要传递领域对象应该为值对象")
  108. }
  109. conditions := sql.NewConditions()
  110. fields, err := sql_mapping.DefaultUsage(valueObject)
  111. if err != nil {
  112. return errResponse, err
  113. }
  114. for _, field := range fields {
  115. hasDeal := false
  116. if conditionFieldCallback != nil {
  117. hasDeal, err = conditionFieldCallback(conditions, field.FieldName, field.ColumnName, field.Value)
  118. if err != nil {
  119. return errResponse, err
  120. }
  121. }
  122. if !hasDeal {
  123. fieldValue := reflect.ValueOf(field.Value)
  124. if !fieldValue.IsZero() {
  125. conditions.Equal(field.ColumnName, field.Value)
  126. }
  127. }
  128. }
  129. err = callbackBeforeQuery(callbacks, c, valueObject, i)
  130. if err != nil {
  131. return callbackOnQueryErrorReturn(callbacks, c, valueObject, err, i)
  132. }
  133. results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  134. TableName: tableName,
  135. Conditions: conditions,
  136. OrderBy: orderBy,
  137. PageNo: queryParams.GetPageNo(),
  138. PageSize: queryParams.GetPageSize(),
  139. })
  140. if err != nil {
  141. return callbackOnQueryErrorReturn(callbacks, c, valueObject, err, i)
  142. }
  143. err = callbackAfterQuery(callbacks, c, valueObject, i)
  144. if err != nil {
  145. return callbackOnQueryErrorReturn(callbacks, c, valueObject, err, i)
  146. }
  147. infos := make([]O, 0)
  148. err = sql.ParseSqlResult(results, &infos)
  149. if err != nil {
  150. return callbackOnQueryErrorReturn(callbacks, c, valueObject, err, i)
  151. }
  152. output := response.InfosData[O]{
  153. Infos: infos,
  154. TotalCount: totalCount,
  155. PageNo: queryParams.GetPageNo(),
  156. }
  157. return callbackOnQuerySuccessReturn(callbacks, c, valueObject, i, output)
  158. }
  159. }
  160. func CreateTx(tableName string, callbacks *CreateCallbacks) binding.ServiceFunc[any] {
  161. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  162. object := objects[0]
  163. if object == nil {
  164. return nil, errors.New("领域实体为空")
  165. }
  166. dbExecutor := i.DBExecutor()
  167. valueObject, ok := objects[0].(value_object.ValueObject)
  168. if !ok {
  169. return nil, errors.New("需要传递领域对象应该为值对象")
  170. }
  171. prepared, err := callbackPrepareCreate(callbacks, c, valueObject, i)
  172. if err != nil {
  173. return nil, callbackOnCreateErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
  174. }
  175. err = valueObject.ForCreate()
  176. if err != nil {
  177. return nil, callbackOnCreateErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
  178. }
  179. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  180. err := callbackBeforeCreate(callbacks, c, valueObject, prepared, i, tx)
  181. if err != nil {
  182. return err
  183. }
  184. err = database.InsertEntity(tx, tableName, valueObject)
  185. if err != nil {
  186. if database.IsErrorDBRecordHasExist(err) {
  187. err = errors.New(valueObject.DomainCNName() + "已存在")
  188. }
  189. return err
  190. }
  191. err = callbackAfterCreate(callbacks, c, valueObject, prepared, i, tx)
  192. if err != nil {
  193. return err
  194. }
  195. return nil
  196. })
  197. if err != nil {
  198. return nil, callbackOnCreateErrorReturn(callbacks, c, valueObject, prepared, err, i)
  199. }
  200. return nil, callbackOnCreateSuccessReturn(callbacks, c, valueObject, prepared, i)
  201. }
  202. }
  203. func DeleteTx(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[any] {
  204. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  205. object := objects[0]
  206. if object == nil {
  207. return nil, errors.New("领域实体为空")
  208. }
  209. dbExecutor := i.DBExecutor()
  210. valueObject, ok := objects[0].(value_object.ValueObject)
  211. if !ok {
  212. return nil, errors.New("需要传递领域对象应该为值对象")
  213. }
  214. prepared, err := callbackPrepareDelete(callbacks, c, valueObject, i)
  215. if err != nil {
  216. return nil, callbackOnDeleteErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
  217. }
  218. err = valueObject.CheckKeyFields()
  219. if err != nil {
  220. return nil, callbackOnDeleteErrorReturn(callbacks, c, valueObject, make(map[string]any), err, i)
  221. }
  222. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  223. err = callbackBeforeDelete(callbacks, c, valueObject, prepared, i, tx)
  224. if err != nil {
  225. return err
  226. }
  227. err = database.DeleteEntity(tx, tableName, valueObject)
  228. if err != nil {
  229. return err
  230. }
  231. err = callbackAfterDelete(callbacks, c, valueObject, prepared, i, tx)
  232. if err != nil {
  233. return err
  234. }
  235. return nil
  236. })
  237. if err != nil {
  238. return nil, callbackOnDeleteErrorReturn(callbacks, c, valueObject, prepared, err, i)
  239. }
  240. return nil, callbackOnDeleteSuccessReturn(callbacks, c, valueObject, prepared, i)
  241. }
  242. }