提交 02de050b 作者: xiaowang

修复:商品编辑接口

上级 ff6a8008
......@@ -40,5 +40,12 @@ public interface MallGoodsDao {
void deleteMallGoodsResources(Long id);
void deleteMallGoodsSpec(Long id);
void deleteGoodsSpec(List<Integer> deleteSpec);
void updateGoodsSpec(GoodsSpecDO goodsSpecDO);
void deleteGoodsSpecValues(List<Integer> deleteSpecValueId);
void updateGoodsSpecValue(GoodsSpecValuesDO goodsSpecValuesDO);
}
......@@ -47,6 +47,7 @@ public class GoodsSpecDO implements Serializable {
private Date updateTime;
public GoodsSpecDO(GoodsSpecVO goodsSpecVO) {
this.id = goodsSpecVO.getId();
this.mallGoodsId = goodsSpecVO.getMallGoodsId();
this.specName = goodsSpecVO.getSpecName();
this.chooseType = goodsSpecVO.getChooseType();
......@@ -58,6 +59,6 @@ public class GoodsSpecDO implements Serializable {
return GoodsSpecVO.builder().id(id).mallGoodsId(mallGoodsId).specName(specName)
.chooseType(chooseType).must(must).skuUnitId(skuUnitId).build();
}
}
......@@ -60,6 +60,7 @@ public class GoodsSpecValuesDO implements Serializable {
private Date updateTime;
public GoodsSpecValuesDO(GoodsSpecValuesVO d) {
this.id = d.getId();
this.goodsSpecId = d.getGoodsSpecId();
this.specValueName = d.getSpecValueName();
this.partNo = d.getPartNo();
......
......@@ -15,6 +15,7 @@ import com.mmc.pms.service.mall.MallGoodsService;
import com.mmc.pms.util.CodeUtil;
import com.mmc.pms.util.SnowFlake;
import com.mmc.pms.util.TDateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -22,6 +23,7 @@ import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
......@@ -52,14 +54,13 @@ public class MallGoodsServiceImpl implements MallGoodsService {
// 将商品图片等资源存入数据库中
insertMallGoodsResources(mallGoodsVO, id);
// 将商品规格存入数据库
insertMallGoodsSpec(mallGoodsVO, id);
insertMallGoodsSpec(mallGoodsVO.getGoodsSpecList(), id);
return ResultBody.success();
}
@Transactional(rollbackFor = Exception.class)
public void insertMallGoodsSpec(MallGoodsVO mallGoodsVO, long id) {
public void insertMallGoodsSpec(List<GoodsSpecVO> goodsSpecList, long id) {
// 获取输入的规格信息
List<GoodsSpecVO> goodsSpecList = mallGoodsVO.getGoodsSpecList();
for (GoodsSpecVO goodsSpecVO : goodsSpecList) {
goodsSpecVO.setMallGoodsId(id);
GoodsSpecDO goodsSpecDO = new GoodsSpecDO(goodsSpecVO);
......@@ -127,6 +128,7 @@ public class MallGoodsServiceImpl implements MallGoodsService {
@Override
@Transactional(rollbackFor = Exception.class)
public ResultBody editMallGoods(MallGoodsVO mallGoodsVO, Integer userAccountId) {
ResultBody resultError = checkInformation(mallGoodsVO, userAccountId);
if (resultError != null) return resultError;
......@@ -136,9 +138,52 @@ public class MallGoodsServiceImpl implements MallGoodsService {
// 修改商城商品的图片等资源信息,先删除后新增
mallGoodsDao.deleteMallGoodsResources(mallGoodsVO.getId());
this.insertMallGoodsResources(mallGoodsVO, mallGoodsVO.getId());
// 修改商城规格信息,先删除后新增
mallGoodsDao.deleteMallGoodsSpec(mallGoodsVO.getId());
this.insertMallGoodsSpec(mallGoodsVO, mallGoodsVO.getId());
// 从数据库获取商品规格信息
List<GoodsSpecDO> dbGoodsSpec = mallGoodsDao.getMallGoodsSpec(mallGoodsVO.getId());
List<Integer> specIds = mallGoodsVO.getGoodsSpecList().stream().map(GoodsSpecVO::getId).filter(Objects::nonNull).collect(Collectors.toList());
// 对比要删除的规格
List<Integer> deleteSpec = dbGoodsSpec.stream().map(GoodsSpecDO::getId).filter(id -> !specIds.contains(id)).collect(Collectors.toList());
if (deleteSpec.size() != 0) {
mallGoodsDao.deleteGoodsSpec(deleteSpec);
}
// 获取规格值信息
Map<Integer, List<GoodsSpecValuesDO>> specValuesMap = mallGoodsDao.getMallGoodsSpecValues(specIds).stream()
.collect(Collectors.groupingBy(GoodsSpecValuesDO::getGoodsSpecId));
// 修改规格信息
for (GoodsSpecVO goodsSpecVO : mallGoodsVO.getGoodsSpecList()) {
if (goodsSpecVO.getId() != null) {
GoodsSpecDO goodsSpecDO = new GoodsSpecDO(goodsSpecVO);
mallGoodsDao.updateGoodsSpec(goodsSpecDO);
// 数据库获取到的规格值信息
List<GoodsSpecValuesDO> dbSpecValues = specValuesMap.get(goodsSpecVO.getId());
List<Integer> specValuesId = goodsSpecVO.getGoodsSpecValuesList().stream().map(GoodsSpecValuesVO::getId).filter(Objects::nonNull).collect(Collectors.toList());
List<Integer> deleteSpecValueId = dbSpecValues.stream().map(GoodsSpecValuesDO::getId).filter(id -> !specValuesId.contains(id)).collect(Collectors.toList());
if (deleteSpecValueId.size() != 0) {
mallGoodsDao.deleteGoodsSpecValues(deleteSpecValueId);
}
// 修改规格值的信息
List<GoodsSpecValuesVO> updateSpecValue = goodsSpecVO.getGoodsSpecValuesList().stream().filter(d -> d.getId() != null).collect(Collectors.toList());
for (GoodsSpecValuesVO goodsSpecValuesVO : updateSpecValue) {
GoodsSpecValuesDO goodsSpecValuesDO = new GoodsSpecValuesDO(goodsSpecValuesVO);
mallGoodsDao.updateGoodsSpecValue(goodsSpecValuesDO);
}
// 新增id为空的规格值
List<GoodsSpecValuesVO> newGoodsSpecValues = goodsSpecVO.getGoodsSpecValuesList().stream().filter(d -> d.getId() == null).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(newGoodsSpecValues)) {
List<GoodsSpecValuesDO> goodsSpecValuesList = newGoodsSpecValues.stream().map(d -> {
d.setGoodsSpecId(goodsSpecVO.getId());
return new GoodsSpecValuesDO(d);
}).collect(Collectors.toList());
// 批量插入规格值的信息
mallGoodsDao.batchInsertSpecValues(goodsSpecValuesList);
}
}
}
// 新增id为空的规格
List<GoodsSpecVO> newGoodsSpec = mallGoodsVO.getGoodsSpecList().stream().filter(d -> d.getId() == null).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(newGoodsSpec)) {
this.insertMallGoodsSpec(newGoodsSpec, mallGoodsVO.getId());
}
return ResultBody.success();
}
}
......@@ -65,15 +65,48 @@
goods_details = #{mallGoodsDO.goodsDetails}
WHERE id = #{mallGoodsDO.id}
</update>
<update id="updateGoodsSpec">
update goods_spec
set spec_name = #{specName},
choose_type = #{chooseType},
must = #{must},
sku_unit_id = #{skuUnitId}
where id = #{id}
</update>
<update id="updateGoodsSpecValue">
update goods_spec_values
set goods_spec_id = #{goodsSpecId},
spec_value_name = #{specValueName},
part_no =#{partNo},
spec_value_image = #{specValueImage},
show_price = #{showPrice},
stock = #{stock},
sale_price = #{salePrice},
channel_price =#{channelPrice}
where id = #{id}
</update>
<delete id="deleteMallGoodsResources">
delete
from mall_goods_resources
where mall_goods_id = #{id}
</delete>
<delete id="deleteMallGoodsSpec">
<delete id="deleteGoodsSpec">
delete
from goods_spec
where mall_goods_id = #{id}
<where>
<foreach collection="list" open="id in (" close=")" item="item" separator=",">
#{item}
</foreach>
</where>
</delete>
<delete id="deleteGoodsSpecValues">
delete
from goods_spec_values
<where>
<foreach collection="list" open="id in (" close=")" item="item" separator=",">
#{item}
</foreach>
</where>
</delete>
<select id="countMallGoodsByName" resultType="java.lang.Integer">
select count(*)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论