第二十一章第四节:规格参数修改功能
规格参数修改数据回显请求:/product/attr/info/{attrId}
请求参数:
规格参数ID:attrId
响应数据示例:
{
"msg": "success",
"code": 0,
"attr": {
"attrId": 7,
"attrName": "电源",
"searchType": 0,
"icon": "xx",
"valueSelect": "外置电源",
"attrType": 1,
"enable": 1,
"catelogId": 225,
"showDesc": 0,
"attrGroupId": 7,
"catelogName": "手机",
"groupName": "主体",
"catelogPath": [2, 34, 225]
}
}
操作数据库表:
pms_attr(基本属性)
pms_attr_attrgroup_relation(通过attrid得到分组id)
pms_attr_group(分组信息)
pms_category(分类信息)
1、在响应vo中定义要回显的分类路径集合变量
com.applesnt.onlinemall.product.vo.AttrRespVo
/*修改规格参数时回显分类*/
private Long[] catelogPath;
2、定义修改规则参数回显数据的接口
com.applesnt.onlinemall.product.service.AttrService
AttrRespVo getAttrInfo(Long attrId);
3、定义修改规则参数回显数据的接口实现
com.applesnt.onlinemall.product.service.impl.AttrServiceImpl
/*注入关联关系dao*/
@Autowired
AttrAttrgroupRelationDao relationDao;
/*注入属性分组dao*/
@Autowired
AttrGroupDao attrGroupDao;
/*注入分类dao*/
@Autowired
CategoryDao categoryDao;
/*注入分类service 调用findCatelogPath 返回完整路径*/
@Autowired
CategoryService categoryService;
@Override
public AttrRespVo getAttrInfo(Long attrId) {
AttrRespVo respVo = new AttrRespVo();
/*根据attrId查询规格参数信息*/
AttrEntity attrEntity = this.baseMapper.selectById(attrId);
/*把基本信息拷贝到响应vo中*/
BeanUtils.copyProperties(attrEntity,respVo);
/*设置分组信息*/
/*根据attrid查询规格参数及属性分组关联表*/
AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attrId));
if(relationEntity!=null){
respVo.setAttrGroupId(relationEntity.getAttrGroupId());
/*根据关联表中的属性分组id查询属性分组信息*/
AttrGroupEntity groupEntity = attrGroupDao.selectById(relationEntity.getAttrGroupId());
if(groupEntity!=null){
respVo.setGroupName(groupEntity.getAttrGroupName());
}
}
/*设置分类信息*/
Long[] categoryPath = this.categoryService.findCatelogPath(attrEntity.getCatelogId());
respVo.setCatelogPath(categoryPath);
CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
if(categoryEntity!=null){
respVo.setCatelogName(categoryEntity.getName());
}
return respVo;
}
4、controller调用
com.applesnt.onlinemall.product.controller.AttrController
/**
* 回显信息
*/
@RequestMapping("/info/{attrId}")
public R info(@PathVariable("attrId") Long attrId){
AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
return R.ok().put("attr", attrRespVo);
}
规格参数修改请求:/product/attr/update
请求参数:
{
attrGroupId: 8,
attrId: 7,
attrName: "电源",
attrType: 1,
catelogId: 225,
enable: 1,
icon: "xx",
searchType: 1,
showDesc: 1,
t: 1623914114385,
valueSelect: "外置电源",
valueType: 0
}
响应数据示例:
{
"msg": "success",
"code": 0
}
操作数据库表:
pms_attr(基本属性)
pms_attr_attrgroup_relation(关联表)
5、定义修改规则参数接口
com.applesnt.onlinemall.product.service.AttrService
void updateAttr(AttrVo attr);
6、定义修改规则参数接口实现
com.applesnt.onlinemall.product.service.impl.AttrServiceImpl
/*注入关联关系dao*/
@Autowired
AttrAttrgroupRelationDao relationDao;
@Transactional/*事务管理*/
@Override
public void updateAttr(AttrVo attr) {
/*规格参数基本信息修改*/
AttrEntity attrEntity = new AttrEntity();
BeanUtils.copyProperties(attr,attrEntity);
this.baseMapper.updateById(attrEntity);
/*
*销售属性不需要修改分组信息,
* arrttype=1是规格属性
* attrtype=0是销售属性
*/
if(attrEntity.getAttrType()==ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
/*修改分组关联*/
AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
relationEntity.setAttrGroupId(attr.getAttrGroupId());
relationEntity.setAttrId(attr.getAttrId());
/*根据attr_id 查询是否有关联关系*/
Integer count = this.relationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
if(count >0){
//如果有就修改
relationDao.update(relationEntity,new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attr.getAttrId()));
}else {
//如果没有就保存一个
relationDao.insert(relationEntity);
}
}
}
7、controller调用
com.applesnt.onlinemall.product.controller.AttrController
/**
* 修改保存
*/
@RequestMapping("/update")
public R update(@RequestBody AttrVo attr){
attrService.updateAttr(attr);
return R.ok();
}

浙公网安备 33010602011771号