1、品牌和分类关联实现

2、维护品牌及分类级联修改删除数据一致性
pull/254/head
xjs 4 years ago
parent 32abbd5ad5
commit 561024a571

@ -0,0 +1,32 @@
import request from '@/utils/request'
//添加品牌关联
export function addCategoryBrandRelation(data) {
return request({
url: '/mall-product/product/categorybrandrelation/save',
method: 'post',
data:data
})
}
//删除品牌关联
export function delCategoryBrandRelation(ids) {
return request({
url: '/mall-product/product/categorybrandrelation/delete',
method: 'delete',
data:ids
})
}
//获取品牌关联
export function categoryBrandRelationList(data) {
return request({
url: '/mall-product/product/categorybrandrelation/catelog/list',
method: 'get',
params:data
})
}

@ -74,17 +74,18 @@
<!-- 弹窗, 新增 / 修改 --> <!-- 弹窗, 新增 / 修改 -->
<add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update> <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
<el-dialog title="关联分类" :visible.sync="cateRelationDialogVisible" width="30%"> <el-dialog title="关联分类" :visible.sync="cateRelationDialogVisible" width="30%" @close="closeDialog">
<div style="margin-bottom: 10px">
<el-popover placement="right-end" v-model="popCatelogSelectVisible"> <el-popover placement="right-end" v-model="popCatelogSelectVisible">
<category-cascader :catelogPath.sync="catelogPath"></category-cascader> <category-cascader :catelogPath.sync="catelogPath"></category-cascader>
<div style="text-align: right; margin: 0"> <div style="text-align: right; margin: 0">
<el-button size="mini" type="text" @click="popCatelogSelectVisible = false">取消</el-button> <el-button size="mini" type="text" @click="popCatelogSelectVisible = false">取消</el-button>
<el-button type="primary" size="mini" @click="addCatelogSelect"></el-button> <el-button type="primary" size="mini" @click="addCatelogSelect"></el-button>
</div> </div>
<el-button slot="reference">新增关联</el-button> <el-button slot="reference" icon="el-icon-circle-plus-outline" size="mini">新增关联</el-button>
</el-popover> </el-popover>
</div>
<el-table :data="cateRelationTableData" style="width: 100%"> <el-table :data="cateRelationTableData" style="width: 100%">
<el-table-column prop="id" label="#"></el-table-column>
<el-table-column prop="brandName" label="品牌名"></el-table-column> <el-table-column prop="brandName" label="品牌名"></el-table-column>
<el-table-column prop="catelogName" label="分类名"></el-table-column> <el-table-column prop="catelogName" label="分类名"></el-table-column>
<el-table-column fixed="right" header-align="center" align="center" label="操作"> <el-table-column fixed="right" header-align="center" align="center" label="操作">
@ -109,7 +110,13 @@
<script> <script>
import AddOrUpdate from "./brand-add-or-update"; import AddOrUpdate from "./brand-add-or-update";
import {editBrand, getBrandList,delBrand} from "@/api/mall/product/brand"; import {editBrand, getBrandList,delBrand} from "@/api/mall/product/brand";
// import CategoryCascader from "../common/category-cascader"; import CategoryCascader from "../../components/mall/category-cascader"
import {
addCategoryBrandRelation,
categoryBrandRelationList,
delCategoryBrandRelation
} from "@/api/mall/product/category-relation"
export default { export default {
name: "Brand", name: "Brand",
data() { data() {
@ -133,7 +140,7 @@ export default {
}, },
components: { components: {
AddOrUpdate, AddOrUpdate,
// CategoryCascader CategoryCascader
}, },
created() { created() {
this.getDataList(); this.getDataList();
@ -141,44 +148,37 @@ export default {
methods: { methods: {
addCatelogSelect() { addCatelogSelect() {
this.popCatelogSelectVisible = false; this.popCatelogSelectVisible = false;
this.$http({ let data ={
url: this.$http.adornUrl("/product/categorybrandrelation/save"),
method: "post",
data: this.$http.adornData({
brandId: this.brandId, brandId: this.brandId,
catelogId: this.catelogPath[this.catelogPath.length - 1] catelogId: this.catelogPath[this.catelogPath.length - 1]
}, false) }
}).then(({data}) => { addCategoryBrandRelation(data).then(res =>{
this.getCateRelation(); this.getCateRelation();
}); })
}, },
deleteCateRelationHandle(id, brandId) { deleteCateRelationHandle(id, brandId) {
this.$http({ delCategoryBrandRelation([id]).then(res =>{
url: this.$http.adornUrl("/product/categorybrandrelation/delete"),
method: "post",
data: this.$http.adornData([id], false)
}).then(({data}) => {
this.getCateRelation(); this.getCateRelation();
}); })
}, },
updateCatelogHandle(brandId) { updateCatelogHandle(brandId) {
this.cateRelationDialogVisible = true; this.cateRelationDialogVisible = true;
this.brandId = brandId; this.brandId = brandId;
this.getCateRelation(); this.getCateRelation();
}, },
getCateRelation() { getCateRelation() {
this.$http({ let data ={
url: this.$http.adornUrl("/product/categorybrandrelation/catelog/list"),
method: "get",
params: this.$http.adornParams({
brandId: this.brandId brandId: this.brandId
}
categoryBrandRelationList(data).then(res =>{
this.cateRelationTableData = res.page;
}) })
}).then(({data}) => {
this.cateRelationTableData = data.data;
});
}, },
// //
getDataList() { getDataList() {
this.dataListLoading = true; this.dataListLoading = true;
@ -246,6 +246,11 @@ export default {
this.pageIndex = 1; this.pageIndex = 1;
this.getDataList(); this.getDataList();
}, },
//
closeDialog() {
this.catelogPath=[]
},
} }
}; };
</script> </script>

@ -10,7 +10,17 @@
</el-form-item> </el-form-item>
</el-form> </el-form>
<el-input
style="width: 400px;margin-bottom: 20px"
clearable
prefix-icon="el-icon-search"
@input="rest"
placeholder="输入关键字进行过滤"
v-model="filterText">
</el-input>
<el-tree <el-tree
:filter-node-method="filterNode"
:data="menus" :data="menus"
:props="defaultProps" :props="defaultProps"
:expand-on-click-node="false" :expand-on-click-node="false"
@ -130,16 +140,27 @@ export default {
productUnit: [ productUnit: [
{ min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' } { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
], ],
} },
filterText: '',
}; };
}, },
// data // data
computed: {}, computed: {},
//data //data
watch: {}, watch: {
filterText(val) {
this.$refs.menuTree.filter(val);
}
},
// //
methods: { methods: {
filterNode(value, data) {
if (!value) return true;
return data.name.indexOf(value) !== -1;
},
// //
selected(name) { selected(name) {
this.category.icon = name; this.category.icon = name;
@ -347,6 +368,13 @@ export default {
}); });
}, },
//
rest() {
if (!this.filterText) {
this.getMenus()
}
},
}, },
// - 访this // - 访this

