service.go 10 KB

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