spu 保存,propertyValue合并到productProperty中

This commit is contained in:
franky-mgmg-pc 2022-06-07 17:16:58 +08:00
parent f07bf8b0fd
commit 4bcc6c374e
16 changed files with 60 additions and 187 deletions

View File

@ -11,7 +11,7 @@ import io.swagger.annotations.*;
public class ProductPropertyValueRespVO extends ProductPropertyValueBaseVO {
@ApiModelProperty(value = "主键", required = true)
private Integer id;
private Long id;
@ApiModelProperty(value = "创建时间")
private Date createTime;

View File

@ -12,8 +12,7 @@ import javax.validation.constraints.*;
@Data
public class ProductSkuBaseVO {
@ApiModelProperty(value = "spu编号", required = true)
@NotNull(message = "spu编号不能为空")
@ApiModelProperty(value = "spu编号")
private Long spuId;
@ApiModelProperty(value = "规格值数组-json格式 [{propertyId: , valueId: }, {propertyId: , valueId: }]", required = true)
@ -46,9 +45,9 @@ public class ProductSkuBaseVO {
@Data
public static class Property {
@NotNull(message = "规格属性名id不能为空")
private Integer propertyId;
private Long propertyId;
@NotNull(message = "规格属性值id不能为空")
private Integer valueId;
private Long valueId;
}
}

View File

@ -15,7 +15,7 @@ import com.alibaba.excel.annotation.ExcelProperty;
public class ProductSkuExcelVO {
@ExcelProperty("主键")
private Integer id;
private Long id;
@ExcelProperty("spu编号")
private Long spuId;

View File

@ -11,7 +11,7 @@ import io.swagger.annotations.*;
public class ProductSkuRespVO extends ProductSkuBaseVO {
@ApiModelProperty(value = "主键", required = true)
private Integer id;
private Long id;
@ApiModelProperty(value = "创建时间")
private Date createTime;

View File

@ -65,8 +65,7 @@ public class ProductSpuController {
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('product:spu:query')")
public CommonResult<SpuRespVO> getSpu(@RequestParam("id") Long id) {
ProductSpuDO spu = spuService.getSpu(id);
return success(ProductSpuConvert.INSTANCE.convert(spu));
return success(spuService.getSpu(id));
}
@GetMapping("/list")

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.product.controller.admin.spu.vo;
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuRespVO;
import lombok.*;
import java.util.*;
import io.swagger.annotations.*;
@ -16,4 +17,6 @@ public class SpuRespVO extends ProductSpuBaseVO {
@ApiModelProperty(value = "创建时间")
private Date createTime;
List<ProductSkuRespVO> productSkuRespVOS;
}

View File

@ -27,7 +27,7 @@ public class ProductPropertyValueDO extends BaseDO {
* 主键
*/
@TableId
private Integer id;
private Long id;
/**
* 规格键 id
*

View File

@ -5,6 +5,7 @@ import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.product.convert.sku.ProductSkuConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.*;
@ -45,4 +46,9 @@ public interface ProductSkuMapper extends BaseMapperX<ProductSkuDO> {
.orderByDesc(ProductSkuDO::getId));
}
default List<ProductSkuDO> selectBySpuId(Long spuId) {
return selectList(new LambdaQueryWrapperX<ProductSkuDO>()
.eqIfPresent(ProductSkuDO::getSpuId, spuId)
);
}
}

View File

@ -81,5 +81,5 @@ public interface ProductPropertyService {
* @param propertyIds 属性名id集合
* @return
*/
List<ProductPropertyRespVO> selectByIds(List<Integer> propertyIds);
List<ProductPropertyRespVO> selectByIds(List<Long> propertyIds);
}

View File

@ -1,10 +1,7 @@
package cn.iocoder.yudao.module.product.service.property;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.admin.property.vo.*;
import cn.iocoder.yudao.module.product.controller.admin.property.vo.ProductPropertyCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.property.vo.ProductPropertyUpdateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.propertyvalue.vo.ProductPropertyValueCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.propertyvalue.vo.ProductPropertyValueRespVO;
import cn.iocoder.yudao.module.product.convert.property.ProductPropertyConvert;
@ -12,7 +9,7 @@ import cn.iocoder.yudao.module.product.convert.propertyvalue.ProductPropertyValu
import cn.iocoder.yudao.module.product.dal.dataobject.property.ProductPropertyDO;
import cn.iocoder.yudao.module.product.dal.dataobject.propertyvalue.ProductPropertyValueDO;
import cn.iocoder.yudao.module.product.dal.mysql.property.ProductPropertyMapper;
import cn.iocoder.yudao.module.product.service.propertyvalue.ProductPropertyValueService;
import cn.iocoder.yudao.module.product.dal.mysql.propertyvalue.ProductPropertyValueMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
@ -21,6 +18,7 @@ import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -39,7 +37,7 @@ public class ProductPropertyServiceImpl implements ProductPropertyService {
private ProductPropertyMapper productPropertyMapper;
@Resource
private ProductPropertyValueService productPropertyValueService;
private ProductPropertyValueMapper productPropertyValueMapper;
@Override
@Transactional(rollbackFor = Exception.class)
@ -52,7 +50,7 @@ public class ProductPropertyServiceImpl implements ProductPropertyService {
List<ProductPropertyValueCreateReqVO> propertyValueList = createReqVO.getPropertyValueList();
List<ProductPropertyValueDO> productPropertyValueDOList = ProductPropertyValueConvert.INSTANCE.convertList03(propertyValueList);
productPropertyValueDOList.stream().forEach(x-> x.setPropertyId(property.getId()));
productPropertyValueService.batchInsert(productPropertyValueDOList);
productPropertyValueMapper.insertBatch(productPropertyValueDOList);
// 返回
return property.getId();
}
@ -66,11 +64,11 @@ public class ProductPropertyServiceImpl implements ProductPropertyService {
ProductPropertyDO updateObj = ProductPropertyConvert.INSTANCE.convert(updateReqVO);
productPropertyMapper.updateById(updateObj);
//更新属性值先删后加
productPropertyValueService.deletePropertyValueByPropertyId(updateReqVO.getId());
productPropertyValueMapper.deletePropertyValueByPropertyId(updateReqVO.getId());
List<ProductPropertyValueCreateReqVO> propertyValueList = updateReqVO.getPropertyValueList();
List<ProductPropertyValueDO> productPropertyValueDOList = ProductPropertyValueConvert.INSTANCE.convertList03(propertyValueList);
productPropertyValueDOList.stream().forEach(x-> x.setPropertyId(updateReqVO.getId()));
productPropertyValueService.batchInsert(productPropertyValueDOList);
productPropertyValueMapper.insertBatch(productPropertyValueDOList);
}
@Override
@ -80,7 +78,7 @@ public class ProductPropertyServiceImpl implements ProductPropertyService {
// 删除
productPropertyMapper.deleteById(id);
//同步删除属性值
productPropertyValueService.deletePropertyValueByPropertyId(id);
productPropertyValueMapper.deletePropertyValueByPropertyId(id);
}
private void validatePropertyExists(Long id) {
@ -117,7 +115,7 @@ public class ProductPropertyServiceImpl implements ProductPropertyService {
List<Long> propertyIds = propertyRespVOPageResult.getList().stream().map(ProductPropertyRespVO::getId).collect(Collectors.toList());
//获取属性值列表
List<ProductPropertyValueDO> productPropertyValueDOList = productPropertyValueService.getPropertyValueListByPropertyId(propertyIds);
List<ProductPropertyValueDO> productPropertyValueDOList = productPropertyValueMapper.getPropertyValueListByPropertyId(propertyIds);
List<ProductPropertyValueRespVO> propertyValueRespVOList = ProductPropertyValueConvert.INSTANCE.convertList(productPropertyValueDOList);
//组装一对多
propertyRespVOPageResult.getList().forEach(x->{
@ -128,13 +126,17 @@ public class ProductPropertyServiceImpl implements ProductPropertyService {
return propertyRespVOPageResult;
}
private List<ProductPropertyValueDO> getPropertyValueListByPropertyId(List<Long> propertyIds) {
return productPropertyValueMapper.getPropertyValueListByPropertyId(propertyIds);
}
@Override
public ProductPropertyRespVO getPropertyResp(Long id) {
//查询规格
ProductPropertyDO property = getProperty(id);
ProductPropertyRespVO propertyRespVO = ProductPropertyConvert.INSTANCE.convert(property);
//查询属性值
List<ProductPropertyValueDO> valueDOList = productPropertyValueService.getPropertyValueListByPropertyId(Arrays.asList(id));
List<ProductPropertyValueDO> valueDOList = productPropertyValueMapper.getPropertyValueListByPropertyId(Arrays.asList(id));
List<ProductPropertyValueRespVO> propertyValueRespVOS = ProductPropertyValueConvert.INSTANCE.convertList(valueDOList);
//组装
propertyRespVO.setPropertyValueList(propertyValueRespVOS);
@ -142,7 +144,12 @@ public class ProductPropertyServiceImpl implements ProductPropertyService {
}
@Override
public List<ProductPropertyRespVO> selectByIds(List<Integer> propertyIds) {
return ProductPropertyConvert.INSTANCE.convertList(productPropertyMapper.selectBatchIds(propertyIds));
public List<ProductPropertyRespVO> selectByIds(List<Long> propertyIds) {
List<ProductPropertyRespVO> productPropertyRespVO = ProductPropertyConvert.INSTANCE.convertList(productPropertyMapper.selectBatchIds(propertyIds));
//查询属性值
List<ProductPropertyValueDO> valueDOList = productPropertyValueMapper.getPropertyValueListByPropertyId(propertyIds);
Map<Long, List<ProductPropertyValueDO>> propertyValuesMap = valueDOList.stream().collect(Collectors.groupingBy(ProductPropertyValueDO::getPropertyId));
productPropertyRespVO.forEach(p -> p.setPropertyValueList(ProductPropertyValueConvert.INSTANCE.convertList(propertyValuesMap.get(p.getId()))));
return productPropertyRespVO;
}
}

View File

@ -1,88 +0,0 @@
package cn.iocoder.yudao.module.product.service.propertyvalue;
import cn.iocoder.yudao.module.product.controller.admin.propertyvalue.vo.*;
import cn.iocoder.yudao.module.product.convert.propertyvalue.ProductPropertyValueConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.propertyvalue.ProductPropertyValueDO;
import cn.iocoder.yudao.module.product.dal.mysql.propertyvalue.ProductPropertyValueMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.PROPERTY_VALUE_NOT_EXISTS;
/**
* 规格值 Service 实现类
*
* @author 芋道源码
*/
@Service
@Validated
public class ProductProductPropertyValueServiceImpl implements ProductPropertyValueService {
@Resource
private ProductPropertyValueMapper productPropertyValueMapper;
// TODO @franky这个合并到 property 他们本身是在一起的哈基本不存在只查询规格而不查询规格值
@Override
public Integer createPropertyValue(ProductPropertyValueCreateReqVO createReqVO) {
// 插入
ProductPropertyValueDO propertyValue = ProductPropertyValueConvert.INSTANCE.convert(createReqVO);
productPropertyValueMapper.insert(propertyValue);
// 返回
return propertyValue.getId();
}
@Override
public void updatePropertyValue(ProductPropertyValueUpdateReqVO updateReqVO) {
// 校验存在
this.validatePropertyValueExists(updateReqVO.getId());
// 更新
ProductPropertyValueDO updateObj = ProductPropertyValueConvert.INSTANCE.convert(updateReqVO);
productPropertyValueMapper.updateById(updateObj);
}
@Override
public void deletePropertyValue(Integer id) {
// 校验存在
this.validatePropertyValueExists(id);
// 删除
productPropertyValueMapper.deleteById(id);
}
private void validatePropertyValueExists(Integer id) {
if (productPropertyValueMapper.selectById(id) == null) {
throw exception(PROPERTY_VALUE_NOT_EXISTS);
}
}
@Override
public ProductPropertyValueDO getPropertyValue(Integer id) {
return productPropertyValueMapper.selectById(id);
}
@Override
public List<ProductPropertyValueDO> getPropertyValueList(Collection<Integer> ids) {
return productPropertyValueMapper.selectBatchIds(ids);
}
@Override
public void batchInsert(List<ProductPropertyValueDO> propertyValues) {
productPropertyValueMapper.insertBatch(propertyValues);
}
@Override
public List<ProductPropertyValueDO> getPropertyValueListByPropertyId(List<Long> propertyIds) {
return productPropertyValueMapper.getPropertyValueListByPropertyId(propertyIds);
}
@Override
public void deletePropertyValueByPropertyId(Long propertyId) {
productPropertyValueMapper.deletePropertyValueByPropertyId(propertyId);
}
}

View File

@ -1,71 +0,0 @@
package cn.iocoder.yudao.module.product.service.propertyvalue;
import java.util.*;
import javax.validation.*;
import cn.iocoder.yudao.module.product.controller.admin.propertyvalue.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.propertyvalue.ProductPropertyValueDO;
/**
* 规格值 Service 接口
*
* @author 芋道源码
*/
public interface ProductPropertyValueService {
/**
* 创建规格值
*
* @param createReqVO 创建信息
* @return 编号
*/
Integer createPropertyValue(@Valid ProductPropertyValueCreateReqVO createReqVO);
/**
* 更新规格值
*
* @param updateReqVO 更新信息
*/
void updatePropertyValue(@Valid ProductPropertyValueUpdateReqVO updateReqVO);
/**
* 删除规格值
*
* @param id 编号
*/
void deletePropertyValue(Integer id);
/**
* 获得规格值
*
* @param id 编号
* @return 规格值
*/
ProductPropertyValueDO getPropertyValue(Integer id);
/**
* 获得规格值列表
*
* @param ids 编号
* @return 规格值列表
*/
List<ProductPropertyValueDO> getPropertyValueList(Collection<Integer> ids);
/**
* 批量插入属性值
* @param propertyValues
*/
void batchInsert(List<ProductPropertyValueDO> propertyValues);
/**
* 根据属性id查询
* @param propertyIds
* @return
*/
List<ProductPropertyValueDO> getPropertyValueListByPropertyId(List<Long> propertyIds);
/**
* 根据属性id 删除
* @param propertyId
*/
void deletePropertyValueByPropertyId(Long propertyId);
}

View File

@ -79,4 +79,12 @@ public interface ProductSkuService {
* @return
*/
void batchSave(List<ProductSkuDO> skuDOList);
/**
* 获得商品sku 集合
*
* @param spuId spu 编号
* @return 商品sku 集合
*/
List<ProductSkuDO> getSkusBySpuId(Long spuId);
}

View File

@ -92,7 +92,7 @@ public class ProductSkuServiceImpl implements ProductSkuService {
public void validatedSkuReq(List<ProductSkuCreateReqVO> skuCreateReqList) {
List<ProductSkuBaseVO.Property> skuPropertyList = skuCreateReqList.stream().flatMap(p -> p.getProperties().stream()).collect(Collectors.toList());
// 校验规格属性以及规格值是否存在
List<Integer> propertyIds = skuPropertyList.stream().map(ProductSkuBaseVO.Property::getPropertyId).collect(Collectors.toList());
List<Long> propertyIds = skuPropertyList.stream().map(ProductSkuBaseVO.Property::getPropertyId).collect(Collectors.toList());
List<ProductPropertyRespVO> propertyAndValueList = productPropertyService.selectByIds(propertyIds);
if (propertyAndValueList.isEmpty())
throw ServiceExceptionUtil.exception(PROPERTY_NOT_EXISTS);
@ -122,4 +122,9 @@ public class ProductSkuServiceImpl implements ProductSkuService {
public void batchSave(List<ProductSkuDO> skuDOList) {
productSkuMapper.insertBatch(skuDOList);
}
@Override
public List<ProductSkuDO> getSkusBySpuId(Long spuId) {
return productSkuMapper.selectBySpuId(spuId);
}
}

View File

@ -41,7 +41,7 @@ public interface ProductSpuService {
* @param id 编号
* @return 商品spu
*/
ProductSpuDO getSpu(Long id);
SpuRespVO getSpu(Long id);
/**
* 获得商品spu列表

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.product.service.spu;
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuRespVO;
import cn.iocoder.yudao.module.product.convert.sku.ProductSkuConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
import cn.iocoder.yudao.module.product.service.category.CategoryService;
@ -89,8 +90,12 @@ public class ProductSpuServiceImpl implements ProductSpuService {
}
@Override
public ProductSpuDO getSpu(Long id) {
return ProductSpuMapper.selectById(id);
public SpuRespVO getSpu(Long id) {
ProductSpuDO spu = ProductSpuMapper.selectById(id);
SpuRespVO spuVO = ProductSpuConvert.INSTANCE.convert(spu);
List<ProductSkuRespVO> skuReqs = ProductSkuConvert.INSTANCE.convertList( productSkuService.getSkusBySpuId(id));
spuVO.setProductSkuRespVOS(skuReqs);
return spuVO;
}
@Override