api.go 7.0 KB

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