allSource.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package style_map_management
  2. import (
  3. "adm-management/errors"
  4. "adm-management/model"
  5. "fmt"
  6. "git.getensh.com/common/gopkgsv2/database"
  7. v1 "adm-management/pb/v1"
  8. "context"
  9. "encoding/json"
  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 AllSource(ctx context.Context, req *v1.AllSourceRequest) (reply *v1.AllSourceReply, err error) {
  16. reply = &v1.AllSourceReply{}
  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.AllSource == "" {
  30. db := database.DB()
  31. sourceInfo := []model.SourceInfo{}
  32. err = db.Raw("SELECT DISTINCT source FROM db_adm_dws.t_adm_dws13 ").Find(&sourceInfo).Error
  33. if err != nil {
  34. if err == gorm.ErrRecordNotFound {
  35. return reply, nil
  36. }
  37. return reply, errors.SystemError
  38. }
  39. reply.List = make([]string, 0, len(sourceInfo))
  40. for _, v := range sourceInfo {
  41. reply.List = append(reply.List, v.Source)
  42. }
  43. }
  44. return reply, nil
  45. }