api.go 7.0 KB

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