licailing пре 5 година
родитељ
комит
3cb6db0c0e

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

@@ -4,10 +4,14 @@ import com.izouma.dingdong.domain.BaseEntity;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.*;
 import lombok.*;
+import org.hibernate.annotations.NotFound;
+import org.hibernate.annotations.NotFoundAction;
 
 
-import javax.persistence.Entity;
+import javax.persistence.*;
+import javax.validation.constraints.Size;
 import java.io.Serializable;
 import java.io.Serializable;
 import java.math.BigDecimal;
 import java.math.BigDecimal;
+import java.util.List;
 
 
 @EqualsAndHashCode(callSuper = true)
 @EqualsAndHashCode(callSuper = true)
 @Data
 @Data
@@ -22,8 +26,20 @@ public class GoodsSpecification extends BaseEntity implements Serializable {
     @ApiModelProperty("商品id")
     @ApiModelProperty("商品id")
     private Long goodsId;
     private Long goodsId;
 
 
+    @Size(max = 30)
+    @Column(length = 30)
     private String name;
     private String name;
 
 
     @ApiModelProperty(value = "价钱", name = "amount")
     @ApiModelProperty(value = "价钱", name = "amount")
     private BigDecimal amount;
     private BigDecimal amount;
+
+    private Long parent;
+
+    private Boolean multiple;
+
+    @OneToMany
+    @JoinColumn(name = "parent", insertable = false, updatable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
+    @NotFound(action = NotFoundAction.IGNORE)
+    @ApiModelProperty(value = "子分类",name = "children")
+    private List<GoodsSpecification> children;
 }
 }

+ 1 - 0
src/main/java/com/izouma/dingdong/domain/merchant/MerchantClassification.java

@@ -52,4 +52,5 @@ public class MerchantClassification extends BaseEntity implements Serializable {
      */
      */
     @ApiModelProperty(value = "分类类型")
     @ApiModelProperty(value = "分类类型")
     private Integer type;
     private Integer type;
+
 }
 }

+ 3 - 0
src/main/java/com/izouma/dingdong/repo/merchant/GoodsSpecificationRepo.java

@@ -11,4 +11,7 @@ public interface GoodsSpecificationRepo extends JpaRepository<GoodsSpecification
     List<GoodsSpecification> findAllByGoodsId(Long goodsId);
     List<GoodsSpecification> findAllByGoodsId(Long goodsId);
 
 
     void deleteByGoodsId(Long goodsId);
     void deleteByGoodsId(Long goodsId);
+
+    List<GoodsSpecification> findAllByGoodsIdAndParentIsNull(Long goodsId);
+
 }
 }

+ 21 - 41
src/main/java/com/izouma/dingdong/service/OrderGoodsSpecService.java

@@ -1,24 +1,22 @@
 package com.izouma.dingdong.service;
 package com.izouma.dingdong.service;
 
 
-import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.core.util.StrUtil;
-import com.alibaba.fastjson.JSONObject;
 import com.izouma.dingdong.converter.LongArrayConverter;
 import com.izouma.dingdong.converter.LongArrayConverter;
 import com.izouma.dingdong.domain.OrderGoodsSpec;
 import com.izouma.dingdong.domain.OrderGoodsSpec;
 import com.izouma.dingdong.domain.merchant.Goods;
 import com.izouma.dingdong.domain.merchant.Goods;
 import com.izouma.dingdong.domain.merchant.GoodsSpecification;
 import com.izouma.dingdong.domain.merchant.GoodsSpecification;
-import com.izouma.dingdong.dto.OrderGoodsSpecDTO;
 import com.izouma.dingdong.enums.ApplyStatus;
 import com.izouma.dingdong.enums.ApplyStatus;
 import com.izouma.dingdong.exception.BusinessException;
 import com.izouma.dingdong.exception.BusinessException;
 import com.izouma.dingdong.repo.OrderGoodsSpecRepo;
 import com.izouma.dingdong.repo.OrderGoodsSpecRepo;
 import com.izouma.dingdong.repo.merchant.GoodsRepo;
 import com.izouma.dingdong.repo.merchant.GoodsRepo;
 import com.izouma.dingdong.repo.merchant.GoodsSpecificationRepo;
 import com.izouma.dingdong.repo.merchant.GoodsSpecificationRepo;
