From 7e6bbd05a75b07cb0717812d0b51aad128361012 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Thu, 19 May 2022 18:43:42 +0800
Subject: [PATCH] fix some problem

---
 src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsService.java |  181 ++++++++++++++++++++++++++-------------------
 1 files changed, 105 insertions(+), 76 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsService.java b/src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsService.java
index d742db5..5f746d0 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsService.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsService.java
@@ -8,6 +8,7 @@
 import cc.mrbird.febs.mall.mapper.*;
 import cc.mrbird.febs.mall.service.IAdminMallGoodsService;
 import cc.mrbird.febs.mall.vo.*;
+import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
@@ -72,34 +73,36 @@
                 return new FebsResponse().fail().message("商品分类不能为空");
             }
 
-            List<AddMallGoodsSkuDto> addSkus = addMallGoodsDto.getAddMallGoodsSkuDtos();
-            if (CollUtil.isEmpty(addSkus)) {
-                return new FebsResponse().fail().message("商品规格不能为空");
-            }
-            for (AddMallGoodsSkuDto addSku : addSkus) {
-                String skuName = addSku.getSkuName();
-                if (StrUtil.isEmpty(skuName)) {
-                    return new FebsResponse().fail().message("商品规格名称不能为空");
+            if (addMallGoodsDto.getIsSku() == 1) {
+                List<AddMallGoodsSkuDto> addSkus = addMallGoodsDto.getAddMallGoodsSkuDtos();
+                if (CollUtil.isEmpty(addSkus)) {
+                    return new FebsResponse().fail().message("商品规格不能为空");
                 }
-                String skuImage = addSku.getSkuImage();
-                if (StrUtil.isEmpty(skuImage)) {
-                    return new FebsResponse().fail().message("商品规格图片不能为空");
-                }
-                Integer skuVolume = addSku.getSkuVolume() == null ? 0 : addSku.getSkuVolume();
-                if (skuVolume < 0) {
-                    return new FebsResponse().fail().message("商品规格销售数量不能小于0");
-                }
-                Integer stock = addSku.getStock() == null ? 0 : addSku.getStock();
-                if (stock < 0) {
-                    return new FebsResponse().fail().message("商品规格库存必须大于0");
-                }
-                String originalPrice = addSku.getOriginalPrice();
-                if (StrUtil.isEmpty(originalPrice)) {
-                    return new FebsResponse().fail().message("商品规格原价不能为空");
-                }
-                String presentPrice = addSku.getPresentPrice();
-                if (StrUtil.isEmpty(presentPrice)) {
-                    return new FebsResponse().fail().message("商品规格现价不能为空");
+                for (AddMallGoodsSkuDto addSku : addSkus) {
+                    String skuName = addSku.getSkuName();
+                    if (StrUtil.isEmpty(skuName)) {
+                        return new FebsResponse().fail().message("商品规格名称不能为空");
+                    }
+                    String skuImage = addSku.getSkuImage();
+                    if (StrUtil.isEmpty(skuImage)) {
+                        return new FebsResponse().fail().message("商品规格图片不能为空");
+                    }
+                    Integer skuVolume = addSku.getSkuVolume() == null ? 0 : addSku.getSkuVolume();
+                    if (skuVolume < 0) {
+                        return new FebsResponse().fail().message("商品规格销售数量不能小于0");
+                    }
+                    Integer stock = addSku.getStock() == null ? 0 : addSku.getStock();
+                    if (stock < 0) {
+                        return new FebsResponse().fail().message("商品规格库存必须大于0");
+                    }
+                    String originalPrice = addSku.getOriginalPrice();
+                    if (StrUtil.isEmpty(originalPrice)) {
+                        return new FebsResponse().fail().message("商品规格原价不能为空");
+                    }
+                    String presentPrice = addSku.getPresentPrice();
+                    if (StrUtil.isEmpty(presentPrice)) {
+                        return new FebsResponse().fail().message("商品规格现价不能为空");
+                    }
                 }
             }
         }
@@ -145,7 +148,6 @@
                 mallGoodsStyleMapper.insert(mallGoodsStyle);
             }
         }
