1. model转message

  1. goodsInfoResponse := proto.GoodsInfoResponse {
  2. Id: goods.ID,
  3. CategoryId: goods.CategoryID,
  4. Name: goods.Name,
  5. GoodsSn: goods.GoodsSn,
  6. ClickNum: goods.ClickNum,
  7. SoldNum: goods.SoldNum,
  8. FavNum: goods.FavNum,
  9. MarketPrice: goods.MarketPrice,
  10. ShopPrice: goods.ShopPrice,
  11. GoodsBrief: goods.GoodsBrief,
  12. ShipFree: goods.ShipFree,
  13. GoodsFrontImage: goods.GoodsFrontImage,
  14. IsNew: goods.IsNew,
  15. IsHot: goods.IsHot,
  16. OnSale: goods.OnSale,
  17. DescImages: goods.DescImages,
  18. Images: goods.Images,
  19. Category: &proto.CategoryBriefInfoResponse{
  20. Id: goods.Category.ID,
  21. Name: goods.Category.Name,
  22. },
  23. Brand: &proto.BrandInfoResponse{
  24. Id: goods.Brands.ID,
  25. Name: goods.Brands.Name,
  26. Logo: goods.Brands.Logo,
  27. },
  28. }

2. goodslist

  1. func (s *GoodsServer) GoodsList(ctx context.Context, req *proto.GoodsFilterRequest) (*proto.GoodsListResponse, error) {
  2. //关键词搜索、查询新品、查询热门商品、通过价格区间筛选, 通过商品分类筛选
  3. goodsListResponse := &proto.GoodsListResponse{}
  4. var goods []model.Goods
  5. localDB := global.DB.Model(model.Goods{})
  6. if req.KeyWords != "" {
  7. //搜索
  8. localDB = localDB.Where("name LIKE ?", "%"+req.KeyWords+"%")
  9. }
  10. if req.IsHot {
  11. localDB = localDB.Where(model.Goods{IsHot: true})
  12. }
  13. if req.IsNew {
  14. localDB = localDB.Where(model.Goods{IsNew: true})
  15. }
  16. if req.PriceMin > 0 {
  17. localDB = localDB.Where("shop_price>=?", req.PriceMin)
  18. }
  19. if req.PriceMax > 0 {
  20. localDB = localDB.Where("shop_price<=?", req.PriceMax)
  21. }
  22. if req.Brand > 0 {
  23. localDB = localDB.Where("brand_id=?", req.Brand)
  24. }
  25. //通过category去查询商品
  26. var subQuery string
  27. if req.TopCategory > 0 {
  28. var category model.Category
  29. if result := global.DB.First(&category, req.TopCategory); result.RowsAffected == 0 {
  30. return nil, status.Errorf(codes.NotFound, "商品分类不存在")
  31. }
  32. if category.Level == 1 {
  33. subQuery = fmt.Sprintf("select id from category where parent_category_id in (select id from category WHERE parent_category_id=%d)", req.TopCategory)
  34. }else if category.Level == 2 {
  35. subQuery = fmt.Sprintf("select id from category WHERE parent_category_id=%d", req.TopCategory)
  36. }else if category.Level == 3 {
  37. subQuery = fmt.Sprintf("select id from category WHERE id=%d", req.TopCategory)
  38. }
  39. localDB = localDB.Where(fmt.Sprintf("category_id in (%s)", subQuery))
  40. }
  41. var count int64
  42. localDB.Count(&count)
  43. goodsListResponse.Total = int32(count)
  44. result := localDB.Preload("Category").Preload("Brands").Scopes(Paginate(int(req.Pages), int(req.PagePerNums))).Find(&goods)
  45. if result.Error != nil {
  46. return nil, result.Error
  47. }
  48. for _, good := range goods {
  49. goodsInfoResponse := ModelToResponse(good)
  50. goodsListResponse.Data = append(goodsListResponse.Data, &goodsInfoResponse)
  51. }
  52. return goodsListResponse, nil
  53. }

3. 批量获取商品信息

  1. func (s *GoodsServer) BatchGetGoods(ctx context.Context, req *proto.BatchGoodsIdInfo) (*proto.GoodsListResponse, error){
  2. goodsListResponse := &proto.GoodsListResponse{}
  3. var goods []model.Goods
  4. //调用where并不会真正执行sql 只是用来生成sql的 当调用find, first才会去执行sql,
  5. result := global.DB.Where(req.Id).Find(&goods)
  6. for _, good := range goods {
  7. goodsInfoResponse := ModelToResponse(good)
  8. goodsListResponse.Data = append(goodsListResponse.Data, &goodsInfoResponse)
  9. }
  10. goodsListResponse.Total = int32(result.RowsAffected)
  11. return goodsListResponse, nil
  12. }

