/**
* Entity - 商品分类
*
* @author MJ Team
* @version 5.0.3
*/
@Entity
@Table(name = "product_category")
public class ProductCategory extends OrderedEntity<Long> {
private static final long serialVersionUID = 5095521437302782717L;
private Long id;
@Min(0)
@Column(name = "orders")
private Integer order;
/**
* 树路径分隔符
*/
public static final String TREE_PATH_SEPARATOR = ",";
/**
* 路径
*/
private static final String PATH = "/product/list/%d";
/**
* 名称
*/
@Length(max = 200)
@Column(nullable = false)
private String name;
/**
* 树路径
*/
@Column(name = "tree_path", nullable = false)
private String treePath;
/**
* 层级
*/
@Column(nullable = false)
private Integer grade;
/**
* 上级分类
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private ProductCategory parent;
/**
* 下级分类
*/
@JsonView(BaseView.class)
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
@OrderBy("order asc")
private Set<ProductCategory> children = new HashSet<>();
/**
* 获取树路径
*
* @return 树路径
*/
public String getTreePath() {
return treePath;
}
/**
* 设置树路径
*
* @param treePath
* 树路径
*/
public void setTreePath(String treePath) {
this.treePath = treePath;
}
/**
* 获取层级
*
* @return 层级
*/
public Integer getGrade() {
return grade;
}
/**
* 设置层级
*
* @param grade
* 层级
*/
public void setGrade(Integer grade) {
this.grade = grade;
}
/**
* 获取上级分类
*
* @return 上级分类
*/
public ProductCategory getParent() {
return parent;
}
/**
* 设置上级分类
*
* @param parent
* 上级分类
*/
public void setParent(ProductCategory parent) {
this.parent = parent;
}
/**
* 获取下级分类
*
* @return 下级分类
*/
public Set<ProductCategory> getChildren() {
return children;
}
/**
* 设置下级分类
*
* @param children
* 下级分类
*/
public void setChildren(Set<ProductCategory> children) {
this.children = children;
}
/**
* 获取所有上级分类ID
*
* @return 所有上级分类ID
*/
@Transient
public Long[] getParentIds() {
String[] parentIds = StringUtils.split(getTreePath(), TREE_PATH_SEPARATOR);
Long[] result = new Long[parentIds.length];
for (int i = 0; i < parentIds.length; i++) {
result[i] = Long.valueOf(parentIds[i]);
}
return result;
}
/**
* 获取所有上级分类
*
* @return 所有上级分类
*/
@Transient
public List<ProductCategory> getParents() {
List<ProductCategory> parents = new ArrayList<>();
recursiveParents(parents, this);
return parents;
}
/**
* 递归上级分类
*
* @param parents
* 上级分类
* @param productCategory
* 商品分类
*/
private void recursiveParents(List<ProductCategory> parents, ProductCategory productCategory) {
if (productCategory == null) {
return;
}
ProductCategory parent = productCategory.getParent();
if (parent != null) {
parents.add(0, parent);
recursiveParents(parents, parent);
}
}
}
/**
* 排序商品分类
*
* @param productCategories
* 商品分类
*/
private void sort(List<ProductCategory> productCategories) {
if (CollectionUtils.isEmpty(productCategories)) {
return;
}
final Map<Long, Integer> orderMap = new HashMap<>();
for (ProductCategory productCategory : productCategories) {
orderMap.put(productCategory.getId(), productCategory.getOrder());
}
Collections.sort(productCategories, new Comparator<ProductCategory>() {
@Override
public int compare(ProductCategory productCategory1, ProductCategory productCategory2) {
Long[] ids1 = (Long[]) ArrayUtils.add(productCategory1.getParentIds(), productCategory1.getId());
Long[] ids2 = (Long[]) ArrayUtils.add(productCategory2.getParentIds(), productCategory2.getId());
Iterator<Long> iterator1 = Arrays.asList(ids1).iterator();
Iterator<Long> iterator2 = Arrays.asList(ids2).iterator();
CompareToBuilder compareToBuilder = new CompareToBuilder();
while (iterator1.hasNext() && iterator2.hasNext()) {
Long id1 = iterator1.next();
Long id2 = iterator2.next();
Integer order1 = orderMap.get(id1);
Integer order2 = orderMap.get(id2);
compareToBuilder.append(order1, order2).append(id1, id2);
if (!iterator1.hasNext() || !iterator2.hasNext()) {
compareToBuilder.append(productCategory1.getGrade(), productCategory2.getGrade());
}
}
return compareToBuilder.toComparison();
}
});
}
List<ProductCategory> result = new ArrayList<ProductCategory>;
sort(result);
实现分类的树形结构 一级分类
二级分类
三级分类
一级分类
二级分类