parent
b9c74832e3
commit
53efa6436e
73 changed files with 846 additions and 247 deletions
@ -0,0 +1,159 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||||
|
* |
||||||
|
* https://www.mall4j.com/
|
||||||
|
* |
||||||
|
* 未经允许,不可做商业用途! |
||||||
|
* |
||||||
|
* 版权所有,侵权必究! |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.huoran.nakadai.controller; |
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil; |
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||||
|
import com.huoran.common.exception.YamiShopBindException; |
||||||
|
import com.huoran.common.response.ServerResponseEntity; |
||||||
|
import com.huoran.common.utils.TokenUtils; |
||||||
|
import com.huoran.nakadai.entity.app.dto.CategoryDto; |
||||||
|
import com.huoran.nakadai.entity.model.Category; |
||||||
|
import com.huoran.nakadai.entity.model.Product; |
||||||
|
import com.huoran.nakadai.service.CategoryService; |
||||||
|
import io.swagger.annotations.Api; |
||||||
|
import io.swagger.annotations.ApiOperation; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.web.bind.annotation.*; |
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import java.util.Date; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Objects; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author lanhai |
||||||
|
*/ |
||||||
|
@RestController |
||||||
|
@RequestMapping("/category") |
||||||
|
@Api(tags = "商品分类管理") |
||||||
|
public class CategoryController { |
||||||
|
|
||||||
|
@Autowired |
||||||
|
private CategoryService categoryService; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 分类信息列表接口 |
||||||
|
*/ |
||||||
|
@GetMapping("/categoryInfo") |
||||||
|
@ApiOperation(value = "根据父id查询分类列表") |
||||||
|
public ServerResponseEntity<List<CategoryDto>> categoryInfo( |
||||||
|
@RequestParam(value = "parentId", defaultValue = "0") Long parentId) { |
||||||
|
List<Category> categories = categoryService.listByParentId(parentId); |
||||||
|
List<CategoryDto> categoryDtos = BeanUtil.copyToList(categories, CategoryDto.class); |
||||||
|
return ServerResponseEntity.success(categoryDtos); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 获取菜单页面的表 |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
@GetMapping("/list") |
||||||
|
@ApiOperation(value = "获取店铺所有分类列表") |
||||||
|
public ServerResponseEntity<List<Category>> table(HttpServletRequest request){ |
||||||
|
List<Category> categoryMenuList = categoryService.tableCategory(TokenUtils.getShopIdByJwtToken(request)); |
||||||
|
return ServerResponseEntity.success(categoryMenuList); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取分类信息 |
||||||
|
*/ |
||||||
|
@GetMapping("/info") |
||||||
|
@ApiOperation(value = "获取分类信息") |
||||||
|
public ServerResponseEntity<Category> info(@RequestParam("categoryId") Long categoryId){ |
||||||
|
Category category = categoryService.getById(categoryId); |
||||||
|
return ServerResponseEntity.success(category); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 保存分类 |
||||||
|
*/ |
||||||
|
@PostMapping("/save") |
||||||
|
@ApiOperation(value = "保存分类信息") |
||||||
|
public ServerResponseEntity<Void> save(@RequestBody Category category,HttpServletRequest request){ |
||||||
|
category.setShopId(TokenUtils.getShopIdByJwtToken(request)); |
||||||
|
category.setRecTime(new Date()); |
||||||
|
Category categoryName = categoryService.getOne(new LambdaQueryWrapper<Category>().eq(Category::getCategoryName,category.getCategoryName()) |
||||||
|
.eq(Category::getShopId,category.getShopId())); |
||||||
|
if(categoryName != null){ |
||||||
|
throw new YamiShopBindException("类目名称已存在!"); |
||||||
|
} |
||||||
|
categoryService.saveCategory(category); |
||||||
|
return ServerResponseEntity.success(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 更新分类 |
||||||
|
*/ |
||||||
|
@PostMapping("/update") |
||||||
|
@ApiOperation(value = "更新分类信息") |
||||||
|
public ServerResponseEntity<String> update(@RequestBody Category category,HttpServletRequest request){ |
||||||
|
category.setShopId(TokenUtils.getShopIdByJwtToken(request)); |
||||||
|
if (Objects.equals(category.getParentId(),category.getCategoryId())) { |
||||||
|
return ServerResponseEntity.showFailMsg("分类的上级不能是自己本身"); |
||||||
|
} |
||||||
|
Category categoryName = categoryService.getOne(new LambdaQueryWrapper<Category>().eq(Category::getCategoryName,category.getCategoryName()) |
||||||
|
.eq(Category::getShopId,category.getShopId()).ne(Category::getCategoryId,category.getCategoryId())); |
||||||
|
if(categoryName != null){ |
||||||
|
throw new YamiShopBindException("类目名称已存在!"); |
||||||
|
} |
||||||
|
Category categoryDb = categoryService.getById(category.getCategoryId()); |
||||||
|
// 如果从下线改成正常,则需要判断上级的状态
|
||||||
|
if (Objects.equals(categoryDb.getStatus(),0) && Objects.equals(category.getStatus(),1) && !Objects.equals(category.getParentId(),0L)){ |
||||||
|
Category parentCategory = categoryService.getOne(new LambdaQueryWrapper<Category>().eq(Category::getCategoryId, category.getParentId())); |
||||||
|
if(Objects.isNull(parentCategory) || Objects.equals(parentCategory.getStatus(),0)){ |
||||||
|
// 修改失败,上级分类不存在或者不为正常状态
|
||||||
|
throw new YamiShopBindException("修改失败,上级分类不存在或者不为正常状态"); |
||||||
|
} |
||||||
|
} |
||||||
|
categoryService.updateCategory(category); |
||||||
|
return ServerResponseEntity.success(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 删除分类 |
||||||
|
*/ |
||||||
|
@PostMapping("/delete") |
||||||
|
@ApiOperation(value = "删除分类信息") |
||||||
|
public ServerResponseEntity<String> delete(@RequestParam("categoryId") Long categoryId){ |
||||||
|
if (categoryService.count(new LambdaQueryWrapper<Category>().eq(Category::getParentId,categoryId)) >0) { |
||||||
|
return ServerResponseEntity.showFailMsg("请删除子分类,再删除该分类"); |
||||||
|
} |
||||||
|
categoryService.deleteCategory(categoryId); |
||||||
|
return ServerResponseEntity.success(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 所有的 |
||||||
|
*/ |
||||||
|
@GetMapping("/listCategory") |
||||||
|
public ServerResponseEntity<List<Category>> listCategory(HttpServletRequest request){ |
||||||
|
return ServerResponseEntity.success(categoryService.list(new LambdaQueryWrapper<Category>() |
||||||
|
.le(Category::getGrade, 2) |
||||||
|
.eq(Category::getShopId, TokenUtils.getShopIdByJwtToken(request)) |
||||||
|
.orderByAsc(Category::getSeq))); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 所有的产品分类 |
||||||
|
*/ |
||||||
|
@GetMapping("/listProdCategory") |
||||||
|
public ServerResponseEntity<List<Category>> listProdCategory(HttpServletRequest request){ |
||||||
|
List<Category> categories = categoryService.treeSelect(TokenUtils.getShopIdByJwtToken(request),2); |
||||||
|
return ServerResponseEntity.success(categories); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,40 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||||
|
* |
||||||
|
* https://www.mall4j.com/
|
||||||
|
* |
||||||
|
* 未经允许,不可做商业用途! |
||||||
|
* |
||||||
|
* 版权所有,侵权必究! |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.huoran.nakadai.mapper; |
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||||
|
import com.huoran.nakadai.entity.model.Category; |
||||||
|
import org.apache.ibatis.annotations.Mapper; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author lanhai |
||||||
|
*/ |
||||||
|
@Mapper |
||||||
|
public interface CategoryMapper extends BaseMapper<Category> { |
||||||
|
|
||||||
|
/** |
||||||
|
* 根据父级id获取分类列表 |
||||||
|
* |
||||||
|
* @param parentId |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
List<Category> listByParentId(Long parentId); |
||||||
|
|
||||||
|
/** |
||||||
|
* 根据店铺id获取分类列表 |
||||||
|
* |
||||||
|
* @param shopId |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
List<Category> tableCategory(Integer shopId); |
||||||
|
} |
@ -0,0 +1,55 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||||
|
<mapper namespace="com.huoran.nakadai.mapper.CategoryMapper"> |
||||||
|
|
||||||
|
<resultMap id="categoryProdMap" type="com.huoran.nakadai.entity.model.Category"> |
||||||
|
<id column="category_id" jdbcType="BIGINT" property="categoryId" /> |
||||||
|
<result column="category_name" jdbcType="VARCHAR" property="categoryName" /> |
||||||
|
<collection property="products" column="prod_id" ofType="com.huoran.nakadai.entity.model.Product"> |
||||||
|
<!--修改--> |
||||||
|
<!--<result column="prod_id" jdbcType="BIGINT" property="prodId" />--> |
||||||
|
<!--<result column="prod_name" jdbcType="VARCHAR" property="prodName" />--> |
||||||
|
<!--<result column="price" jdbcType="DECIMAL" property="price" />--> |
||||||
|
<!--<result column="brief" jdbcType="VARCHAR" property="brief" />--> |
||||||
|
<!--<result column="imgs" jdbcType="VARCHAR" property="imgs" />--> |
||||||
|
<!--<result column="prod_category_id" jdbcType="BIGINT" property="categoryId" />--> |
||||||
|
<!--<result column="buys" jdbcType="INTEGER" property="buys" />--> |
||||||
|
<!--<result column="stocks" jdbcType="INTEGER" property="stocks" />--> |
||||||
|
<!--<result column="has_prop" jdbcType="INTEGER" property="hasProp" />--> |
||||||
|
<!--<result column="spec_list_str" jdbcType="VARCHAR" property="specListStr" />--> |
||||||
|
|
||||||
|
<id property="prodId" column="prod_id"/> |
||||||
|
<result property="prodName" column="prod_name"/> |
||||||
|
<result property="shopId" column="shop_id"/> |
||||||
|
<result property="oriPrice" column="ori_price"/> |
||||||
|
<result property="price" column="price"/> |
||||||
|
<result property="brief" column="brief"/> |
||||||
|
<result property="content" column="content"/> |
||||||
|
<result property="imgs" column="imgs"/> |
||||||
|
<result property="status" column="status"/> |
||||||
|
<result property="categoryId" column="category_id"/> |
||||||
|
<result property="soldNum" column="sold_num"/> |
||||||
|
<result property="totalStocks" column="total_stocks"/> |
||||||
|
<!-- <result property="transportMode" column="transport_mode"/>--> |
||||||
|
<!-- <result property="transportId" column="transport_id"/>--> |
||||||
|
<result property="createTime" column="create_time"/> |
||||||
|
<result property="updateTime" column="update_time"/> |
||||||
|
|
||||||
|
<!--<collection property="skuList" column="sku_id" ofType="com.yami.shop.model.model.Sku">--> |
||||||
|
<!--<result column="sku_id" jdbcType="BIGINT" property="skuId" />--> |
||||||
|
<!--<result column="sPrice" jdbcType="DECIMAL" property="price" />--> |
||||||
|
<!--<result column="sStocks" jdbcType="INTEGER" property="stocks" />--> |
||||||
|
<!--<result column="sName" jdbcType="VARCHAR" property="name" />--> |
||||||
|
<!--</collection>--> |
||||||
|
</collection> |
||||||
|
</resultMap> |
||||||
|
|
||||||
|
|
||||||
|
<select id="listByParentId" resultType="com.huoran.nakadai.entity.model.Category"> |
||||||
|
select category_id,category_name,`seq`,`status`,pic from tz_category where parent_id = #{parentId} and `status` = 1 order by seq |
||||||
|
</select> |
||||||
|
|
||||||
|
<select id="tableCategory" resultType="com.huoran.nakadai.entity.model.Category"> |
||||||
|
select category_id ,parent_id ,category_name,pic,seq,status from tz_category where shop_id = #{shopId} order by seq |
||||||
|
</select> |
||||||
|
</mapper> |
@ -0,0 +1,65 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||||
|
* |
||||||
|
* https://www.mall4j.com/
|
||||||
|
* |
||||||
|
* 未经允许,不可做商业用途! |
||||||
|
* |
||||||
|
* 版权所有,侵权必究! |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.huoran.nakadai.service; |
||||||
|
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService; |
||||||
|
import com.huoran.nakadai.entity.model.Category; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author lanhai |
||||||
|
* 商品分类 |
||||||
|
*/ |
||||||
|
public interface CategoryService extends IService<Category> { |
||||||
|
|
||||||
|
/** |
||||||
|
* 根据parentId获取分类 |
||||||
|
* @param parentId 0 一级分类 |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
List<Category> listByParentId(Long parentId); |
||||||
|
|
||||||
|
/** |
||||||
|
* 获取用于页面表单展现的category列表,根据seq排序 |
||||||
|
* @param shopId 店铺id |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
List<Category> tableCategory(Integer shopId); |
||||||
|
|
||||||
|
/** |
||||||
|
* 保存分类、品牌、参数 |
||||||
|
* @param category |
||||||
|
*/ |
||||||
|
void saveCategory(Category category); |
||||||
|
|
||||||
|
/** |
||||||
|
* 修改分类、品牌、参数 |
||||||
|
* @param category |
||||||
|
*/ |
||||||
|
void updateCategory(Category category); |
||||||
|
|
||||||
|
/** |
||||||
|
* 删除分类、品牌、参数 以及分类对应的图片 |
||||||
|
* @param categoryId 分类id |
||||||
|
*/ |
||||||
|
void deleteCategory(Long categoryId); |
||||||
|
|
||||||
|
/** |
||||||
|
* 根据店铺id和层级,获取商品分类树 |
||||||
|
* @param shopId |
||||||
|
* @param grade |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
List<Category> treeSelect(Integer shopId,int grade); |
||||||
|
|
||||||
|
} |
@ -0,0 +1,145 @@ |
|||||||
|
/* |
||||||
|
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved. |
||||||
|
* |
||||||
|
* https://www.mall4j.com/
|
||||||
|
* |
||||||
|
* 未经允许,不可做商业用途! |
||||||
|
* |
||||||
|
* 版权所有,侵权必究! |
||||||
|
*/ |
||||||
|
|
||||||
|
package com.huoran.nakadai.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.huoran.nakadai.entity.model.Category; |
||||||
|
import com.huoran.nakadai.mapper.CategoryMapper; |
||||||
|
import com.huoran.nakadai.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;
|
||||||
|
|
||||||
|
|
||||||
|
@Override |
||||||
|
public List<Category> listByParentId(Long parentId) { |
||||||
|
return categoryMapper.listByParentId(parentId); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public List<Category> tableCategory(Integer shopId) { |
||||||
|
return categoryMapper.tableCategory(shopId); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
@Transactional(rollbackFor = Exception.class) |
||||||
|
public void saveCategory(Category category) { |
||||||
|
category.setRecTime(new Date()); |
||||||
|
// 保存分类信息
|
||||||
|
categoryMapper.insert(category); |
||||||
|
|
||||||
|
// insertBrandsAndAttributes(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());
|
||||||
|
// }
|
||||||
|
} |
||||||
|
|
||||||
|
@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());
|
||||||
|
// }
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// private void deleteBrandsAndAttributes(Long categoryId) {
|
||||||
|
// // 删除所有分类所关联的品牌
|
||||||
|
// categoryBrandMapper.deleteByCategoryId(categoryId);
|
||||||
|
// // 删除所有分类所关联的参数
|
||||||
|
// categoryPropMapper.deleteByCategoryId(categoryId);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 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());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
@Override |
||||||
|
public List<Category> treeSelect(Integer shopId,int grade) { |
||||||
|
List<Category> categories = categoryMapper.selectList(new LambdaQueryWrapper<Category>().le(Category::getGrade,grade).eq(Category::getShopId,shopId)); |
||||||
|
return categoryListToTree(categories); |
||||||
|
} |
||||||
|
|
||||||
|
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; |
||||||
|
} |
||||||
|
|
||||||
|
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); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue