|
|
@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* https://www.mall4j.com/
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* 未经允许,不可做商业用途!
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* 版权所有,侵权必究!
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package com.yami.shop.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
|
|
|
import com.yami.shop.bean.model.Category;
|
|
|
|
|
|
|
|
import com.yami.shop.dao.CategoryBrandMapper;
|
|
|
|
|
|
|
|
import com.yami.shop.dao.CategoryMapper;
|
|
|
|
|
|
|
|
import com.yami.shop.dao.CategoryPropMapper;
|
|
|
|
|
|
|
|
import com.yami.shop.service.AttachFileService;
|
|
|
|
|
|
|
|
import com.yami.shop.service.CategoryService;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 商品分类服务实现类,提供商品分类相关的业务逻辑实现
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @author lanhai
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
|
|
|
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private CategoryMapper categoryMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private CategoryBrandMapper categoryBrandMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private CategoryPropMapper categoryPropMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private AttachFileService attachFileService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 根据父级ID查询分类列表
|
|
|
|
|
|
|
|
* @param parentId 父级ID
|
|
|
|
|
|
|
|
* @return 分类列表
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public List<Category> listByParentId(Long parentId) {
|
|
|
|
|
|
|
|
return categoryMapper.listByParentId(parentId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 根据店铺ID查询分类列表,并根据seq排序,用于页面表单展示
|
|
|
|
|
|
|
|
* @param shopId 店铺ID
|
|
|
|
|
|
|
|
* @return 分类列表
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public List<Category> tableCategory(Long shopId) {
|
|
|
|
|
|
|
|
return categoryMapper.tableCategory(shopId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 保存分类信息,包括品牌和参数
|
|
|
|
|
|
|
|
* @param category 分类对象
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
|
|
|
public void saveCategory(Category category) {
|
|
|
|
|
|
|
|
category.setRecTime(new Date());
|
|
|
|
|
|
|
|
// 保存分类信息
|
|
|
|
|
|
|
|
categoryMapper.insert(category);
|
|
|
|
|
|
|
|
insertBrandsAndAttributes(category);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 更新分类信息,包括品牌和参数
|
|
|
|
|
|
|
|
* @param category 分类对象
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
|
|
|
public void updateCategory(Category category) {
|
|
|
|
|
|
|
|
Category dbCategory = categoryMapper.selectById(category.getCategoryId());
|
|
|
|
|
|
|
|
category.setUpdateTime(new Date());
|
|
|
|
|
|
|
|
// 保存分类信息
|
|
|
|
|
|
|
|
categoryMapper.updateById(category);
|
|
|
|
|
|
|
|
// 先删除后增加
|
|
|
|
|
|
|
|
deleteBrandsAndAttributes(category.getCategoryId());
|
|
|
|
|
|
|
|
insertBrandsAndAttributes(category);
|
|
|
|
|
|
|
|
// 如果以前有图片,并且图片与现在不同,则删除以前的图片
|
|
|
|
|
|
|
|
// if (StrUtil.isNotBlank(dbCategory.getPic()) && !dbCategory.getPic().equals(category.getPic())) {
|
|
|
|
|
|
|
|
// attachFileService.deleteFile(dbCategory.getPic());
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 删除分类信息,包括品牌、参数以及分类对应的图片
|
|
|
|
|
|
|
|
* @param categoryId 分类ID
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
|
|
|
public void deleteCategory(Long categoryId) {
|
|
|
|
|
|
|
|
Category category = categoryMapper.selectById(categoryId);
|
|
|
|
|
|
|
|
categoryMapper.deleteById(categoryId);
|
|
|
|
|
|
|
|
deleteBrandsAndAttributes(categoryId);
|
|
|
|
|
|
|
|
// if (StrUtil.isNotBlank(category.getPic())) {
|
|
|
|
|
|
|
|
// attachFileService.deleteFile(category.getPic());
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 删除所有分类所关联的品牌和参数
|
|
|
|
|
|
|
|
* @param categoryId 分类ID
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void deleteBrandsAndAttributes(Long categoryId) {
|
|
|
|
|
|
|
|
// 删除所有分类所关联的品牌
|
|
|
|
|
|
|
|
categoryBrandMapper.deleteByCategoryId(categoryId);
|
|
|
|
|
|
|
|
// 删除所有分类所关联的参数
|
|
|
|
|
|
|
|
categoryPropMapper.deleteByCategoryId(categoryId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 保存分类与品牌信息,保存分类与参数信息
|
|
|
|
|
|
|
|
* @param category 分类对象
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void insertBrandsAndAttributes(Category category) {
|
|
|
|
|
|
|
|
// 保存分类与品牌信息
|
|
|
|
|
|
|
|
if(CollUtil.isNotEmpty(category.getBrandIds())){
|
|
|
|
|
|
|
|
categoryBrandMapper.insertCategoryBrand(category.getCategoryId(), category.getBrandIds());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 保存分类与参数信息
|
|
|
|
|
|
|
|
if(CollUtil.isNotEmpty(category.getAttributeIds())){
|
|
|
|
|
|
|
|
categoryPropMapper.insertCategoryProp(category.getCategoryId(), category.getAttributeIds());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 根据店铺ID和层级,获取商品分类树
|
|
|
|
|
|
|
|
* @param shopId 店铺ID
|
|
|
|
|
|
|
|
* @param grade 分类层级
|
|
|
|
|
|
|
|
* @return 分类树列表
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public List<Category> treeSelect(Long shopId, int grade) {
|
|
|
|
|
|
|
|
List<Category> categories = categoryMapper.selectList(new LambdaQueryWrapper<Category>().le(Category::getGrade, grade).eq(Category::getShopId, shopId));
|
|
|
|
|
|
|
|
return categoryListToTree(categories);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 将分类列表转换为树形结构
|
|
|
|
|
|
|
|
* @param categorys 分类列表
|
|
|
|
|
|
|
|
* @return 树形结构的分类列表
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public List<Category> categoryListToTree(List<Category> categorys){
|
|
|
|
|
|
|
|
if (CollectionUtils.isEmpty(categorys)) {
|
|
|
|
|
|
|
|
return Lists.newArrayList();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<Long, List<Category>> categoryMap = categorys.stream().collect(Collectors.groupingBy(Category::getParentId));
|
|
|
|
|
|
|
|
List<Category> rootList = categoryMap.get(0L);
|
|
|
|
|
|
|
|
transformCategoryTree(rootList, categoryMap);
|
|
|
|
|
|
|
|
return rootList;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 递归构建分类树
|
|
|
|
|
|
|
|
* @param categorys 当前层级的分类列表
|
|
|
|
|
|
|
|
* @param categoryMap 所有分类的映射
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void transformCategoryTree(List<Category> categorys, Map<Long, List<Category>> categoryMap) {
|
|
|
|
|
|
|
|
for (Category category : categorys) {
|
|
|
|
|
|
|
|
List<Category> nextList = categoryMap.get(category.getCategoryId());
|
|
|
|
|
|
|
|
if (CollectionUtils.isEmpty(nextList)) {
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 将排序完成的list设为下一层级
|
|
|
|
|
|
|
|
category.setCategories(nextList);
|
|
|
|
|
|
|
|
// 处理下一层级
|
|
|
|
|
|
|
|
transformCategoryTree(nextList, categoryMap);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|