|
|
@@ -1,16 +1,18 @@
|
|
|
package query_rule
|
|
|
|
|
|
import (
|
|
|
- "git.sxidc.com/go-framework/baize/convenient/value_object_crud"
|
|
|
+ "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/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"
|
|
|
)
|
|
|
|
|
|
// Simple Bind参数
|
|
|
@@ -20,45 +22,125 @@ type Simple struct {
|
|
|
}
|
|
|
|
|
|
func (simple *Simple) bind(binder *binding.Binder) {
|
|
|
- queryRuleTableName := domain.TableName(simple.Schema, &ValueObject{})
|
|
|
-
|
|
|
- value_object_crud.BindSimple(binder, &value_object_crud.Simple[any]{
|
|
|
- ValueObject: &ValueObject{},
|
|
|
- Schema: simple.Schema,
|
|
|
- CreateJsonBody: &AddQueryRuleJsonBody{},
|
|
|
- DeleteJsonBody: &RemoveQueryRuleJsonBody{},
|
|
|
- }, value_object_crud.WithDisableQuery[any]())
|
|
|
+ queryRuleTableName := domain.TableName(simple.Schema, &Entity{})
|
|
|
|
|
|
binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
|
|
|
Path: "/queryRuleDefinition/get",
|
|
|
SendResponseFunc: response.SendMapResponse,
|
|
|
RequestParams: &GetQueryRuleDefinitionQueryParams{},
|
|
|
- Objects: []domain.Object{&ValueObject{}},
|
|
|
+ Objects: []domain.Object{&Entity{}},
|
|
|
ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
|
|
|
errResponse := map[string]any{
|
|
|
"definition": make([]QueryRuleDefinitionItem, 0),
|
|
|
}
|
|
|
|
|
|
- valueObject, err := domain.ToConcrete[*ValueObject](objects[0])
|
|
|
+ e, err := domain.ToConcrete[*Entity](objects[0])
|
|
|
if err != nil {
|
|
|
return errResponse, err
|
|
|
}
|
|
|
|
|
|
return map[string]any{
|
|
|
- "definition": GetQueryRuleDefinition(valueObject.RuleDomainName),
|
|
|
+ "definition": GetQueryRuleDefinition(e.DomainName),
|
|
|
+ }, nil
|
|
|
+ },
|
|
|
+ })
|
|
|
+
|
|
|
+ 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{
|
|
|
+ Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
|
|
|
+ queryRuleEntity, err := domain.ToConcrete[*Entity](e)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ hasExistQueryRule, err := database.CheckExist(i.DBExecutor(), &sql.CheckExistExecuteParams{
|
|
|
+ TableName: domain.TableName(simple.Schema, &Entity{}),
|
|
|
+ Conditions: sql.NewConditions().
|
|
|
+ Equal(ColumnScope, queryRuleEntity.Scope).
|
|
|
+ Equal(ColumnScope, queryRuleEntity.DomainName),
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if hasExistQueryRule {
|
|
|
+ return nil, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ queryRuleEntity.Enabled = true
|
|
|
+
|
|
|
+ return nil, 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/get",
|
|
|
+ Path: "/queryRule/enabled/get",
|
|
|
SendResponseFunc: response.SendInfoResponse[Info],
|
|
|
- RequestParams: &GetQueryRuleQueryParams{},
|
|
|
- Objects: []domain.Object{&ValueObject{}},
|
|
|
+ RequestParams: &GetEnabledQueryRuleQueryParams{},
|
|
|
ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (Info, error) {
|
|
|
dbExecutor := i.DBExecutor()
|
|
|
|
|
|
- valueObject, err := domain.ToConcrete[*ValueObject](objects[0])
|
|
|
+ jsonBody, err := request.ToConcrete[*GetEnabledQueryRuleQueryParams](params)
|
|
|
if err != nil {
|
|
|
return Info{}, err
|
|
|
}
|
|
|
@@ -66,8 +148,9 @@ func (simple *Simple) bind(binder *binding.Binder) {
|
|
|
result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
|
|
|
TableName: queryRuleTableName,
|
|
|
Conditions: sql.NewConditions().
|
|
|
- Equal(ColumnScope, valueObject.Scope).
|
|
|
- Equal(ColumnDomainName, valueObject.RuleDomainName),
|
|
|
+ Equal(ColumnScope, jsonBody.Scope).
|
|
|
+ Equal(ColumnDomainName, jsonBody.DomainName).
|
|
|
+ Equal(ColumnEnabled, true),
|
|
|
})
|
|
|
if err != nil {
|
|
|
return Info{}, err
|
|
|
@@ -82,6 +165,64 @@ func (simple *Simple) bind(binder *binding.Binder) {
|
|
|
return *info, nil
|
|
|
},
|
|
|
})
|
|
|
+
|
|
|
+ binding.PostBind(binder, &binding.SimpleBindItem[any]{
|
|
|
+ Path: "/queryRule/enable",
|
|
|
+ SendResponseFunc: response.SendMsgResponse,
|
|
|
+ RequestParams: &EnableQueryRuleJsonBody{},
|
|
|
+ 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
|
|
|
+ },
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
func Bind(app *application.App, simple *Simple) {
|