api.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package query_rule
  2. import (
  3. "errors"
  4. "git.sxidc.com/go-framework/baize/convenient/entity_crud"
  5. "git.sxidc.com/go-framework/baize/framework/binding"
  6. "git.sxidc.com/go-framework/baize/framework/core/api"
  7. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  8. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  9. "git.sxidc.com/go-framework/baize/framework/core/application"
  10. "git.sxidc.com/go-framework/baize/framework/core/domain"
  11. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  12. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  13. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  14. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  15. "git.sxidc.com/go-tools/utils/strutils"
  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. entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
  53. Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
  54. queryRuleEntity, err := domain.ToConcrete[*Entity](e)
  55. if err != nil {
  56. return nil, err
  57. }
  58. hasExistQueryRule, err := database.CheckExist(i.DBExecutor(), &sql.CheckExistExecuteParams{
  59. TableName: domain.TableName(simple.Schema, &Entity{}),
  60. Conditions: sql.NewConditions().
  61. Equal(ColumnScope, queryRuleEntity.Scope).
  62. Equal(ColumnDomainName, queryRuleEntity.DomainName),
  63. })
  64. if err != nil {
  65. return nil, err
  66. }
  67. if hasExistQueryRule {
  68. return nil, nil
  69. }
  70. queryRuleEntity.Enabled = true
  71. return nil, nil
  72. },
  73. }),
  74. entity_crud.WithDeleteCallbacks(&entity_crud.DeleteCallbacks{
  75. Prepare: func(c *api.Context, e entity.Entity, i *infrastructure.Infrastructure) (map[string]any, error) {
  76. queryRuleEntity, err := domain.ToConcrete[*Entity](e)
  77. if err != nil {
  78. return nil, err
  79. }
  80. hasExistQueryRule, err := database.CheckExist(i.DBExecutor(), &sql.CheckExistExecuteParams{
  81. TableName: domain.TableName(simple.Schema, &Entity{}),
  82. Conditions: sql.NewConditions().
  83. Equal(entity.ColumnID, queryRuleEntity.ID).
  84. Equal(ColumnEnabled, true),
  85. })
  86. if err != nil {
  87. return nil, err
  88. }
  89. if hasExistQueryRule {
  90. return nil, errors.New("不能删除启用的规则")
  91. }
  92. return nil, nil
  93. },
  94. }))
  95. binding.PostBind(binder, &binding.SimpleBindItem[response.InfosData[Info]]{
  96. Path: "/queryRule/query",
  97. SendResponseFunc: response.SendInfosResponse[Info],
  98. RequestParams: &GetQueryRulesQueryParams{},
  99. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[Info], error) {
  100. errResponse := response.InfosData[Info]{
  101. Infos: make([]Info, 0),
  102. }
  103. dbExecutor := i.DBExecutor()
  104. jsonBody, err := request.ToConcrete[*GetQueryRulesQueryParams](params)
  105. if err != nil {
  106. return errResponse, err
  107. }
  108. conditions := sql.NewConditions()
  109. if strutils.IsStringNotEmpty(jsonBody.Scope) {
  110. conditions.Equal(ColumnScope, jsonBody.Scope)
  111. }
  112. if strutils.IsStringNotEmpty(jsonBody.DomainName) {
  113. conditions.Equal(ColumnDomainName, jsonBody.DomainName)
  114. }
  115. results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  116. TableName: queryRuleTableName,
  117. Conditions: conditions,
  118. OrderBy: ColumnEnabled + " DESC",
  119. PageNo: jsonBody.PageNo,
  120. PageSize: jsonBody.PageSize,
  121. })
  122. if err != nil {
  123. return errResponse, err
  124. }
  125. infos := make([]Info, 0)
  126. err = sql.ParseSqlResult(results, &infos)
  127. if err != nil {
  128. return errResponse, err
  129. }
  130. return response.InfosData[Info]{
  131. Infos: infos,
  132. TotalCount: totalCount,
  133. PageNo: jsonBody.PageNo,
  134. }, nil
  135. },
  136. })
  137. binding.PostBind(binder, &binding.SimpleBindItem[Info]{
  138. Path: "/queryRule/enabled/get",
  139. SendResponseFunc: response.SendInfoResponse[Info],
  140. RequestParams: &GetEnabledQueryRuleQueryParams{},
  141. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (Info, error) {
  142. dbExecutor := i.DBExecutor()
  143. jsonBody, err := request.ToConcrete[*GetEnabledQueryRuleQueryParams](params)
  144. if err != nil {
  145. return Info{}, err
  146. }
  147. result, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  148. TableName: queryRuleTableName,
  149. Conditions: sql.NewConditions().
  150. Equal(ColumnScope, jsonBody.Scope).
  151. Equal(ColumnDomainName, jsonBody.DomainName).
  152. Equal(ColumnEnabled, true),
  153. })
  154. if err != nil {
  155. return Info{}, err
  156. }
  157. info := new(Info)
  158. err = sql.ParseSqlResult(result, info)
  159. if err != nil {
  160. return Info{}, err
  161. }
  162. return *info, nil
  163. },
  164. })
  165. binding.PostBind(binder, &binding.SimpleBindItem[any]{
  166. Path: "/queryRule/enable",
  167. SendResponseFunc: response.SendMsgResponse,
  168. RequestParams: &EnableQueryRuleJsonBody{},
  169. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
  170. dbExecutor := i.DBExecutor()
  171. e, err := domain.ToConcrete[*Entity](objects[0])
  172. if err != nil {
  173. return nil, err
  174. }
  175. existQueryRuleResult, err := database.QueryOne(dbExecutor, &sql.QueryOneExecuteParams{
  176. TableName: domain.TableName(simple.Schema, e),
  177. Conditions: sql.NewConditions().Equal(entity.ColumnID, e.ID),
  178. })
  179. if err != nil {
  180. return nil, err
  181. }
  182. existQueryRuleEntity := new(Entity)
  183. err = sql.ParseSqlResult(existQueryRuleResult, existQueryRuleEntity)
  184. if err != nil {
  185. return nil, err
  186. }
  187. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  188. err := database.Update(tx, &sql.UpdateExecuteParams{
  189. TableName: domain.TableName(simple.Schema, existQueryRuleEntity),
  190. TableRow: sql.NewTableRow().Add(ColumnEnabled, false),
  191. Conditions: sql.NewConditions().
  192. Equal(ColumnScope, existQueryRuleEntity.Scope).
  193. Equal(ColumnDomainName, existQueryRuleEntity.DomainName),
  194. })
  195. if err != nil {
  196. return err
  197. }
  198. err = database.Update(tx, &sql.UpdateExecuteParams{
  199. TableName: domain.TableName(simple.Schema, existQueryRuleEntity),
  200. TableRow: sql.NewTableRow().Add(ColumnEnabled, true),
  201. Conditions: sql.NewConditions().
  202. Equal(entity.ColumnID, existQueryRuleEntity.ID),
  203. })
  204. if err != nil {
  205. return err
  206. }
  207. return nil
  208. })
  209. if err != nil {
  210. return nil, err
  211. }
  212. return nil, nil
  213. },
  214. })
  215. }
  216. func Bind(app *application.App, simple *Simple) {
  217. binder := binding.NewBinder(app.Api().ChooseRouter(api.RouterPrefix, ""), app.Infrastructure())
  218. simple.bind(binder)
  219. }