//查询分组关联的属性
@GetMapping("/{attrgroupId}/attr/relation")
public R attrRelation(@PathVariable("attrgroupId")Long attrgroupId){
List<AttrEntity> attrEntity=attrService.getRelationAttr(attrgroupId);
return R.ok().put("data", attrEntity);
}
//查询分组id查找关联的所有属性
@Override
public List<AttrEntity> getRelationAttr(Long attrgroupId) {
List<AttrAttrgroupRelationEntity> entities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
List<Long> attrIds = entities.stream()
.map(item->item.getAttrId())
.collect(Collectors.toList());
List<AttrEntity>attrEntityList=null;
if(!CollectionUtils.isEmpty(attrIds)){
attrEntityList=this.listByIds(attrIds);
}
// ListattrEntityList=null;
// if(!CollectionUtils.isEmpty(attrIds)){
// attrEntityList=attrDao.findAttrEntityByAttrIds(attrIds);
// }
return attrEntityList;
}
//删除属性与分组的关联关系
@PostMapping("/attr/relation/delete")
public R deleteRelation(@RequestBody AttrGroupRelationVo[]vos){
relationService.deleteRelation(vos);
return R.ok();
}
@Override
public void deleteRelation(AttrGroupRelationVo[] vos) {
List<AttrAttrgroupRelationEntity> entities = Arrays.asList(vos).stream().map((item) -> {
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(item, relationEntity);
return relationEntity;
}).collect(Collectors.toList());
this.baseMapper.deleteBatchRelation(entities);
}
@Repository
@Mapper
public interface AttrAttrgroupRelationDao extends BaseMapper<AttrAttrgroupRelationEntity> {
void deleteBatchRelation(@Param("entities") List<AttrAttrgroupRelationEntity> entities);
}
<delete id="deleteBatchRelation">
delete from pms_attr_attrgroup_relation where
<foreach collection="entities" item="item" separator=" or ">
(attr_id=#{item.attrId} and attr_group_id=#{item.attrGroupId})
</foreach>
</delete>
//响应数据
{
"msg": "success",
"code": 0,
"page": {
"totalCount": 3,
"pageSize": 10,
"totalPage": 1,
"currPage": 1,
"list": [{
"attrId": 1,
"attrName": "aaa",
"searchType": 1,
"valueType": 1,
"icon": "aa",
"valueSelect": "aa;ddd;sss;aaa2",
"attrType": 1,
"enable": 1,
"catelogId": 225,
"showDesc": 1
}]
}
}
//获取属性分组没有关联的其他属性
@GetMapping("/{attrgroupId}/noattr/relation")
public R attrNoattrRelation(@RequestParam Map<String,Object>params,
@PathVariable("attrgroupId")Long attrgroupId){
PageUtils page= attrService.getNoattrRelationAttr(params,attrgroupId);
return R.ok().put("page",page);
}
/**
* 获取属性分组没有关联的其他属性
* @param params
* @param attrgroupId
* @return
*/
@Override
public PageUtils getNoattrRelationAttr(Map<String, Object> params, Long attrgroupId) {
//1. 当前分组只能关联自己所属的分类里面的所有属性
AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId);
Long catelogId = attrGroupEntity.getCatelogId();
//2. 当前分组只能关联别的分组没有引用的属性
//2.1 当前分类下的其他分组
//List group = attrGroupDao.selectList(new QueryWrapper().eq("catelog_id", catelogId).ne("attr_group_id", attrgroupId));
List<AttrGroupEntity> group = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
List<Long> collect = group.stream().map(item -> item.getAttrGroupId()).collect(Collectors.toList());
//2.2 这些分组关联的属性
List<AttrAttrgroupRelationEntity> groupId = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", collect));
List<Long> attrIds = groupId.stream().map(item -> item.getAttrId()).collect(Collectors.toList());
//2.3 从当前分类的所有属性中移除这些属性
QueryWrapper<AttrEntity> wrapper = new QueryWrapper<AttrEntity>().eq("catelog_id", catelogId);
if(!CollectionUtils.isEmpty(attrIds)){
wrapper.notIn("attr_id", attrIds);
}
String key=(String) params.get("key");
if(!StringUtils.isEmpty(key)){
wrapper.and(w->w.eq("attr_id",key).or().like("attr_name",key));
}
wrapper.eq("attr_type",ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params),wrapper);
return new PageUtils(page);
}
@Data
public class AttrGroupRelationVo {
private Long attrId;
private Long attrGroupId;
}
//添加属性与分组关联关系
@PostMapping("/attr/relation")
//public R addRelation(@RequestBody Listvos){
public R addRelation(@RequestBody AttrGroupRelationVo[] vos){
relationService.saveAttrRelation(vos);
return R.ok();
}
/**
* 添加属性与分组关联关系
* @param vos
*/
@Override
public void saveAttrRelation(AttrGroupRelationVo[] vos) {
List<AttrAttrgroupRelationEntity> entities = Arrays.asList(vos).stream().map(item -> {
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
BeanUtils.copyProperties(item, relationEntity);
return relationEntity;
}).collect(Collectors.toList());
this.saveBatch(entities);
}
(1). 安装 pubsub-js:
npm install --save pubsub-js
(2). 在 main.js 中引入
//导入
import PubSub from 'pubsub-js'
//挂载全局
Vue.prototype.PubSub = PubSu
②. 我们在新增商品的时候,会出现会员接口404,这个时候需要调整会员接口
③. 配置会员服务的网关地址、配置注册中心地址
spring:
cloud:
gateway:
routes:
# 会员服务
- id: member_route
uri: lb://gulimall-member
predicates:
- Path=/api/member/**
filters:
- RewritePath=/api/(?>/?.*), /$\{segment}
// application.properties
spring.cloud.nacos.config.server-addr=127.0.0.1
spring.cloud.nacos.config.namespace=f8608f8a-635c-4883-9613-996910120e57
spring.cloud.nacos.config.file-extension=yml
spring.application.name=gulimall-member
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.56.11:3306/gulimall_ums?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
username: root
password: root
application:
name: gulimall-member
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
mybatis-plus:
# classpath*:/mapper/**/*.xml 表示不止扫描自己类路径下的mapper,其他引入的mapper也会进行扫描
# classpath*:/mapper/**/*.xml 只扫描自己的路径下
mapper-locations: classpath:/mapper/**/*.xml
# 如下代码是mybatisplus中配置自增主键
global-config:
db-config:
id-type: auto
server:
port: 8000
@Override
public PageUtils queryPage(Map<String, Object> params) {
QueryWrapper<MemberLevelEntity> wrapper = new QueryWrapper<>();
String key=(String) params.get("key");
if(!StringUtils.isEmpty(key)){
wrapper.and(w->w.eq("id",key).or().like("name",key));
}
IPage<MemberLevelEntity> page = this.page(
new Query<MemberLevelEntity>().getPage(params),
wrapper
);
return new PageUtils(page);
}
①. 根据后台接口,进行分析:/product/categorybrandrelation/brands/list
②. 核心代码如下
/**
* 获取分类关联的品牌
* 1.controller只来处理请求,接收和校验数据
* 2.service接收controller数据,进行业务处理
* 3.controller接收service处理完的数据,封装成页面指定的vo
* @param catId
* @return
*/
@GetMapping("/brands/list")
public R relationBrandsList(@RequestParam(value = "catId",required = true)Long catId){
List<BrandEntity> brandEntities=categoryBrandRelationService.getBrandsByCatId(catId);
List<BrandVo> data = brandEntities.stream().map(item -> {
//这里属性没有对应,需要对拷贝
BrandVo brandVo = new BrandVo();
brandVo.setBrandName(item.getName());
brandVo.setBrandId(item.getBrandId());
return brandVo;
}).collect(Collectors.toList());
return R.ok().put("data",data);
}
@Override
public List<BrandEntity> getBrandsByCatId(Long catId) {
List<CategoryBrandRelationEntity> catelogId = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
List<BrandEntity> brandEntities = catelogId.stream().map(item -> {
Long brandId = item.getBrandId();
BrandEntity byId = brandService.getById(brandId);
return byId;
}).collect(Collectors.toList());
return brandEntities;
}
@Data
public class AttrGroupWithAttrsVo {
//分组id
private Long attrGroupId;
//组名
private String attrGroupName;
//排序
private Integer sort;
//描述
private String descript;
//组图标
private String icon;
//所属分类id
private Long catelogId;
private Integer valueType;
private List<AttrEntity> attrs;
}
//获取分类下所有分组&关联属性
@GetMapping("/{catelogId}/withattr")
public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
//1. 查出当前分类下的所有属性分组
//2. 查出每个属性分组的所有属性
List<AttrGroupWithAttrsVo>vos=attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
return R.ok().put("data",vos);
}
/**
* 根据分类Id查出所有的分组,以及这些组里面的属性
* 获取分类下所有分组&关联属性
* @param catelogId
* @return
*/
@Override
public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
//1. 查询分组信息
List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
//2. 查询所有属性
List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(item -> {
AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
BeanUtils.copyProperties(item, attrGroupWithAttrsVo);
List<AttrEntity> relationAttr = attrService.getRelationAttr(item.getAttrGroupId());
attrGroupWithAttrsVo.setAttrs(relationAttr);
return attrGroupWithAttrsVo;
}).collect(Collectors.toList());
return collect;
}