123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package service
- import (
- "baize-demo/project/server/application/domain/student"
- "git.sxidc.com/go-framework/baize/convenient/domain/query_rule/rule"
- "git.sxidc.com/go-framework/baize/convenient/entity_crud"
- "git.sxidc.com/go-framework/baize/framework/binding"
- "git.sxidc.com/go-framework/baize/framework/core/api"
- "git.sxidc.com/go-framework/baize/framework/core/api/request"
- "git.sxidc.com/go-framework/baize/framework/core/api/response"
- "git.sxidc.com/go-framework/baize/framework/core/application"
- "git.sxidc.com/go-framework/baize/framework/core/domain"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/clause"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
- )
- var studentService = &StudentService{}
- type StudentService struct{}
- func (svc *StudentService) Init(appInstance *application.App) error {
- err := student.RegisterQueryRule()
- if err != nil {
- return err
- }
- svc.v1(appInstance)
- return nil
- }
- func (svc *StudentService) Destroy() error {
- return nil
- }
- func (svc *StudentService) v1(appInstance *application.App) {
- v1Binder := binding.NewBinder(appInstance.ChooseRouter(api.RouterPrefix, "v1"), appInstance.Infrastructure())
- entity_crud.BindSimple[student.Info](v1Binder, &entity_crud.Simple[student.Info]{
- Entity: &student.Entity{},
- Schema: dbSchema,
- CreateJsonBody: &student.CreateStudentJsonBody{},
- DeleteQueryParams: &student.DeleteStudentQueryParams{},
- UpdateJsonBody: &student.UpdateStudentJsonBody{},
- GetByIDQueryParams: &student.GetStudentByIDQueryParams{},
- }, entity_crud.WithDisableQuery[student.Info]())
- binding.GetBind(v1Binder, &binding.SimpleBindItem[response.InfosData[student.Info]]{
- Path: "/student/query",
- SendResponseFunc: response.SendInfosResponse[student.Info],
- RequestParams: &student.GetStudentsQueryParams{},
- Objects: []domain.Object{&student.Entity{}},
- ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[student.Info], error) {
- errResponse := response.InfosData[student.Info]{
- Infos: make([]student.Info, 0),
- }
- queryParams, err := request.ToConcrete[*student.GetStudentsQueryParams](params)
- if err != nil {
- return errResponse, err
- }
- e, err := domain.ToConcrete[*student.Entity](objects[0])
- if err != nil {
- return errResponse, err
- }
- hasRule, err := rule.HasRule(dbSchema, "global", e.DomainCamelName(), i)
- if err != nil {
- return errResponse, err
- }
- if !hasRule {
- queryFunc := entity_crud.Query[student.Info](domain.TableName(dbSchema, e), "", nil, nil, nil)
- return queryFunc(c, params, objects, i)
- }
- conditionClause, err := rule.FormConditionClause(dbSchema, "global", e.DomainCamelName(), i, e.FormQueryRuleParams())
- if err != nil {
- return errResponse, err
- }
- selectClause := clause.NewSelect(
- nil,
- clause.NewFrom([]clause.Clause{clause.TableName(domain.TableName(dbSchema, e))}),
- clause.NewWhere(conditionClause),
- clause.NewLimit(queryParams.PageNo, queryParams.PageSize))
- selectClauseStr, err := selectClause.Clause()
- if err != nil {
- return errResponse, err
- }
- results, err := database.ExecuteRawSql(i.DBExecutor(), selectClauseStr, selectClause.Args()...)
- if err != nil {
- return errResponse, err
- }
- countClause := clause.NewSelect(
- []string{"COUNT(*) AS total"},
- clause.NewFrom([]clause.Clause{clause.TableName(domain.TableName(dbSchema, e))}),
- clause.NewWhere(conditionClause))
- countClauseStr, err := countClause.Clause()
- if err != nil {
- return errResponse, err
- }
- countResults, err := database.ExecuteRawSql(i.DBExecutor(), countClauseStr, countClause.Args()...)
- if err != nil {
- return errResponse, err
- }
- infos := make([]student.Info, 0)
- err = sql.ParseSqlResult(results, &infos)
- if err != nil {
- return errResponse, err
- }
- return response.InfosData[student.Info]{
- Infos: infos,
- TotalCount: countResults[0].ColumnValueInt64("total"),
- PageNo: queryParams.PageNo,
- }, nil
- },
- })
- }
|