-import com.izouma.dingdong.service.user.ShoppingCartService;
 import lombok.AllArgsConstructor;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
 import java.math.BigDecimal;
 import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
 import java.util.List;
 
 
 @Service
 @Service
@@ -28,44 +26,7 @@ public class OrderGoodsSpecService {
     private OrderGoodsSpecRepo orderGoodsSpecRepo;
     private OrderGoodsSpecRepo orderGoodsSpecRepo;
     private GoodsSpecificationRepo goodsSpecificationRepo;
     private GoodsSpecificationRepo goodsSpecificationRepo;
     private GoodsRepo goodsRepo;
     private GoodsRepo goodsRepo;
-    private ShoppingCartService shoppingCartService;
-
-    /*
-    添加规格
-     */
-    public OrderGoodsSpec add(OrderGoodsSpecDTO orderGoodsSpecDTO) {
-        OrderGoodsSpec goodsSpec = null;
-        if (orderGoodsSpecDTO.getId()!=null){
-            goodsSpec = orderGoodsSpecRepo.findById(orderGoodsSpecDTO.getId()).orElseThrow(new BusinessException("无记录"));
-        }
-
-        String spec = null;
-        //String ids = null;
-        Goods goods = goodsRepo.findById(orderGoodsSpecDTO.getGoodsId()).orElseThrow(new BusinessException("无商品"));
-
-        //判断商品是否通过审核
-        if (!ApplyStatus.PASS.equals(goods.getStatus())) {
-            throw new BusinessException("商品审核中或未通过");
-        }
-
-        BigDecimal total = goods.getAmount();
-        BigDecimal real = goods.getDiscountAmount();
-
-/*
-        if (CollUtil.isNotEmpty(goodsSpecifications)) {
-            for (GoodsSpecification g : goodsSpecifications) {
-                total = total.add(g.getAmount());
-                real = real.add(g.getAmount());
-
 
 
-            }
-            spec = JSONObject.toJSONString(goodsSpecifications);
-*/
-
-//        }
-        return null;
-
-    }
 
 
     public OrderGoodsSpec add(Long goodsId, String goodsSpecificationIds, Integer num) {
     public OrderGoodsSpec add(Long goodsId, String goodsSpecificationIds, Integer num) {
         Goods goods = goodsRepo.findById(goodsId).orElseThrow(new BusinessException("无商品"));
         Goods goods = goodsRepo.findById(goodsId).orElseThrow(new BusinessException("无商品"));
@@ -82,8 +43,23 @@ public class OrderGoodsSpecService {
             LongArrayConverter longArrayConverter = new LongArrayConverter();
             LongArrayConverter longArrayConverter = new LongArrayConverter();
             List<Long> longs = longArrayConverter.convertToEntityAttribute(goodsSpecificationIds);
             List<Long> longs = longArrayConverter.convertToEntityAttribute(goodsSpecificationIds);
 
 
+            List<Long> parents = new ArrayList<>();
             for (Long l : longs) {
             for (Long l : longs) {
                 GoodsSpecification spec = goodsSpecificationRepo.findById(l).orElseThrow(new BusinessException("无规格"));
                 GoodsSpecification spec = goodsSpecificationRepo.findById(l).orElseThrow(new BusinessException("无规格"));
+
+                if (ObjectUtil.isEmpty(spec.getParent())) {
+                    throw new BusinessException("选择规格分类下的规格商品");
+                } else {
+                    GoodsSpecification parentSpec = goodsSpecificationRepo.findById(spec.getParent()).orElseThrow(new BusinessException("无规格分类"));
+                    if (!parentSpec.getMultiple()) {
+                        parents.add(spec.getParent());
+                    }
+                }
+
+                if (ObjectUtil.isEmpty(spec.getAmount())) {
+                    spec.setAmount(BigDecimal.ZERO);
+                }
+
                 //价钱
                 //价钱
                 total = total.add(spec.getAmount());
                 total = total.add(spec.getAmount());
                 real = real.add(spec.getAmount());
                 real = real.add(spec.getAmount());
@@ -95,6 +71,10 @@ public class OrderGoodsSpecService {
                     specName = new StringBuilder(spec.getName());
                     specName = new StringBuilder(spec.getName());
                 }
                 }
             }
             }
+
+            if (parents.size() != new HashSet<>(parents).size()) {
+                throw new BusinessException("存在不可多选规格");
+            }
         }
         }
         if (num == null || num <= 0) {
         if (num == null || num <= 0) {
             num = 1;
             num = 1;

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

@@ -11,4 +11,6 @@ public class GoodsSpecificationService {
 
 
     private GoodsSpecificationRepo goodsSpecificationRepo;
     private GoodsSpecificationRepo goodsSpecificationRepo;
 
 
+
+
 }
 }

+ 6 - 0
src/main/java/com/izouma/dingdong/web/merchant/GoodsSpecificationController.java

@@ -74,5 +74,11 @@ public class GoodsSpecificationController extends BaseController {
     public List<GoodsSpecification> byGoodsId(Long goodsId) {
     public List<GoodsSpecification> byGoodsId(Long goodsId) {
         return goodsSpecificationRepo.findAllByGoodsId(goodsId);
         return goodsSpecificationRepo.findAllByGoodsId(goodsId);
     }
     }
+
+    @GetMapping("/parent")
+    @ApiOperation("按商品ID查出规格分类")
+    public List<GoodsSpecification> parent(Long goodsId){
+        return goodsSpecificationRepo.findAllByGoodsIdAndParentIsNull(goodsId);
+    }
 }
 }
 
 

+ 1 - 1
src/test/java/com/izouma/dingdong/contorller/OrderInfoController.java → src/test/java/com/izouma/dingdong/contorller/OrderInfoControllerTest.java

@@ -22,7 +22,7 @@ import java.util.Set;
 
 
 @RunWith(SpringRunner.class)
 @RunWith(SpringRunner.class)
 @SpringBootTest
 @SpringBootTest
-public class OrderInfoController {
+public class OrderInfoControllerTest {
     @Autowired
     @Autowired
     private AuthenticationController auth;
     private AuthenticationController auth;
     @Autowired
     @Autowired

+ 55 - 0
src/test/java/com/izouma/dingdong/contorller/SpecControllerTest.java

@@ -0,0 +1,55 @@
+package com.izouma.dingdong.contorller;
+
+import com.izouma.dingdong.domain.OrderGoodsSpec;
+import com.izouma.dingdong.domain.merchant.GoodsSpecification;
+import com.izouma.dingdong.repo.OrderGoodsSpecRepo;
+import com.izouma.dingdong.repo.merchant.GoodsSpecificationRepo;
+import com.izouma.dingdong.service.OrderGoodsSpecService;
+import com.izouma.dingdong.web.merchant.GoodsSpecificationController;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+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;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class SpecControllerTest {
+    @Autowired
+    private GoodsSpecificationController controller;
+
+    @Autowired
+    private GoodsSpecificationRepo repo;
+
+    @Autowired
+    private OrderGoodsSpecService orderGoodsSpecService;
+
+//    @Autowired
+//    private OrderGoodsSpecRepo orderGoodsSpecRepo;
+
+    @Test
+    public void test(){
+        GoodsSpecification build = GoodsSpecification.builder()
+                .goodsId(698L)
+                .name("全糖")
+                .parent(703L)
+                //.amount(BigDecimal.valueOf(12))
+                //.multiple(false)
+                .build();
+        System.out.println(controller.save(build));
+    }
+
+    @Test
+    public void testMy(){
+        System.out.println(repo.findAllByGoodsIdAndParentIsNull(698L));
+    }
+
+    @Test
+    public void testOrder(){
+        //System.out.println(orderGoodsSpecRepo.findById(716L).orElse(null));
+         System.out.println(orderGoodsSpecService.add(698L, "701,705", 1));
+    }
+
+}