licailing 5 лет назад
Родитель
Сommit
cd7028f688

+ 4 - 0
src/main/java/com/izouma/dingdong/domain/merchant/Goods.java

@@ -103,4 +103,8 @@ public class Goods extends BaseEntity implements Serializable {
      */
     @ApiModelProperty(value = "推荐", name = "recommend")
     private Boolean recommend = false;
+
+    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
+    @JoinColumn(name = "goodsId")
+    private List<GoodsSpecification> specifications;
 }

+ 22 - 2
src/main/java/com/izouma/dingdong/service/merchant/GoodsSpecificationService.java

@@ -10,6 +10,8 @@ import org.springframework.stereotype.Service;
 import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 @Service
 @AllArgsConstructor
@@ -23,10 +25,28 @@ public class GoodsSpecificationService {
         List<GoodsSpecification> all = new ArrayList<>();
         spec.forEach(s -> {
             goodsRepo.findById(s.getGoodsId()).orElseThrow(new BusinessException("商品不存在"));
-            if (s.getAmount().compareTo(BigDecimal.ZERO) < 0) {
+
+/*            if (s.getAmount().compareTo(BigDecimal.ZERO) < 0) {
                 throw new BusinessException("规格价钱不得小于零");
+            }*/
+            if (s.getId() != null && s.getId() < 0 && s.getParent() == null) {
+                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());
+
+                collect.forEach(c -> {
+                    c.setParent(save.getId());
+                    if (s.getId() != null && s.getId() < 0) {
+
+                        all.add(goodsSpecificationRepo.save(c));
+                    }
+                });
+//                }
+                all.add(save);
+            } else if (s.getParent() > 0) {
+                goodsSpecificationRepo.findById(s.getParent()).orElseThrow(new BusinessException("无分类"));
+                all.add(goodsSpecificationRepo.save(s));
             }
-            all.add(goodsSpecificationRepo.save(s));
         });
 
         return all;

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

@@ -8,6 +8,7 @@ import com.izouma.dingdong.exception.BusinessException;
 import com.izouma.dingdong.repo.merchant.GoodsRepo;
 import com.izouma.dingdong.repo.merchant.GoodsSpecificationRepo;
 import com.izouma.dingdong.service.merchant.GoodsService;
+import com.izouma.dingdong.service.merchant.GoodsSpecificationService;
 import com.izouma.dingdong.utils.SecurityUtils;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -15,6 +16,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
+import java.math.BigDecimal;
+import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
@@ -32,6 +35,9 @@ public class GoodsServiceTest {
     @Autowired
     private GoodsSpecificationRepo goodsSpecificationRepo;
 
+    @Autowired
+    private GoodsSpecificationService goodsSpecificationService;
+
     @Test
     public void testApp(){
         System.out.println(appraisalService.goodsAppraisals(214L));
@@ -51,4 +57,68 @@ public class GoodsServiceTest {
 
         System.out.println(map);
     }
+
+    @Test
+    public void goodsSpec(){
+/*        GoodsSpecification spec1 = GoodsSpecification.builder()
+                .multiple(false)
+                .name("冰量")
+                .goodsId(121L)
+                .build();
+        spec1.setId(-1L);*/
+        GoodsSpecification spec2 = GoodsSpecification.builder()
+                .amount(BigDecimal.ZERO)
+                .name("正常冰")
+                .goodsId(121L)
+                .parent(1082L)
+                .build();
+        spec2.setId(-2L);
+/*        GoodsSpecification spec3 = GoodsSpecification.builder()
+                .amount(BigDecimal.ZERO)
+                .name("少冰")
+                .goodsId(121L)
+                .parent(-1L)
+                .build();
+        spec3.setId(-3L);*/
+
+/*        GoodsSpecification spec4 = GoodsSpecification.builder()
+                .multiple(false)
+                .name("酸度")
+                .goodsId(121L)
+                .build();
+        spec4.setId(-4L);*/
+       GoodsSpecification spec5 = GoodsSpecification.builder()
+                .amount(BigDecimal.ZERO)
+                .name("微酸")
+                .goodsId(121L)
+                .parent(1089L)
+                .build();
+        spec5.setId(-5L);
+/*         GoodsSpecification spec6 = GoodsSpecification.builder()
+                .amount(BigDecimal.ZERO)
+                .name("半糖")
+                .goodsId(121L)
+                .parent(-4L)
+                .build();
+        spec6.setId(-6L);
+        GoodsSpecification spec7 = GoodsSpecification.builder()
+                .amount(BigDecimal.ZERO)
+                .name("全糖")
+                .goodsId(121L)
+                .parent(-4L)
+                .build();
+        spec7.setId(-7L);*/
+
+
+
+        List<GoodsSpecification> specifications = new ArrayList<>(2);
+//        specifications.add(spec1);
+        specifications.add(spec2);
+//        specifications.add(spec3);
+//        specifications.add(spec4);
+        specifications.add(spec5);
+//        specifications.add(spec6);
+//        specifications.add(spec7);
+        goodsSpecificationService.saveAll(specifications);
+    }
 }