首先我在控制器中将分类id封装到一个列表中
@GetMapping("/list")
@ApiOperation(value = "查询分类数据接口")
public Result arrayList(Category category){
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
List list = categoryService.list(queryWrapper);
List categoryIds = list.stream().map(Category::getId).collect(Collectors.toList());
CategoryDto categoryDto = categoryService.getByCategoryId(categoryIds);
return Result.success(categoryDto);
}
然后想在服务层把产品表的数据通过分类id这个公共字段封装到列表信息中
@Override
public CategoryDto getByCategoryId(List categoryIds) {
List category = categoryService.listByIds(categoryIds);
CategoryDto categoryDto = new CategoryDto();
BeanUtils.copyProperties(category,categoryDto);
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(Medicine::getCategoryId,categoryIds);
List medicines = medicineService.list(queryWrapper);
categoryDto.setMedicines(medicines);
categoryDto.setCategories(category);
return categoryDto;
}
这是我的返回结果,完全不对,分类信息和产品信息没有绑定
{
"code": 1,
"msg": null,
"data": {
"id": null,
"name": null,
"orgId": null,
"sort": null,
"createTime": null,
"updateTime": null,
"createUser": null,
"updateUser": null,
"categories": [
{
"id": "1397844263642378242",
"name": "感冒发烧",
"orgId": "1",
"sort": 1,
"createTime": "2016-09-17 06:05:23",
"updateTime": "2003-05-02 19:59:57",
"createUser": "1413386191767674881",
"updateUser": "1413386191767674881"
},
{
"id": "1397844391040167938",
"name": "头晕头痛",
"orgId": "1",
"sort": 2,
"createTime": "2001-08-26 16:55:31",
"updateTime": "2007-09-25 14:59:04",
"createUser": "1515923243633070081",
"updateUser": "1515923243633070081"
}
],
"medicines": [
{
"id": "1413386191767674881",
"name": "川贝枇杷露",
"brandName": "京都念慈菴",
"categoryId": "1397844263642378242",
"price": 22,
"medicineType": "0",
"code": "S2Nhz7q7Ig",
"image": "pipalu.jpg",
"productionDate": "2022-08-05",
"expirationDate": "2025-08-05",
"quantity": "50",
"description": "感冒发烧、头疼脑热",
"status": 1,
"sort": 268,
"createTime": "2019-06-04 18:57:41",
"updateTime": "2002-08-04 21:28:36",
"createUser": "1413386191767674881",
"updateUser": "1413386191767674881",
"isDeleted": 0
},
{
"id": "1515923243633070081",
"name": "布洛芬",
"brandName": "芬必得",
"categoryId": "1397844391040167938",
"price": 25,
"medicineType": "0",
"code": "3mz3RnCXVV",
"image": "buluofen.jpg",
"productionDate": "2022-08-05",
"expirationDate": "2025-08-05",
"quantity": "50",
"description": "头晕头痛",
"status": 1,
"sort": 403,
"createTime": "2003-01-04 03:51:22",
"updateTime": "2021-02-10 02:26:51",
"createUser": "1515923243633070081",
"updateUser": "1515923243633070081",
"isDeleted": 0
}
]
},
"map": {}
}
我想要达到的结果
大概知道应该用循环来封装,但奈何技术不到位,不知道具体怎么实现。
你是要把分类信息写到产品信息里面吧?
是的话
List<Category> category = categoryService.listByIds(categoryIds);
final Map<Long, String> categoryIdNameMap = category.stream().collect(Collectors.toMap(Category::getCategoryId, Category::getName));
List<Medicine> medicines = medicineService.list(categoryIds);
medicines.forEach(medicine -> medicine.setCategoryName(categoryIdNameMap.getOrDefault(medicine.getCategoryId(), null)));
如果不是就追问吧.
这种结构不就是分类信息包含产品集合么,就在分类dto加个产品list字段,然后set进去
BeanUtils.copyProperties(category,categoryDto);
category不是个list吗?怎么把属性复制给categoryDto单个对象的?