service.go 15 KB

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