service.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  321. err = callbackBeforeCreate(callbacks, c, params, e, prepared, i, tx)
  322. if err != nil {
  323. return err
  324. }
  325. err = database.InsertEntity(tx, tableName, e)
  326. if err != nil {
  327. if database.IsErrorDBRecordHasExist(err) {
  328. err = errors.New(e.DomainCNName() + "已存在")
  329. }
  330. return err
  331. }
  332. err = callbackAfterCreate(callbacks, c, params, e, prepared, i, tx)
  333. if err != nil {
  334. return err
  335. }
  336. return nil
  337. })
  338. if err != nil {
  339. return callbackOnCreateErrorReturn(callbacks, c, params, e, prepared, err, i)
  340. }
  341. return callbackOnCreateSuccessReturn(callbacks, c, params, e, prepared, i, e.GetID())
  342. }
  343. }
  344. func DeleteTx(tableName string, callbacks *DeleteCallbacks) binding.ServiceFunc[any] {
  345. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  346. object := objects[0]
  347. if object == nil {
  348. return nil, errors.New("领域实体为空")
  349. }
  350. dbExecutor := i.DBExecutor()
  351. e, ok := objects[0].(entity.Entity)
  352. if !ok {
  353. return nil, errors.New("需要传递领域对象应该为实体")
  354. }
  355. prepared, err := callbackPrepareDelete(callbacks, c, params, e, i)
  356. if err != nil {
  357. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  358. }
  359. err = domain.CheckFieldsForDelete(e, e.GetFieldMap())
  360. if err != nil {
  361. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  362. }
  363. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  364. err = callbackBeforeDelete(callbacks, c, params, e, prepared, i, tx)
  365. if err != nil {
  366. return err
  367. }
  368. err = database.DeleteEntity(tx, tableName, e)
  369. if err != nil {
  370. return err
  371. }
  372. err = callbackAfterDelete(callbacks, c, params, e, prepared, i, tx)
  373. if err != nil {
  374. return err
  375. }
  376. return nil
  377. })
  378. if err != nil {
  379. return nil, callbackOnDeleteErrorReturn(callbacks, c, params, e, prepared, err, i)
  380. }
  381. return nil, callbackOnDeleteSuccessReturn(callbacks, c, params, e, prepared, i)
  382. }
  383. }
  384. func UpdateTx(tableName string, needLastUpdateUserID bool, callbacks *UpdateCallbacks) binding.ServiceFunc[any] {
  385. return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  386. object := objects[0]
  387. if object == nil {
  388. return nil, errors.New("领域实体为空")
  389. }
  390. dbExecutor := i.DBExecutor()
  391. e, ok := objects[0].(entity.Entity)
  392. if !ok {
  393. return nil, errors.New("需要传递领域对象应该为实体")
  394. }
  395. exist, err := database.CheckExist(dbExecutor, &sql.CheckExistExecuteParams{
  396. TableName: tableName,
  397. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.GetID()),
  398. })
  399. if err != nil {
  400. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  401. }
  402. if !exist {
  403. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), errors.New(e.DomainCNName()+"不存在"), i)
  404. }
  405. if needLastUpdateUserID {
  406. userInfo := c.GetUserInfo()
  407. err := domain.SetField(e, entity.FieldLastUpdateUserID, userInfo.GetID())
  408. if err != nil {
  409. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  410. }
  411. }
  412. prepared, err := callbackPrepareUpdate(callbacks, c, params, e, i)
  413. if err != nil {
  414. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  415. }
  416. err = domain.CheckFieldsForUpdate(e, e.GetFieldMap())
  417. if err != nil {
  418. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, make(map[string]any), err, i)
  419. }
  420. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  421. err = callbackBeforeUpdate(callbacks, c, params, e, prepared, i, tx)
  422. if err != nil {
  423. return err
  424. }
  425. err = database.UpdateEntity(tx, tableName, e)
  426. if err != nil {
  427. return err
  428. }
  429. err = callbackAfterUpdate(callbacks, c, params, e, prepared, i, tx)
  430. if err != nil {
  431. return err
  432. }
  433. return nil
  434. })
  435. if err != nil {
  436. return nil, callbackOnUpdateErrorReturn(callbacks, c, params, e, prepared, err, i)
  437. }
  438. return nil, callbackOnUpdateSuccessReturn(callbacks, c, params, e, prepared, i)
  439. }
  440. }