商品服务API品牌管理-品牌分类关联与级联更新

This commit is contained in:
Kirk Lin 2021-06-14 15:16:02 +08:00
parent 519c8ab16b
commit caaf701723
13 changed files with 152 additions and 67 deletions

View file

@ -0,0 +1,26 @@
package name.lkk.kkmall.product.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* <p>Title: MybatisConfig</p>
* Description引入分页插件
* date2020/6/2 15:20
*/
@EnableTransactionManagement
@Configuration
public class MybatisConfig {
// 最新版
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}

View file

@ -1,19 +1,16 @@
package name.lkk.kkmall.product.controller;
import java.util.Arrays;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import name.lkk.kkmall.product.entity.CategoryBrandRelationEntity;
import name.lkk.kkmall.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import name.lkk.common.utils.PageUtils;
import name.lkk.common.utils.R;
import name.lkk.kkmall.product.entity.CategoryBrandRelationEntity;
import name.lkk.kkmall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@ -27,26 +24,38 @@ import name.lkk.common.utils.R;
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
@Autowired
private CategoryBrandRelationService categoryBrandRelationService;
/**
* 获取当前品牌的所有分类列表
*/
@GetMapping("/catelog/list")
public R list(@RequestParam("brandId") Long brandId){
List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId)
);
return R.ok().put("data", data);
}
/**
* 列表
*/
@RequestMapping("/list")
//@RequiresPermissions("product:categorybrandrelation:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = categoryBrandRelationService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 信息
*/
@RequestMapping("/info/{id}")
//@RequiresPermissions("product:categorybrandrelation:info")
public R info(@PathVariable("id") Long id){
CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
@ -57,9 +66,8 @@ public class CategoryBrandRelationController {
* 保存
*/
@RequestMapping("/save")
//@RequiresPermissions("product:categorybrandrelation:save")
public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
categoryBrandRelationService.save(categoryBrandRelation);
categoryBrandRelationService.saveDetail(categoryBrandRelation);
return R.ok();
}
@ -68,7 +76,6 @@ public class CategoryBrandRelationController {
* 修改
*/
@RequestMapping("/update")
//@RequiresPermissions("product:categorybrandrelation:update")
public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
categoryBrandRelationService.updateById(categoryBrandRelation);
@ -79,11 +86,10 @@ public class CategoryBrandRelationController {
* 删除
*/
@RequestMapping("/delete")
//@RequiresPermissions("product:categorybrandrelation:delete")
public R delete(@RequestBody Long[] ids){
categoryBrandRelationService.removeByIds(Arrays.asList(ids));
return R.ok();
}
}

View file

@ -1,20 +1,16 @@
package name.lkk.kkmall.product.controller;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import name.lkk.common.utils.R;
import name.lkk.kkmall.product.entity.CategoryEntity;
import name.lkk.kkmall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import name.lkk.kkmall.product.entity.CategoryEntity;
import name.lkk.kkmall.product.service.CategoryService;
import name.lkk.common.utils.PageUtils;
import name.lkk.common.utils.R;
import java.util.Arrays;
import java.util.List;
@ -89,7 +85,7 @@ public class CategoryController {
@RequestMapping("/update")
//@RequiresPermissions("product:category:update")
public R update(@RequestBody CategoryEntity category){
categoryService.updateById(category);
categoryService.updateCascade(category);
return R.ok();
}

View file

@ -1,8 +1,9 @@
package name.lkk.kkmall.product.dao;
import name.lkk.kkmall.product.entity.CategoryBrandRelationEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import name.lkk.kkmall.product.entity.CategoryBrandRelationEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* 品牌分类关联
@ -14,4 +15,6 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CategoryBrandRelationDao extends BaseMapper<CategoryBrandRelationEntity> {
void updateCategory(@Param("catId") Long catId, String name);
}

View file

@ -19,5 +19,8 @@ public interface CategoryBrandRelationService extends IService<CategoryBrandRela
void updateBrand(Long brandId, String name);
void saveDetail(CategoryBrandRelationEntity categoryBrandRelation);
void updateCategory(Long catId, String name);
}

View file

@ -39,5 +39,9 @@ public interface CategoryService extends IService<CategoryEntity> {
*/
Long[] findCateLogPath(Long catelogId);
/**
* 级联更新所有数据
*/
void updateCascade(CategoryEntity category);
}

View file

@ -1,22 +1,21 @@
package name.lkk.kkmall.product.service.impl;
import name.lkk.kkmall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import name.lkk.common.utils.PageUtils;
import name.lkk.common.utils.Query;
import name.lkk.kkmall.product.dao.BrandDao;
import name.lkk.kkmall.product.entity.BrandEntity;
import name.lkk.kkmall.product.service.BrandService;
import name.lkk.kkmall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.util.ObjectUtils;
import java.util.List;
import java.util.Map;
@Service("brandService")
@ -26,11 +25,15 @@ public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> impleme
@Override
public PageUtils queryPage(Map<String, Object> params) {
QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
String key = (String) params.get("key");
if(!ObjectUtils.isEmpty(key)){
wrapper.eq("brand_id", key).or().like("name", key);
}
IPage<BrandEntity> page = this.page(
new Query<BrandEntity>().getPage(params),
new QueryWrapper<BrandEntity>()
wrapper
);
return new PageUtils(page);
}
@ -42,7 +45,7 @@ public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> impleme
public void updateDetail(BrandEntity brand) {
// 保证冗余字段的数据一致
this.updateById(brand);
if(!StringUtils.isEmpty(brand.getName())){
if(!ObjectUtils.isEmpty(brand.getName())){
// 同步更新其他关联表的数据
categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
// TODO 更新其它关联

View file

@ -1,21 +1,31 @@
package name.lkk.kkmall.product.service.impl;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import name.lkk.common.utils.PageUtils;
import name.lkk.common.utils.Query;
import name.lkk.kkmall.product.dao.BrandDao;
import name.lkk.kkmall.product.dao.CategoryBrandRelationDao;
import name.lkk.kkmall.product.dao.CategoryDao;
import name.lkk.kkmall.product.entity.BrandEntity;
import name.lkk.kkmall.product.entity.CategoryBrandRelationEntity;
import name.lkk.kkmall.product.entity.CategoryEntity;
import name.lkk.kkmall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
@Autowired
private BrandDao brandDao;
@Autowired
private CategoryDao categoryDao;
@Override
public PageUtils queryPage(Map<String, Object> params) {
@ -36,4 +46,24 @@ public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandR
this.update(entity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
}
@Override
public void updateCategory(Long catId, String name) {
this.baseMapper.updateCategory(catId, name);
}
/**
* 根据获取品牌id 三级分类id查询对应的名字保存到数据库
*/
@Override
public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
// 获取品牌id 三级分类id
Long brandId = categoryBrandRelation.getBrandId();
Long catelogId = categoryBrandRelation.getCatelogId();
// 根据id查询详细名字
BrandEntity brandEntity = brandDao.selectById(brandId);
CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
categoryBrandRelation.setBrandName(brandEntity.getName());
categoryBrandRelation.setCatelogName(categoryEntity.getName());
this.save(categoryBrandRelation);
}
}

View file

@ -7,8 +7,11 @@ import name.lkk.common.utils.PageUtils;
import name.lkk.common.utils.Query;
import name.lkk.kkmall.product.dao.CategoryDao;
import name.lkk.kkmall.product.entity.CategoryEntity;
import name.lkk.kkmall.product.service.CategoryBrandRelationService;
import name.lkk.kkmall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;
@ -17,6 +20,9 @@ import java.util.stream.Collectors;
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
@Autowired
private CategoryBrandRelationService categoryBrandRelationService;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<CategoryEntity> page = this.page(
@ -35,15 +41,11 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity
public List<CategoryEntity> listWithTree() {
List<CategoryEntity> entities = baseMapper.selectList(null);
// 筛选出所有一级分类
List<CategoryEntity> level1Menus = entities.stream().
filter((categoryEntity) -> categoryEntity.getParentCid() == 0)
.map((menu) -> {
menu.setChildren(getChildrens(menu, entities));
return menu;
}).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
.collect(Collectors.toList());
return level1Menus;
return entities.stream().
filter((categoryEntity) -> categoryEntity.getParentCid() == 0)
.peek((menu) -> menu.setChildren(getChildrens(menu, entities))).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
.collect(Collectors.toList());
}
/**
@ -55,7 +57,7 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity
@Override
public List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> all){
List<CategoryEntity> children = all.stream().filter(categoryEntity ->
categoryEntity.getParentCid() == root.getCatId()
categoryEntity.getParentCid().equals(root.getCatId())
).map(categoryEntity -> {
categoryEntity.setChildren(getChildrens(categoryEntity, all));
return categoryEntity;
@ -78,6 +80,13 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity
return paths.toArray(new Long[paths.size()]);
}
@Transactional
@Override
public void updateCascade(CategoryEntity category) {
this.updateById(category);
categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
}
/**
* 递归收集所有节点
*/

View file

@ -12,5 +12,8 @@
<result property="catelogName" column="catelog_name"/>
</resultMap>
<update id="updateCategory">
UPDATE `pms_category_brand_relation` SET catelog_name = #{name} WHERE catelog_id = #{catId}
</update>
</mapper>

View file

@ -1,7 +1,7 @@
<template>
<div>
<el-upload
action="http://mall-fire.oss-cn-shenzhen.aliyuncs.com"
action="http://mall-kk.oss-cn-hangzhou.aliyuncs.com"
:data="dataObj"
:list-type="listType"
:file-list="fileList"
@ -21,8 +21,9 @@
</div>
</template>
<script>
import { policy } from "./policy";
import { getUUID } from '@/utils'
import {policy} from "./policy";
import {getUUID} from '@/utils'
export default {
name: "multiUpload",
props: {

View file

@ -1,8 +1,9 @@
import http from '@/utils/httpRequest.js'
export function policy() {
return new Promise((resolve,reject)=>{
http({
url: http.adornUrl("/third/party/oss/policy"),
url: http.adornUrl("/third/server/oss/policy"),
method: "get",
params: http.adornParams({})
}).then(({ data }) => {

View file

@ -1,7 +1,7 @@
<template>
<div>
<el-upload
action="http://mall-fire.oss-cn-shenzhen.aliyuncs.com"
action="http://mall-kk.oss-cn-hangzhou.aliyuncs.com"
:data="dataObj"
list-type="picture"
:multiple="false"
@ -21,8 +21,8 @@
</div>
</template>
<script>
import { policy } from "./policy";
import { getUUID } from "@/utils";
import {policy} from "./policy";
import {getUUID} from "@/utils";
export default {
name: "singleUpload",