123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- 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, params request.Params, 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
- },
- })
- }
|