@ -76,7 +76,7 @@ public class BrandController {
@ApiOperation("修改") @ApiOperation("修改")
@Log(title = "品牌管理", businessType = BusinessType.UPDATE) @Log(title = "品牌管理", businessType = BusinessType.UPDATE)
public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand){ public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand){
brandService.updateById(brand); brandService.updateDetail(brand);
return R.ok(); return R.ok();
} }

@ -1,20 +1,20 @@
package com.xjs.mall.product.controller; package com.xjs.mall.product.controller;
import java.util.Arrays; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import java.util.Map; import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
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 com.xjs.mall.product.entity.CategoryBrandRelationEntity; import com.xjs.mall.product.entity.CategoryBrandRelationEntity;
import com.xjs.mall.product.service.CategoryBrandRelationService; import com.xjs.mall.product.service.CategoryBrandRelationService;
import com.xjs.utils.PageUtils;
import com.xjs.utils.R; import com.xjs.utils.R;
import com.xjs.validation.group.AddGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
/** /**
@ -22,10 +22,11 @@ import com.xjs.utils.R;
* *
* @author xiejs * @author xiejs
* @email 1294405880@qq.com * @email 1294405880@qq.com
* @date 2022-03-15 10:16:53 * @since 2022-03-15 10:16:53
*/ */
@RestController @RestController
@RequestMapping("product/categorybrandrelation") @RequestMapping("product/categorybrandrelation")
@Api(tags = "商城-商品-品牌分类关联")
public class CategoryBrandRelationController { public class CategoryBrandRelationController {
@Autowired @Autowired
private CategoryBrandRelationService categoryBrandRelationService; private CategoryBrandRelationService categoryBrandRelationService;
@ -33,48 +34,38 @@ public class CategoryBrandRelationController {
/** /**
* *
*/ */
@RequestMapping("/list") @GetMapping("/catelog/list")
public R list(@RequestParam Map<String, Object> params){ @ApiOperation("列表")
PageUtils page = categoryBrandRelationService.queryPage(params); public R list(@RequestParam Long brandId) {
return R.ok().put("page", page);
}
LambdaQueryWrapper<CategoryBrandRelationEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CategoryBrandRelationEntity::getBrandId, brandId);
/** List<CategoryBrandRelationEntity> list = categoryBrandRelationService.list(wrapper);
*
*/
@RequestMapping("/info/{id}")
public R info(@PathVariable("id") Long id){
CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
return R.ok().put("categoryBrandRelation", categoryBrandRelation); return R.ok().put("page", list);
} }
/** /**
* *
*/ */
@RequestMapping("/save") @PostMapping("/save")
public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){ @ApiOperation("保存")
categoryBrandRelationService.save(categoryBrandRelation); @Log(title = "品牌分类关联", businessType = BusinessType.INSERT)
public R save(@Validated(AddGroup.class) @RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
categoryBrandRelationService.saveDetail(categoryBrandRelation);
return R.ok(); return R.ok();
} }
/**
*
*/
@RequestMapping("/update")
public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
categoryBrandRelationService.updateById(categoryBrandRelation);
return R.ok();
}
/** /**
* *
*/ */
@RequestMapping("/delete") @DeleteMapping("/delete")
@ApiOperation("删除")
@Log(title = "品牌分类关联", businessType = BusinessType.DELETE)
public R delete(@RequestBody Long[] ids) { public R delete(@RequestBody Long[] ids) {
categoryBrandRelationService.removeByIds(Arrays.asList(ids)); categoryBrandRelationService.removeByIds(Arrays.asList(ids));

@ -74,7 +74,7 @@ public class CategoryController {
@ApiOperation("修改") @ApiOperation("修改")
@Log(title = "商品分类", businessType = BusinessType.UPDATE) @Log(title = "商品分类", businessType = BusinessType.UPDATE)
public R update(@Validated(UpdateGroup.class) @RequestBody CategoryEntity category) { public R update(@Validated(UpdateGroup.class) @RequestBody CategoryEntity category) {
categoryService.updateById(category); categoryService.updateCascade(category);
return R.ok(); return R.ok();
} }

@ -2,10 +2,12 @@ package com.xjs.mall.product.entity;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.xjs.validation.group.AddGroup;
import com.xjs.validation.group.UpdateGroup;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date;
import lombok.Data;
/** /**
* *
@ -27,17 +29,21 @@ public class CategoryBrandRelationEntity implements Serializable {
/** /**
* id * id
*/ */
@NotNull(groups = {AddGroup.class, UpdateGroup.class})
private Long brandId; private Long brandId;
/** /**
* id * id
*/ */
@NotNull(groups = {AddGroup.class, UpdateGroup.class})
private Long catelogId; private Long catelogId;
/** /**
* *
*/ */
private String brandName; private String brandName;
/** /**
* *
*/ */
private String catelogName; private String catelogName;

@ -16,5 +16,11 @@ import java.util.Map;
public interface BrandService extends IService<BrandEntity> { public interface BrandService extends IService<BrandEntity> {
PageUtils queryPage(Map<String, Object> params); PageUtils queryPage(Map<String, Object> params);
/**
*
* @param brand
*/
void updateDetail(BrandEntity brand);
} }

@ -1,20 +1,36 @@
package com.xjs.mall.product.service; package com.xjs.mall.product.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.xjs.utils.PageUtils;
import com.xjs.mall.product.entity.CategoryBrandRelationEntity; import com.xjs.mall.product.entity.CategoryBrandRelationEntity;
import java.util.Map;
/** /**
* *
* *
* @author xiejs * @author xiejs
* @email 1294405880@qq.com * @email 1294405880@qq.com
* @date 2022-03-15 10:16:53 * @since 2022-03-15 10:16:53
*/ */
public interface CategoryBrandRelationService extends IService<CategoryBrandRelationEntity> { public interface CategoryBrandRelationService extends IService<CategoryBrandRelationEntity> {
PageUtils queryPage(Map<String, Object> params);
/**
*
* @param categoryBrandRelation entity
*/
void saveDetail(CategoryBrandRelationEntity categoryBrandRelation);
/**
*
* @param brandId id
* @param name
*/
void updateBrand(Long brandId, String name);
/**
*
* @param catId id
* @param name
*/
void updateCategory(Long catId, String name);
} }

@ -36,5 +36,11 @@ public interface CategoryService extends IService<CategoryEntity> {
* @return long s * @return long s
*/ */
Long[] finCatelogPath(Long catelogId); Long[] finCatelogPath(Long catelogId);
/**
*
* @param category
*/
void updateCascade(CategoryEntity category);
} }

@ -11,11 +11,13 @@ import com.xjs.utils.PageUtils;
import com.xjs.utils.Query; import com.xjs.utils.Query;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Map; import java.util.Map;
@Service("attrGroupService") @Service("attrGroupService")
@Transactional
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService { public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
@Override @Override

@ -6,17 +6,24 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjs.mall.product.dao.BrandDao; import com.xjs.mall.product.dao.BrandDao;
import com.xjs.mall.product.entity.BrandEntity; import com.xjs.mall.product.entity.BrandEntity;
import com.xjs.mall.product.service.BrandService; import com.xjs.mall.product.service.BrandService;
import com.xjs.mall.product.service.CategoryBrandRelationService;
import com.xjs.utils.PageUtils; import com.xjs.utils.PageUtils;
import com.xjs.utils.Query; import com.xjs.utils.Query;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Map; import java.util.Map;
@Service("brandService") @Service("brandService")
@Transactional
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService { public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
@Autowired
private CategoryBrandRelationService categoryBrandRelationService;
@Override @Override
public PageUtils queryPage(Map<String, Object> params) { public PageUtils queryPage(Map<String, Object> params) {
String key = (String) params.get(Query.KEY_NAME); String key = (String) params.get(Query.KEY_NAME);
@ -36,4 +43,17 @@ public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> impleme
return new PageUtils(page); return new PageUtils(page);
} }
@Override
public void updateDetail(BrandEntity brand) {
//保证冗余字段的数据一致性
super.updateById(brand);
if (StringUtils.isNotEmpty(brand.getName())) {
//同步更新其他表
categoryBrandRelationService.updateBrand(brand.getBrandId(), brand.getName());
// todo 更新其他关联信息
}
}
} }

@ -1,29 +1,62 @@
package com.xjs.mall.product.service.impl; package com.xjs.mall.product.service.impl;
import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjs.utils.PageUtils; import com.xjs.mall.product.dao.BrandDao;
import com.xjs.utils.Query;
import com.xjs.mall.product.dao.CategoryBrandRelationDao; import com.xjs.mall.product.dao.CategoryBrandRelationDao;
import com.xjs.mall.product.dao.CategoryDao;
import com.xjs.mall.product.entity.BrandEntity;
import com.xjs.mall.product.entity.CategoryBrandRelationEntity; import com.xjs.mall.product.entity.CategoryBrandRelationEntity;
import com.xjs.mall.product.entity.CategoryEntity;
import com.xjs.mall.product.service.CategoryBrandRelationService; import com.xjs.mall.product.service.CategoryBrandRelationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Service("categoryBrandRelationService") @Service("categoryBrandRelationService")
@Transactional
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService { public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
@Resource
private BrandDao brandDao;
@Resource
private CategoryDao categoryDao;
@Override
public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
Long brandId = categoryBrandRelation.getBrandId();
Long catelogId = categoryBrandRelation.getCatelogId();
BrandEntity brandEntity = brandDao.selectById(brandId);
CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
categoryBrandRelation.setBrandName(brandEntity.getName());
categoryBrandRelation.setCatelogName(categoryEntity.getName());
super.save(categoryBrandRelation);
}
@Override @Override
public PageUtils queryPage(Map<String, Object> params) { public void updateBrand(Long brandId, String name) {
IPage<CategoryBrandRelationEntity> page = this.page( CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
new Query<CategoryBrandRelationEntity>().getPage(params), entity.setBrandId(brandId);
new QueryWrapper<CategoryBrandRelationEntity>() entity.setBrandName(name);
);
return new PageUtils(page); super.update(entity, new LambdaUpdateWrapper<CategoryBrandRelationEntity>()
.eq(CategoryBrandRelationEntity::getBrandId, brandId));
} }
@Override
public void updateCategory(Long catId, String name) {
CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
entity.setCatelogId(catId);
entity.setCatelogName(name);
super.update(entity, new LambdaUpdateWrapper<CategoryBrandRelationEntity>()
.eq(CategoryBrandRelationEntity::getCatelogId, catId));
}
} }