4. 获取商品的详情

  1. func (s *GoodsServer) GetGoodsDetail(ctx context.Context, req *proto.GoodInfoRequest) (*proto.GoodsInfoResponse, error){
  2. var goods model.Goods
  3. if result := global.DB.First(&goods, req.Id); result.RowsAffected == 0 {
  4. return nil, status.Errorf(codes.NotFound, "商品不存在")
  5. }
  6. goodsInfoResponse := ModelToResponse(goods)
  7. return &goodsInfoResponse, nil
  8. }

5. 添加商品

  1. func (s *GoodsServer) CreateGoods(ctx context.Context, req *proto.CreateGoodsInfo) (*proto.GoodsInfoResponse, error) {
  2. var category model.Category
  3. if result := global.DB.First(&category, req.CategoryId); result.RowsAffected == 0 {
  4. return nil, status.Errorf(codes.InvalidArgument, "商品分类不存在")
  5. }
  6. var brand model.Brands
  7. if result := global.DB.First(&brand, req.BrandId); result.RowsAffected == 0 {
  8. return nil, status.Errorf(codes.InvalidArgument, "品牌不存在")
  9. }
  10. //这里没有看到图片文件是如何上传, 在微服务中 普通的文件上传已经不再使用
  11. goods := model.Goods{
  12. Brands: brand,
  13. BrandsID: brand.ID,
  14. Category: category,
  15. CategoryID: category.ID,
  16. Name: req.Name,
  17. GoodsSn: req.GoodsSn,
  18. MarketPrice: req.MarketPrice,
  19. ShopPrice: req.ShopPrice,
  20. GoodsBrief: req.GoodsBrief,
  21. ShipFree: req.ShipFree,
  22. Images: req.Images,
  23. DescImages: req.DescImages,
  24. GoodsFrontImage: req.GoodsFrontImage,
  25. IsNew: req.IsNew,
  26. IsHot: req.IsHot,
  27. OnSale: req.OnSale,
  28. }
  29. global.DB.Save(&goods)
  30. return &proto.GoodsInfoResponse{
  31. Id: goods.ID,
  32. }, nil
  33. }

6. 删除商品

  1. func (s *GoodsServer) DeleteGoods(ctx context.Context, req *proto.DeleteGoodsInfo) (*emptypb.Empty, error) {
  2. if result := global.DB.Delete(&model.Goods{}, req.Id); result.RowsAffected == 0 {
  3. return nil, status.Errorf(codes.NotFound, "商品不存在")
  4. }
  5. return &emptypb.Empty{}, nil
  6. }

7. 更新商品

  1. func (s *GoodsServer) UpdateGoods(ctx context.Context, req *proto.CreateGoodsInfo) (*emptypb.Empty, error){
  2. var goods model.Goods
  3. if result := global.DB.First(&goods, req.Id); result.RowsAffected == 0 {
  4. return nil, status.Errorf(codes.InvalidArgument, "商品不存在")
  5. }
  6. var category model.Category
  7. if result := global.DB.First(&category, req.CategoryId); result.RowsAffected == 0 {
  8. return nil, status.Errorf(codes.InvalidArgument, "商品分类不存在")
  9. }
  10. var brand model.Brands
  11. if result := global.DB.First(&brand, req.BrandId); result.RowsAffected == 0 {
  12. return nil, status.Errorf(codes.InvalidArgument, "品牌不存在")
  13. }
  14. goods.Brands = brand
  15. goods.Category = category
  16. goods.Name = req.Name
  17. goods.GoodsSn = req.GoodsSn
  18. goods.MarketPrice= req.MarketPrice
  19. goods.ShopPrice= req.ShopPrice
  20. goods.GoodsBrief= req.GoodsBrief
  21. goods.ShipFree= req.ShipFree
  22. goods.Images= req.Images
  23. goods.DescImages= req.DescImages
  24. goods.GoodsFrontImage= req.GoodsFrontImage
  25. goods.IsNew= req.IsNew
  26. goods.IsHot= req.IsHot
  27. goods.OnSale= req.OnSale
  28. global.DB.Save(&goods)
  29. return &emptypb.Empty{}, nil
  30. }