This commit is contained in:
haoxr 2021-08-03 07:54:57 +08:00
parent fbd3e46c23
commit 77499b7341
5 changed files with 73 additions and 29 deletions

View File

@ -1,7 +1,6 @@
package com.youlai.mall.pms.pojo.dto.admin;
import com.youlai.mall.pms.pojo.entity.PmsSku;
import com.youlai.mall.pms.pojo.entity.PmsSpuAttributeValue;
import lombok.Data;
import java.util.List;
@ -15,7 +14,6 @@ public class GoodsFormDTO {
private String name;
private Long categoryId;
private Long brandId;
private Long originPrice;
private Long price;
private String picUrl;
private List<String> album;

View File

@ -9,13 +9,12 @@ import lombok.Data;
public class PmsSku extends BaseEntity {
@TableId(type = IdType.AUTO)
private Long id;
private Long spuId;
private String name;
private String sn;
private String picUrl;
private String specs;
private Long originPrice;
private String name;
private Long spuId;
private String specIds;
private Long price;
private Integer stock;
private Integer lockedStock;
private String picUrl;
}

View File

@ -13,7 +13,6 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.mall.pms.common.constant.PmsConstants;
import com.youlai.mall.pms.common.enums.AttributeTypeEnum;
import com.youlai.mall.pms.component.BloomRedisService;
import com.youlai.mall.pms.mapper.PmsSkuMapper;
import com.youlai.mall.pms.mapper.PmsSpuMapper;
import com.youlai.mall.pms.pojo.dto.admin.GoodsFormDTO;
import com.youlai.mall.pms.pojo.entity.PmsSku;
@ -28,12 +27,11 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author <a href="mailto:xianrui0365@163.com">xianrui</a>
*/
@ -102,20 +100,47 @@ public class PmsSpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> impleme
// 规格列表
List<GoodsFormDTO.AttributeValue> specList = goods.getSpecList();
Map<String, Long> tempIdIdMap = this.saveSpecification(goodsId, specList);
Map<String, Long> specTempIdIdMap = this.saveSpecification(goodsId, specList);
// SKU列表
List<PmsSku> list = iPmsSkuService.list(new LambdaQueryWrapper<PmsSku>().eq(PmsSku::getSpuId, goodsId));
List<PmsSku> skuList = goods.getSkuList();
this.saveSku(goodsId, skuList, specTempIdIdMap);
// SPU
skuList.forEach(sku->{
});
return true;
}
private boolean saveSku(Long goodsId, List<PmsSku> skuList, Map<String, Long> specTempIdIdMap) {
List<PmsSku> pmsSkuList = skuList.stream().map(sku -> {
// 临时规格ID转换
String specIds = Arrays.asList(sku.getSpecIds().split("_")).stream()
.map(specId ->
specId.startsWith(PmsConstants.TEMP_ID_PREFIX) ? specTempIdIdMap.get(specId) + "" : specId
)
.collect(Collectors.joining("_"));
sku.setSpecIds(specIds);
sku.setSpuId(goodsId);
return sku;
}).collect(Collectors.toList());
// 新增修改SKU
return true;
}
/**
* 保存商品属性
*
* @param goodsId
* @param attrValList
* @return
*/
private boolean saveAttribute(Long goodsId, List<GoodsFormDTO.AttributeValue> attrValList) {
List<Long> formAttrValIds = attrValList.stream()
.filter(item -> item.getId() != null)
@ -133,21 +158,33 @@ public class PmsSpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> impleme
iPmsSpuAttributeValueService.removeByIds(removeAttrValIds);
}
// 新增或修改商品属性
List<PmsSpuAttributeValue> pmsSpuAttributeValueList = attrValList.stream().map(item -> {
PmsSpuAttributeValue pmsSpuAttributeValue = new PmsSpuAttributeValue();
BeanUtil.copyProperties(item, pmsSpuAttributeValue);
pmsSpuAttributeValue.setSpuId(goodsId);
pmsSpuAttributeValue.setType(AttributeTypeEnum.ATTRIBUTE.getValue());
return pmsSpuAttributeValue;
}).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(pmsSpuAttributeValueList)) {
return iPmsSpuAttributeValueService.saveOrUpdateBatch(pmsSpuAttributeValueList);
}
return true;
}
/**
* 保存商品规格新增的规格需要返回临时ID和持久化到数据库的ID的映射关系替换SKU中的规格ID集合
*
* @param goodsId 商品ID
* @param goodsId 商品ID
* @param specList 规格列表
* @return Map: key-临时IDvalue-持久化返回ID
* @return Map: key-临时IDvalue-持久化返回ID
*/
private Map<String, Long> saveSpecification(Long goodsId, List<GoodsFormDTO.AttributeValue> specList) {
// 删除规格
List<Long> formSpecValIds = specList.stream()
.filter(item -> item.getId() != null && !item.getId().startsWith(PmsConstants.TEMP_ID_PREFIX))
.map(item->Convert.toLong(item.getId()))
.map(item -> Convert.toLong(item.getId()))
.collect(Collectors.toList());
List<Long> dbSpecValIds = iPmsSpuAttributeValueService.list(new LambdaQueryWrapper<PmsSpuAttributeValue>()
@ -163,7 +200,7 @@ public class PmsSpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> impleme
// 新增规格
Map<String, Long> tempIdIdMap = new HashMap<>();
List<GoodsFormDTO.AttributeValue> newSpecList = specList.stream()
.filter(item -> item.getId() != null && item.getId().startsWith(PmsConstants.TEMP_ID_PREFIX)).collect(Collectors.toList());
.filter(item -> item.getId().startsWith(PmsConstants.TEMP_ID_PREFIX)).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(newSpecList)) {
newSpecList.forEach(item -> {
PmsSpuAttributeValue specification = new PmsSpuAttributeValue();
@ -171,9 +208,22 @@ public class PmsSpuServiceImpl extends ServiceImpl<PmsSpuMapper, PmsSpu> impleme
specification.setSpuId(goodsId);
specification.setType(AttributeTypeEnum.SPECIFICATION.getValue());
iPmsSpuAttributeValueService.save(specification);
tempIdIdMap.put(item.getId(),specification.getId());
tempIdIdMap.put(item.getId(), specification.getId());
});
}
// 修改规格
List<PmsSpuAttributeValue> pmsSpuAttributeValueList = specList.stream()
.filter(item -> !item.getId().startsWith(PmsConstants.TEMP_ID_PREFIX))
.map(spec -> {
PmsSpuAttributeValue pmsSpuAttributeValue = new PmsSpuAttributeValue();
BeanUtil.copyProperties(spec, pmsSpuAttributeValue);
pmsSpuAttributeValue.setSpuId(goodsId);
pmsSpuAttributeValue.setType(AttributeTypeEnum.SPECIFICATION.getValue());
return pmsSpuAttributeValue;
}).collect(Collectors.toList());
if (CollectionUtil.isNotEmpty(pmsSpuAttributeValueList)) {
iPmsSpuAttributeValueService.updateBatchById(pmsSpuAttributeValueList);
}
return tempIdIdMap;
}

