student.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "git.sxidc.com/go-tools/utils/strutils"
  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. QueryQueryParams: &student.GetStudentsQueryParams{},
  39. GetByIDQueryParams: &student.GetStudentByIDQueryParams{},
  40. })
  41. binding.GetBind(v1Binder, &binding.SimpleBindItem[response.InfosData[student.Info]]{
  42. Path: "/student/query/withRule",
  43. SendResponseFunc: response.SendInfosResponse[student.Info],
  44. RequestParams: &student.GetStudentsQueryParams{},
  45. Objects: []domain.Object{&student.Entity{}},
  46. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[student.Info], error) {
  47. errResponse := response.InfosData[student.Info]{
  48. Infos: make([]student.Info, 0),
  49. }
  50. queryParams, err := request.ToConcrete[*student.GetStudentsQueryParams](params)
  51. if err != nil {
  52. return errResponse, err
  53. }
  54. e, err := domain.ToConcrete[*student.Entity](objects[0])
  55. if err != nil {
  56. return errResponse, err
  57. }
  58. hasRule, err := query_rule.HasRule(dbSchema, "global", e.DomainCamelName(), i)
  59. if err != nil {
  60. return errResponse, err
  61. }
  62. var conditions *sql.Conditions
  63. if !hasRule {
  64. conditions = sql.NewConditions()
  65. if strutils.IsStringNotEmpty(e.Name) {
  66. conditions.Equal(student.ColumnName, e.Name)
  67. }
  68. } else {
  69. ruleConditions, err := query_rule.GetRulesAndFormConditions(dbSchema, "global", e.DomainCamelName(), i)
  70. if err != nil {
  71. return errResponse, err
  72. }
  73. conditions = ruleConditions
  74. }
  75. results, totalCount, err := database.Query(i.DBExecutor(), &sql.QueryExecuteParams{
  76. TableName: domain.TableName(dbSchema, e),
  77. Conditions: conditions,
  78. PageNo: queryParams.PageNo,
  79. PageSize: queryParams.PageSize,
  80. })
  81. if err != nil {
  82. return errResponse, err
  83. }
  84. infos := make([]student.Info, 0)
  85. err = sql.ParseSqlResult(results, &infos)
  86. if err != nil {
  87. return errResponse, err
  88. }
  89. return response.InfosData[student.Info]{
  90. Infos: infos,
  91. TotalCount: totalCount,
  92. PageNo: queryParams.PageNo,
  93. }, nil
  94. },
  95. })
  96. }