dependency.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package task_management
  2. import (
  3. "adm-management/errors"
  4. "adm-management/model"
  5. v1 "adm-management/pb/v1"
  6. "context"
  7. "encoding/json"
  8. "fmt"
  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 Dependency(ctx context.Context, req *v1.DependencyRequest) (reply *v1.DependencyReply, err error) {
  16. defer func() {
  17. if r := recover(); r != nil {
  18. err = fmt.Errorf("%+v", r)
  19. e := &status.Status{}
  20. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  21. logger.Error("err",
  22. zap.String("system_err", err.Error()),
  23. zap.Stack("stacktrace"))
  24. }
  25. }
  26. }()
  27. // 源表
  28. if req.Type == 1 {
  29. reply = &v1.DependencyReply{}
  30. db := database.DB()
  31. dependencyInfo := []model.DependencyInfo{}
  32. err = db.Raw("SELECT * FROM t_adm_task_data_relation as t1 LEFT JOIN `t_adm_data_management` as t2 on t1.source_code=t2.source_code where t1.task_id= ?", req.TaskId).Find(&dependencyInfo).Error
  33. if err != nil {
  34. if err == gorm.ErrRecordNotFound {
  35. return reply, nil
  36. }
  37. return reply, errors.SystemError
  38. }
  39. for _, v := range dependencyInfo {
  40. reply.List = append(reply.List, &v1.Dependency{
  41. TableName: v.TableName,
  42. Desc: v.Desc,
  43. })
  44. }
  45. return reply, nil
  46. }
  47. // 依赖表
  48. if req.Type == 2 {
  49. reply = &v1.DependencyReply{}
  50. db := database.DB()
  51. dependencyInfo := []model.DependencyInfo{}
  52. err = db.Raw("SELECT * FROM t_adm_task_dependency as t1 LEFT JOIN `t_adm_data_management` as t2 on t1.source_code=t2.source_code where t1.task_id= ?", req.TaskId).Find(&dependencyInfo).Error
  53. if err != nil {
  54. if err == gorm.ErrRecordNotFound {
  55. return reply, nil
  56. }
  57. return reply, errors.SystemError
  58. }
  59. for _, v := range dependencyInfo {
  60. reply.List = append(reply.List, &v1.Dependency{
  61. TableName: v.TableName,
  62. Desc: v.Desc,
  63. })
  64. }
  65. return reply, nil
  66. }
  67. return reply, nil
  68. }