p01_brand.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package query
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "adm-data/errors"
  7. "adm-data/model"
  8. v1 "adm-data/pb/v1"
  9. "adm-data/utils"
  10. jsoniter "github.com/json-iterator/go"
  11. "git.getensh.com/common/gopkgsv2/database"
  12. "git.getensh.com/common/gopkgsv2/logger"
  13. "go.uber.org/zap"
  14. "google.golang.org/grpc/status"
  15. "gorm.io/gorm"
  16. )
  17. type BrandItem struct {
  18. BrandID string `json:"brand_id"`
  19. BrandName string `json:"brand_name"`
  20. Logo string `json:"logo"`
  21. BrandNamePrex string `json:"brand_name_prex"`
  22. }
  23. type BrandListResponse struct {
  24. List []BrandItem `json:"list"`
  25. }
  26. func BrandList(ctx context.Context, params string) (reply *v1.QueryResponse, err error) {
  27. reply = &v1.QueryResponse{}
  28. // 捕获各个task中的异常并返回给调用者
  29. defer func() {
  30. if r := recover(); r != nil {
  31. err = fmt.Errorf("%+v", r)
  32. e := &status.Status{}
  33. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  34. logger.Error("err",
  35. zap.String("system_err", err.Error()),
  36. zap.Stack("stacktrace"))
  37. }
  38. }
  39. }()
  40. // 构造分页类
  41. pagination := model.NewPagination(0, 10000, 0).GetLimitOffset()
  42. list, err := model.NewSyBrand().List(database.DB().Where("status = 1"), pagination)
  43. if err != nil && err != gorm.ErrRecordNotFound {
  44. return reply, errors.SystemError
  45. }
  46. if err == gorm.ErrRecordNotFound {
  47. return nil, errors.DataNotExistError
  48. }
  49. res := BrandListResponse{
  50. List: make([]BrandItem, 0, len(list)),
  51. }
  52. for _, v := range list {
  53. res.List = append(res.List, BrandItem{
  54. BrandID: v.BrandId,
  55. BrandName: v.BrandName,
  56. Logo: utils.GetBrandLogoById(v.BrandId, int32(v.HasImg)),
  57. BrandNamePrex: v.Initial,
  58. })
  59. }
  60. reply.Data, _ = jsoniter.MarshalToString(res)
  61. return reply, nil
  62. }