数据库设计
构建一个VO类
public class CrumbsTypeVo {
private CourseType ownerProductType;
private List<CourseType> otherProductTypes = new ArrayList<>();
}
其中,CourseType
是数据库表对应的实体类。
业务实现
public List<CrumbsTypeVo> getCrumbs(Long TypeId) {
// 根据 分类id 查询对应的path
CourseType courseType = selectById(TypeId);
AssertUtil.isNotNull(courseType,ErrorConstantCode.COURSE_TYPE_NULL_30050);
String path = courseType.getPath();
// path 校验, path 拆分 得到[顶级,二级,三级 ...]
List<CrumbsTypeVo> crumbs = new ArrayList<>();
String[] split = path.split("\."); // 字符串拆分,有可能需要做字符转义
log.info("拆分得到的数组:{}", Arrays.toString(split));
for (String sid : split) {
CrumbsTypeVo crumbsTypeVo = new CrumbsTypeVo();
CourseType type = selectById(sid);
crumbsTypeVo.setOwnerProductType(type);
// 查兄弟 ,拥有相同pid
Wrapper<CourseType> warpper = new EntityWrapper<>();
warpper.eq("pid", type.getPid());
List<CourseType> courseTypes = selectList(warpper);
crumbsTypeVo.setOtherProductTypes(courseTypes);
crumbs.add(crumbsTypeVo);
}
return crumbs;
}