-
 
         if (CollUtil.isNotEmpty(addMallGoodsSkuDtos)) {
             for (AddMallGoodsSkuDto addMallGoodsSkuDto : addMallGoodsSkuDtos) {
@@ -233,6 +235,17 @@
     }
 
     @Override
+    public MallGoods selectGoodsById(long id) {
+        MallGoods mallGoods = mallGoodsMapper.selectById(id);
+
+        List<MallGoodsSku> skus = mallGoodsSkuMapper.selectSkuByGoodsId(mallGoods.getId());
+        List<String> thumbs = mallGoodsImagesMapper.selectByGoodId(mallGoods.getId());
+        mallGoods.setImages(thumbs);
+        mallGoods.setSkus(skus);
+        return mallGoods;
+    }
+
+    @Override
     @Transactional
     public FebsResponse updateMallGoods(MallGoodsUpdateDto mallGoodsUpdateDto) {
         String goodsName = mallGoodsUpdateDto.getGoodsName();
@@ -251,58 +264,54 @@
         if (mallGoodsByGoodsNo > 0) {
             return new FebsResponse().fail().message("商品编号不能重复");
         }
-        Long categoryId = mallGoodsUpdateDto.getCategoryId();
-        if (ObjectUtil.isEmpty(categoryId)) {
-            return new FebsResponse().fail().message("商品分类不能为空");
-        }
-        List<MailGoodsSkuDto> addSkus = mallGoodsUpdateDto.getMailGoodsSkuDto();
-        if (CollUtil.isEmpty(addSkus)) {
-            return new FebsResponse().fail().message("商品规格不能为空");
-        }
-        for (MailGoodsSkuDto addSku : addSkus) {
-            String skuName = addSku.getSkuName();
-            if (StrUtil.isEmpty(skuName)) {
-                return new FebsResponse().fail().message("商品规格名称不能为空");
-            }
-            String skuImage = addSku.getSkuImage();
-            if (StrUtil.isEmpty(skuImage)) {
-                return new FebsResponse().fail().message("商品规格图片不能为空");
-            }
-            Integer skuVolume = addSku.getSkuVolume() == null ? 0 : addSku.getSkuVolume();
-            if (skuVolume < 0) {
-                return new FebsResponse().fail().message("商品规格销售数量不能小于0");
-            }
-            Integer stock = addSku.getStock() == null ? 0 : addSku.getStock();
-            if (stock < 0) {
-                return new FebsResponse().fail().message("商品规格库存必须大于0");
-            }
-            BigDecimal originalPrice = addSku.getOriginalPrice();
-            if (originalPrice.compareTo(BigDecimal.ZERO) <= 0) {
-                return new FebsResponse().fail().message("商品规格原价不能小于0");
-            }
-            BigDecimal presentPrice = addSku.getPresentPrice();
-            if (presentPrice.compareTo(BigDecimal.ZERO) <= 0) {
-                return new FebsResponse().fail().message("商品规格现价不能小于0");
+
+        if (mallGoodsUpdateDto.getGoodsType() != 2) {
+            Long categoryId = mallGoodsUpdateDto.getCategoryId();
+            if (ObjectUtil.isEmpty(categoryId)) {
+                return new FebsResponse().fail().message("商品分类不能为空");
             }
 
+            if (mallGoodsUpdateDto.getIsSku() == 1) {
+                List<MailGoodsSkuDto> addSkus = mallGoodsUpdateDto.getMailGoodsSkuDto();
+                if (CollUtil.isEmpty(addSkus)) {
+                    return new FebsResponse().fail().message("商品规格不能为空");
+                }
+                for (MailGoodsSkuDto addSku : addSkus) {
+                    String skuName = addSku.getSkuName();
+                    if (StrUtil.isEmpty(skuName)) {
+                        return new FebsResponse().fail().message("商品规格名称不能为空");
+                    }
+                    String skuImage = addSku.getSkuImage();
+                    if (StrUtil.isEmpty(skuImage)) {
+                        return new FebsResponse().fail().message("商品规格图片不能为空");
+                    }
+                    Integer skuVolume = addSku.getSkuVolume() == null ? 0 : addSku.getSkuVolume();
+                    if (skuVolume < 0) {
+                        return new FebsResponse().fail().message("商品规格销售数量不能小于0");
+                    }
+                    Integer stock = addSku.getStock() == null ? 0 : addSku.getStock();
+                    if (stock < 0) {
+                        return new FebsResponse().fail().message("商品规格库存必须大于0");
+                    }
+                    BigDecimal originalPrice = addSku.getOriginalPrice();
+                    if (originalPrice.compareTo(BigDecimal.ZERO) <= 0) {
+                        return new FebsResponse().fail().message("商品规格原价不能小于0");
+                    }
+                    BigDecimal presentPrice = addSku.getPresentPrice();
+                    if (presentPrice.compareTo(BigDecimal.ZERO) <= 0) {
+                        return new FebsResponse().fail().message("商品规格现价不能小于0");
+                    }
+
+                }
+            }
         }
+
         //新增商品
         MallGoods mallGoods = mallGoodsMapper.selectById(mallGoodsUpdateDto.getId());
-        mallGoods.setGoodsNo(mallGoodsUpdateDto.getGoodsNo());
-        mallGoods.setGoodsName(mallGoodsUpdateDto.getGoodsName());
-        mallGoods.setGoodsIntrodution(mallGoodsUpdateDto.getGoodsIntrodution());
-        mallGoods.setUnit(mallGoodsUpdateDto.getUnit());
-        mallGoods.setThumb(mallGoodsUpdateDto.getThumb());
-        mallGoods.setGoodsDetails(mallGoodsUpdateDto.getGoodsDetails());
-        mallGoods.setOriginalPrice(mallGoodsUpdateDto.getOriginalPrice());
-        mallGoods.setCategoryId(mallGoodsUpdateDto.getCategoryId());
-        mallGoods.setPresentPrice(mallGoodsUpdateDto.getPresentPrice());
-        mallGoods.setIsHot(mallGoodsUpdateDto.getIsHot());
-//        mallGoods.setIsSale(MallGoods.ISSALE_STATUS_DISABLED);
+        BeanUtil.copyProperties(mallGoodsUpdateDto, mallGoods);
         mallGoodsMapper.updateById(mallGoods);
 
         mallGoodsImagesMapper.deleteByGoodsId(mallGoodsUpdateDto.getId());
-
         String thumbs = mallGoodsUpdateDto.getThumbs();
         if (StrUtil.isNotEmpty(thumbs)) {
             List<String> imgs = StrUtil.splitTrim(thumbs, ",");
@@ -319,15 +328,33 @@
             }
         }
 
+        if (mallGoodsUpdateDto.getGoodsType() == 2) {
+            return new FebsResponse().success().message("编辑成功");
+        }
+
+        // 删除已存在sku
+        List<Long> delSkuIds = mallGoodsUpdateDto.getDelSkuId();
+        if (CollUtil.isNotEmpty(delSkuIds)) {
+            mallGoodsSkuMapper.delSkuByIds(delSkuIds);
+
+            // 如果该样式下sku全删除,则删除该样式
+            List<MallGoodsStyle> styles = mallGoodsStyleMapper.selectByGoodsId(mallGoods.getId());
+            for (MallGoodsStyle style : styles) {
+                if (CollUtil.isEmpty(style.getSkus())) {
+                    mallGoodsStyleMapper.deleteById(style.getId());
+                }
+            }
+        }
+
         List<MailGoodsSkuDto> mailGoodsSkuDto = mallGoodsUpdateDto.getMailGoodsSkuDto();
         Set<String> styles = new HashSet<>();
         if (CollUtil.isNotEmpty(mailGoodsSkuDto)) {
             for (MailGoodsSkuDto addStyleDto : mailGoodsSkuDto) {
-                if (ObjectUtil.isNotEmpty(addStyleDto.getDelLog())) {
-                    mallGoodsSkuMapper.deleteById(addStyleDto.getId());
+//                if (ObjectUtil.isNotEmpty(addStyleDto.getDelLog())) {
+//                    mallGoodsSkuMapper.deleteById(addStyleDto.getId());
+//                    mallShoppingCartMapper.deleteByGoodsIdAndSkuId(addStyleDto.getId(), addStyleDto.getGoodsId());
+//                }
 
-                    mallShoppingCartMapper.deleteByGoodsIdAndSkuId(addStyleDto.getId(), addStyleDto.getGoodsId());
-                }
                 if (ObjectUtil.isEmpty(addStyleDto.getStyleId())) {
                     MallGoodsStyle mallGoodsStyleSku = mallGoodsStyleMapper.selectByStyleName(addStyleDto.getStyleName(), mallGoods.getId());
                     if (ObjectUtil.isEmpty(mallGoodsStyleSku)) {
@@ -359,6 +386,7 @@
                         mallGoodsSku.setPresentPrice(addMallGoodsSkuDto.getPresentPrice());
                         mallGoodsSku.setStyleId(addMallGoodsSkuDto.getStyleId());
                         mallGoodsSku.setGoodsId(mallGoods.getId());
+                        mallGoodsSku.setCostPrice(addMallGoodsSkuDto.getCostPrice());
                         mallGoodsSkuMapper.updateById(mallGoodsSku);
                     } else {
                         //新增商品规格
@@ -371,6 +399,7 @@
                         mallGoodsSku.setOriginalPrice(addMallGoodsSkuDto.getOriginalPrice());
                         mallGoodsSku.setPresentPrice(addMallGoodsSkuDto.getPresentPrice());
                         mallGoodsSku.setStyleId(mallGoodsStyleSku.getId());
+                        mallGoodsSku.setCostPrice(addMallGoodsSkuDto.getCostPrice());
                         mallGoodsSku.setGoodsId(mallGoods.getId());
                         mallGoodsSkuMapper.insert(mallGoodsSku);
                     }

--
Gitblit v1.9.1