@ -5,10 +5,13 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjs.mall.product.dao.CategoryDao; import com.xjs.mall.product.dao.CategoryDao;
import com.xjs.mall.product.entity.CategoryEntity; import com.xjs.mall.product.entity.CategoryEntity;
import com.xjs.mall.product.service.CategoryBrandRelationService;
import com.xjs.mall.product.service.CategoryService; import com.xjs.mall.product.service.CategoryService;
import com.xjs.utils.PageUtils; import com.xjs.utils.PageUtils;
import com.xjs.utils.Query; import com.xjs.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.*; import java.util.*;
@ -16,11 +19,15 @@ import java.util.stream.Collectors;
@Service("categoryService") @Service("categoryService")
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService { public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
@Resource @Resource
private CategoryDao categoryDao; private CategoryDao categoryDao;
@Autowired
private CategoryBrandRelationService categoryBrandRelationService;
@Override @Override
public PageUtils queryPage(Map<String, Object> params) { public PageUtils queryPage(Map<String, Object> params) {
IPage<CategoryEntity> page = this.page( IPage<CategoryEntity> page = this.page(
@ -63,6 +70,15 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity
return parentPath.toArray(new Long[parentPath.size()]); return parentPath.toArray(new Long[parentPath.size()]);
} }
@Override
public void updateCascade(CategoryEntity category) {
//更新自己
super.updateById(category);
//更新关联表
categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
}
//225,25,2 //225,25,2
private List<Long> findParentPath(Long catelogId, List<Long> paths) { private List<Long> findParentPath(Long catelogId, List<Long> paths) {
//1、收集当前节点id //1、收集当前节点id

Loading…
Cancel
Save