规格参数新增与VO
属性分组的查询时设置模糊查询功能
gulimall.product.service.impl
AttrGroupServiceImpl.java
package com.doudou.gulimall.product.service.impl;
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.doudou.common.utils.PageUtils;
import com.doudou.common.utils.Query;
import com.doudou.gulimall.product.dao.AttrGroupDao;
import com.doudou.gulimall.product.entity.AttrGroupEntity;
import com.doudou.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<AttrGroupEntity> page = this.page(
new Query<AttrGroupEntity>().getPage(params),
new QueryWrapper<AttrGroupEntity>()
);
return new PageUtils(page);
}
@Override
public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
String key = (String) params.get("key");
QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
if(!StringUtils.isEmpty(key)){
wrapper.and((obj)->{
obj.eq("attr_group_id",key).or().like("attr_group_name",key);
});
}
if(catelogId == 0){
IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
return new PageUtils(page);
}else{
wrapper.eq("catelog_id",catelogId);
IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
return new PageUtils(page);
}
}
}
doudou.gulimall.product.vo
AttrVo.java
package com.doudou.gulimall.product.vo;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class AttrVo {
@TableId
private Long attrId;
private String attrName;
private Integer searchType;
private String icon;
private String valueSelect;
private Integer attrType;
private Long enable;
private Long catelogId;
private Integer showDesc;
private Long attrGroupId;
}
gulimall.product.service.impl
AttrServiceImpl.java
package com.doudou.gulimall.product.service.impl;
import com.doudou.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.doudou.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.doudou.gulimall.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.doudou.common.utils.PageUtils;
import com.doudou.common.utils.Query;
import com.doudou.gulimall.product.dao.AttrDao;
import com.doudou.gulimall.product.entity.AttrEntity;
import com.doudou.gulimall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
@Autowired
AttrAttrgroupRelationDao relationDao;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
new QueryWrapper<AttrEntity>()
);
return new PageUtils(page);
}
@Transactional
@Override
public void saveAttr(AttrVo attr) {
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
this.save(attrEntity);
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationEntity.setAttrId(attrEntity.getAttrId());
relationDao.insert(relationEntity);
}
}
规格参数列表
package com.doudou.gulimall.product.controller;
import java.util.Arrays;
import java.util.Map;
import com.doudou.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.doudou.gulimall.product.entity.AttrEntity;
import com.doudou.gulimall.product.service.AttrService;
import com.doudou.common.utils.PageUtils;
import com.doudou.common.utils.R;
@RestController
@RequestMapping("product/attr")
public class AttrController {
@Autowired
private AttrService attrService;
@GetMapping("/base/list/{catelogId}")
public R baseAttrList(@RequestParam Map<String, Object> params,
@PathVariable("catelogId") Long catelogId){
PageUtils page = attrService.queryBaseAttrPage(params,catelogId);
return R.ok().put("page", page);
}
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = attrService.queryPage(params);
return R.ok().put("page", page);
}
@RequestMapping("/info/{attrId}")
public R info(@PathVariable("attrId") Long attrId){
AttrEntity attr = attrService.getById(attrId);
return R.ok().put("attr", attr);
}
@RequestMapping("/save")
public R save(@RequestBody AttrVo attr){
attrService.saveAttr(attr);
return R.ok();
}
@RequestMapping("/update")
public R update(@RequestBody AttrEntity attr){
attrService.updateById(attr);
return R.ok();
}
@RequestMapping("/delete")
public R delete(@RequestBody Long[] attrIds){
attrService.removeByIds(Arrays.asList(attrIds));
return R.ok();
}
}
gulimall.product.service.impl
AttrServiceImpl.java
package com.doudou.gulimall.product.service.impl;
import com.doudou.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.doudou.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.doudou.gulimall.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.doudou.common.utils.PageUtils;
import com.doudou.common.utils.Query;
import com.doudou.gulimall.product.dao.AttrDao;
import com.doudou.gulimall.product.entity.AttrEntity;
import com.doudou.gulimall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
@Autowired
AttrAttrgroupRelationDao relationDao;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
new QueryWrapper<AttrEntity>()
);
return new PageUtils(page);
}
@Transactional
@Override
public void saveAttr(AttrVo attr) {
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
this.save(attrEntity);
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationEntity.setAttrId(attrEntity.getAttrId());
relationDao.insert(relationEntity);
}
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
if (catelogId != 0){
queryWrapper.eq("catelog_id",catelogId);
}
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)){
queryWrapper.and((wrapper)->{
wrapper.eq("attr_id",key).or().like("attr_name",key);
});
}
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
queryWrapper
);
return new PageUtils(page);
}
}
查询所属分类与分组
doudou.gulimall.product.vo
AttrRespVo.java
package com.doudou.gulimall.product.vo;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class AttrRespVo {
@TableId
private Long attrId;
private String attrName;
private Integer searchType;
private String icon;
private String valueSelect;
private Integer attrType;
private Long enable;
private Long catelogId;
private Integer showDesc;
private Long attrGroupId;
private String catelogName;
private String groupName;
}
gulimall.product.service.impl
AttrServiceImpl .java
package com.doudou.gulimall.product.service.impl;
import com.doudou.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.doudou.gulimall.product.dao.AttrGroupDao;
import com.doudou.gulimall.product.dao.CategoryDao;
import com.doudou.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.doudou.gulimall.product.entity.AttrGroupEntity;
import com.doudou.gulimall.product.entity.CategoryEntity;
import com.doudou.gulimall.product.vo.AttrRespVo;
import com.doudou.gulimall.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
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.doudou.common.utils.PageUtils;
import com.doudou.common.utils.Query;
import com.doudou.gulimall.product.dao.AttrDao;
import com.doudou.gulimall.product.entity.AttrEntity;
import com.doudou.gulimall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
@Autowired
AttrAttrgroupRelationDao relationDao;
@Autowired
AttrGroupDao attrGroupDao;
@Autowired
CategoryDao categoryDao;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
new QueryWrapper<AttrEntity>()
);
return new PageUtils(page);
}
@Transactional
@Override
public void saveAttr(AttrVo attr) {
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
this.save(attrEntity);
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationEntity.setAttrId(attrEntity.getAttrId());
relationDao.insert(relationEntity);
}
@Override
public PageUtils queryBaseAttrPage(Map<String, Object> params, Long catelogId) {
QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
if (catelogId != 0){
queryWrapper.eq("catelog_id",catelogId);
}
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)){
queryWrapper.and((wrapper)->{
wrapper.eq("attr_id",key).or().like("attr_name",key);
});
}
IPage<AttrEntity> page = this.page(
new Query<AttrEntity>().getPage(params),
queryWrapper
);
PageUtils pageUtils = new PageUtils(page);
List<AttrEntity> records = page.getRecords();
List<AttrRespVo> respVos = records.stream().map((attrEntity) -> {
AttrRespVo attrRespVo = new AttrRespVo();
BeanUtils.copyProperties(attrEntity, attrRespVo);
AttrAttrgroupRelationEntity attrId = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
if (attrId != null) {
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrId.getAttrGroupId());
attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
}
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if (categoryEntity != null) {
attrRespVo.setCatelogName(categoryEntity.getName());
}
return attrRespVo;
}).collect(Collectors.toList());
pageUtils.setList(respVos);
return pageUtils;
}
}
B站学习网址:全网最强电商教程《谷粒商城》对标阿里P6/P7,40-60万年薪_哔哩哔哩_bilibili
|