1. CategoryBrandList

  1. func (s *GoodsServer) CategoryBrandList(ctx context.Context, req *proto.CategoryBrandFilterRequest) (*proto.CategoryBrandListResponse, error) {
  2. var categoryBrands []model.GoodsCategoryBrand
  3. categoryBrandListResponse := proto.CategoryBrandListResponse{}
  4. var total int64
  5. global.DB.Model(&model.GoodsCategoryBrand{}).Count(&total)
  6. categoryBrandListResponse.Total = int32(total)
  7. global.DB.Scopes(Paginate(int(req.Pages), int(req.PagePerNums))).Find(&categoryBrands)
  8. var categoryResponses []*proto.CategoryBrandResponse
  9. for _, categoryBrand := range categoryBrands {
  10. categoryResponses = append(categoryResponses, &proto.CategoryBrandResponse{
  11. Category: &proto.CategoryInfoResponse{
  12. Id: categoryBrand.Category.ID,
  13. Name: categoryBrand.Category.Name,
  14. Level: categoryBrand.Category.Level,
  15. IsTab: categoryBrand.Category.IsTab,
  16. ParentCategory: categoryBrand.Category.ParentCategoryID,
  17. },
  18. Brand: &proto.BrandInfoResponse{
  19. Id: categoryBrand.Brands.ID,
  20. Name: categoryBrand.Brands.Name,
  21. Logo: categoryBrand.Brands.Logo,
  22. },
  23. })
  24. }
  25. categoryBrandListResponse.Data = categoryResponses
  26. return &categoryBrandListResponse, nil
  27. }

2. GetCategoryBrandList

  1. func (s *GoodsServer) GetCategoryBrandList(ctx context.Context, req *proto.CategoryInfoRequest) (*proto.BrandListResponse, error){
  2. brandListResponse := proto.BrandListResponse{}
  3. var category model.Category
  4. if result := global.DB.Find(&category, req.Id).First(&category); result.RowsAffected == 0 {
  5. return nil, status.Errorf(codes.InvalidArgument, "品牌分类不存在")
  6. }
  7. var categoryBrands []model.GoodsCategoryBrand
  8. if result := global.DB.Where(&model.GoodsCategoryBrand{CategoryID: category.ID}).Find(&categoryBrands); result.RowsAffected > 0 {
  9. brandListResponse.Total = int32(result.RowsAffected)
  10. }
  11. var brandInfoResponses []*proto.BrandInfoResponse
  12. for _, categoryBrand := range categoryBrands {
  13. brandInfoResponses = append(brandInfoResponses, &proto.BrandInfoResponse{
  14. Id: int32(categoryBrand.Brand.ID),
  15. Name: categoryBrand.Brand.Name,
  16. Logo: categoryBrand.Brand.Logo,
  17. })
  18. }
  19. brandListResponse.Data = brandInfoResponses
  20. return &brandListResponse, nil
  21. }

3. CreateCategoryBrand

  1. func (s *GoodsServer) CreateCategoryBrand(ctx context.Context, req *proto.CategoryBrandRequest) (*proto.CategoryBrandResponse, 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. categoryBrand := model.GoodsCategoryBrand{
  11. CategoryID: req.CategoryId,
  12. BrandID: req.BrandId,
  13. }
  14. global.DB.Save(&categoryBrand)
  15. return &proto.CategoryBrandResponse{Id: int32(categoryBrand.ID)}, nil
  16. }

4. DeleteCategoryBrand

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

5. UpdateCategoryBrand

  1. func (s *GoodsServer) UpdateCategoryBrand(ctx context.Context, req *proto.CategoryBrandRequest) (*emptypb.Empty, error) {
  2. var categoryBrand model.GoodsCategoryBrand
  3. if result := global.DB.First(&categoryBrand, 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. categoryBrand.CategoryID = req.CategoryId
  15. categoryBrand.BrandID = req.BrandId
  16. global.DB.Save(&categoryBrand)
  17. return &emptypb.Empty{}, nil
  18. }