service.go 16 KB

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