student.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package service
  2. import (
  3. "baize-demo/project/server/application/domain/student"
  4. "git.sxidc.com/go-framework/baize/convenient/domain/query_rule/rule"
  5. "git.sxidc.com/go-framework/baize/convenient/entity_crud"
  6. "git.sxidc.com/go-framework/baize/framework/binding"
  7. "git.sxidc.com/go-framework/baize/framework/core/api"
  8. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  9. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  10. "git.sxidc.com/go-framework/baize/framework/core/application"
  11. "git.sxidc.com/go-framework/baize/framework/core/domain"
  12. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  13. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  14. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/clause"
  15. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  16. )
  17. var studentService = &StudentService{}
  18. type StudentService struct{}
  19. func (svc *StudentService) Init(appInstance *application.App) error {
  20. err := student.RegisterQueryRule()
  21. if err != nil {
  22. return err
  23. }
  24. svc.v1(appInstance)
  25. return nil
  26. }
  27. func (svc *StudentService) Destroy() error {
  28. return nil
  29. }
  30. func (svc *StudentService) v1(appInstance *application.App) {
  31. v1Binder := binding.NewBinder(appInstance.ChooseRouter(api.RouterPrefix, "v1"), appInstance.Infrastructure())
  32. entity_crud.BindSimple[student.Info](v1Binder, &entity_crud.Simple[student.Info]{
  33. Entity: &student.Entity{},
  34. Schema: dbSchema,
  35. CreateJsonBody: &student.CreateStudentJsonBody{},
  36. DeleteQueryParams: &student.DeleteStudentQueryParams{},
  37. UpdateJsonBody: &student.UpdateStudentJsonBody{},
  38. GetByIDQueryParams: &student.GetStudentByIDQueryParams{},
  39. }, entity_crud.WithDisableQuery[student.Info]())
  40. binding.GetBind(v1Binder, &binding.SimpleBindItem[response.InfosData[student.Info]]{
  41. Path: "/student/query",
  42. SendResponseFunc: response.SendInfosResponse[student.Info],
  43. RequestParams: &student.GetStudentsQueryParams{},
  44. Objects: []domain.Object{&student.Entity{}},
  45. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[student.Info], error) {
  46. errResponse := response.InfosData[student.Info]{
  47. Infos: make([]student.Info, 0),
  48. }
  49. queryParams, err := request.ToConcrete[*student.GetStudentsQueryParams](params)
  50. if err != nil {
  51. return errResponse, err
  52. }
  53. e, err := domain.ToConcrete[*student.Entity](objects[0])
  54. if err != nil {
  55. return errResponse, err
  56. }
  57. hasRule, err := rule.HasRule(dbSchema, "global", e.DomainCamelName(), i)
  58. if err != nil {
  59. return errResponse, err
  60. }
  61. if !hasRule {
  62. queryFunc := entity_crud.Query[student.Info](domain.TableName(dbSchema, e), "", nil, nil, nil)
  63. return queryFunc(c, params, objects, i)
  64. }
  65. conditionClause, err := rule.FormConditionClause(dbSchema, "global", e.DomainCamelName(), i, e.FormQueryRuleParams())
  66. if err != nil {
  67. return errResponse, err
  68. }
  69. selectClause := clause.NewSelect(
  70. nil,
  71. clause.NewFrom([]clause.Clause{clause.TableName(domain.TableName(dbSchema, e))}),
  72. clause.NewWhere(conditionClause),
  73. clause.NewLimit(queryParams.PageNo, queryParams.PageSize))
  74. selectClauseStr, err := selectClause.Clause()
  75. if err != nil {
  76. return errResponse, err
  77. }
  78. results, err := database.ExecuteRawSql(i.DBExecutor(), selectClauseStr, selectClause.Args()...)
  79. if err != nil {
  80. return errResponse, err
  81. }
  82. countClause := clause.NewSelect(
  83. []string{"COUNT(*) AS total"},
  84. clause.NewFrom([]clause.Clause{clause.TableName(domain.TableName(dbSchema, e))}),
  85. clause.NewWhere(conditionClause))
  86. countClauseStr, err := countClause.Clause()
  87. if err != nil {
  88. return errResponse, err
  89. }
  90. countResults, err := database.ExecuteRawSql(i.DBExecutor(), countClauseStr, countClause.Args()...)
  91. if err != nil {
  92. return errResponse, err
  93. }
  94. infos := make([]student.Info, 0)
  95. err = sql.ParseSqlResult(results, &infos)
  96. if err != nil {
  97. return errResponse, err
  98. }
  99. return response.InfosData[student.Info]{
  100. Infos: infos,
  101. TotalCount: countResults[0].ColumnValueInt64("total"),
  102. PageNo: queryParams.PageNo,
  103. }, nil
  104. },
  105. })
  106. }