relation.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package data_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 Relation(ctx context.Context, req *v1.RelationRequest) (reply *v1.RelationReply, 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. reply = &v1.RelationReply{}
  28. db := database.DB()
  29. dataTaskInfo := []model.DataTaskInfo{}
  30. err = db.Raw("SELECT * FROM t_adm_task_data_relation as t1 LEFT JOIN `t_adm_tasks_management` as t2 on t1.task_id=t2.task_id where t1.source_code= ?", req.SourceCode).Find(&dataTaskInfo).Error
  31. if err != nil {
  32. if err == gorm.ErrRecordNotFound {
  33. return reply, nil
  34. }
  35. return reply, errors.SystemError
  36. }
  37. var desc string
  38. for _, v := range dataTaskInfo {
  39. err = db.Raw("SELECT `desc` FROM t_adm_tasks_management where task_id= ?", v.TaskId).Find(&desc).Error
  40. if err != nil {
  41. if err == gorm.ErrRecordNotFound {
  42. return reply, nil
  43. }
  44. return reply, errors.SystemError
  45. }
  46. reply.List = append(reply.List, &v1.Relation{
  47. Id: v.Id,
  48. TaskId: v.TaskId,
  49. TaskName: v.TaskName,
  50. IsOn: v.IsOn,
  51. Desc: desc,
  52. })
  53. }
  54. return reply, nil
  55. }