service.go 17 KB

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