mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-25 08:25:07 +08:00
product:优化商品属性值的代码
This commit is contained in:
@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.Produc
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValuePageReqVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValueRespVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValueUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.product.convert.propertyvalue.ProductPropertyValueConvert;
|
||||
import cn.iocoder.yudao.module.product.service.property.ProductPropertyValueService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
@ -57,13 +58,13 @@ public class ProductPropertyValueController {
|
||||
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
|
||||
@PreAuthorize("@ss.hasPermission('product:property:query')")
|
||||
public CommonResult<ProductPropertyValueRespVO> getProperty(@RequestParam("id") Long id) {
|
||||
return success(productPropertyValueService.getPropertyValue(id));
|
||||
return success(ProductPropertyValueConvert.INSTANCE.convert(productPropertyValueService.getPropertyValue(id)));
|
||||
}
|
||||
|
||||
@GetMapping("/page")
|
||||
@ApiOperation("获得属性值分页")
|
||||
@PreAuthorize("@ss.hasPermission('product:property:query')")
|
||||
public CommonResult<PageResult<ProductPropertyValueRespVO>> getPropertyValuePage(@Valid ProductPropertyValuePageReqVO pageVO) {
|
||||
return success(productPropertyValueService.getPropertyValueListPage(pageVO));
|
||||
return success(ProductPropertyValueConvert.INSTANCE.convertPage(productPropertyValueService.getPropertyValuePage(pageVO)));
|
||||
}
|
||||
}
|
||||
|
@ -3,16 +3,12 @@ package cn.iocoder.yudao.module.product.controller.admin.property.vo.property;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ApiModel("管理后台 - 商品属性项 + 属性值 Response VO")
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
public class ProductPropertyAndValueRespVO extends ProductPropertyBaseVO {
|
||||
public class ProductPropertyAndValueRespVO {
|
||||
|
||||
@ApiModelProperty(value = "属性项的编号", required = true, example = "1024")
|
||||
private Long id;
|
||||
|
@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.product.service.property;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValueCreateReqVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValuePageReqVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValueRespVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValueUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.property.ProductPropertyValueDO;
|
||||
import cn.iocoder.yudao.module.product.service.property.bo.ProductPropertyValueDetailRespBO;
|
||||
@ -46,7 +45,7 @@ public interface ProductPropertyValueService {
|
||||
* @param id 编号
|
||||
* @return 属性值
|
||||
*/
|
||||
ProductPropertyValueRespVO getPropertyValue(Long id);
|
||||
ProductPropertyValueDO getPropertyValue(Long id);
|
||||
|
||||
/**
|
||||
* 根据属性项编号数组,获得属性值列表
|
||||
@ -64,14 +63,6 @@ public interface ProductPropertyValueService {
|
||||
*/
|
||||
List<ProductPropertyValueDetailRespBO> getPropertyValueDetailList(Collection<Long> ids);
|
||||
|
||||
/**
|
||||
* 根据属性项编号,获得属性值数组
|
||||
*
|
||||
* @param propertyIds 属性项编号数组
|
||||
* @return 属性值
|
||||
*/
|
||||
List<ProductPropertyValueRespVO> getPropertyValueListByPropertyId(List<Long> propertyIds);
|
||||
|
||||
/**
|
||||
* 根据属性项编号,活的属性值数量
|
||||
*
|
||||
@ -86,7 +77,7 @@ public interface ProductPropertyValueService {
|
||||
* @param pageReqVO 查询条件
|
||||
* @return 属性值的分页
|
||||
*/
|
||||
PageResult<ProductPropertyValueRespVO> getPropertyValueListPage(ProductPropertyValuePageReqVO pageReqVO);
|
||||
PageResult<ProductPropertyValueDO> getPropertyValuePage(ProductPropertyValuePageReqVO pageReqVO);
|
||||
|
||||
/**
|
||||
* 删除指定属性项编号下的属性值们
|
||||
|
@ -4,14 +4,12 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValueCreateReqVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValuePageReqVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValueRespVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValueUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.product.convert.propertyvalue.ProductPropertyValueConvert;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.property.ProductPropertyDO;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.property.ProductPropertyValueDO;
|
||||
import cn.iocoder.yudao.module.product.dal.mysql.property.ProductPropertyValueMapper;
|
||||
import cn.iocoder.yudao.module.product.service.property.bo.ProductPropertyValueDetailRespBO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
@ -24,6 +22,7 @@ import java.util.List;
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PROPERTY_VALUE_EXISTS;
|
||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PROPERTY_VALUE_NOT_EXISTS;
|
||||
|
||||
/**
|
||||
* 商品属性值 Service 实现类
|
||||
@ -43,34 +42,47 @@ public class ProductPropertyValueServiceImpl implements ProductPropertyValueServ
|
||||
|
||||
@Override
|
||||
public Long createPropertyValue(ProductPropertyValueCreateReqVO createReqVO) {
|
||||
// 校验名字唯一
|
||||
if (productPropertyValueMapper.selectByName(createReqVO.getPropertyId(), createReqVO.getName()) != null) {
|
||||
throw exception(PROPERTY_VALUE_EXISTS);
|
||||
}
|
||||
ProductPropertyValueDO convert = ProductPropertyValueConvert.INSTANCE.convert(createReqVO);
|
||||
productPropertyValueMapper.insert(convert);
|
||||
return convert.getId();
|
||||
|
||||
// 新增
|
||||
ProductPropertyValueDO value = ProductPropertyValueConvert.INSTANCE.convert(createReqVO);
|
||||
productPropertyValueMapper.insert(value);
|
||||
return value.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePropertyValue(ProductPropertyValueUpdateReqVO updateReqVO) {
|
||||
ProductPropertyValueDO productPropertyValueDO = productPropertyValueMapper.selectByName(updateReqVO.getPropertyId(), updateReqVO.getName());
|
||||
validatePropertyValueExists(updateReqVO.getId());
|
||||
// 校验名字唯一
|
||||
ProductPropertyValueDO productPropertyValueDO = productPropertyValueMapper.selectByName
|
||||
(updateReqVO.getPropertyId(), updateReqVO.getName());
|
||||
if (productPropertyValueDO != null && !productPropertyValueDO.getId().equals(updateReqVO.getId())) {
|
||||
throw exception(PROPERTY_VALUE_EXISTS);
|
||||
}
|
||||
ProductPropertyValueDO convert = ProductPropertyValueConvert.INSTANCE.convert(updateReqVO);
|
||||
productPropertyValueMapper.updateById(convert);
|
||||
|
||||
// 更新
|
||||
ProductPropertyValueDO updateObj = ProductPropertyValueConvert.INSTANCE.convert(updateReqVO);
|
||||
productPropertyValueMapper.updateById(updateObj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deletePropertyValue(Long id) {
|
||||
validatePropertyValueExists(id);
|
||||
productPropertyValueMapper.deleteById(id);
|
||||
}
|
||||
|
||||
private void validatePropertyValueExists(Long id) {
|
||||
if (productPropertyValueMapper.selectById(id) == null) {
|
||||
throw exception(PROPERTY_VALUE_NOT_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProductPropertyValueRespVO getPropertyValue(Long id) {
|
||||
ProductPropertyValueDO productPropertyValueDO = productPropertyValueMapper.selectOne(new LambdaQueryWrapper<ProductPropertyValueDO>()
|
||||
.eq(ProductPropertyValueDO::getId, id));
|
||||
return ProductPropertyValueConvert.INSTANCE.convert(productPropertyValueDO);
|
||||
public ProductPropertyValueDO getPropertyValue(Long id) {
|
||||
return productPropertyValueMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -95,19 +107,14 @@ public class ProductPropertyValueServiceImpl implements ProductPropertyValueServ
|
||||
return ProductPropertyValueConvert.INSTANCE.convertList(values, keys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ProductPropertyValueRespVO> getPropertyValueListByPropertyId(List<Long> propertyIds) {
|
||||
return ProductPropertyValueConvert.INSTANCE.convertList(productPropertyValueMapper.selectList("property_id", propertyIds));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPropertyValueCountByPropertyId(Long propertyId) {
|
||||
return productPropertyValueMapper.selectCountByPropertyId(propertyId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<ProductPropertyValueRespVO> getPropertyValueListPage(ProductPropertyValuePageReqVO pageReqVO) {
|
||||
return ProductPropertyValueConvert.INSTANCE.convertPage(productPropertyValueMapper.selectPage(pageReqVO));
|
||||
public PageResult<ProductPropertyValueDO> getPropertyValuePage(ProductPropertyValuePageReqVO pageReqVO) {
|
||||
return productPropertyValueMapper.selectPage(pageReqVO);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,13 +1,12 @@
|
||||
package cn.iocoder.yudao.module.product.service.sku;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuUpdateStockReqDTO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValueRespVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuBaseVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuCreateOrUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.product.convert.sku.ProductSkuConvert;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.property.ProductPropertyDO;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.property.ProductPropertyValueDO;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
|
||||
import cn.iocoder.yudao.module.product.dal.mysql.sku.ProductSkuMapper;
|
||||
import cn.iocoder.yudao.module.product.enums.ErrorCodeConstants;
|
||||
@ -25,6 +24,7 @@ import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;
|
||||
|
||||
@ -95,7 +95,7 @@ public class ProductSkuServiceImpl implements ProductSkuService {
|
||||
}
|
||||
|
||||
// 2. 校验,一个 SKU 下,没有重复的属性。校验方式是,遍历每个 SKU ,看看是否有重复的属性 propertyId
|
||||
Map<Long, ProductPropertyValueRespVO> propertyValueMap = CollectionUtils.convertMap(productPropertyValueService.getPropertyValueListByPropertyId(new ArrayList<>(propertyIds)), ProductPropertyValueRespVO::getId);
|
||||
Map<Long, ProductPropertyValueDO> propertyValueMap = convertMap(productPropertyValueService.getPropertyValueListByPropertyId(propertyIds), ProductPropertyValueDO::getId);
|
||||
skus.forEach(sku -> {
|
||||
Set<Long> skuPropertyIds = convertSet(sku.getProperties(), propertyItem -> propertyValueMap.get(propertyItem.getValueId()).getPropertyId());
|
||||
if (skuPropertyIds.size() != sku.getProperties().size()) {
|
||||
|
@ -5,7 +5,6 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.ProductPropertyViewRespVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.property.vo.value.ProductPropertyValueRespVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuBaseVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuCreateOrUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuRespVO;
|
||||
@ -15,6 +14,7 @@ import cn.iocoder.yudao.module.product.controller.app.spu.vo.AppSpuPageRespVO;
|
||||
import cn.iocoder.yudao.module.product.convert.sku.ProductSkuConvert;
|
||||
import cn.iocoder.yudao.module.product.convert.spu.ProductSpuConvert;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.property.ProductPropertyDO;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.property.ProductPropertyValueDO;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
|
||||
import cn.iocoder.yudao.module.product.dal.mysql.spu.ProductSpuMapper;
|
||||
@ -34,6 +34,7 @@ import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
|
||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SPU_NOT_EXISTS;
|
||||
|
||||
/**
|
||||
@ -141,7 +142,7 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
}
|
||||
Map<Long, List<ProductSkuBaseVO.Property>> propertyMaps = properties.stream().collect(Collectors.groupingBy(ProductSkuBaseVO.Property::getPropertyId));
|
||||
|
||||
List<ProductPropertyValueRespVO> propertyValueList = productPropertyValueService.getPropertyValueListByPropertyId(new ArrayList<>(propertyMaps.keySet()));
|
||||
List<ProductPropertyValueDO> propertyValueList = productPropertyValueService.getPropertyValueListByPropertyId(propertyMaps.keySet());
|
||||
List<ProductPropertyDO> propertyList = productPropertyService.getPropertyList(propertyMaps.keySet());
|
||||
// 装载组装过后的属性
|
||||
List<ProductPropertyViewRespVO> productPropertyViews = new ArrayList<>();
|
||||
@ -151,7 +152,7 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
productPropertyViewRespVO.setName(p.getName());
|
||||
List<ProductPropertyViewRespVO.Tuple2> propertyValues = new ArrayList<>();
|
||||
// 转换成map是为了能快速获取
|
||||
Map<Long, ProductPropertyValueRespVO> propertyValueMaps = CollectionUtils.convertMap(propertyValueList, ProductPropertyValueRespVO::getId);
|
||||
Map<Long, ProductPropertyValueDO> propertyValueMaps = convertMap(propertyValueList, ProductPropertyValueDO::getId);
|
||||
propertyMaps.get(p.getId()).forEach(pv -> {
|
||||
ProductPropertyViewRespVO.Tuple2 tuple2 = new ProductPropertyViewRespVO.Tuple2(pv.getValueId(), propertyValueMaps.get(pv.getValueId()).getName());
|
||||
propertyValues.add(tuple2);
|
||||
|
Reference in New Issue
Block a user