document
API test

更新属性

POST
http://localhost:88/api/product/attr/update

API description

更新规格参数或销售属性

Request Parameters

parameter
type
description
required
attrGroupId
long
示例:1
optional
attrId
long
示例:7
required
attrName
string
示例:入网型号
required
attrType
int
示例:1
required
catelogId
int
示例:225
required
enable
int
示例:1
required
icon
string
示例:xxx
required
searchType
int
示例:0
required
showDesc
int
示例:0
required
valueSelect
string
示例:A2217;C3J;以官网信息为准
required
valueType
int
示例:0
required

Response Parameters

parameter
type
description
required
msg
string
示例:success
required
code
int
示例:0
required

Description or Example

# BUG修复 ## 更新规格参数问题 > 如果规格参数和属性分组绑定的时候, 更新分类破坏了同一个分类的前提, 因此需要判断, 不让他更新 # 核心代码 ```java @RequestMapping("/update") // @RequiresPermissions("product:attr:update") public R update(@RequestBody AttrVO attrVO){ attrService.updateDetailById(attrVO); return R.ok(); } ``` ```java @Override @Transactional public void updateDetailById(AttrVO attr) { // 规格参数的前提下, 或者是规格参数的同时又是销售属性 if (attr.getAttrType() == ProductConstant.Attr.ATTR_BASE.getCode() || attr.getAttrType() == ProductConstant.Attr.ATTR_SALE$BASE.getCode()) { Long attrId = attr.getAttrId(); // 获取属性ID AttrEntity attrEntity = this.getById(attrId); if (!Objects.equals(attrEntity.getCatelogId(), attr.getCatelogId()) && relationService.groupIfExists(attrId)) { throw new IllegalArgumentException("不能够修改拥有关联关系的规格参数"); } } // 把属性自己给更新了 this.updateById(attr); // 只有规格参数才能改关联信息既是也是也可以 if ((ProductConstant.Attr.ATTR_BASE.getCode() == attr.getAttrType() || ProductConstant.Attr.ATTR_SALE$BASE.getCode() == attr.getAttrType()) && attr.getAttrGroupId() != null) { // 查询关联表 if (!relationService.groupIfExists(attr.getAttrId())) // 不存在对应的关系 relationService.addAttrGroup(attr.getAttrId(), attr.getAttrGroupId()); // 更新关联表 else relationService.updateAttrGroupByAttr(attr.getAttrId(), attr.getAttrGroupId()); } } ``` ```java @Override public void updateAttrGroupByAttr(Long attrId, Long attrGroupId) { LambdaUpdateWrapper<AttrAttrgroupRelationEntity> wrapper = new LambdaUpdateWrapper<>(); wrapper.eq(AttrAttrgroupRelationEntity::getAttrId, attrId); AttrAttrgroupRelationEntity relation = new AttrAttrgroupRelationEntity().setAttrId(attrId).setAttrGroupId(attrGroupId); this.update(relation, wrapper); } @Override public boolean groupIfExists(Long attrId) { LambdaQueryWrapper<AttrAttrgroupRelationEntity> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(AttrAttrgroupRelationEntity::getAttrId, attrId); return this.getOne(wrapper) != null; } @Override public void addAttrGroup(Long attrId, Long attrGroupId) { this.save(new AttrAttrgroupRelationEntity().setAttrId(attrId).setAttrGroupId(attrGroupId)); } ``` # 扩展知识 ## 更新属性的时候为什么需要判断关系是否存在? > 因为规格参数或销售属性可能在添加的时候没有绑定和属性分组的关系, 因此, 关系表中可能不会存在对应的关系 > 因此, 如果更新一个不存在的关系是无效的, 因此需要判断, 条件更新, 如果没有就添加, 有就更新