licailing 5 years ago
parent
commit
c9f7afbc41

+ 2 - 1
src/main/java/com/izouma/dingdong/domain/merchant/GoodsSpecification.java

@@ -42,9 +42,10 @@ public class GoodsSpecification extends BaseEntity implements Serializable {
     @OneToMany
     @JoinColumn(name = "parent", insertable = false, updatable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
     @NotFound(action = NotFoundAction.IGNORE)
-    @ApiModelProperty(value = "子分类",name = "children")
+    @ApiModelProperty(value = "子分类", name = "children")
     private List<GoodsSpecification> children;
 
+    @Builder.Default
     @Column(nullable = false)
     private Boolean enabled = true;
 }

+ 10 - 4
src/main/java/com/izouma/dingdong/service/merchant/GoodsSpecificationService.java

@@ -4,10 +4,10 @@ import com.izouma.dingdong.domain.merchant.GoodsSpecification;
 import com.izouma.dingdong.exception.BusinessException;
 import com.izouma.dingdong.repo.merchant.GoodsRepo;
 import com.izouma.dingdong.repo.merchant.GoodsSpecificationRepo;
+import com.izouma.dingdong.utils.ObjUtils;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 
-import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
@@ -24,12 +24,11 @@ public class GoodsSpecificationService {
     public List<GoodsSpecification> saveAll(List<GoodsSpecification> spec) {
         List<GoodsSpecification> all = new ArrayList<>();
         spec.forEach(s -> {
-            goodsRepo.findById(s.getGoodsId()).orElseThrow(new BusinessException("商品不存在"));
-
 /*            if (s.getAmount().compareTo(BigDecimal.ZERO) < 0) {
                 throw new BusinessException("规格价钱不得小于零");
             }*/
             if (s.getId() != null && s.getId() < 0 && s.getParent() == null) {
+                goodsRepo.findById(s.getGoodsId()).orElseThrow(new BusinessException("商品不存在"));
                 GoodsSpecification save = goodsSpecificationRepo.save(s);
 //                if (s.getParent() == null) {
                 Set<GoodsSpecification> collect = spec.stream().filter(g -> g.getParent() != null && g.getParent().equals(s.getId())).collect(Collectors.toSet());
@@ -43,9 +42,16 @@ public class GoodsSpecificationService {
                 });
 //                }
                 all.add(save);
-            } else if (s.getParent() > 0) {
+            } else if ((s.getId() == null || s.getId() < 0) && s.getParent() > 0) {
+                goodsRepo.findById(s.getGoodsId()).orElseThrow(new BusinessException("商品不存在"));
                 goodsSpecificationRepo.findById(s.getParent()).orElseThrow(new BusinessException("无分类"));
                 all.add(goodsSpecificationRepo.save(s));
+            } else if (s.getId() > 0) {
+                //修改
+                GoodsSpecification goodsSpecification = goodsSpecificationRepo.findById(s.getId()).orElseThrow(new BusinessException("不存在"));
+                ObjUtils.merge(goodsSpecification, s);
+                goodsRepo.findById(goodsSpecification.getGoodsId()).orElseThrow(new BusinessException("商品不存在"));
+                all.add(goodsSpecificationRepo.save(goodsSpecification));
             }
         });
 

+ 9 - 5
src/main/java/com/izouma/dingdong/web/merchant/GoodsSpecificationController.java

@@ -1,6 +1,8 @@
 package com.izouma.dingdong.web.merchant;
 
-import cn.hutool.core.util.ObjectUtil;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.izouma.dingdong.repo.merchant.GoodsRepo;
 import com.izouma.dingdong.web.BaseController;
 import com.izouma.dingdong.domain.merchant.GoodsSpecification;
@@ -14,12 +16,12 @@ import com.izouma.dingdong.utils.excel.ExcelUtils;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
 import org.springframework.data.domain.Page;
-import org.springframework.security.access.prepost.PreAuthorize;
+
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
-import java.sql.Wrapper;
+
 import java.util.List;
 
 @RestController
@@ -85,8 +87,10 @@ public class GoodsSpecificationController extends BaseController {
 
     @PostMapping("/saveAll")
     @ApiOperation("批量保存规格")
-    public List<GoodsSpecification> saveAll(List<GoodsSpecification> spec) {
-        return goodsSpecificationService.saveAll(spec);
+    public List<GoodsSpecification> saveAll(String spec) {
+        List<GoodsSpecification> specifications = JSONObject.parseArray(spec, GoodsSpecification.class);
+
+        return goodsSpecificationService.saveAll(specifications);
     }
 }
 

+ 10 - 10
src/test/java/com/izouma/dingdong/service/GoodsServiceTest.java

@@ -67,12 +67,12 @@ public class GoodsServiceTest {
                 .build();
         spec1.setId(-1L);*/
         GoodsSpecification spec2 = GoodsSpecification.builder()
-                .amount(BigDecimal.ZERO)
-                .name("正常冰")
-                .goodsId(121L)
-                .parent(1082L)
+                //.amount(BigDecimal.ZERO)
+                .name("超级酸")
+                //.goodsId(121L)
+                //.parent(1089L)
                 .build();
-        spec2.setId(-2L);
+        spec2.setId(1097L);
 /*        GoodsSpecification spec3 = GoodsSpecification.builder()
                 .amount(BigDecimal.ZERO)
                 .name("少冰")
@@ -88,12 +88,12 @@ public class GoodsServiceTest {
                 .build();
         spec4.setId(-4L);*/
        GoodsSpecification spec5 = GoodsSpecification.builder()
-                .amount(BigDecimal.ZERO)
-                .name("微酸")
-                .goodsId(121L)
-                .parent(1089L)
+                .amount(BigDecimal.ONE)
+               // .name("微酸")
+                //.goodsId(121L)
+                //.parent(1089L)
                 .build();
-        spec5.setId(-5L);
+        spec5.setId(1098L);
 /*         GoodsSpecification spec6 = GoodsSpecification.builder()
                 .amount(BigDecimal.ZERO)
                 .name("半糖")