@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {
@Autowired
private ProductService productService;
@Autowired
private CategoryService categoryService;
@GetMapping("/list")
public ResultVO list() {
//1. 查詢所有的上架商品
List<ProductInfo> productInfoList = productService.findUpAll();
//2. 查詢類目(一次性查詢)
List<Integer> categoryTypeList = new ArrayList<>();
for (ProductInfo productInfo : productInfoList) {
categoryTypeList.add(productInfo.getCategoryType());
}
//3. 數據拼裝
List<ProductVO> productVOList = new ArrayList<>();
for (ProductCategory productCategory: productCategoryList) {
ProductVO productVO = new ProductVO();
//商品數
productVO.setCategoryType(productCategory.getCategoryType());
//類目名
productVO.setCategoryName(productCategory.getCategoryName());
List<ProductInfoVO> productInfoVOList = new ArrayList<>();
for (ProductInfo productInfo: productInfoList) {
if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
ProductInfoVO productInfoVO = new ProductInfoVO();
//拷貝數據到productInfoVo對象
BeanUtils.copyProperties(productInfo, productInfoVO);
productInfoVOList.add(productInfoVO);
}
}
productVO.setProductInfoVOList(productInfoVOList);
productVOList.add(productVO);
}
return ResultVOUtil.success(productVOList);
}
}
小菜蛋
在項目中if判斷二個字段狀態是和不是
- 是二個字段屬性equals判斷
- 例如不是字段前面加 !不等於 equals判斷