search.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package manual_task
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "adm-management/errors"
  7. "adm-management/model"
  8. v1 "adm-management/pb/v1"
  9. "git.getensh.com/common/gopkgsv2/database"
  10. "git.getensh.com/common/gopkgsv2/logger"
  11. "go.uber.org/zap"
  12. "google.golang.org/grpc/status"
  13. "gorm.io/gorm"
  14. )
  15. func Source(ctx context.Context, req *v1.SourceRequest) (reply *v1.SourceReply, err error) {
  16. reply = &v1.SourceReply{}
  17. // 捕获各个task中的异常并返回给调用者
  18. defer func() {
  19. if r := recover(); r != nil {
  20. err = fmt.Errorf("%+v", r)
  21. e := &status.Status{}
  22. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  23. logger.Error("err",
  24. zap.String("system_err", err.Error()),
  25. zap.Stack("stacktrace"))
  26. }
  27. }
  28. }()
  29. if req.SourceName == "" {
  30. // 源表搜索
  31. // 构造分页类
  32. pagination := model.NewPagination(0, 100000, 0).GetLimitOffset()
  33. list, err := model.NewDataList().List(database.DB().Where("table_name is NOT null"), pagination)
  34. if err != nil && err != gorm.ErrRecordNotFound {
  35. return reply, errors.SystemError
  36. }
  37. if err == gorm.ErrRecordNotFound {
  38. return reply, nil
  39. }
  40. reply.List = make([]*v1.Source, 0, len(list))
  41. for _, v := range list {
  42. reply.List = append(reply.List, &v1.Source{
  43. TableName: v.TableName,
  44. Desc: v.Desc,
  45. })
  46. }
  47. }
  48. return reply, nil
  49. }
  50. func Task(ctx context.Context, req *v1.TaskRequest) (reply *v1.TaskReply, err error) {
  51. reply = &v1.TaskReply{}
  52. // 捕获各个task中的异常并返回给调用者
  53. defer func() {
  54. if r := recover(); r != nil {
  55. err = fmt.Errorf("%+v", r)
  56. e := &status.Status{}
  57. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  58. logger.Error("err",
  59. zap.String("system_err", err.Error()),
  60. zap.Stack("stacktrace"))
  61. }
  62. }
  63. }()
  64. if req.Task == "" {
  65. // 任务搜索
  66. // 构造分页类
  67. pagination := model.NewPagination(0, 100000, 0).GetLimitOffset()
  68. list, err := model.NewTaskList().List(database.DB().Where("task_name is NOT null"), pagination)
  69. if err != nil && err != gorm.ErrRecordNotFound {
  70. return reply, errors.SystemError
  71. }
  72. if err == gorm.ErrRecordNotFound {
  73. return reply, nil
  74. }
  75. reply.List = make([]*v1.Task, 0, len(list))
  76. for _, v := range list {
  77. reply.List = append(reply.List, &v1.Task{
  78. TaskName: v.TaskName,
  79. Desc: v.Desc,
  80. })
  81. }
  82. }
  83. if req.Task != "" {
  84. db := database.DB()
  85. relationInfo := []model.TaskInfo{}
  86. err = db.Raw("SELECT DISTINCT t1.task_id, t3.task_name, t3.`desc` FROM t_adm_task_data_relation as t1 LEFT JOIN `t_adm_data_management` as t2 on t1.source_code=t2.source_code LEFT JOIN `t_adm_tasks_management` as t3 on t1.task_id = t3.task_id where t2.table_name= ?", req.Task).Find(&relationInfo).Error
  87. if err != nil {
  88. if err == gorm.ErrRecordNotFound {
  89. return reply, nil
  90. }
  91. return reply, errors.SystemError
  92. }
  93. reply.List = make([]*v1.Task, 0, len(relationInfo))
  94. for _, v := range relationInfo {
  95. reply.List = append(reply.List, &v1.Task{
  96. TaskName: v.TaskName,
  97. Desc: v.Desc,
  98. })
  99. }
  100. }
  101. return reply, nil
  102. }