student.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package service
  2. import (
  3. "baize-demo/project/server/application/domain/student"
  4. "git.sxidc.com/go-framework/baize/convenient/domain/query_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/sql"
  15. )
  16. var studentService = &StudentService{}
  17. type StudentService struct{}
  18. func (svc *StudentService) Init(appInstance *application.App) error {
  19. err := student.RegisterQueryRule()
  20. if err != nil {
  21. return err
  22. }
  23. svc.v1(appInstance)
  24. return nil
  25. }
  26. func (svc *StudentService) Destroy() error {
  27. return nil
  28. }
  29. func (svc *StudentService) v1(appInstance *application.App) {
  30. v1Binder := binding.NewBinder(appInstance.ChooseRouter(api.RouterPrefix, "v1"), appInstance.Infrastructure())
  31. entity_crud.BindSimple[student.Info](v1Binder, &entity_crud.Simple[student.Info]{
  32. Entity: &student.Entity{},
  33. Schema: dbSchema,
  34. CreateJsonBody: &student.CreateStudentJsonBody{},
  35. DeleteQueryParams: &student.DeleteStudentQueryParams{},
  36. UpdateJsonBody: &student.UpdateStudentJsonBody{},
  37. GetByIDQueryParams: &student.GetStudentByIDQueryParams{},
  38. }, entity_crud.WithDisableQuery[student.Info]())
  39. binding.GetBind(v1Binder, &binding.SimpleBindItem[response.InfosData[student.Info]]{
  40. Path: "/student/query",
  41. SendResponseFunc: response.SendInfosResponse[student.Info],
  42. RequestParams: &student.GetStudentsQueryParams{},
  43. Objects: []domain.Object{&student.Entity{}},
  44. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[student.Info], error) {
  45. errResponse := response.InfosData[student.Info]{
  46. Infos: make([]student.Info, 0),
  47. }
  48. queryParams, err := request.ToConcrete[*student.GetStudentsQueryParams](params)
  49. if err != nil {
  50. return errResponse, err
  51. }
  52. e, err := domain.ToConcrete[*student.Entity](objects[0])
  53. if err != nil {
  54. return errResponse, err
  55. }
  56. hasRule, err := query_rule.HasRule(dbSchema, "global", e.DomainCamelName(), i)
  57. if err != nil {
  58. return errResponse, err
  59. }
  60. if !hasRule {
  61. queryFunc := entity_crud.Query[student.Info](domain.TableName(dbSchema, e), "", nil, nil)
  62. return queryFunc(c, params, objects, i)
  63. }
  64. conditions, err := query_rule.GetRulesAndFormConditions(dbSchema, "global", e.DomainCamelName(), i)
  65. if err != nil {
  66. return errResponse, err
  67. }
  68. results, totalCount, err := database.Query(i.DBExecutor(), &sql.QueryExecuteParams{
  69. TableName: domain.TableName(dbSchema, e),
  70. Conditions: conditions,
  71. PageNo: queryParams.PageNo,
  72. PageSize: queryParams.PageSize,
  73. })
  74. if err != nil {
  75. return errResponse, err
  76. }
  77. infos := make([]student.Info, 0)
  78. err = sql.ParseSqlResult(results, &infos)
  79. if err != nil {
  80. return errResponse, err
  81. }
  82. return response.InfosData[student.Info]{
  83. Infos: infos,
  84. TotalCount: totalCount,
  85. PageNo: queryParams.PageNo,
  86. }, nil
  87. },
  88. })
  89. }