| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package service
- import (
- "baize-demo/project/server/application/domain/student"
- "git.sxidc.com/go-framework/baize/convenient/domain/query_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/sql"
- "git.sxidc.com/go-tools/utils/strutils"
- )
- 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{},
- QueryQueryParams: &student.GetStudentsQueryParams{},
- GetByIDQueryParams: &student.GetStudentByIDQueryParams{},
- })
- binding.GetBind(v1Binder, &binding.SimpleBindItem[response.InfosData[student.Info]]{
- Path: "/student/query/withRule",
- 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 := query_rule.HasRule(dbSchema, "global", e.DomainCamelName(), i)
- if err != nil {
- return errResponse, err
- }
- var conditions *sql.Conditions
- if !hasRule {
- conditions = sql.NewConditions()
- if strutils.IsStringNotEmpty(e.Name) {
- conditions.Equal(student.ColumnName, e.Name)
- }
- } else {
- ruleConditions, err := query_rule.GetRulesAndFormConditions(dbSchema, "global", e.DomainCamelName(), i)
- if err != nil {
- return errResponse, err
- }
- conditions = ruleConditions
- }
- results, totalCount, err := database.Query(i.DBExecutor(), &sql.QueryExecuteParams{
- TableName: domain.TableName(dbSchema, e),
- Conditions: conditions,
- PageNo: queryParams.PageNo,
- PageSize: queryParams.PageSize,
- })
- 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: totalCount,
- PageNo: queryParams.PageNo,
- }, nil
- },
- })
- }
|