|
|
package com.yoho.ufo.service.impl;
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.yoho.ufo.dal.SaleCategoryMapper;
|
|
|
import com.yoho.ufo.model.commoditybasicrole.category.ProductSort;
|
|
|
import com.yoho.ufo.model.salecategory.SaleCategory;
|
|
|
import com.yoho.ufo.service.ISaleCategoryService;
|
|
|
import com.yoho.ufo.util.CollectionUtil;
|
|
|
import com.yoho.ufo.util.OrikaUtils;
|
|
|
import com.yohobuy.ufo.model.request.productsort.ProductSortRequestBo;
|
|
|
import com.yohobuy.ufo.model.response.productsort.ProductSortTreeViewResponseBo;
|
|
|
import com.yohobuy.ufo.model.response.salecategory.SaleCategoryTreeViewResponseBo;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class SaleCategoryServiceImpl implements ISaleCategoryService {
|
|
|
|
|
|
/**
|
|
|
* 顶级id
|
|
|
*/
|
|
|
private static final Integer TOP_PARENT_ID = 0;
|
|
|
|
|
|
/**
|
|
|
* 一级层级
|
|
|
*/
|
|
|
private static final Integer PRODUCT_SORT_LEVEL_1 = 1;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 二级层级
|
|
|
*/
|
|
|
private static final Integer PRODUCT_SORT_LEVEL_2 = 2;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 二级层级
|
|
|
*/
|
|
|
private static final Integer PRODUCT_SORT_LEVEL_3 = 3;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private SaleCategoryMapper saleCategoryMapper;
|
|
|
|
|
|
public List<SaleCategoryTreeViewResponseBo> getSaleCategoryList() {
|
|
|
log.info("enter getSaleCategoryList param = {}");
|
|
|
List<SaleCategory> productSorts = saleCategoryMapper.selectAll();
|
|
|
List<SaleCategoryTreeViewResponseBo> responseBos = new ArrayList<>();
|
|
|
processResult(productSorts, responseBos);
|
|
|
return responseBos;
|
|
|
}
|
|
|
|
|
|
|
|
|
private void processResult(List<SaleCategory> categoryList, List<SaleCategoryTreeViewResponseBo> responseBos) {
|
|
|
if(CollectionUtils.isEmpty(categoryList)){
|
|
|
return ;
|
|
|
}
|
|
|
Iterator<SaleCategory> iterator = categoryList.iterator();
|
|
|
// 挑选出一级分类
|
|
|
while (iterator.hasNext()) {
|
|
|
SaleCategory saleCategory1 = iterator.next();
|
|
|
if (PRODUCT_SORT_LEVEL_1.equals(saleCategory1.getLevel()) && TOP_PARENT_ID.equals(saleCategory1.getParentId())) {
|
|
|
responseBos.add(OrikaUtils.map(saleCategory1, SaleCategoryTreeViewResponseBo.class));
|
|
|
iterator.remove();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 找出该一级分类下的二级分类
|
|
|
for (SaleCategoryTreeViewResponseBo treeViewResponseBo : responseBos) {
|
|
|
iterator = categoryList.iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
SaleCategory category2 = iterator.next();
|
|
|
|
|
|
if (treeViewResponseBo.getId().equals(category2.getParentId())) {
|
|
|
List<SaleCategoryTreeViewResponseBo> children = treeViewResponseBo.getChildren();
|
|
|
if (children == null) {
|
|
|
children = new ArrayList<>();
|
|
|
}
|
|
|
SaleCategoryTreeViewResponseBo treeViewResponseBo1 = OrikaUtils.map(category2, SaleCategoryTreeViewResponseBo.class);
|
|
|
//treeViewResponseBo1.setState(StringUtils.EMPTY);
|
|
|
children.add(treeViewResponseBo1);
|
|
|
treeViewResponseBo.setChildren(children);
|
|
|
iterator.remove();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 找出三级分类
|
|
|
for (SaleCategoryTreeViewResponseBo treeViewResponseBoTmp : responseBos) {
|
|
|
if (CollectionUtils.isEmpty(treeViewResponseBoTmp.getChildren())) {
|
|
|
continue;
|
|
|
}
|
|
|
for(SaleCategoryTreeViewResponseBo snd: treeViewResponseBoTmp.getChildren()){
|
|
|
iterator = categoryList.iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
SaleCategory category3 = iterator.next();
|
|
|
|
|
|
if (snd.getId().equals(category3.getParentId())) {
|
|
|
List<SaleCategoryTreeViewResponseBo> children = snd.getChildren();
|
|
|
if (children == null) {
|
|
|
children = new ArrayList<>();
|
|
|
}
|
|
|
SaleCategoryTreeViewResponseBo treeViewResponseBo1 = OrikaUtils.map(category3, SaleCategoryTreeViewResponseBo.class);
|
|
|
treeViewResponseBo1.setState(StringUtils.EMPTY);
|
|
|
children.add(treeViewResponseBo1);
|
|
|
snd.setChildren(children);
|
|
|
iterator.remove();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 处理一下一级分类下没有二级分类的state,把state值改为空字符串
|
|
|
for (SaleCategoryTreeViewResponseBo treeViewResponseBo : responseBos) {
|
|
|
if (CollectionUtils.isEmpty(treeViewResponseBo.getChildren())) {
|
|
|
treeViewResponseBo.setState(StringUtils.EMPTY);
|
|
|
}else{
|
|
|
//处理二级
|
|
|
for(SaleCategoryTreeViewResponseBo snd: treeViewResponseBo.getChildren()){
|
|
|
if (CollectionUtils.isEmpty(snd.getChildren())) {
|
|
|
snd.setState(StringUtils.EMPTY);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|