新RabbitMQ精讲 提升工程实践能力 培养架构思维
v: ititit111222333
/**
* 根据父级id和配置编码(列表)
*
* @param id 数据id
* @param configCode 配置编码
* @return 列表
*/
@ApiOperationSupport(order = 15)
@ApiOperation(value = "根据id和配置编码(列表)", notes = "根据id和配置编码(列表)")
@GetMapping(value = "sublist/{id}/{configCode}")
public Result<List<CategoryVO>> sublist(@PathVariable("id") @ApiParam("数据id") Long id,
@PathVariable("configCode") @ApiParam("配置编码") String configCode) {
log.info("====> /api/product/category/sublist/{parentId}/{configCode},id=,configCode=" + id + "/" + configCode);
List<Category> list = this.service.getSubTreeByParentIdAndConfigCode(id, configCode);
List<CategoryVO> voList = forVos(toVO(list));
Collections.sort(voList);
return success(voList);
}
/**
* 获取分类一级数据
*
* @param configCode 配置编码
* @return 列表
*/
@ApiOperationSupport(order = 16)
@ApiOperation(value = "获取分类一级数据", notes = "获取分类一级数据")
@GetMapping(value = "get-first-level/{configCode}")
public Result<List<CategoryVO>> getFirstLevel(@PathVariable("configCode") @ApiParam("配置编码") String configCode) {
log.info("====> /api/product/category/get-first-level/{configCode}, configCode=" + configCode);
List<Category> list = this.service.lambdaQuery()
.eq(Category::getParentId, RemoveFlag.N)
.eq(Category::getConfigCode, configCode)
.eq(Category::getState, DataState.ENABLED)
.list();
if (ObjectUtil.isNull(list)) {
return success(Lists.newArrayList());
}
Collections.sort(toVO(list));
return success(toVO(list));
}
//**************************************************** 私有方法 ************************************************//
/**
* 设置子类值
*/
private List<CategoryVO> forVos(List<CategoryVO> vos) {
if (ObjectUtil.isEmpty(vos)) {
return Lists.newArrayList();
}
// 获取分类ids
List<Long> categoryIds = CollectionUtil.getPropertyList(vos, CategoryVO::getId);
// 获取分类税局中间数据
List<CategoryTax> categoryTaxes = this.categoryTaxService.listCategoryTaxByCategoryIds(categoryIds);
// 构建map categoryId = CategoryTax
Map<Long, CategoryTax> longCategoryTaxMap = toMap(categoryTaxes, CategoryTax::getCategoryId);
// 获取分类模板中间数据
List<CategoryAttributeTemplate> categoryAttributeTemplates = this.categoryAttributeTemplateService.getCategoryAttributeTemplateByCategoryIds(categoryIds);
// 构建map categoryId = templateId
Map<Long, Long> longLongMap = toMap(categoryAttributeTemplates, CategoryAttributeTemplate::getCategoryId, CategoryAttributeTemplate::getTemplateId);
// 获取模板ids
Set<Long> templateIds = CollectionUtil.getPropertySet(categoryAttributeTemplates, CategoryAttributeTemplate::getTemplateId);
// 根据模板ids获取模板数据
List<AttributeTemplate> attributeTemplates = attributeTemplateService.listByIds(templateIds);
// 构建map id = code
Map<Long, String> codeMap = toMap(attributeTemplates, AttributeTemplate::getId, AttributeTemplate::getTemplateCode);
// 构建map id = name
Map<Long, String> nameMap = toMap(attributeTemplates, AttributeTemplate::getId, AttributeTemplate::getTemplateName);
// 构建分类与其他分类的关系
List<CategoryMapping> categoryMappings = this.categoryMappingService.listCategoryMappingByCategoryIds(categoryIds);
List<Long> existsIds = getPropertyList(categoryMappings, CategoryMapping::getCategoryId);
for (CategoryVO it : vos) {
// 获取关联模板数据
if (longLongMap.containsKey(it.getId())) {
// 设置属性模板编码
it.setTemplateCode(codeMap.get(it.getTemplateId()));
}
if (nameMap.containsKey(it.getId())) {
// 设置属性模板编码
it.setTemplateName(nameMap.get(it.getTemplateId()));
}
if (longCategoryTaxMap.containsKey(it.getId())) {
it.setTaxCode(longCategoryTaxMap.get(it.getId()).getTaxCode());
}
if (longCategoryTaxMap.containsKey(it.getId())) {
it.setTaxName(longCategoryTaxMap.get(it.getId()).getTaxName());
}
if (longCategoryTaxMap.containsKey(it.getId())) {
it.setTaxRate(longCategoryTaxMap.get(it.getId()).getTaxRate());
}
// 关联则1 关联则0
it.setRelationTaxTag(longCategoryTaxMap.containsKey(it.getId()) ? 1 : 0);
// 关联则1 关联则0
it.setRelationTemplateTag(longLongMap.containsKey(it.getId()) ? 1 : 0);
// 关联则1 关联则0
it.setRelationCategoryTag(existsIds.contains(it.getId()) ? 1 : 0);
}
return vos;
}
/**
* 验证vo的完整性
*/
private void validateVO(CategoryVO vo) {
vo.setCategoryNamePy(PinyinUtil.toPinyin(vo.getCategoryName()));
CategoryConfig categoryConfig = this.categoryConfigService.getByConfigCode(vo.getConfigCode());
if (ObjectUtil.isNull(vo.getConfigId())) {
vo.setConfigId(categoryConfig.getId());
}
if (vo.getDepth() > categoryConfig.getMaxDepth()) {
throw new ParameterException("新增类别层级不能大于该分类层级");
}
}