c2.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package maintainance
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "adm-vehicle-style/errors"
  7. "adm-vehicle-style/model"
  8. v1 "adm-vehicle-style/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 C2List(ctx context.Context, req *v1.C2ListRequest) (reply *v1.C2ListReply, err error) {
  16. reply = &v1.C2ListReply{}
  17. defer func() {
  18. if r := recover(); r != nil {
  19. err = fmt.Errorf("%+v", r)
  20. e := &status.Status{}
  21. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  22. logger.Error("err",
  23. zap.String("system_err", err.Error()),
  24. zap.Stack("stacktrace"))
  25. }
  26. }
  27. }()
  28. list, err := model.NewStyleItemModel().List(database.DB().Where("style_id = ?", req.StyleId))
  29. if err != nil && err != gorm.ErrRecordNotFound {
  30. return nil, errors.SystemError
  31. }
  32. c2List, err := model.NewC2ItemModel().List(database.DB())
  33. if err != nil && err != gorm.ErrRecordNotFound {
  34. return nil, errors.SystemError
  35. }
  36. if err == gorm.ErrRecordNotFound {
  37. return reply, nil
  38. }
  39. exist := func(c2Id int64, list []model.StyleItem) bool {
  40. if len(list) == 0 {
  41. return false
  42. }
  43. for _, v := range list {
  44. if c2Id == v.C2Id {
  45. return true
  46. }
  47. }
  48. return false
  49. }
  50. reply.List = make([]*v1.C2ListReply_Info, 0, len(c2List))
  51. for _, v := range c2List {
  52. if !exist(v.ID, list) {
  53. reply.List = append(reply.List, &v1.C2ListReply_Info{
  54. Id: v.ID,
  55. Name: v.Name,
  56. })
  57. }
  58. }
  59. return reply, nil
  60. }