第二十章第七节:品牌管理条件查询以及关联分类功能(含修改功能)
1、品牌条件查询
查询数据请求:/product/brand/list
请求参数:
{
page: 1,//当前页码
limit: 10,//每页记录数
sidx: 'id',//排序字段
order: 'asc/desc',//排序方式
key: '华为'//检索关键字
}
响应数据示例:
{
"msg": "success",
"code": 0,
"page": {
"totalCount": 0,
"pageSize": 10,
"totalPage": 0,
"currPage": 1,
"list": [{
"brandId": 1,
"name": "小米0",
"logo": "https://onlinemall2021.oss-cn-beijing.aliyuncs.com/2021-06-09/0a7459c8-bba8-453e-8c87-002e7cd91565_4.png",
"descript": "小米",
"showStatus": 0,
"firstLetter": "m",
"sort": 1
}, {
"brandId": 21,
"name": "oppo",
"logo": "https://onlinemall2021.oss-cn-beijing.aliyuncs.com/2021-06-15/ef4cb417-aa70-4f2e-80eb-6bcf17861c6f_11a.png",
"descript": "oppo",
"showStatus": 1,
"firstLetter": "b",
"sort": 1
}]
}
}
操作数据库表:pms_brand
com.applesnt.onlinemall.product.service.impl.BrandServiceImpl
修改PageUtils查询方法
package com.applesnt.onlinemall.product.service.impl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
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.applesnt.common.utils.PageUtils;
import com.applesnt.common.utils.Query;
import com.applesnt.onlinemall.product.dao.BrandDao;
import com.applesnt.onlinemall.product.entity.BrandEntity;
import com.applesnt.onlinemall.product.service.BrandService;
@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
/*从前端传递过来的map中得到key*/
String key = (String) params.get("key");
/*声明一个查询条件*/
QueryWrapper<BrandEntity> wrapper = new QueryWrapper<BrandEntity>();
/*如key不为空*/
if (!StringUtils.isEmpty(key)) {
/*查询条件进行拼接,即品牌id等于key或者品牌名称等于key*/
wrapper.eq("brand_id", key).or().like("name", key);
}
IPage<BrandEntity> page = this.page(
new Query<BrandEntity>().getPage(params), wrapper);
return new PageUtils(page);
}
}
2、品牌关联分类查询功能
品牌关联分类查询请求:product/categorybrandrelation/catelog/list
请求参数:
{
brandId: 1
}
响应数据示例:
{
"msg": "success",
"code": 0,
"data": [{
"id": 11,
"brandId": 1,
"catelogId": 225,
"brandName": "小米",
"catelogName": "手机"
}, {
"id": 12,
"brandId": 1,
"catelogId": 231,
"brandName": "小米",
"catelogName": "移动电源"
}]
}
操作数据库表:pms_category_brand_relation
com.applesnt.onlinemall.product.controller.CategoryBrandRelationController
/**
* 获取当前品牌关联的分类
*/
@GetMapping("/catelog/list")
public R catelogList(@RequestParam Map<String, Object> params){
/*获取前端传递的品牌id*/
String brandId = (String) params.get("brandId");
log.info("请求的id为:"+brandId);
/*声明一个查询条件*/
QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<CategoryBrandRelationEntity>();
/*根据条件查询关联列表*/
List<CategoryBrandRelationEntity> relationEntityList= categoryBrandRelationService.list(
wrapper.eq("brand_id",brandId));
return R.ok().put("data", relationEntityList);
}
3、品牌关联分类添加功能
品牌关联分类添加请求:product/categorybrandrelation/save
请求参数:
{
brandId: 1,
catelogId: 233
}
响应数据示例:
{
"msg": "success",
"code": 0,
"data": [{
"id": 11,
"brandId": 1,
"catelogId": 225,
"brandName": "小米",
"catelogName": "手机"
}, {
"id": 12,
"brandId": 1,
"catelogId": 231,
"brandName": "小米",
"catelogName": "移动电源"
}, {
"id": 13,
"brandId": 1,
"catelogId": 233,
"brandName": "小米",
"catelogName": "蓝牙耳机"
}]
}
操作数据库表:
pms_category(根据catelogId获取brandName)
pms_brand(根据brandId获取catelogName)
pms_category_brand_relation(保存brandId,brandName,catelogId,catelogName)
修改原来的保存方法,还要把品牌名称和分类名称一并保存到数据库
查询品牌管理分类列表请求:/product/categorybrandrelation/save
1》编写添加功能接口
com.applesnt.onlinemall.product.service.CategoryBrandRelationService
void saveDetail(CategoryBrandRelationEntity categoryBrandRelation);
2》编写添加功能接口实现
com.applesnt.onlinemall.product.service.impl.CategoryBrandRelationServiceImpl
/*注入品牌dao层*/
@Autowired
BrandDao brandDao;
/*注入分类dao层*/
@Autowired
CategoryDao categoryDao;
@Override
public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
/*获取品牌id*/
Long brandId = categoryBrandRelation.getBrandId();
/*获取分类id*/
Long catelogId = categoryBrandRelation.getCatelogId();
/*根据品牌id查询出当前品牌对象*/
BrandEntity brandEntity= brandDao.selectById(brandId);
/*根据分类id 查询出当前分类对象*/
CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
/*把当前品牌对象的名字set到关联表中*/
categoryBrandRelation.setBrandName(brandEntity.getName());
/*把当前分类的名字set到关联表中*/
categoryBrandRelation.setCatelogName(categoryEntity.getName());
this.baseMapper.insert(categoryBrandRelation);
}
3》编写controller调用
@RequestMapping("/save")
public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
categoryBrandRelationService.saveDetail(categoryBrandRelation);
return R.ok();
}
4、修改品牌信息时,同步修改品牌分类关联表中的品牌名称
修改品牌查询请求:/product/brand/info/{brandId}
请求参数:
品牌ID: brandId
响应数据示例:
{
"msg": "success",
"code": 0,
"brand": {
"brandId": 1,
"name": "小米",
"logo": "https://onlinemall2021.oss-cn-beijing.aliyuncs.com/2021-06-09/0a7459c8-bba8-453e-8c87-002e7cd91565_4.png",
"descript": "小米",
"showStatus": 0,
"firstLetter": "m",
"sort": 1
}
}
操作数据库表:
pms_brand
修改品牌请求:/product/brand/update
请求参数:
{
brandId: 1,
descript: "小米",
firstLetter: "m",
logo: "https://onlinemall2021.oss-cn-beijing.aliyuncs.com/2021-06-09/0a7459c8-bba8-453e-8c87-002e7cd91565_4.png",
name: "小米",
showStatus: 0,
sort: 1,
}
响应数据示例:
{
"msg": "success",
"code": 0
}
操作数据库表:
pms_brand
pms_category_brand_relation(同步修改关联表中的品牌名称)
由于在品牌分类关联表中存放了品牌id和品牌名称,一旦品牌数据发送修改,品牌名称也要在品牌分类关联表中修改
1>定义修改品牌信息的接口
com.applesnt.onlinemall.product.service.BrandService
/*修改品牌信息*/
void updateDetail(BrandEntity brand);
2>定义修改品牌信息的接口实现
com.applesnt.onlinemall.product.service.impl.BrandServiceImpl
/*注入品牌分类关联表的服务*/
@Autowired
CategoryBrandRelationService categoryBrandRelationService;
/*修改品牌信息*/
@Override
public void updateDetail(BrandEntity brand) {
/*更新自己表中的数据*/
this.baseMapper.updateById(brand);
if(!StringUtils.isEmpty(brand.getName())){
/*同步更新其他表中的关联数据*/
categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
}
}
3>定义同步修改品牌分类关联表接口
com.applesnt.onlinemall.product.service.CategoryBrandRelationService
void updateBrand(Long brandId, String name);
4>定义同步修改品牌分类关联表接口实现
/*当中品牌数据修改时,同步修改品牌分类关联表的品牌名称*/
@Override
public void updateBrand(Long brandId, String name) {
CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
relationEntity.setBrandId(brandId);
relationEntity.setBrandName(name);
/*根据品牌id修改品牌名称 UpdateWrapper*/
this.baseMapper.update(relationEntity,
new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
}
5>修改controller调用
com.applesnt.onlinemall.product.controller.BrandController
/**
* 品牌信息修改
* @Validated:实体类开启校验,并且指定用UpdateGroup分组
*/
@RequestMapping("/update")
public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
// brandService.updateById(brand);
brandService.updateDetail(brand);
return R.ok();
}
5、修改分类信息时,同步修改品牌分类关联表中的分类名称
修改分类查询请求:/product/category/info/{catId}
请求参数:
分类ID: catId
响应数据示例:
{
"msg": "success",
"code": 0,
"data": {
"catId": 1,
"name": "图书、音像、电子书刊",
"parentCid": 0,
"catLevel": 1,
"showStatus": 1,
"sort": 0,
"icon": null,
"productUnit": null,
"productCount": 0
}
}
操作数据库表:
pms_category
修改分类请求:/product/brand/update
请求参数:
{
catId: 1,
icon: "xx",
name: "图书、音像、电子书刊",
productUnit: "件"
}
响应数据示例:
{
"msg": "success",
"code": 0
}
操作数据库表:
pms_category
pms_category_brand_relation(同步修改关联表中的分类名称)
由于在品牌分类关联表中存放了分类id和分类名称,一旦分类数据发送修改,分类名称也要在品牌分类关联表中修改
1>定义修改分类信息的接口
com.applesnt.onlinemall.product.service.CategoryService
/*修改分类信息,同时要修改关联数据*/
void updateCascade(CategoryEntity category);
2>定义修改分类信息的接口实现
com.applesnt.onlinemall.product.service.impl.CategoryServiceImpl
/*注入品牌分类关联表的服务*/
@Autowired
CategoryBrandRelationService relationService;
/*修改分类信息,同时要修改关联数据*/
@Override
public void updateCascade(CategoryEntity category) {
this.baseMapper.updateById(category);
this.relationService.updateCategory(category.getCatId(),category.getName());
}
3>定义同步修改品牌分类关联表接口
com.applesnt.onlinemall.product.service.CategoryBrandRelationService
void updateCategory(Long catId, String name);
4>定义同步修改品牌分类关联表接口实现
@Override
public void updateCategory(Long catId, String name) {
CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
relationEntity.setCatelogId(catId);
relationEntity.setCatelogName(name);
this.baseMapper.update(relationEntity,
new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));
}
5>修改controller调用
com.applesnt.onlinemall.product.controller.CategoryController
/**
* 修改分类信息的同时,也修改相品牌分类关联表的信息
*/
@RequestMapping("/update")
public R update(@RequestBody CategoryEntity category){
categoryService.updateCascade(category);
return R.ok();
}

浙公网安备 33010602011771号