View File

@ -10,8 +10,7 @@
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="sn" column="sn" jdbcType="VARCHAR"/>
<result property="picUrl" column="pic_url" jdbcType="VARCHAR"/>
<result property="specs" column="specs" jdbcType="VARCHAR"/>
<result property="originPrice" column="origin_price" jdbcType="BIGINT"/>
<result property="specIds" column="specs" jdbcType="VARCHAR"/>
<result property="price" column="price" jdbcType="BIGINT"/>
<result property="stock" column="stock" jdbcType="INTEGER"/>
<result property="lockedStock" column="locked_stock" jdbcType="INTEGER"/>

View File

@ -26,8 +26,7 @@
<result property="name" column="skuName" jdbcType="VARCHAR"/>
<result property="sn" column="sn" jdbcType="VARCHAR"/>
<result property="picUrl" column="skuPicUrl" jdbcType="VARCHAR"/>
<result property="specs" column="specs" jdbcType="VARCHAR"/>
<result property="originPrice" column="skuOriginPrice" jdbcType="BIGINT"/>
<result property="specIds" column="specIds" jdbcType="VARCHAR"/>
<result property="price" column="skuPrice" jdbcType="BIGINT"/>
<result property="stock" column="stock" jdbcType="INTEGER"/>
<result property="lockedStock" column="locked_stock" jdbcType="INTEGER"/>
@ -50,8 +49,7 @@
t2.NAME skuName,
t2.sn,
t2.pic_url skuPicUrl,
t2.specs,
t2.origin_price skuOriginPrice,
t2.spec_ids,
t2.price skuPrice,
t2.stock
FROM pms_spu t1