1. CategoryBrandList
func (s *GoodsServer) CategoryBrandList(ctx context.Context, req *proto.CategoryBrandFilterRequest) (*proto.CategoryBrandListResponse, error) { var categoryBrands []model.GoodsCategoryBrand categoryBrandListResponse := proto.CategoryBrandListResponse{} var total int64 global.DB.Model(&model.GoodsCategoryBrand{}).Count(&total) categoryBrandListResponse.Total = int32(total) global.DB.Scopes(Paginate(int(req.Pages), int(req.PagePerNums))).Find(&categoryBrands) var categoryResponses []*proto.CategoryBrandResponse for _, categoryBrand := range categoryBrands { categoryResponses = append(categoryResponses, &proto.CategoryBrandResponse{ Category: &proto.CategoryInfoResponse{ Id: categoryBrand.Category.ID, Name: categoryBrand.Category.Name, Level: categoryBrand.Category.Level, IsTab: categoryBrand.Category.IsTab, ParentCategory: categoryBrand.Category.ParentCategoryID, }, Brand: &proto.BrandInfoResponse{ Id: categoryBrand.Brands.ID, Name: categoryBrand.Brands.Name, Logo: categoryBrand.Brands.Logo, }, }) } categoryBrandListResponse.Data = categoryResponses return &categoryBrandListResponse, nil}
2. GetCategoryBrandList
func (s *GoodsServer) GetCategoryBrandList(ctx context.Context, req *proto.CategoryInfoRequest) (*proto.BrandListResponse, error){ brandListResponse := proto.BrandListResponse{} var category model.Category if result := global.DB.Find(&category, req.Id).First(&category); result.RowsAffected == 0 { return nil, status.Errorf(codes.InvalidArgument, "品牌分类不存在") } var categoryBrands []model.GoodsCategoryBrand if result := global.DB.Where(&model.GoodsCategoryBrand{CategoryID: category.ID}).Find(&categoryBrands); result.RowsAffected > 0 { brandListResponse.Total = int32(result.RowsAffected) } var brandInfoResponses []*proto.BrandInfoResponse for _, categoryBrand := range categoryBrands { brandInfoResponses = append(brandInfoResponses, &proto.BrandInfoResponse{ Id: int32(categoryBrand.Brand.ID), Name: categoryBrand.Brand.Name, Logo: categoryBrand.Brand.Logo, }) } brandListResponse.Data = brandInfoResponses return &brandListResponse, nil}
3. CreateCategoryBrand
func (s *GoodsServer) CreateCategoryBrand(ctx context.Context, req *proto.CategoryBrandRequest) (*proto.CategoryBrandResponse, error) { var category model.Category if result := global.DB.First(&category, req.CategoryId); result.RowsAffected == 0 { return nil, status.Errorf(codes.InvalidArgument, "商品分类不存在") } var brand model.Brands if result := global.DB.First(&brand, req.BrandId); result.RowsAffected == 0 { return nil, status.Errorf(codes.InvalidArgument, "品牌不存在") } categoryBrand := model.GoodsCategoryBrand{ CategoryID: req.CategoryId, BrandID: req.BrandId, } global.DB.Save(&categoryBrand) return &proto.CategoryBrandResponse{Id: int32(categoryBrand.ID)}, nil}
4. DeleteCategoryBrand
func (s *GoodsServer) DeleteCategoryBrand(ctx context.Context, req *proto.CategoryBrandRequest) (*emptypb.Empty, error) { if result := global.DB.Delete(&model.GoodsCategoryBrand{}, req.Id); result.RowsAffected == 0 { return nil, status.Errorf(codes.NotFound, "品牌分类不存在") } return &emptypb.Empty{}, nil}
5. UpdateCategoryBrand
func (s *GoodsServer) UpdateCategoryBrand(ctx context.Context, req *proto.CategoryBrandRequest) (*emptypb.Empty, error) { var categoryBrand model.GoodsCategoryBrand if result := global.DB.First(&categoryBrand, req.Id); result.RowsAffected == 0 { return nil, status.Errorf(codes.InvalidArgument, "品牌分类不存在") } var category model.Category if result := global.DB.First(&category, req.CategoryId); result.RowsAffected == 0 { return nil, status.Errorf(codes.InvalidArgument, "商品分类不存在") } var brand model.Brands if result := global.DB.First(&brand, req.BrandId); result.RowsAffected == 0 { return nil, status.Errorf(codes.InvalidArgument, "品牌不存在") } categoryBrand.CategoryID = req.CategoryId categoryBrand.BrandID = req.BrandId global.DB.Save(&categoryBrand) return &emptypb.Empty{}, nil}