package rule import ( "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/domain" "git.sxidc.com/go-framework/baize/framework/core/domain/entity" "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" "github.com/pkg/errors" ) // Simple Bind参数 type Simple struct { // schema Schema string } func (simple *Simple) Bind(binder *binding.Binder) { queryRuleTableName := domain.TableName(simple.Schema, &Entity{}) entity_crud.BindSimple(binder, &entity_crud.Simple[any]{ Entity: &Entity{}, Schema: simple.Schema, CreateJsonBody: &AddQueryRuleJsonBody{}, DeleteQueryParams: &RemoveQueryRuleQueryParams{}, }, entity_crud.WithDisableUpdate(), entity_crud.WithDisableQuery[any](), entity_crud.WithDisableGetByID[any](), entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{ Before: func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error { ruleEntity, err := domain.ToConcrete[*Entity](e) if err != nil { return err } return ruleEntity.LintRule() }, After: nil, OnSuccessReturn: nil, OnErrorReturn: nil, })) binding.PostBind(binder, &binding.SimpleBindItem[response.InfosData[Info]]{ Path: "/queryRule/query", SendResponseFunc: response.SendInfosResponse[Info], RequestParams: &GetQueryRulesQueryParams{}, ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[Info], error) { errResponse := response.InfosData[Info]{ Infos: make([]Info, 0), } dbExecutor := i.DBExecutor() jsonBody, err := request.ToConcrete[*GetQueryRulesQueryParams](params) if err != nil { return errResponse, err } conditions := sql.NewConditions() if strutils.IsStringNotEmpty(jsonBody.Scope) { conditions.Equal(ColumnScope, jsonBody.Scope) } if strutils.IsStringNotEmpty(jsonBody.DomainName) { conditions.Equal(ColumnDomainName, jsonBody.DomainName) } results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{ TableName: queryRuleTableName, Conditions: conditions, OrderBy: ColumnEnabled + " DESC", PageNo: jsonBody.PageNo, PageSize: jsonBody.PageSize, }) if err != nil { return errResponse, err } infos := make([]Info, 0) err = sql.ParseSqlResult(results, &infos) if err != nil { return errResponse, err } return response.InfosData[Info]{ Infos: infos, TotalCount: totalCount, PageNo: jsonBody.PageNo, }, nil }, }) binding.PostBind(binder, &binding.SimpleBindItem[Info]{ Path: "/queryRule/enabled/get", SendResponseFunc: response.SendInfoResponse[Info], RequestParams: &GetEnabledQueryRuleQueryParams{}, ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (Info, error) { dbExecutor := i.DBExecutor() jsonBody, err := request.ToConcrete[*GetEnabledQueryRuleQueryParams](params) if err != nil { return Info{}, err } result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{ TableName: queryRuleTableName, Conditions: sql.NewConditions(). Equal(ColumnScope, jsonBody.Scope). Equal(ColumnDomainName, jsonBody.DomainName). Equal(ColumnEnabled, true), }) if err != nil { if database.IsErrorDBRecordNotExist(err) { return Info{}, errors.New("没有启用的查询规则") } return Info{}, err } info := new(Info) err = sql.ParseSqlResult(result, info) if err != nil { return Info{}, err } return *info, nil }, }) binding.PostBind(binder, &binding.SimpleBindItem[any]{ Path: "/queryRule/enable", SendResponseFunc: response.SendMsgResponse, RequestParams: &EnableQueryRuleJsonBody{}, Objects: []domain.Object{&Entity{}}, ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) { dbExecutor := i.DBExecutor() e, err := domain.ToConcrete[*Entity](objects[0]) if err != nil { return nil, err } existQueryRuleResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{ TableName: domain.TableName(simple.Schema, e), Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID), }) if err != nil { return nil, err } existQueryRuleEntity := new(Entity) err = sql.ParseSqlResult(existQueryRuleResult, existQueryRuleEntity) if err != nil { return nil, err } err = database.Transaction(dbExecutor, func(tx database.Executor) error { err := database.Update(tx, &sql.UpdateExecuteParams{ TableName: domain.TableName(simple.Schema, existQueryRuleEntity), TableRow: sql.NewTableRow().Add(ColumnEnabled, false), Conditions: sql.NewConditions(). Equal(ColumnScope, existQueryRuleEntity.Scope). Equal(ColumnDomainName, existQueryRuleEntity.DomainName), }) if err != nil { return err } err = database.Update(tx, &sql.UpdateExecuteParams{ TableName: domain.TableName(simple.Schema, existQueryRuleEntity), TableRow: sql.NewTableRow().Add(ColumnEnabled, true), Conditions: sql.NewConditions(). Equal(entity.ColumnID, existQueryRuleEntity.ID), }) if err != nil { return err } return nil }) if err != nil { return nil, err } return nil, nil }, }) binding.PostBind(binder, &binding.SimpleBindItem[any]{ Path: "/queryRule/disable", SendResponseFunc: response.SendMsgResponse, RequestParams: &EnableQueryRuleJsonBody{}, Objects: []domain.Object{&Entity{}}, ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) { dbExecutor := i.DBExecutor() e, err := domain.ToConcrete[*Entity](objects[0]) if err != nil { return nil, err } err = database.Update(dbExecutor, &sql.UpdateExecuteParams{ TableName: domain.TableName(simple.Schema, e), TableRow: sql.NewTableRow().Add(ColumnEnabled, false), Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID), }) if err != nil { return nil, err } return nil, nil }, }) }