api.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package query_rule
  2. import (
  3. "git.sxidc.com/go-framework/baize/convenient/entity_crud"
  4. "git.sxidc.com/go-framework/baize/framework/binding"
  5. "git.sxidc.com/go-framework/baize/framework/core/api"
  6. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  7. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  8. "git.sxidc.com/go-framework/baize/framework/core/application"
  9. "git.sxidc.com/go-framework/baize/framework/core/domain"
  10. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  11. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  12. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  13. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  14. "git.sxidc.com/go-tools/utils/strutils"
  15. "github.com/pkg/errors"
  16. )
  17. // Simple Bind参数
  18. type Simple struct {
  19. // schema
  20. Schema string
  21. }
  22. func (simple *Simple) bind(binder *binding.Binder) {
  23. queryRuleTableName := domain.TableName(simple.Schema, &Entity{})
  24. binding.GetBind(binder, &binding.SimpleBindItem[map[string]any]{
  25. Path: "/queryRuleDefinition/get",
  26. SendResponseFunc: response.SendMapResponse,
  27. RequestParams: &GetQueryRuleDefinitionQueryParams{},
  28. Objects: []domain.Object{&Entity{}},
  29. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
  30. errResponse := map[string]any{
  31. "definition": make([]QueryRuleDefinitionItem, 0),
  32. }
  33. e, err := domain.ToConcrete[*Entity](objects[0])
  34. if err != nil {
  35. return errResponse, err
  36. }
  37. return map[string]any{
  38. "definition": GetQueryRuleDefinition(e.DomainName),
  39. }, nil
  40. },
  41. })
  42. entity_crud.BindSimple(binder,
  43. &entity_crud.Simple[any]{
  44. Entity: &Entity{},
  45. Schema: simple.Schema,
  46. CreateJsonBody: &AddQueryRuleJsonBody{},
  47. DeleteQueryParams: &RemoveQueryRuleQueryParams{},
  48. },
  49. entity_crud.WithDisableUpdate(),
  50. entity_crud.WithDisableQuery[any](),
  51. entity_crud.WithDisableGetByID[any]())
  52. binding.PostBind(binder, &binding.SimpleBindItem[response.InfosData[Info]]{
  53. Path: "/queryRule/query",
  54. SendResponseFunc: response.SendInfosResponse[Info],
  55. RequestParams: &GetQueryRulesQueryParams{},
  56. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[Info], error) {
  57. errResponse := response.InfosData[Info]{
  58. Infos: make([]Info, 0),
  59. }
  60. dbExecutor := i.DBExecutor()
  61. jsonBody, err := request.ToConcrete[*GetQueryRulesQueryParams](params)
  62. if err != nil {
  63. return errResponse, err
  64. }
  65. conditions := sql.NewConditions()
  66. if strutils.IsStringNotEmpty(jsonBody.Scope) {
  67. conditions.Equal(ColumnScope, jsonBody.Scope)
  68. }
  69. if strutils.IsStringNotEmpty(jsonBody.DomainName) {
  70. conditions.Equal(ColumnDomainName, jsonBody.DomainName)
  71. }
  72. results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  73. TableName: queryRuleTableName,
  74. Conditions: conditions,
  75. OrderBy: ColumnEnabled + " DESC",
  76. PageNo: jsonBody.PageNo,
  77. PageSize: jsonBody.PageSize,
  78. })
  79. if err != nil {
  80. return errResponse, err
  81. }
  82. infos := make([]Info, 0)
  83. err = sql.ParseSqlResult(results, &infos)
  84. if err != nil {
  85. return errResponse, err
  86. }
  87. return response.InfosData[Info]{
  88. Infos: infos,
  89. TotalCount: totalCount,
  90. PageNo: jsonBody.PageNo,
  91. }, nil
  92. },
  93. })
  94. binding.PostBind(binder, &binding.SimpleBindItem[Info]{
  95. Path: "/queryRule/enabled/get",
  96. SendResponseFunc: response.SendInfoResponse[Info],
  97. RequestParams: &GetEnabledQueryRuleQueryParams{},
  98. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (Info, error) {
  99. dbExecutor := i.DBExecutor()
  100. jsonBody, err := request.ToConcrete[*GetEnabledQueryRuleQueryParams](params)
  101. if err != nil {
  102. return Info{}, err
  103. }
  104. result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  105. TableName: queryRuleTableName,
  106. Conditions: sql.NewConditions().
  107. Equal(ColumnScope, jsonBody.Scope).
  108. Equal(ColumnDomainName, jsonBody.DomainName).
  109. Equal(ColumnEnabled, true),
  110. })
  111. if err != nil {
  112. if database.IsErrorDBRecordNotExist(err) {
  113. return Info{}, errors.New("没有启用的查询规则")
  114. }
  115. return Info{}, err
  116. }
  117. info := new(Info)
  118. err = sql.ParseSqlResult(result, info)
  119. if err != nil {
  120. return Info{}, err
  121. }
  122. return *info, nil
  123. },
  124. })
  125. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  126. Path: "/queryRule/enable",
  127. SendResponseFunc: response.SendMsgResponse,
  128. RequestParams: &EnableQueryRuleJsonBody{},
  129. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  130. dbExecutor := i.DBExecutor()
  131. e, err := domain.ToConcrete[*Entity](objects[0])
  132. if err != nil {
  133. return nil, err
  134. }
  135. existQueryRuleResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  136. TableName: domain.TableName(simple.Schema, e),
  137. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID),
  138. })
  139. if err != nil {
  140. return nil, err
  141. }
  142. existQueryRuleEntity := new(Entity)
  143. err = sql.ParseSqlResult(existQueryRuleResult, existQueryRuleEntity)
  144. if err != nil {
  145. return nil, err
  146. }
  147. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  148. err := database.Update(tx, &sql.UpdateExecuteParams{
  149. TableName: domain.TableName(simple.Schema, existQueryRuleEntity),
  150. TableRow: sql.NewTableRow().Add(ColumnEnabled, false),
  151. Conditions: sql.NewConditions().
  152. Equal(ColumnScope, existQueryRuleEntity.Scope).
  153. Equal(ColumnDomainName, existQueryRuleEntity.DomainName),
  154. })
  155. if err != nil {
  156. return err
  157. }
  158. err = database.Update(tx, &sql.UpdateExecuteParams{
  159. TableName: domain.TableName(simple.Schema, existQueryRuleEntity),
  160. TableRow: sql.NewTableRow().Add(ColumnEnabled, true),
  161. Conditions: sql.NewConditions().
  162. Equal(entity.ColumnID, existQueryRuleEntity.ID),
  163. })
  164. if err != nil {
  165. return err
  166. }
  167. return nil
  168. })
  169. if err != nil {
  170. return nil, err
  171. }
  172. return nil, nil
  173. },
  174. })
  175. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  176. Path: "/queryRule/disable",
  177. SendResponseFunc: response.SendMsgResponse,
  178. RequestParams: &EnableQueryRuleJsonBody{},
  179. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  180. dbExecutor := i.DBExecutor()
  181. e, err := domain.ToConcrete[*Entity](objects[0])
  182. if err != nil {
  183. return nil, err
  184. }
  185. err = database.Update(dbExecutor, &sql.UpdateExecuteParams{
  186. TableName: domain.TableName(simple.Schema, e),
  187. TableRow: sql.NewTableRow().Add(ColumnEnabled, false),
  188. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID),
  189. })
  190. if err != nil {
  191. return nil, err
  192. }
  193. return nil, nil
  194. },
  195. })
  196. }
  197. func Bind(app *application.App, simple *Simple) {
  198. binder := binding.NewBinder(app.Api().ChooseRouter(api.RouterPrefix, ""), app.Infrastructure())
  199. simple.bind(binder)
  200. }