|
|
@ -15,25 +15,61 @@ limitations under the License. |
|
|
*/ |
|
|
*/ |
|
|
package cc.hiver.mall.serviceimpl.mybatis; |
|
|
package cc.hiver.mall.serviceimpl.mybatis; |
|
|
|
|
|
|
|
|
|
|
|
import cc.hiver.core.common.constant.MallConstant; |
|
|
|
|
|
import cc.hiver.mall.common.AttributeTypeEnum; |
|
|
|
|
|
import cc.hiver.mall.converter.GoodsAttributeConverter; |
|
|
|
|
|
import cc.hiver.mall.converter.GoodsConverter; |
|
|
import cc.hiver.mall.dao.mapper.GoodsMapper; |
|
|
import cc.hiver.mall.dao.mapper.GoodsMapper; |
|
|
import cc.hiver.mall.entity.Goods; |
|
|
import cc.hiver.mall.entity.Goods; |
|
|
|
|
|
import cc.hiver.mall.entity.GoodsAttribute; |
|
|
|
|
|
import cc.hiver.mall.entity.GoodsStock; |
|
|
|
|
|
import cc.hiver.mall.pojo.form.GoodsAttributeForm; |
|
|
import cc.hiver.mall.pojo.form.GoodsForm; |
|
|
import cc.hiver.mall.pojo.form.GoodsForm; |
|
|
import cc.hiver.mall.pojo.query.GoodsPageQuery; |
|
|
import cc.hiver.mall.pojo.query.GoodsPageQuery; |
|
|
import cc.hiver.mall.pojo.vo.GoodsDetailVO; |
|
|
import cc.hiver.mall.pojo.vo.GoodsDetailVO; |
|
|
import cc.hiver.mall.pojo.vo.GoodsPageVO; |
|
|
import cc.hiver.mall.pojo.vo.GoodsPageVO; |
|
|
|
|
|
import cc.hiver.mall.pojo.vo.MallGoodsDetailVO; |
|
|
|
|
|
import cc.hiver.mall.pojo.vo.MallGoodsPageVO; |
|
|
|
|
|
import cc.hiver.mall.service.mybatis.GoodsAttributeService; |
|
|
import cc.hiver.mall.service.mybatis.GoodsService; |
|
|
import cc.hiver.mall.service.mybatis.GoodsService; |
|
|
|
|
|
import cc.hiver.mall.service.mybatis.GoodsStockService; |
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil; |
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil; |
|
|
|
|
|
import cn.hutool.core.lang.Assert; |
|
|
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.stereotype.Service; |
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
import javax.transaction.Transactional; |
|
|
|
|
|
import java.util.*; |
|
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @author Yazhi Li |
|
|
* @author Yazhi Li |
|
|
*/ |
|
|
*/ |
|
|
@Service |
|
|
@Service |
|
|
|
|
|
@RequiredArgsConstructor |
|
|
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService { |
|
|
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService { |
|
|
|
|
|
@Autowired |
|
|
|
|
|
private GoodsMapper goodsMapper; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private GoodsStockService goodsStockService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private GoodsAttributeService goodsAttributeService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private GoodsConverter goodsConverter; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private GoodsAttributeConverter goodsAttributeConverter; |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public IPage<GoodsPageVO> listGoodsPages(GoodsPageQuery queryParams) { |
|
|
public IPage<GoodsPageVO> listGoodsPages(GoodsPageQuery queryParams) { |
|
|
Page<GoodsPageVO> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize()); |
|
|
Page<GoodsPageVO> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize()); |
|
|
@ -42,23 +78,309 @@ public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements |
|
|
return page; |
|
|
return page; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public IPage<MallGoodsPageVO> listMallGoodsPages(GoodsPageQuery queryParams) { |
|
|
|
|
|
Page<MallGoodsPageVO> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize()); |
|
|
|
|
|
List<MallGoodsPageVO> list = this.baseMapper.listMallGoodsPages(page, queryParams); |
|
|
|
|
|
page.setRecords(list); |
|
|
|
|
|
return page; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public GoodsDetailVO getGoodsDetail(String id) { |
|
|
public GoodsDetailVO getGoodsDetail(String id) { |
|
|
return null; |
|
|
GoodsDetailVO goodsDetailVO = new GoodsDetailVO(); |
|
|
|
|
|
|
|
|
|
|
|
Goods entity = this.getById(id); |
|
|
|
|
|
Assert.isTrue(entity != null, "商品不存在"); |
|
|
|
|
|
|
|
|
|
|
|
BeanUtil.copyProperties(entity, goodsDetailVO, "album"); |
|
|
|
|
|
goodsDetailVO.setSubPicUrls(entity.getAlbum()); |
|
|
|
|
|
|
|
|
|
|
|
// 商品属性列表
|
|
|
|
|
|
List<GoodsAttribute> attrList = goodsAttributeService.list(new LambdaQueryWrapper<GoodsAttribute>() |
|
|
|
|
|
.eq(GoodsAttribute::getId, id) |
|
|
|
|
|
.eq(GoodsAttribute::getType, AttributeTypeEnum.ATTR.getValue())); |
|
|
|
|
|
goodsDetailVO.setAttrList(attrList); |
|
|
|
|
|
|
|
|
|
|
|
// 商品规格列表
|
|
|
|
|
|
List<GoodsAttribute> specList = goodsAttributeService.list(new LambdaQueryWrapper<GoodsAttribute>() |
|
|
|
|
|
.eq(GoodsAttribute::getId, id) |
|
|
|
|
|
.eq(GoodsAttribute::getType, AttributeTypeEnum.SPEC.getValue())); |
|
|
|
|
|
goodsDetailVO.setSpecList(specList); |
|
|
|
|
|
|
|
|
|
|
|
// 商品库存列表
|
|
|
|
|
|
List<GoodsStock> goodsStockList = goodsStockService.list(new LambdaQueryWrapper<GoodsStock>().eq(GoodsStock::getGoodsId, id)); |
|
|
|
|
|
goodsDetailVO.setGoodsStockList(goodsStockList); |
|
|
|
|
|
return goodsDetailVO; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public MallGoodsDetailVO getMallGoodsDetail(String id) { |
|
|
|
|
|
Goods pmsSpu = this.getById(id); |
|
|
|
|
|
Assert.isTrue(pmsSpu != null, "商品不存在"); |
|
|
|
|
|
|
|
|
|
|
|
MallGoodsDetailVO mallGoodsDetailVO = new MallGoodsDetailVO(); |
|
|
|
|
|
|
|
|
|
|
|
// 商品基本信息
|
|
|
|
|
|
MallGoodsDetailVO.GoodsInfo goodsInfo = new MallGoodsDetailVO.GoodsInfo(); |
|
|
|
|
|
BeanUtil.copyProperties(pmsSpu, goodsInfo, "album"); |
|
|
|
|
|
|
|
|
|
|
|
List<String> album = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
|
|
if (StrUtil.isNotBlank(pmsSpu.getPicUrl())) { |
|
|
|
|
|
album.add(pmsSpu.getPicUrl()); |
|
|
|
|
|
} |
|
|
|
|
|
if (pmsSpu.getAlbum() != null && pmsSpu.getAlbum().length > 0) { |
|
|
|
|
|
album.addAll(Arrays.asList(pmsSpu.getAlbum())); |
|
|
|
|
|
goodsInfo.setAlbum(album); |
|
|
|
|
|
} |
|
|
|
|
|
mallGoodsDetailVO.setGoodsInfo(goodsInfo); |
|
|
|
|
|
|
|
|
|
|
|
// 商品属性列表
|
|
|
|
|
|
List<MallGoodsDetailVO.Attribute> attributeList = goodsAttributeService.list(new LambdaQueryWrapper<GoodsAttribute>() |
|
|
|
|
|
.eq(GoodsAttribute::getType, AttributeTypeEnum.ATTR.getValue()).eq(GoodsAttribute::getGoodsId, id) |
|
|
|
|
|
.select(GoodsAttribute::getId, GoodsAttribute::getName, GoodsAttribute::getValue)).stream(). |
|
|
|
|
|
map(item -> { |
|
|
|
|
|
MallGoodsDetailVO.Attribute attribute = new MallGoodsDetailVO.Attribute(); |
|
|
|
|
|
BeanUtil.copyProperties(item, attribute); |
|
|
|
|
|
return attribute; |
|
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
|
mallGoodsDetailVO.setAttributeList(attributeList); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 商品规格列表
|
|
|
|
|
|
List<GoodsAttribute> specSourceList = goodsAttributeService.list(new LambdaQueryWrapper<GoodsAttribute>() |
|
|
|
|
|
.eq(GoodsAttribute::getType, AttributeTypeEnum.SPEC.getValue()) |
|
|
|
|
|
.eq(GoodsAttribute::getGoodsId, id) |
|
|
|
|
|
.select(GoodsAttribute::getId, GoodsAttribute::getName, GoodsAttribute::getValue)); |
|
|
|
|
|
|
|
|
|
|
|
List<MallGoodsDetailVO.Specification> specList = new ArrayList<>(); |
|
|
|
|
|
// 规格Map [key:"颜色",value:[{id:1,value:"黑"},{id:2,value:"白"}]]
|
|
|
|
|
|
Map<String, List<GoodsAttribute>> specValueMap = specSourceList.stream().collect(Collectors.groupingBy(GoodsAttribute::getName)); |
|
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<String, List<GoodsAttribute>> entry : specValueMap.entrySet()) { |
|
|
|
|
|
String specName = entry.getKey(); |
|
|
|
|
|
List<GoodsAttribute> specValueSourceList = entry.getValue(); |
|
|
|
|
|
|
|
|
|
|
|
// 规格映射处理
|
|
|
|
|
|
MallGoodsDetailVO.Specification spec = new MallGoodsDetailVO.Specification(); |
|
|
|
|
|
spec.setName(specName); |
|
|
|
|
|
if (CollectionUtil.isNotEmpty(specValueSourceList)) { |
|
|
|
|
|
List<MallGoodsDetailVO.Specification.Value> specValueList = specValueSourceList.stream().map(item -> { |
|
|
|
|
|
MallGoodsDetailVO.Specification.Value specValue = new MallGoodsDetailVO.Specification.Value(); |
|
|
|
|
|
specValue.setId(item.getId()); |
|
|
|
|
|
specValue.setValue(item.getValue()); |
|
|
|
|
|
return specValue; |
|
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
|
spec.setValues(specValueList); |
|
|
|
|
|
specList.add(spec); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
mallGoodsDetailVO.setSpecList(specList); |
|
|
|
|
|
|
|
|
|
|
|
// 商品库存列表
|
|
|
|
|
|
List<GoodsStock> skuSourceList = goodsStockService.list(new LambdaQueryWrapper<GoodsStock>().eq(GoodsStock::getGoodsId, id)); |
|
|
|
|
|
if (CollectionUtil.isNotEmpty(skuSourceList)) { |
|
|
|
|
|
List<MallGoodsDetailVO.Sku> skuList = skuSourceList.stream().map(item -> { |
|
|
|
|
|
MallGoodsDetailVO.Sku sku = new MallGoodsDetailVO.Sku(); |
|
|
|
|
|
BeanUtil.copyProperties(item, sku); |
|
|
|
|
|
return sku; |
|
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
|
mallGoodsDetailVO.setSkuList(skuList); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return mallGoodsDetailVO; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
|
|
|
@Transactional |
|
|
public boolean addGoods(GoodsForm formData) { |
|
|
public boolean addGoods(GoodsForm formData) { |
|
|
return false; |
|
|
Goods entity = goodsConverter.form2Entity(formData); |
|
|
|
|
|
boolean result = this.save(entity); |
|
|
|
|
|
if (result) { |
|
|
|
|
|
String goodsId = entity.getId(); |
|
|
|
|
|
// 保存属性
|
|
|
|
|
|
List<GoodsAttributeForm> attrList = formData.getAttrList(); |
|
|
|
|
|
this.saveSpuAttrs(goodsId, attrList); |
|
|
|
|
|
// 保存规格
|
|
|
|
|
|
List<GoodsAttributeForm> specList = formData.getSpecList(); |
|
|
|
|
|
Map<String, String> tempWithNewSpecIdMap = this.saveSpuSpecs(goodsId, specList); |
|
|
|
|
|
// 保存SKU
|
|
|
|
|
|
List<GoodsStock> skuList = formData.getGoodsStockList(); |
|
|
|
|
|
this.saveSku(goodsId, skuList, tempWithNewSpecIdMap); |
|
|
|
|
|
} |
|
|
|
|
|
// 无异常返回true
|
|
|
|
|
|
return result; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
|
|
|
@Transactional |
|
|
public boolean updateGoods(GoodsForm formData) { |
|
|
public boolean updateGoods(GoodsForm formData) { |
|
|
return false; |
|
|
Goods entity = goodsConverter.form2Entity(formData); |
|
|
|
|
|
boolean result = this.updateById(entity); |
|
|
|
|
|
if (result) { |
|
|
|
|
|
// 属性保存
|
|
|
|
|
|
List<GoodsAttributeForm> attrList = formData.getAttrList(); |
|
|
|
|
|
this.saveSpuAttrs(entity.getId(), attrList); |
|
|
|
|
|
|
|
|
|
|
|
// 保存商品规格值
|
|
|
|
|
|
List<GoodsAttributeForm> specList = formData.getSpecList(); |
|
|
|
|
|
Map<String, String> specTempIdIdMap = this.saveSpuSpecs(entity.getId(), specList); |
|
|
|
|
|
|
|
|
|
|
|
// SKU保存
|
|
|
|
|
|
List<GoodsStock> skuList = formData.getGoodsStockList(); |
|
|
|
|
|
this.saveSku(entity.getId(), skuList, specTempIdIdMap); |
|
|
|
|
|
} |
|
|
|
|
|
return result; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
|
|
|
@Transactional |
|
|
public boolean removeByGoodsIds(String[] ids) { |
|
|
public boolean removeByGoodsIds(String[] ids) { |
|
|
return false; |
|
|
for (String id : ids) { |
|
|
|
|
|
goodsStockService.remove(new LambdaQueryWrapper<GoodsStock>().eq(GoodsStock::getGoodsId, id)); |
|
|
|
|
|
// 规格
|
|
|
|
|
|
goodsAttributeService.remove(new LambdaQueryWrapper<GoodsAttribute>().eq(GoodsAttribute::getGoodsId, id)); |
|
|
|
|
|
// 属性
|
|
|
|
|
|
goodsAttributeService.remove(new LambdaQueryWrapper<GoodsAttribute>().eq(GoodsAttribute::getGoodsId, id)); |
|
|
|
|
|
// SPU
|
|
|
|
|
|
this.removeById(id); |
|
|
|
|
|
} |
|
|
|
|
|
// 无异常直接返回true
|
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 保存SKU,需要替换提交表单中的临时规格ID |
|
|
|
|
|
* |
|
|
|
|
|
* @param goodsId |
|
|
|
|
|
* @param skuList |
|
|
|
|
|
* @param specTempIdIdMap |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
private boolean saveSku(String goodsId, List<GoodsStock> skuList, Map<String, String> specTempIdIdMap) { |
|
|
|
|
|
// 删除SKU
|
|
|
|
|
|
List<String> formSkuIds = skuList.stream().map(GoodsStock::getId).collect(Collectors.toList()); |
|
|
|
|
|
List<String> dbSkuIds = goodsStockService.list(new LambdaQueryWrapper<GoodsStock>().eq(GoodsStock::getGoodsId, goodsId) |
|
|
|
|
|
.select(GoodsStock::getId)).stream().map(GoodsStock::getId) |
|
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
List<String> removeSkuIds = dbSkuIds.stream().filter(dbSkuId -> !formSkuIds.contains(dbSkuId)).collect(Collectors.toList()); |
|
|
|
|
|
if (CollectionUtil.isNotEmpty(removeSkuIds)) { |
|
|
|
|
|
goodsStockService.removeByIds(removeSkuIds); |
|
|
|
|
|
} |
|
|
|
|
|
// 新增/修改SKU
|
|
|
|
|
|
List<GoodsStock> pmsSkuList = skuList.stream().map(sku -> { |
|
|
|
|
|
// 临时规格ID转换
|
|
|
|
|
|
String specIds = Arrays.stream(sku.getSpecIds().split("\\|")) |
|
|
|
|
|
.map(specId -> specId.startsWith(MallConstant.SPEC_TEMP_ID_PREFIX) ? specTempIdIdMap.get(specId) + "" : specId) |
|
|
|
|
|
.collect(Collectors.joining("_")); |
|
|
|
|
|
sku.setSpecIds(specIds); |
|
|
|
|
|
sku.setGoodsId(goodsId); |
|
|
|
|
|
return sku; |
|
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
|
return goodsStockService.saveOrUpdateBatch(pmsSkuList); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 保存商品属性 |
|
|
|
|
|
* |
|
|
|
|
|
* @param goodsId 商品ID |
|
|
|
|
|
* @param attrList 商品属性集合 |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
private boolean saveSpuAttrs(String goodsId, List<GoodsAttributeForm> attrList) { |
|
|
|
|
|
// 1. 【删除】此次提交移除的商品规格
|
|
|
|
|
|
// 1.1 此次提交保留的商品属性ID
|
|
|
|
|
|
List<String> retainAttrIds = attrList.stream() |
|
|
|
|
|
.filter(item -> item.getId() != null) |
|
|
|
|
|
.map(item -> item.getId()) |
|
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
// 1.2 获取原商品属性ID集合
|
|
|
|
|
|
List<String> originAttrIds = goodsAttributeService.list(new LambdaQueryWrapper<GoodsAttribute>() |
|
|
|
|
|
.eq(GoodsAttribute::getGoodsId, goodsId).eq(GoodsAttribute::getType, AttributeTypeEnum.ATTR.getValue()) |
|
|
|
|
|
.select(GoodsAttribute::getId)) |
|
|
|
|
|
.stream() |
|
|
|
|
|
.map(GoodsAttribute::getId) |
|
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
// 1.3 需要删除的商品属性:原商品属性-此次提交保留的属性
|
|
|
|
|
|
List<String> removeAttrValIds = originAttrIds.stream() |
|
|
|
|
|
.filter(id -> !retainAttrIds.contains(id)) |
|
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
if (CollectionUtil.isNotEmpty(removeAttrValIds)) { |
|
|
|
|
|
goodsStockService.removeByIds(removeAttrValIds); |
|
|
|
|
|
} |
|
|
|
|
|
// 新增或修改商品属性
|
|
|
|
|
|
List<GoodsAttribute> entities = attrList.stream().map(item -> { |
|
|
|
|
|
GoodsAttribute entity = goodsAttributeConverter.form2Entity(item); |
|
|
|
|
|
entity.setId(item.getId()); |
|
|
|
|
|
entity.setGoodsId(goodsId); |
|
|
|
|
|
entity.setType(AttributeTypeEnum.ATTR.getValue()); |
|
|
|
|
|
return entity; |
|
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
|
if (CollectionUtil.isNotEmpty(entities)) { |
|
|
|
|
|
return goodsAttributeService.saveOrUpdateBatch(entities); |
|
|
|
|
|
} |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 保存商品规格 |
|
|
|
|
|
* <p> |
|
|
|
|
|
* 新增的规格需要返回临时ID和持久化到数据库的ID的映射关系,替换SKU中的规格ID集合 |
|
|
|
|
|
* |
|
|
|
|
|
* @param goodsId 商品ID |
|
|
|
|
|
* @param specList 规格列表 |
|
|
|
|
|
* @return Map: key-临时ID;value-持久化返回ID |
|
|
|
|
|
*/ |
|
|
|
|
|
private Map<String, String> saveSpuSpecs(String goodsId, List<GoodsAttributeForm> specList) { |
|
|
|
|
|
// 1. 【删除】此次提交移除的商品规格
|
|
|
|
|
|
// 1.1 此次提交保留的规格
|
|
|
|
|
|
List<String> retainSpuSpecIds = specList.stream() |
|
|
|
|
|
.filter(item -> !item.getId().startsWith(MallConstant.SPEC_TEMP_ID_PREFIX)) |
|
|
|
|
|
.map(item -> item.getId()) |
|
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
// 1.2 原商品规格
|
|
|
|
|
|
List<String> originSpuSpecIds = goodsAttributeService.list(new LambdaQueryWrapper<GoodsAttribute>() |
|
|
|
|
|
.eq(GoodsAttribute::getGoodsId, goodsId) |
|
|
|
|
|
.eq(GoodsAttribute::getType, AttributeTypeEnum.SPEC.getValue()) |
|
|
|
|
|
.select(GoodsAttribute::getId)) |
|
|
|
|
|
.stream().map(GoodsAttribute::getId) |
|
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
// 1.3 需要删除的商品规格:原商品规格-此次提交保留的规格
|
|
|
|
|
|
List<String> removeSpuSpecIds = originSpuSpecIds.stream().filter(id -> !retainSpuSpecIds.contains(id)) |
|
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
if (CollectionUtil.isNotEmpty(removeSpuSpecIds)) { |
|
|
|
|
|
// 删除商品的规格
|
|
|
|
|
|
goodsAttributeService.removeByIds(removeSpuSpecIds); |
|
|
|
|
|
} |
|
|
|
|
|
// 2. 【新增】此次提交的新加的商品规格
|
|
|
|
|
|
// 临时规格ID和持久化数据库得到的规格ID的映射,用于替换SKU临时的规格ID
|
|
|
|
|
|
Map<String, String> tempWithNewSpecIdMap = new HashMap<>(); |
|
|
|
|
|
List<GoodsAttributeForm> newSpecList = specList.stream() |
|
|
|
|
|
.filter(item -> item.getId().startsWith(MallConstant.SPEC_TEMP_ID_PREFIX)) |
|
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
if (CollectionUtil.isNotEmpty(newSpecList)) { |
|
|
|
|
|
newSpecList.forEach(item -> { |
|
|
|
|
|
GoodsAttribute entity = goodsAttributeConverter.form2Entity(item); |
|
|
|
|
|
entity.setGoodsId(goodsId); |
|
|
|
|
|
entity.setType(AttributeTypeEnum.SPEC.getValue()); |
|
|
|
|
|
goodsAttributeService.save(entity); |
|
|
|
|
|
tempWithNewSpecIdMap.put(item.getId(), entity.getId()); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
// 3. 【修改】此次提交的需要修改的商品规格
|
|
|
|
|
|
List<GoodsAttribute> pmsSpuAttributeList = specList.stream() |
|
|
|
|
|
.filter(item -> !item.getId().startsWith(MallConstant.SPEC_TEMP_ID_PREFIX)) |
|
|
|
|
|
.map(item -> { |
|
|
|
|
|
GoodsAttribute entity = goodsAttributeConverter.form2Entity(item); |
|
|
|
|
|
entity.setId(item.getId()); |
|
|
|
|
|
entity.setGoodsId(goodsId); |
|
|
|
|
|
entity.setType(AttributeTypeEnum.SPEC.getValue()); |
|
|
|
|
|
return entity; |
|
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
|
if (CollectionUtil.isNotEmpty(pmsSpuAttributeList)) { |
|
|
|
|
|
goodsAttributeService.updateBatchById(pmsSpuAttributeList); |
|
|
|
|
|
} |
|
|
|
|
|
return tempWithNewSpecIdMap; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|