simple.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package task_runner
  2. import (
  3. "git.sxidc.com/go-framework/baize/convenient/domain/task_runner/task"
  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. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[task.Info]]{
  23. Path: "/task/get",
  24. SendResponseFunc: response.SendInfosResponse[task.Info],
  25. RequestParams: &task.GetTaskQueryParams{},
  26. Objects: []domain.Object{&task.Entity{}},
  27. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[task.Info], error) {
  28. errResponse := response.InfosData[task.Info]{
  29. Infos: make([]task.Info, 0),
  30. TotalCount: 0,
  31. PageNo: 0,
  32. }
  33. queryParams, err := request.ToConcrete[*task.GetTaskQueryParams](params)
  34. if err != nil {
  35. return errResponse, err
  36. }
  37. conditions := sql.NewConditions()
  38. if strutils.IsStringNotEmpty(queryParams.Group) {
  39. conditions.Equal(task.ColumnGroup, queryParams.Group)
  40. }
  41. if strutils.IsStringNotEmpty(queryParams.Status) {
  42. e := task.Entity{Status: queryParams.Status}
  43. err := e.TransferStatus2StatusCode()
  44. if err != nil {
  45. return errResponse, err
  46. }
  47. conditions.Equal(task.ColumnStatusCode, e.StatusCode)
  48. }
  49. if strutils.IsStringNotEmpty(queryParams.StartCreatedTime) {
  50. conditions.GreaterThanAndEqual(entity.ColumnCreatedTime, queryParams.StartCreatedTime)
  51. }
  52. if strutils.IsStringNotEmpty(queryParams.EndCreatedTime) {
  53. conditions.LessThanAndEqual(entity.ColumnCreatedTime, queryParams.EndCreatedTime)
  54. }
  55. results, totalCount, err := database.Query(i.DBExecutor(), &sql.QueryExecuteParams{
  56. TableName: domain.TableName(simple.Schema, &task.Entity{}),
  57. Conditions: conditions,
  58. PageNo: queryParams.PageNo,
  59. PageSize: queryParams.PageSize,
  60. })
  61. if err != nil {
  62. return errResponse, err
  63. }
  64. infos, err := task.FormInfos(results)
  65. if err != nil {
  66. return errResponse, err
  67. }
  68. return response.InfosData[task.Info]{
  69. Infos: infos,
  70. TotalCount: totalCount,
  71. PageNo: queryParams.PageNo,
  72. }, nil
  73. },
  74. })
  75. }
  76. func Bind(app *application.App, simple *Simple) {
  77. binder := binding.NewBinder(app.Api().ChooseRouter(api.RouterPrefix, ""), app.Infrastructure())
  78. simple.bind(binder)
  79. }