api.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
  34. Before: func(c *api.Context, params request.Params, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
  35. ruleEntity, err := domain.ToConcrete[*Entity](e)
  36. if err != nil {
  37. return err
  38. }
  39. return ruleEntity.LintRule()
  40. },
  41. After: nil,
  42. OnSuccessReturn: nil,
  43. OnErrorReturn: nil,
  44. }))
  45. binding.PostBind(binder, &binding.SimpleBindItem[response.InfosData[Info]]{
  46. Path: "/queryRule/query",
  47. SendResponseFunc: response.SendInfosResponse[Info],
  48. RequestParams: &GetQueryRulesQueryParams{},
  49. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[Info], error) {
  50. errResponse := response.InfosData[Info]{
  51. Infos: make([]Info, 0),
  52. }
  53. dbExecutor := i.DBExecutor()
  54. jsonBody, err := request.ToConcrete[*GetQueryRulesQueryParams](params)
  55. if err != nil {
  56. return errResponse, err
  57. }
  58. conditions := sql.NewConditions()
  59. if strutils.IsStringNotEmpty(jsonBody.Scope) {
  60. conditions.Equal(ColumnScope, jsonBody.Scope)
  61. }
  62. if strutils.IsStringNotEmpty(jsonBody.DomainName) {
  63. conditions.Equal(ColumnDomainName, jsonBody.DomainName)
  64. }
  65. results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  66. TableName: queryRuleTableName,
  67. Conditions: conditions,
  68. OrderBy: ColumnEnabled + " DESC",
  69. PageNo: jsonBody.PageNo,
  70. PageSize: jsonBody.PageSize,
  71. })
  72. if err != nil {
  73. return errResponse, err
  74. }
  75. infos := make([]Info, 0)
  76. err = sql.ParseSqlResult(results, &infos)
  77. if err != nil {
  78. return errResponse, err
  79. }
  80. return response.InfosData[Info]{
  81. Infos: infos,
  82. TotalCount: totalCount,
  83. PageNo: jsonBody.PageNo,
  84. }, nil
  85. },
  86. })
  87. binding.PostBind(binder, &binding.SimpleBindItem[Info]{
  88. Path: "/queryRule/enabled/get",
  89. SendResponseFunc: response.SendInfoResponse[Info],
  90. RequestParams: &GetEnabledQueryRuleQueryParams{},
  91. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (Info, error) {
  92. dbExecutor := i.DBExecutor()
  93. jsonBody, err := request.ToConcrete[*GetEnabledQueryRuleQueryParams](params)
  94. if err != nil {
  95. return Info{}, err
  96. }
  97. result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  98. TableName: queryRuleTableName,
  99. Conditions: sql.NewConditions().
  100. Equal(ColumnScope, jsonBody.Scope).
  101. Equal(ColumnDomainName, jsonBody.DomainName).
  102. Equal(ColumnEnabled, true),
  103. })
  104. if err != nil {
  105. if database.IsErrorDBRecordNotExist(err) {
  106. return Info{}, errors.New("没有启用的查询规则")
  107. }
  108. return Info{}, err
  109. }
  110. info := new(Info)
  111. err = sql.ParseSqlResult(result, info)
  112. if err != nil {
  113. return Info{}, err
  114. }
  115. return *info, nil
  116. },
  117. })
  118. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  119. Path: "/queryRule/enable",
  120. SendResponseFunc: response.SendMsgResponse,
  121. RequestParams: &EnableQueryRuleJsonBody{},
  122. Objects: []domain.Object{&Entity{}},
  123. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  124. dbExecutor := i.DBExecutor()
  125. e, err := domain.ToConcrete[*Entity](objects[0])
  126. if err != nil {
  127. return nil, err
  128. }
  129. existQueryRuleResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  130. TableName: domain.TableName(simple.Schema, e),
  131. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID),
  132. })
  133. if err != nil {
  134. return nil, err
  135. }
  136. existQueryRuleEntity := new(Entity)
  137. err = sql.ParseSqlResult(existQueryRuleResult, existQueryRuleEntity)
  138. if err != nil {
  139. return nil, err
  140. }
  141. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  142. err := database.Update(tx, &sql.UpdateExecuteParams{
  143. TableName: domain.TableName(simple.Schema, existQueryRuleEntity),
  144. TableRow: sql.NewTableRow().Add(ColumnEnabled, false),
  145. Conditions: sql.NewConditions().
  146. Equal(ColumnScope, existQueryRuleEntity.Scope).
  147. Equal(ColumnDomainName, existQueryRuleEntity.DomainName),
  148. })
  149. if err != nil {
  150. return err
  151. }
  152. err = database.Update(tx, &sql.UpdateExecuteParams{
  153. TableName: domain.TableName(simple.Schema, existQueryRuleEntity),
  154. TableRow: sql.NewTableRow().Add(ColumnEnabled, true),
  155. Conditions: sql.NewConditions().
  156. Equal(entity.ColumnID, existQueryRuleEntity.ID),
  157. })
  158. if err != nil {
  159. return err
  160. }
  161. return nil
  162. })
  163. if err != nil {
  164. return nil, err
  165. }
  166. return nil, nil
  167. },
  168. })
  169. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  170. Path: "/queryRule/disable",
  171. SendResponseFunc: response.SendMsgResponse,
  172. RequestParams: &EnableQueryRuleJsonBody{},
  173. Objects: []domain.Object{&Entity{}},
  174. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  175. dbExecutor := i.DBExecutor()
  176. e, err := domain.ToConcrete[*Entity](objects[0])
  177. if err != nil {
  178. return nil, err
  179. }
  180. err = database.Update(dbExecutor, &sql.UpdateExecuteParams{
  181. TableName: domain.TableName(simple.Schema, e),
  182. TableRow: sql.NewTableRow().Add(ColumnEnabled, false),
  183. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID),
  184. })
  185. if err != nil {
  186. return nil, err
  187. }
  188. return nil, nil
  189. },
  190. })
  191. }