|
|
|
@ -1,13 +1,24 @@
|
|
|
|
|
package com.xjs.mall.product.service.impl;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.TypeReference;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
import com.ruoyi.common.core.utils.StringUtils;
|
|
|
|
|
import com.xjs.exception.MallException;
|
|
|
|
|
import com.xjs.mall.product.dao.CategoryDao;
|
|
|
|
|
import com.xjs.mall.product.entity.AttrEntity;
|
|
|
|
|
import com.xjs.mall.product.entity.AttrGroupEntity;
|
|
|
|
|
import com.xjs.mall.product.entity.CategoryBrandRelationEntity;
|
|
|
|
|
import com.xjs.mall.product.entity.CategoryEntity;
|
|
|
|
|
import com.xjs.mall.product.service.AttrGroupService;
|
|
|
|
|
import com.xjs.mall.product.service.AttrService;
|
|
|
|
|
import com.xjs.mall.product.service.CategoryBrandRelationService;
|
|
|
|
|
import com.xjs.mall.product.service.CategoryService;
|
|
|
|
|
import com.xjs.mall.product.vo.Catelog2Vo;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
@ -15,6 +26,9 @@ import javax.annotation.Resource;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
import static com.xjs.consts.RedisConst.CATALOG_AFTER;
|
|
|
|
|
import static com.xjs.consts.RedisConst.CATALOG_BEFORE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Service("categoryService")
|
|
|
|
|
@Transactional
|
|
|
|
@ -26,26 +40,78 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity
|
|
|
|
|
@Autowired
|
|
|
|
|
private CategoryBrandRelationService categoryBrandRelationService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private StringRedisTemplate stringRedisTemplate;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private AttrGroupService attrGroupService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private AttrService attrService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<CategoryEntity> listWithTree() {
|
|
|
|
|
//查缓存
|
|
|
|
|
String cache = stringRedisTemplate.opsForValue().get(CATALOG_AFTER);
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isEmpty(cache)) {
|
|
|
|
|
//1、查询所有分类
|
|
|
|
|
List<CategoryEntity> entities = categoryDao.selectList(null);
|
|
|
|
|
|
|
|
|
|
//2、找到所有一级分类
|
|
|
|
|
return entities.stream().filter(categoryEntity ->
|
|
|
|
|
categoryEntity.getParentCid() == 0)
|
|
|
|
|
List<CategoryEntity> collect = entities.stream()
|
|
|
|
|
.filter(categoryEntity -> categoryEntity.getParentCid() == 0)
|
|
|
|
|
.peek(menu -> menu.setChildren(this.getChildrens(menu, entities)))
|
|
|
|
|
.sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
//存入缓存
|
|
|
|
|
String jsonString = JSON.toJSONString(collect);
|
|
|
|
|
stringRedisTemplate.opsForValue().set(CATALOG_AFTER, jsonString);
|
|
|
|
|
|
|
|
|
|
return collect;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
return JSON.parseObject(cache, new TypeReference<List<CategoryEntity>>() {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void removeMenuByIds(List<Long> asList) {
|
|
|
|
|
// todo 检查当前要删除的菜单是否被其他的地方引用
|
|
|
|
|
//检查当前要删除的菜单是否被其他的地方引用
|
|
|
|
|
|
|
|
|
|
for (Long id : asList) {
|
|
|
|
|
// 1、品牌关联
|
|
|
|
|
LambdaQueryWrapper<CategoryBrandRelationEntity> wrapperBrand = new LambdaQueryWrapper<CategoryBrandRelationEntity>()
|
|
|
|
|
.eq(CategoryBrandRelationEntity::getCatelogId, id);
|
|
|
|
|
List<CategoryBrandRelationEntity> relationEntityList = categoryBrandRelationService.list(wrapperBrand);
|
|
|
|
|
if (CollUtil.isNotEmpty(relationEntityList)) {
|
|
|
|
|
throw new MallException("请先删除关联品牌信息");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2、属性分组关联
|
|
|
|
|
LambdaQueryWrapper<AttrGroupEntity> wrapperAttrGroup = new LambdaQueryWrapper<AttrGroupEntity>()
|
|
|
|
|
.eq(AttrGroupEntity::getCatelogId, id);
|
|
|
|
|
List<AttrGroupEntity> groupEntityList = attrGroupService.list(wrapperAttrGroup);
|
|
|
|
|
if (CollUtil.isNotEmpty(groupEntityList)) {
|
|
|
|
|
throw new MallException("请先删除关联属性分组信息");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3、销售属性/规格参数
|
|
|
|
|
LambdaQueryWrapper<AttrEntity> wrapperAttr = new LambdaQueryWrapper<AttrEntity>()
|
|
|
|
|
.eq(AttrEntity::getCatelogId, id);
|
|
|
|
|
List<AttrEntity> attrEntityList = attrService.list(wrapperAttr);
|
|
|
|
|
if (CollUtil.isNotEmpty(attrEntityList)) {
|
|
|
|
|
throw new MallException("请先删除关联销售属性/规格参数信息");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
categoryDao.deleteBatchIds(asList);
|
|
|
|
|
|
|
|
|
|
//删除后清除缓存
|
|
|
|
|
stringRedisTemplate.delete(Arrays.asList(CATALOG_BEFORE,CATALOG_AFTER));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -66,6 +132,9 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity
|
|
|
|
|
|
|
|
|
|
//更新关联表
|
|
|
|
|
categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
|
|
|
|
|
|
|
|
|
|
//删除缓存
|
|
|
|
|
stringRedisTemplate.delete(Arrays.asList(CATALOG_BEFORE,CATALOG_AFTER));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -77,13 +146,28 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Map<String, List<Catelog2Vo>> getCatalogJson() {
|
|
|
|
|
//先查缓存
|
|
|
|
|
String catalogJSON = stringRedisTemplate.opsForValue().get(CATALOG_BEFORE);
|
|
|
|
|
if (StringUtils.isEmpty(catalogJSON)) {
|
|
|
|
|
//缓存没有查数据库并且放入
|
|
|
|
|
Map<String, List<Catelog2Vo>> catalogJsonFormDb = this.getCatalogJsonFormDb();
|
|
|
|
|
String jsonString = JSON.toJSONString(catalogJsonFormDb);
|
|
|
|
|
stringRedisTemplate.opsForValue().set(CATALOG_BEFORE, jsonString);
|
|
|
|
|
return catalogJsonFormDb;
|
|
|
|
|
} else {
|
|
|
|
|
return JSON.parseObject(catalogJSON, new TypeReference<Map<String, List<Catelog2Vo>>>() {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//可优化
|
|
|
|
|
private Map<String, List<Catelog2Vo>> getCatalogJsonFormDb() {
|
|
|
|
|
//查出所有1级分类
|
|
|
|
|
List<CategoryEntity> level1Categorys = getLevel1Categorys();
|
|
|
|
|
|
|
|
|
|
//封装数据
|
|
|
|
|
return level1Categorys.stream().collect(Collectors.toMap(
|
|
|
|
|
k -> k.getCatId().toString(),
|
|
|
|
|
v -> {
|
|
|
|
|
return level1Categorys.stream().collect(Collectors.toMap(k -> k.getCatId().toString(), v -> {
|
|
|
|
|
//每一个的一级分类,查到这个一级分类的二级分类
|
|
|
|
|
List<CategoryEntity> categoryEntities = super.baseMapper.selectList(new LambdaQueryWrapper<CategoryEntity>().eq(CategoryEntity::getParentCid, v.getCatId()));
|
|
|
|
|
|
|
|
|
@ -110,8 +194,7 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return catelog2Vos;
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//225,25,2
|
|
|
|
@ -139,10 +222,7 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity
|
|
|
|
|
//-----------peek和map区别---------------//
|
|
|
|
|
//map有返回值,peek没有返回值
|
|
|
|
|
|
|
|
|
|
return all.stream().filter(categoryEntity -> Objects.equals(categoryEntity.getParentCid(), root.getCatId()))
|
|
|
|
|
.peek(categoryEntity -> categoryEntity.setChildren(getChildrens(categoryEntity, all)))
|
|
|
|
|
.sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
return all.stream().filter(categoryEntity -> Objects.equals(categoryEntity.getParentCid(), root.getCatId())).peek(categoryEntity -> categoryEntity.setChildren(getChildrens(categoryEntity, all))).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))).collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|