service.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. package entity_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/entity"
  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. "git.sxidc.com/go-tools/utils/reflectutils"
  14. "github.com/pkg/errors"
  15. "reflect"
  16. )
  17. func Create(tableName string, needCreateUserID bool, callbacks *CreateCallbacks, needTx bool) binding.ServiceFunc[string] {
  18. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (string, error) {
  19. object := objects[0]
  20. if object == nil {
  21. return "", errors.New("领域实体为空")
  22. }
  23. dbExecutor := i.DBExecutor()
  24. e, ok := objects[0].(entity.Entity)
  25. if !ok {
  26. return "", errors.New("需要传递领域对象应该为实体")
  27. }
  28. err := e.GenerateID()
  29. if err != nil {
  30. return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  31. }
  32. if needCreateUserID {
  33. userInfo := c.GetUserInfo()
  34. err := domain.SetField(e, entity.FieldCreateUserID, userInfo.GetID())
  35. if err != nil {
  36. return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  37. }
  38. }
  39. prepared, err := callbackPrepareCreate(callbacks, c, params, e, i)
  40. if err != nil {
  41. return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  42. }
  43. err = domain.CheckFieldsForCreate(e, e.GetFieldMap())
  44. if err != nil {
  45. return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  46. }
  47. if domain.HasField(e, entity.FieldCreateUserID) && domain.HasField(e, entity.FieldLastUpdateUserID) {
  48. createUserID, err := domain.Field[string](e, entity.FieldCreateUserID)
  49. if err != nil {
  50. return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  51. }
  52. err = domain.SetField(e, entity.FieldLastUpdateUserID, createUserID)
  53. if err != nil {
  54. return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  55. }
  56. }
  57. err = domain.CheckFieldsForCreate(e, e.GetFieldMap())
  58. if err != nil {
  59. return callbackOnCreateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  60. }
  61. if needTx {
  62. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  63. err = callbackBeforeCreate(callbacks, c, params, e, prepared, i, tx)
  64. if err != nil {
  65. return err
  66. }
  67. err = database.InsertEntity(tx, tableName, e)
  68. if err != nil {
  69. if database.IsErrorDBRecordHasExist(err) {
  70. err = errors.New(e.DomainCNName() + "已存在")
  71. }
  72. return err
  73. }
  74. err = callbackAfterCreate(callbacks, c, params, e, prepared, i, tx)
  75. if err != nil {
  76. return err
  77. }
  78. return nil
  79. })
  80. if err != nil {
  81. return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i)
  82. }
  83. } else {
  84. err = callbackBeforeCreate(callbacks, c, params, e, prepared, i, nil)
  85. if err != nil {
  86. return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i)
  87. }
  88. err = database.InsertEntity(dbExecutor, tableName, e)
  89. if err != nil {
  90. if database.IsErrorDBRecordHasExist(err) {
  91. err = errors.New(e.DomainCNName() + "已存在")
  92. }
  93. return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i)
  94. }
  95. err = callbackAfterCreate(callbacks, c, params, e, prepared, i, nil)
  96. if err != nil {
  97. return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i)
  98. }
  99. }
  100. return callbackOnCreateSuccessReturn(callbacks, c, params, e, prepared, i, e.GetID())
  101. }
  102. }
  103. func Delete(tableName string, callbacks *DeleteCallbacks, needTx bool) binding.ServiceFunc[any] {
  104. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  105. object := objects[0]
  106. if object == nil {
  107. return nil, errors.New("领域实体为空")
  108. }
  109. dbExecutor := i.DBExecutor()
  110. e, ok := objects[0].(entity.Entity)
  111. if !ok {
  112. return nil, errors.New("需要传递领域对象应该为实体")
  113. }
  114. prepared, err := callbackPrepareDelete(callbacks, c, params, e, i)
  115. if err != nil {
  116. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  117. }
  118. err = domain.CheckFieldsForDelete(e, e.GetFieldMap())
  119. if err != nil {
  120. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  121. }
  122. if needTx {
  123. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  124. err = callbackBeforeDelete(callbacks, c, params, e, prepared, i, tx)
  125. if err != nil {
  126. return err
  127. }
  128. err = database.DeleteEntity(tx, tableName, e)
  129. if err != nil {
  130. return err
  131. }
  132. err = callbackAfterDelete(callbacks, c, params, e, prepared, i, tx)
  133. if err != nil {
  134. return err
  135. }
  136. return nil
  137. })
  138. if err != nil {
  139. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
  140. }
  141. } else {
  142. err = callbackBeforeDelete(callbacks, c, params, e, prepared, i, nil)
  143. if err != nil {
  144. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
  145. }
  146. err = database.DeleteEntity(dbExecutor, tableName, e)
  147. if err != nil {
  148. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
  149. }
  150. err = callbackAfterDelete(callbacks, c, params, e, prepared, i, nil)
  151. if err != nil {
  152. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
  153. }
  154. }
  155. return nil, callbackOnDeleteSuccessReturn(callbacks, c, params, e, prepared, i)
  156. }
  157. }
  158. func Update(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallbacks, needTx bool) binding.ServiceFunc[any] {
  159. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  160. object := objects[0]
  161. if object == nil {
  162. return nil, errors.New("领域实体为空")
  163. }
  164. dbExecutor := i.DBExecutor()
  165. e, ok := objects[0].(entity.Entity)
  166. if !ok {
  167. return nil, errors.New("需要传递领域对象应该为实体")
  168. }
  169. exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  170. TableName: tableName,
  171. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.GetID()),
  172. })
  173. if err != nil {
  174. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  175. }
  176. if !exist {
  177. err := errors.New(e.DomainCNName() + "不存在")
  178. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  179. }
  180. if needLastUpdateUserID {
  181. userInfo := c.GetUserInfo()
  182. err := domain.SetField(e, entity.FieldLastUpdateUserID, userInfo.GetID())
  183. if err != nil {
  184. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  185. }
  186. }
  187. prepared, err := callbackPrepareUpdate(callbacks, c, params, e, i)
  188. if err != nil {
  189. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  190. }
  191. err = domain.CheckFieldsForUpdate(e, e.GetFieldMap())
  192. if err != nil {
  193. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  194. }
  195. if needTx {
  196. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  197. err = callbackBeforeUpdate(callbacks, c, params, e, prepared, i, tx)
  198. if err != nil {
  199. return err
  200. }
  201. err = database.UpdateEntity(tx, tableName, e)
  202. if err != nil {
  203. return err
  204. }
  205. err = callbackAfterUpdate(callbacks, c, params, e, prepared, i, tx)
  206. if err != nil {
  207. return err
  208. }
  209. return nil
  210. })
  211. if err != nil {
  212. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
  213. }
  214. } else {
  215. err = callbackBeforeUpdate(callbacks, c, params, e, prepared, i, nil)
  216. if err != nil {
  217. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
  218. }
  219. err = database.UpdateEntity(dbExecutor, tableName, e)
  220. if err != nil {
  221. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
  222. }
  223. err = callbackAfterUpdate(callbacks, c, params, e, prepared, i, nil)
  224. if err != nil {
  225. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
  226. }
  227. }
  228. return nil, callbackOnUpdateSuccessReturn(callbacks, c, params, e, prepared, i)
  229. }
  230. }
  231. type ConditionFieldCallback func(conditions *sql.Conditions, fieldName string, columnName string, value any) (hasDeal bool, err error)
  232. type CustomCondition struct {
  233. Conditions *sql.Conditions
  234. OrderBy string
  235. }
  236. type FormCustomConditionFunc func(c *api.Context, params request.Params, e entity.Entity, i *infrastructure.Infrastructure) (*CustomCondition, error)
  237. func Query[O any](tableName string, orderBy string, callbacks *QueryCallbacks[O], conditionFieldCallback ConditionFieldCallback, formCustomConditionFunc FormCustomConditionFunc) binding.ServiceFunc[response.InfosData[O]] {
  238. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[O], error) {
  239. errResponse := response.InfosData[O]{
  240. Infos: make([]O, 0),
  241. }
  242. if params == nil {
  243. return errResponse, errors.New("请求参数为空")
  244. }
  245. object := objects[0]
  246. if object == nil {
  247. return errResponse, errors.New("领域实体为空")
  248. }
  249. dbExecutor := i.DBExecutor()
  250. queryParams, ok := params.(request.QueryRequestParams)
  251. if !ok {
  252. return errResponse, errors.New("请求参数不是Query接口")
  253. }
  254. e, ok := objects[0].(entity.Entity)
  255. if !ok {
  256. return errResponse, errors.New("需要传递领域对象应该为实体")
  257. }
  258. conditions := sql.NewConditions()
  259. fields, err := sql_mapping.DefaultUsage(e)
  260. if err != nil {
  261. return errResponse, err
  262. }
  263. if formCustomConditionFunc == nil {
  264. for _, field := range fields {
  265. hasDeal := false
  266. if conditionFieldCallback != nil {
  267. hasDeal, err = conditionFieldCallback(conditions, field.FieldName, field.ColumnName, field.Value)
  268. if err != nil {
  269. return errResponse, err
  270. }
  271. }
  272. if !hasDeal {
  273. fieldValue := reflect.ValueOf(field.Value)
  274. if !fieldValue.IsZero() {
  275. conditions.Equal(field.ColumnName, field.Value)
  276. }
  277. }
  278. }
  279. } else {
  280. customCondition, err := formCustomConditionFunc(c, params, e, i)
  281. if err != nil {
  282. return errResponse, err
  283. }
  284. conditions = customCondition.Conditions
  285. orderBy = customCondition.OrderBy
  286. }
  287. err = callbackBeforeQuery(callbacks, c, params, e, i)
  288. if err != nil {
  289. return callbackOnQueryErrorReturn(callbacks, c, params, e, err, i)
  290. }
  291. results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  292. TableName: tableName,
  293. Conditions: conditions,
  294. OrderBy: orderBy,
  295. PageNo: queryParams.GetPageNo(),
  296. PageSize: queryParams.GetPageSize(),
  297. })
  298. if err != nil {
  299. return callbackOnQueryErrorReturn(callbacks, c, params, e, err, i)
  300. }
  301. err = callbackAfterQuery(callbacks, c, params, e, i)
  302. if err != nil {
  303. return callbackOnQueryErrorReturn(callbacks, c, params, e, err, i)
  304. }
  305. infos := make([]O, 0)
  306. err = sql.ParseSqlResult(results, &infos)
  307. if err != nil {
  308. return callbackOnQueryErrorReturn(callbacks, c, params, e, err, i)
  309. }
  310. output := response.InfosData[O]{
  311. Infos: infos,
  312. TotalCount: totalCount,
  313. PageNo: queryParams.GetPageNo(),
  314. }
  315. return callbackOnQuerySuccessReturn(callbacks, c, params, e, i, output)
  316. }
  317. }
  318. func GetByID[O any](tableName string, callbacks *GetByIDCallbacks[O]) binding.ServiceFunc[O] {
  319. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (O, error) {
  320. outputZero := reflectutils.Zero[O]()
  321. object := objects[0]
  322. if object == nil {
  323. return outputZero, errors.New("领域实体为空")
  324. }
  325. dbExecutor := i.DBExecutor()
  326. e, ok := objects[0].(entity.Entity)
  327. if !ok {
  328. return outputZero, errors.New("需要传递领域对象应该为实体")
  329. }
  330. err := domain.CheckField(e, entity.FieldID, e.GetFieldMap())
  331. if err != nil {
  332. return callbackOnGetByIDErrorReturn(callbacks, c, params, e, err, i)
  333. }
  334. err = callbackBeforeGetByID(callbacks, c, params, e, i)
  335. if err != nil {
  336. return callbackOnGetByIDErrorReturn(callbacks, c, params, e, err, i)
  337. }
  338. result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  339. TableName: tableName,
  340. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.GetID()),
  341. })
  342. if err != nil {
  343. if database.IsErrorDBRecordNotExist(err) {
  344. err = errors.New(e.DomainCNName() + "不存在")
  345. }
  346. return callbackOnGetByIDErrorReturn(callbacks, c, params, e, err, i)
  347. }
  348. err = callbackAfterGetByID(callbacks, c, params, e, i)
  349. if err != nil {
  350. return callbackOnGetByIDErrorReturn(callbacks, c, params, e, err, i)
  351. }
  352. info := reflectutils.Zero[O]()
  353. err = sql.ParseSqlResult(result, &info)
  354. if err != nil {
  355. return callbackOnGetByIDErrorReturn(callbacks, c, params, e, err, i)
  356. }
  357. return callbackOnGetByIDSuccessReturn(callbacks, c, params, e, i, info)
  358. }
  359. }