api.go 6.3 KB

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