From be2d7ec232fbd22b5e46ec133485ea751520636d Mon Sep 17 00:00:00 2001 From: CeDo <cedoogle@gmail.com> Date: 星期四, 22 四月 2021 09:47:58 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/DifferentLongListUtil.java | 50 +++++++++++++++++++++++++ springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComShopGoodsServiceImpl.java | 24 +++++++++--- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/DifferentLongListUtil.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/DifferentLongListUtil.java new file mode 100644 index 0000000..89b00be --- /dev/null +++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/DifferentLongListUtil.java @@ -0,0 +1,50 @@ +package com.panzhihua.common.utlis; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @Author: llming + * @Description: 获取不同两个list不同元素 + */ +public class DifferentLongListUtil { + public static List<Long> getDiffrent(List<Long> list1, List<Long> list2) { + Map<Long, Integer> map = new HashMap<Long, Integer>(list1.size() + list2.size()); + List<Long> diff = new ArrayList<Long>(); + List<Long> maxList = list1; + List<Long> minList = list2; + if (list2.size() > list1.size()) { + maxList = list2; + minList = list1; + } + + for (Long Long : maxList) { + map.put(Long, 1); + } + + for (Long Long : minList) { + Integer cc = map.get(Long); + if (cc != null) { + map.put(Long, ++cc); + continue; + } + map.put(Long, 1); + } + + for (Map.Entry<Long, Integer> entry : map.entrySet()) { + if (entry.getValue() == 1) { + diff.add(entry.getKey()); + } + } + return diff; + } + + public static void main(Long[] args) { +// List<Long> Longs1 = Arrays.asList(1L, 2L, 2L, 3L, 4L); +// List<Long> Longs2 = Arrays.asList(1L, 2L, 5L, 6L, 4L); +// List<Long> diffrent = getDiffrent(Longs1, Longs2); +// System.out.println(diffrent); + } +} diff --git a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComShopGoodsServiceImpl.java b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComShopGoodsServiceImpl.java index d4978aa..71152b0 100644 --- a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComShopGoodsServiceImpl.java +++ b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/ComShopGoodsServiceImpl.java @@ -13,6 +13,7 @@ import com.panzhihua.common.model.vos.shop.AddShopGoodsAttrVO; import com.panzhihua.common.model.vos.shop.PageShopGoodsVO; import com.panzhihua.common.model.vos.shop.AddShopGoodsVO; +import com.panzhihua.common.utlis.DifferentLongListUtil; import com.panzhihua.common.utlis.StringUtils; import com.panzhihua.service_community.dao.ComShopGoodsAttrDAO; import com.panzhihua.common.model.vos.shop.ComShopGoodsAttrVO; @@ -31,6 +32,7 @@ import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * @auther lyq @@ -132,12 +134,8 @@ if (update < 1) { throw new ServiceException("修改失败"); } - addShopGoodsVO.getGoodsAttrVOList().forEach(goodsAttr -> { - ComShopGoodsAttrDO comShopGoodsAttrDO = shopGoodsAttrDAO.selectById(goodsAttr.getGoodsAttrId()); - comShopGoodsAttrDO.setPrice(goodsAttr.getAttrPrice()); - comShopGoodsAttrDO.setGoodsAttr(goodsAttr.getGoodsAttr()); - shopGoodsAttrDAO.updateById(comShopGoodsAttrDO); - }); + //修改规格表 + List<ComShopGoodsAttrDO> comShopGoodsAttrDOS = shopGoodsAttrDAO.selectList(new LambdaQueryWrapper<ComShopGoodsAttrDO>().eq(ComShopGoodsAttrDO::getGoodsId, id)); if (addShopGoodsVO.getGoodsAttrVOList().size() == 0) { shopGoodsAttrDAO.delete(new LambdaQueryWrapper<ComShopGoodsAttrDO>().eq(ComShopGoodsAttrDO::getGoodsId, id)); ComShopGoodsAttrDO comShopGoodsAttrDO = new ComShopGoodsAttrDO(); @@ -149,6 +147,20 @@ comShopGoodsAttrDO.setIsDefault(1); comShopGoodsAttrDO.setPrice(addShopGoodsVO.getPrice()); shopGoodsAttrDAO.insert(comShopGoodsAttrDO); + }else{ + List<Long> attrIds = comShopGoodsAttrDOS.stream().map(ComShopGoodsAttrDO::getId).collect(Collectors.toList()); + List<Long> voAttrIds = addShopGoodsVO.getGoodsAttrVOList().stream().map(AddShopGoodsAttrVO::getGoodsAttrId).collect(Collectors.toList()); + //需要删除的规格ID——针对编辑时删除了规格的情况 + List<Long> deleteIds = DifferentLongListUtil.getDiffrent(attrIds, voAttrIds); + if(deleteIds.size()!=0){ + shopGoodsAttrDAO.deleteBatchIds(deleteIds); + } + addShopGoodsVO.getGoodsAttrVOList().forEach(goodsAttr -> { + ComShopGoodsAttrDO comShopGoodsAttrDO = shopGoodsAttrDAO.selectById(goodsAttr.getGoodsAttrId()); + comShopGoodsAttrDO.setPrice(goodsAttr.getAttrPrice()); + comShopGoodsAttrDO.setGoodsAttr(goodsAttr.getGoodsAttr()); + shopGoodsAttrDAO.updateById(comShopGoodsAttrDO); + }); } return R.ok(); } -- Gitblit v1.7.1