Ver código fonte

购物车加购 以及 评价的排序显示

licailing 5 anos atrás
pai
commit
894ac81f94
19 arquivos alterados com 380 adições e 59 exclusões
  1. 2 2
      src/main/java/com/izouma/dingdong/domain/OrderGoodsSpec.java
  2. 1 0
      src/main/java/com/izouma/dingdong/domain/user/ShoppingCart.java
  3. 15 0
      src/main/java/com/izouma/dingdong/enums/AppraisalSort.java
  4. 5 0
      src/main/java/com/izouma/dingdong/repo/AppraisalRepo.java
  5. 8 0
      src/main/java/com/izouma/dingdong/repo/OrderGoodsSpecRepo.java
  6. 5 0
      src/main/java/com/izouma/dingdong/repo/OrderInfoRepo.java
  7. 2 0
      src/main/java/com/izouma/dingdong/repo/merchant/FullReductionRepo.java
  8. 4 0
      src/main/java/com/izouma/dingdong/repo/merchant/GoodsSpecificationRepo.java
  9. 5 0
      src/main/java/com/izouma/dingdong/repo/merchant/MerchantClassificationRepo.java
  10. 52 2
      src/main/java/com/izouma/dingdong/service/AppraisalService.java
  11. 3 3
      src/main/java/com/izouma/dingdong/service/OrderGoodsSpecService.java
  12. 8 4
      src/main/java/com/izouma/dingdong/service/merchant/GoodsSpecificationService.java
  13. 27 13
      src/main/java/com/izouma/dingdong/service/merchant/MerchantSettingsService.java
  14. 184 6
      src/main/java/com/izouma/dingdong/service/user/ShoppingCartService.java
  15. 11 4
      src/main/java/com/izouma/dingdong/web/AppraisalController.java
  16. 2 3
      src/main/java/com/izouma/dingdong/web/merchant/MerchantController.java
  17. 13 4
      src/main/java/com/izouma/dingdong/web/user/ShoppingCartController.java
  18. 15 15
      src/test/java/com/izouma/dingdong/service/GoodsServiceTest.java
  19. 18 3
      src/test/java/com/izouma/dingdong/service/ShoppingCartServiceTest.java

+ 2 - 2
src/main/java/com/izouma/dingdong/domain/OrderGoodsSpec.java

@@ -39,8 +39,8 @@ public class OrderGoodsSpec extends BaseEntity implements Serializable {
 
     // private String specificationId;
 
-//    @Convert(converter = LongArrayConverter.class)
-//    private List<Long> specificationId;
+    @Convert(converter = LongArrayConverter.class)
+    private List<Long> specificationId;
 
     @ApiModelProperty(value = "规格", name = "specification")
     private String specification;

+ 1 - 0
src/main/java/com/izouma/dingdong/domain/user/ShoppingCart.java

@@ -1,5 +1,6 @@
 package com.izouma.dingdong.domain.user;
 
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
 import com.izouma.dingdong.domain.BaseEntity;
 import com.izouma.dingdong.domain.OrderGoodsSpec;
 import com.izouma.dingdong.domain.User;

+ 15 - 0
src/main/java/com/izouma/dingdong/enums/AppraisalSort.java

@@ -0,0 +1,15 @@
+package com.izouma.dingdong.enums;
+
+public enum AppraisalSort {
+    //全部,按照时间顺序显示
+    ALL,
+    //最新,按时间
+    LATEST,
+    //好评
+    PRAISE,
+    //差评
+    BAD_REVIEW,
+    //有图
+    HAVE_PIC,
+    ;
+}

+ 5 - 0
src/main/java/com/izouma/dingdong/repo/AppraisalRepo.java

@@ -23,5 +23,10 @@ public interface AppraisalRepo extends JpaRepository<Appraisal, Long>, JpaSpecif
     @Transactional
     void deleteById(Long id);
 
+    @Query("update Appraisal t set t.enabled = false where t.merchantId = ?1")
+    @Modifying
+    @Transactional
+    void deleteAllByMerchantId(Long merchantId);
+
 
 }

+ 8 - 0
src/main/java/com/izouma/dingdong/repo/OrderGoodsSpecRepo.java

@@ -3,7 +3,10 @@ package com.izouma.dingdong.repo;
 import com.izouma.dingdong.domain.OrderGoodsSpec;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
 
+import javax.transaction.Transactional;
 import java.util.List;
 
 public interface OrderGoodsSpecRepo extends JpaRepository<OrderGoodsSpec, Long>, JpaSpecificationExecutor<OrderGoodsSpec> {
@@ -14,4 +17,9 @@ public interface OrderGoodsSpecRepo extends JpaRepository<OrderGoodsSpec, Long>,
 
     List<OrderGoodsSpec> findAllByShoppingCartId(Long shoppingCardId);
 
+    @Query("update OrderGoodsSpec t set t.enabled = false where t.id = ?1")
+    @Modifying
+    @Transactional
+    void deleteById(Long id);
+
 }

+ 5 - 0
src/main/java/com/izouma/dingdong/repo/OrderInfoRepo.java

@@ -31,4 +31,9 @@ public interface OrderInfoRepo extends JpaRepository<OrderInfo, Long>, JpaSpecif
 
     @Query("select sum (t.realAmount) from OrderInfo t where t.userId = ?1")
     BigDecimal sumRealAmountByUserId(Long userId);
+
+    @Query("update OrderInfo t set t.enabled = false where t.merchantId = ?1")
+    @Modifying
+    @Transactional
+    void deleteAllByMerchantId(Long merchantId);
 }

+ 2 - 0
src/main/java/com/izouma/dingdong/repo/merchant/FullReductionRepo.java

@@ -13,4 +13,6 @@ public interface FullReductionRepo extends JpaRepository<FullReduction, Long>, J
 
     //查找商户下的所有满减
 //    List<FullReduction> findAllByUserId(Long userId);
+
+
 }

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

@@ -22,4 +22,8 @@ public interface GoodsSpecificationRepo extends JpaRepository<GoodsSpecification
     @Transactional
     void deleteById(Long id);
 
+    @Query("update GoodsSpecification t set t.enabled = false where t.goodsId = ?1")
+    @Modifying
+    @Transactional
+    void deleteAllByGoodsId(Long goodsId);
 }

+ 5 - 0
src/main/java/com/izouma/dingdong/repo/merchant/MerchantClassificationRepo.java

@@ -25,4 +25,9 @@ public interface MerchantClassificationRepo extends JpaRepository<MerchantClassi
     @Transactional
     void deleteById(Long id);
 
+    @Query("update MerchantClassification t set t.enabled = false where t.merchantId = ?1")
+    @Modifying
+    @Transactional
+    void deleteAllByMerchantId(Long merchantId);
+
 }

+ 52 - 2
src/main/java/com/izouma/dingdong/service/AppraisalService.java

@@ -2,12 +2,14 @@ package com.izouma.dingdong.service;
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
 import com.izouma.dingdong.domain.Appraisal;
 import com.izouma.dingdong.domain.OrderGoodsSpec;
 import com.izouma.dingdong.domain.OrderInfo;
 import com.izouma.dingdong.domain.User;
 import com.izouma.dingdong.domain.merchant.Goods;
 import com.izouma.dingdong.dto.AppraisalMerDTO;
+import com.izouma.dingdong.enums.AppraisalSort;
 import com.izouma.dingdong.enums.OrderStatus;
 import com.izouma.dingdong.exception.BusinessException;
 import com.izouma.dingdong.repo.AppraisalRepo;
@@ -15,10 +17,13 @@ import com.izouma.dingdong.repo.OrderGoodsSpecRepo;
 import com.izouma.dingdong.repo.OrderInfoRepo;
 
 import com.izouma.dingdong.service.merchant.SalesService;
+import com.izouma.dingdong.utils.SecurityUtils;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 
 import javax.transaction.Transactional;
+import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -49,7 +54,7 @@ public class AppraisalService {
             appraisal.setRiderId(order.getRiderId());
             appraisal.setLikes(0);
 
-             appraisal = appraisalRepo.save(appraisal);
+            appraisal = appraisalRepo.save(appraisal);
             //更新销量数据
             salesService.addLike(appraisal);
             return appraisal;
@@ -80,7 +85,12 @@ public class AppraisalService {
         return appraisals;
     }
 
-
+    /**
+     * 转商家显示dto
+     *
+     * @param appraisal 评价
+     * @return 商家需要信息的评价dto
+     */
     public AppraisalMerDTO toDTO(Appraisal appraisal) {
         OrderInfo order = orderInfoRepo.findById(appraisal.getOrderInfoId()).orElseThrow(new BusinessException("无订单"));
         List<Goods> collect = order.getOrderGoodsSpecs().stream().map(OrderGoodsSpec::getGoods).collect(Collectors.toList());
@@ -99,5 +109,45 @@ public class AppraisalService {
                 .build();
     }
 
+    /**
+     * 商户评价排序
+     *
+     * @param merchantId    商户id
+     * @param appraisalSort 评价规则
+     * @return 商户所有评价
+     */
+    public List<Appraisal> appraisalsSort(Long merchantId, AppraisalSort appraisalSort) {
+        List<Appraisal> appraisals = appraisalRepo.findAllByMerchantId(merchantId);
+        //默认时间排序
+        appraisals.sort(Comparator.comparing(Appraisal::getAppraiseTime));
+
+        List<Appraisal> good = appraisals.stream().filter(Appraisal::getGoodsLike).collect(Collectors.toList());
+        List<Appraisal> bad = appraisals.stream().filter(a -> !a.getGoodsLike()).collect(Collectors.toList());
+
+        List<Appraisal> appraisals1 = new ArrayList<>();
+        switch (appraisalSort) {
+            //好评
+            case PRAISE:
+                appraisals1.addAll(good);
+                appraisals1.addAll(bad);
+                break;
+            //差评
+            case BAD_REVIEW:
+                appraisals1.addAll(bad);
+                appraisals1.addAll(good);
+                break;
+            //有图
+            case HAVE_PIC:
+                List<Appraisal> pic = appraisals.stream().filter(a -> StrUtil.isNotBlank(a.getImg())).collect(Collectors.toList());
+                List<Appraisal> noPic = appraisals.stream().filter(a -> StrUtil.isBlank(a.getImg())).collect(Collectors.toList());
+                appraisals1.addAll(pic);
+                appraisals1.addAll(noPic);
+                break;
+            default:
+                appraisals1.addAll(appraisals);
+        }
+        return appraisals1;
+    }
+
 
 }

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

@@ -11,7 +11,6 @@ import com.izouma.dingdong.exception.BusinessException;
 import com.izouma.dingdong.repo.OrderGoodsSpecRepo;
 import com.izouma.dingdong.repo.merchant.GoodsRepo;
 import com.izouma.dingdong.repo.merchant.GoodsSpecificationRepo;
-import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 
@@ -39,10 +38,11 @@ public class OrderGoodsSpecService {
         BigDecimal total = goods.getAmount();
         BigDecimal real = goods.getDiscountAmount();
 
+        List<Long> longs = new ArrayList<>();
 
         if (StrUtil.isNotBlank(goodsSpecificationIds)) {
             LongArrayConverter longArrayConverter = new LongArrayConverter();
-            List<Long> longs = longArrayConverter.convertToEntityAttribute(goodsSpecificationIds);
+            longs = longArrayConverter.convertToEntityAttribute(goodsSpecificationIds);
 
             List<Long> parents = new ArrayList<>();
             for (Long l : longs) {
@@ -86,7 +86,7 @@ public class OrderGoodsSpecService {
 
         OrderGoodsSpec save = OrderGoodsSpec.builder()
                 .goodsId(goodsId)
-                //.specificationId(ids)
+                .specificationId(longs)
                 //.specificationId(longs)
                 .num(num)
                 .goodsPrice(total)

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

@@ -1,5 +1,7 @@
 package com.izouma.dingdong.service.merchant;
 
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.util.ObjectUtil;
 import com.izouma.dingdong.domain.merchant.GoodsSpecification;
 import com.izouma.dingdong.exception.BusinessException;
 import com.izouma.dingdong.repo.merchant.GoodsRepo;
@@ -34,10 +36,12 @@ public class GoodsSpecificationService {
                 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));
+                    //c.setParent(save.getId());
+                    GoodsSpecification g = new GoodsSpecification();
+                    BeanUtil.copyProperties(c, g);
+                    g.setParent(save.getId());
+                    if (c.getId() != null && c.getId() < 0) {
+                        all.add(goodsSpecificationRepo.save(g));
                     }
                 });
 //                }

+ 27 - 13
src/main/java/com/izouma/dingdong/service/merchant/MerchantSettingsService.java

@@ -8,11 +8,13 @@ import com.izouma.dingdong.domain.backstage.Banner;
 import com.izouma.dingdong.domain.backstage.Category;
 import com.izouma.dingdong.domain.backstage.Promote;
 import com.izouma.dingdong.domain.backstage.TimeTag;
+import com.izouma.dingdong.domain.merchant.Goods;
 import com.izouma.dingdong.domain.merchant.Merchant;
 import com.izouma.dingdong.domain.merchant.MerchantSettings;
 import com.izouma.dingdong.dto.MerchantDTO;
 import com.izouma.dingdong.enums.FinancialType;
 import com.izouma.dingdong.exception.BusinessException;
+import com.izouma.dingdong.repo.AppraisalRepo;
 import com.izouma.dingdong.repo.MoneyRecordRepo;
 import com.izouma.dingdong.repo.OrderInfoRepo;
 import com.izouma.dingdong.repo.UserRepo;
@@ -20,9 +22,7 @@ import com.izouma.dingdong.repo.backstage.BannerRepo;
 import com.izouma.dingdong.repo.backstage.CategoryRepo;
 import com.izouma.dingdong.repo.backstage.PromoteRepo;
 import com.izouma.dingdong.repo.backstage.TimeTagRepo;
-import com.izouma.dingdong.repo.merchant.GoodsRepo;
-import com.izouma.dingdong.repo.merchant.MerchantRepo;
-import com.izouma.dingdong.repo.merchant.MerchantSettingsRepo;
+import com.izouma.dingdong.repo.merchant.*;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 
@@ -30,6 +30,7 @@ import java.math.BigDecimal;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
@@ -48,6 +49,10 @@ public class MerchantSettingsService {
     private CategoryRepo categoryRepo;
     private PromoteRepo promoteRepo;
     private GoodsRepo goodsRepo;
+    private AppraisalRepo appraisalRepo;
+    private GoodsSpecificationRepo goodsSpecificationRepo;
+    private MerchantClassificationRepo merchantClassificationRepo;
+    // private FullReductionRepo fullReductionRepo;
 
     /*
     显示所有订单
@@ -134,15 +139,17 @@ public class MerchantSettingsService {
         LocalDate date = LocalDate.now().minusMonths(1);
         LocalTime time = LocalTime.parse("00:00:00");
         LocalDateTime dateTime = LocalDateTime.of(date, time);
-        List<Merchant> merchants = merchantRepo.findAllByEstablishTimeAfter(dateTime);
-        merchants.subList(0, 8);
+        List<Merchant> newMerchants = merchantRepo.findAllByEstablishTimeAfter(dateTime);
+        newMerchants.subList(0, 8);
 
-        //推荐商家
 
-
-        //发现美食
-
-        return null;
+        Map<String, Object> map = new HashMap<>();
+        map.put("banner", banners);
+        map.put("categories", categories);
+        map.put("promote1", promote1);
+        map.put("promote2", promote2);
+        map.put("newMerchants", newMerchants);
+        return map;
     }
 
     /*
@@ -172,26 +179,33 @@ public class MerchantSettingsService {
             total = total.add(c);
         }
 
-        return total.divide(BigDecimal.valueOf(collect.size()),2);
+        return total.divide(BigDecimal.valueOf(collect.size()), 2);
     }
 
 
     /*
     删除商家
      */
-    public void del(Long merchantId){
+    public void del(Long merchantId) {
         //删除商户
         merchantRepo.deleteById(merchantId);
         merchantSettingsRepo.deleteByMerchantId(merchantId);
 
+        //删除商品规格
+        List<Long> collect = goodsRepo.findAllByMerchantId(merchantId).stream().map(Goods::getId).collect(Collectors.toList());
+        collect.forEach(c -> goodsSpecificationRepo.deleteByGoodsId(c));
+
         //删除商品
         goodsRepo.deleteAllByMerchantId(merchantId);
 
         //删除订单
+        orderInfoRepo.deleteAllByMerchantId(merchantId);
 
         //删除评价
+        appraisalRepo.deleteAllByMerchantId(merchantId);
 
-        //删除
+        //删除商家分类
+        merchantClassificationRepo.findAllByMerchantId(merchantId);
 
     }
 

+ 184 - 6
src/main/java/com/izouma/dingdong/service/user/ShoppingCartService.java

@@ -20,9 +20,12 @@ import com.izouma.dingdong.service.merchant.GoodsService;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 
+import javax.persistence.Column;
 import java.math.BigDecimal;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 @Service
 @AllArgsConstructor
@@ -35,7 +38,6 @@ public class ShoppingCartService {
     private MerchantSettingsRepo merchantSettingsRepo;
     private FullReductionRepo fullReductionRepo;
     private OrderGoodsSpecRepo orderGoodsSpecRepo;
-    private GoodsService goodsService;
 
     /*
     创建购物车
@@ -66,7 +68,6 @@ public class ShoppingCartService {
         BigDecimal sub = orderGoodsSpec.getGoodsPrice().subtract(orderGoodsSpec.getGoodsRealPrice()).multiply(BigDecimal.valueOf(orderGoodsSpec.getNum()));
 
         //包装费
-
         BigDecimal packingPrice = BigDecimal.ZERO;
 
         if (ObjectUtil.isNotNull(goods.getPackingPrice())) {
@@ -85,6 +86,7 @@ public class ShoppingCartService {
                     .firstBuy(merchant.getFirstOrder())
                     .enabled(true)
                     .build();
+            shoppingCart.setId(-1L);
         } else {
             //商品总价(原价)
             shoppingCart.setGoodsTotal(shoppingCart.getGoodsTotal().add(orderGoodsSpec.getGoodsPrice().multiply(BigDecimal.valueOf(orderGoodsSpec.getNum()))));
@@ -153,14 +155,190 @@ public class ShoppingCartService {
         shoppingCart.setRealAmount(real);
         shoppingCart.setReducedAmount(sub2);
 
-        shoppingCart = shoppingCartRepo.save(shoppingCart);
+        ShoppingCart cart = shoppingCartRepo.save(shoppingCart);
+
+        this.changeOrderSpec(cart, orderGoodsSpec);
+
+        return cart;
+    }
+
+
+    //改订单规格
+    public void changeOrderSpec(ShoppingCart cart, OrderGoodsSpec orderGoodsSpec) {
+
+        orderGoodsSpec.setShoppingCartId(cart.getId());
+        List<OrderGoodsSpec> orderGoodsSpecs = cart.getOrderGoodsSpecs();
+
+        boolean f = true;
+        for (OrderGoodsSpec o : orderGoodsSpecs) {//商品id相同
+            if (!o.getId().equals(orderGoodsSpec.getId()))
+                if (orderGoodsSpec.getGoodsId().equals(o.getGoodsId())) {
+                    //且规格id都相同或都为空
+                    List<Long> specificationId = orderGoodsSpec.getSpecificationId();
+                    List<Long> specificationId1 = o.getSpecificationId();
+                    if ((CollUtil.isEmpty(specificationId) && CollUtil.isEmpty(specificationId1)) || specificationId.equals(specificationId1)) {
+                        //原有数量加上现在买的数量
+                        o.setNum(o.getNum() + orderGoodsSpec.getNum());
+                        // o.setGoodsPrice(o.getGoodsPrice().add(orderGoodsSpec.getGoodsPrice()));
+                        // o.setGoodsRealPrice(o.getGoodsRealPrice().add(orderGoodsSpec.getGoodsPrice()));
+
+                        orderGoodsSpecRepo.save(o);
+                        orderGoodsSpecRepo.deleteById(orderGoodsSpec.getId());
+                        f = false;
+                        break;
+                    }
+                }
+        }
+        if (f) {
+            orderGoodsSpecRepo.save(orderGoodsSpec);
+        }
+    }
+
+    //计算价钱
+    public ShoppingCart calculatePrice(Long cartId, Long userId) {
+
+        //购物车
+        ShoppingCart cart = shoppingCartRepo.findById(cartId).orElseThrow(new BusinessException("无购物车"));
+        List<OrderGoodsSpec> specs = cart.getOrderGoodsSpecs();
+
+        //商品总价 商品实际
+        BigDecimal goodsTotal = BigDecimal.ZERO;
+        BigDecimal goodsReal = BigDecimal.ZERO;
+
+        //满减
+        boolean flag = true;
+        //包装费
+        BigDecimal packingPrice = BigDecimal.ZERO;
+
+        for (OrderGoodsSpec s : specs) {
+            //商品总价 商品实际
+            goodsTotal = goodsTotal.add(s.getGoodsPrice().multiply(new BigDecimal(s.getNum())));
+            goodsReal = goodsReal.add(s.getGoodsRealPrice().multiply(new BigDecimal(s.getNum())));
+
+            Goods goods = goodsRepo.findById(s.getGoodsId()).orElseThrow(new BusinessException("无商品"));
+            //满减
+            if (!goods.getIsFullReduction()) {
+                flag = false;
+                cart.setReducedAmount(goodsTotal.subtract(goodsReal));
+            }
+
+            //包装费
+            if (ObjectUtil.isNotNull(goods.getPackingPrice())) {
+                packingPrice = goods.getPackingPrice().multiply(BigDecimal.valueOf(s.getNum()));
+            }
+        }
+
+        //总价 商品总价+包装费(可参与满减金额)
+        BigDecimal total = goodsTotal.add(packingPrice);
+
+        //按商家创建购物车
+        Long merchantId = specs.get(0).getGoods().getMerchantId();
+        //商家信息
+        MerchantSettings merchant = merchantSettingsRepo.findByMerchantId(merchantId).orElseThrow(new BusinessException("无商户"));
+
+        if (flag) {
+            //参与满减
+            List<FullReduction> fullReductions = fullReductionRepo.findAllByMerchantId(merchantId);
+            fullReductions.sort(Comparator.comparing(FullReduction::getFullAmount));
+            for (FullReduction f : fullReductions) {
+                if (f.getFullAmount().compareTo(total) <= 0) {
+                    //满减金额
+                    cart.setFullReduction(f.getMinusAmount());
+                    //不是折扣,是满减,就置为减去的金额
+                    cart.setReducedAmount(f.getMinusAmount());
+                }
+            }
+        }
+
+        //查找是不是第一次购买
+        //List<OrderInfo> userOrders = orderInfoRepo.findAllByUserId(userId);
+        List<OrderInfo> userOrdersMerchant = orderInfoRepo.findAllByUserIdAndMerchantId(userId, merchantId);
+        //是新用户也是首单
+//        if (CollUtil.isEmpty(userOrders)) {
+//            shoppingCart.setNewUser(merchant.getNewUser());
+//            shoppingCart.setFirstBuy(merchant.getFirstOrder());
+//        } else {
+        //首单
+        if (CollUtil.isEmpty(userOrdersMerchant)) {
+            //shoppingCart.setNewUser(BigDecimal.ZERO);
+            cart.setFirstBuy(merchant.getFirstOrder());
+        } else {
+            //非首单设置为0
+            //shoppingCart.setNewUser(BigDecimal.ZERO);
+            cart.setFirstBuy(BigDecimal.ZERO);
+        }
+//        }
 
-        orderGoodsSpec.setShoppingCartId(shoppingCart.getId());
+        //获取配送费
+        cart.setDeliveryAmount(BigDecimal.TEN);
+
+        //需要减去的金额=首单+折扣/满减
+        BigDecimal sub2 = cart.getReducedAmount();
+        if (ObjectUtil.isNotNull(cart.getFirstBuy())) {
+            sub2 = sub2.add(cart.getFirstBuy());
+        }
 
-        orderGoodsSpecRepo.save(orderGoodsSpec);
+        cart.setGoodsTotal(goodsTotal);
+        cart.setReducedAmount(sub2);
+        cart.setRealAmount(total.subtract(sub2).add(cart.getDeliveryAmount()));
+
+        return shoppingCartRepo.save(cart);
 
-        return shoppingCart;
     }
 
+    //加规格
+    public ShoppingCart add(Long userId, OrderGoodsSpec orderGoodsSpec) {
+        //按商家创建购物车
+        Long merchantId = goodsRepo.findById(orderGoodsSpec.getGoodsId()).orElseThrow(new BusinessException("无商品")).getMerchantId();
+
+        //商家信息
+        //MerchantSettings merchant = merchantSettingsRepo.findByMerchantId(merchantId).orElseThrow(new BusinessException("无商户"));
+
+        //查找是不是第一次购买
+        userRepo.findById(userId).orElseThrow(new BusinessException("无用户"));
+
+        //购物车中是否已有
+        ShoppingCart shoppingCart = shoppingCartRepo.findByUserIdAndMerchantId(userId, merchantId);
+        //加规格
+        List<OrderGoodsSpec> specs = CollUtil.newArrayList();
+        specs.add(orderGoodsSpec);
+
+        if (ObjectUtil.isNull(shoppingCart)) {
+            shoppingCart = ShoppingCart.builder()
+                    .merchantId(merchantId)
+                    .userId(userId)
+                    .goodsTotal(orderGoodsSpec.getGoodsPrice().multiply(BigDecimal.valueOf(orderGoodsSpec.getNum())))
+                    .reducedAmount(BigDecimal.ZERO)
+                    .orderGoodsSpecs(specs)
+                    .packingPrice(BigDecimal.ZERO)
+                    .fullReduction(BigDecimal.ZERO)
+                    .firstBuy(BigDecimal.ZERO)
+                    .enabled(true)
+                    .build();
+            shoppingCart.setId(-1L);
+
+        } else {
+            specs.addAll(shoppingCart.getOrderGoodsSpecs());
+            shoppingCart.setOrderGoodsSpecs(specs);
+        }
+
+        ShoppingCart save = shoppingCartRepo.save(shoppingCart);
+
+        this.changeOrderSpec(save, orderGoodsSpec);
+
+        return this.calculatePrice(save.getId(), userId);
+    }
+
+    //减规格
+    public ShoppingCart sub(Long specId, Long userId) {
+        OrderGoodsSpec spec = orderGoodsSpecRepo.findById(specId).orElseThrow(new BusinessException("无订单详情"));
+
+        Long cartId = spec.getShoppingCartId();
+        //if (spec.getShoppingCartId() == null) {
+        orderGoodsSpecRepo.deleteById(specId);
+        //}
+        //shoppingCartRepo.findById(spec.getShoppingCartId());
+        return this.calculatePrice(cartId, userId);
+    }
 
 }

+ 11 - 4
src/main/java/com/izouma/dingdong/web/AppraisalController.java

@@ -2,6 +2,7 @@ package com.izouma.dingdong.web;
 
 import com.izouma.dingdong.domain.Appraisal;
 import com.izouma.dingdong.dto.AppraisalMerDTO;
+import com.izouma.dingdong.enums.AppraisalSort;
 import com.izouma.dingdong.service.AppraisalService;
 import com.izouma.dingdong.dto.PageQuery;
 import com.izouma.dingdong.exception.BusinessException;
@@ -73,17 +74,23 @@ public class AppraisalController extends BaseController {
 
     @ApiOperation("商家回复评价")
     @GetMapping("/merchantReply")
-    public Appraisal merchantApply(Long id, String reply) {
+    public Appraisal merchantApply(@RequestParam Long id, String reply) {
         Appraisal appraisal = appraisalRepo.findById(id).orElseThrow(new BusinessException("评价不存在"));
         appraisal.setMerchantReply(reply);
         return appraisalRepo.save(appraisal);
     }
 
+    /**
+     * 显示所有评价
+     * @param appraisalSort  排序规则
+     * @return 所有
+     */
     @GetMapping("/my")
     @ApiOperation("显示商户的所有评价")
-    public List<AppraisalMerDTO> my() {
-        List<Appraisal> allByMerchantId = appraisalRepo.findAllByMerchantId(merchantService.findMerchantId(SecurityUtils.getAuthenticatedUser().getId()));
-        return allByMerchantId.stream().map(appraisalService::toDTO).collect(Collectors.toList());
+    public List<AppraisalMerDTO> my(@RequestParam AppraisalSort appraisalSort) {
+        //List<Appraisal> allByMerchantId = appraisalRepo.findAllByMerchantId(merchantService.findMerchantId(SecurityUtils.getAuthenticatedUser().getId()));
+        List<Appraisal> appraisals = appraisalService.appraisalsSort(merchantService.findMerchantId(SecurityUtils.getAuthenticatedUser().getId()), appraisalSort);
+        return appraisals.stream().map(appraisalService::toDTO).collect(Collectors.toList());
 
     }
 

+ 2 - 3
src/main/java/com/izouma/dingdong/web/merchant/MerchantController.java

@@ -59,9 +59,8 @@ public class MerchantController extends BaseController {
 
     @PostMapping("/del/{id}")
     public void del(@PathVariable Long id) {
-
-        merchantRepo.deleteById(id);
-        merchantSettingsRepo.deleteByMerchantId(id);
+        //删除商家相关所有信息
+        merchantSettingsService.del(id);
     }
 
     @GetMapping("/excel")

+ 13 - 4
src/main/java/com/izouma/dingdong/web/user/ShoppingCartController.java

@@ -70,18 +70,27 @@ public class ShoppingCartController extends BaseController {
         ExcelUtils.export(response, data);
     }
 
+    /**
+     * 选择规格后加入购物车
+     *
+     * @param goodsId               商品Id
+     * @param goodsSpecificationIds 规格Id,无规格时,可不填,为null即可
+     * @param num                   数量
+     * @return 购物车列表
+     */
     @PostMapping("/cart")
     @ApiOperation("选择规格后就加入购物车")
     public ShoppingCart cart(Long goodsId, String goodsSpecificationIds, Integer num) {
         //无规格时,goodsSpecifications可不填
         OrderGoodsSpec orderGoodsSpec = orderGoodsSpecService.add(goodsId, goodsSpecificationIds, num);
-        return shoppingCartService.save(SecurityUtils.getAuthenticatedUser().getId(), orderGoodsSpec);
+        return shoppingCartService.add(SecurityUtils.getAuthenticatedUser().getId(), orderGoodsSpec);
     }
 
-    @PostMapping("/delGoods")
+    @GetMapping("/delGoods")
     @ApiOperation("从购物车中删除商品")
-    public void delGoods(Long orderGoodsSpec) {
-        orderGoodsSpecRepo.deleteById(orderGoodsSpec);
+    public ShoppingCart delGoods(Long specId) {
+        //orderGoodsSpecRepo.deleteById(orderGoodsSpec);
+        return shoppingCartService.sub(specId, SecurityUtils.getAuthenticatedUser().getId());
     }
 
     @GetMapping("/my")

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

@@ -60,19 +60,19 @@ public class GoodsServiceTest {
 
     @Test
     public void goodsSpec(){
-/*        GoodsSpecification spec1 = GoodsSpecification.builder()
+        GoodsSpecification spec1 = GoodsSpecification.builder()
                 .multiple(false)
-                .name("冰量")
-                .goodsId(121L)
+                .name("甜度")
+                .goodsId(1258L)
                 .build();
-        spec1.setId(-1L);*/
+        spec1.setId(-1L);
         GoodsSpecification spec2 = GoodsSpecification.builder()
-                //.amount(BigDecimal.ZERO)
-                .name("超级酸")
-                //.goodsId(121L)
-                //.parent(1089L)
+                .amount(BigDecimal.ZERO)
+                .name("全糖")
+                .goodsId(1258L)
+                .parent(-1L)
                 .build();
-        spec2.setId(1097L);
+        spec2.setId(-2L);
 /*        GoodsSpecification spec3 = GoodsSpecification.builder()
                 .amount(BigDecimal.ZERO)
                 .name("少冰")
@@ -89,11 +89,11 @@ public class GoodsServiceTest {
         spec4.setId(-4L);*/
        GoodsSpecification spec5 = GoodsSpecification.builder()
                 .amount(BigDecimal.ONE)
-               // .name("微微酸")
-                //.goodsId(121L)
-                //.parent(1089L)
+                .name("半糖")
+                .goodsId(1258L)
+                .parent(-1L)
                 .build();
-        spec5.setId(1098L);
+        spec5.setId(-3L);
 /*         GoodsSpecification spec6 = GoodsSpecification.builder()
                 .amount(BigDecimal.ZERO)
                 .name("半糖")
@@ -111,8 +111,8 @@ public class GoodsServiceTest {
 
 
 
-        List<GoodsSpecification> specifications = new ArrayList<>(2);
-//        specifications.add(spec1);
+        List<GoodsSpecification> specifications = new ArrayList<>(3);
+        specifications.add(spec1);
         specifications.add(spec2);
 //        specifications.add(spec3);
 //        specifications.add(spec4);

+ 18 - 3
src/test/java/com/izouma/dingdong/service/ShoppingCartServiceTest.java

@@ -2,6 +2,7 @@ package com.izouma.dingdong.service;
 
 import com.izouma.dingdong.domain.OrderGoodsSpec;
 import com.izouma.dingdong.repo.OrderGoodsSpecRepo;
+import com.izouma.dingdong.repo.user.ShoppingCartRepo;
 import com.izouma.dingdong.service.user.ShoppingCartService;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -16,6 +17,8 @@ public class ShoppingCartServiceTest {
     @Autowired
     private ShoppingCartService shoppingCartService;
 
+    @Autowired
+    private ShoppingCartRepo shoppingCartRepo;
     @Autowired
     private OrderGoodsSpecService orderGoodsSpecService;
 
@@ -23,11 +26,23 @@ public class ShoppingCartServiceTest {
     private OrderGoodsSpecRepo orderGoodsSpecRepo;
 
     @Test
-    public void testCart(){
-        OrderGoodsSpec add = orderGoodsSpecService.add(988L, "995", 1);
+    public void testCart() {
+        OrderGoodsSpec add = orderGoodsSpecService.add(1258L, null, 1);
 
-       // OrderGoodsSpec add = orderGoodsSpecRepo.findById(1318L).orElse(null);
+        // OrderGoodsSpec add = orderGoodsSpecRepo.findById(1318L).orElse(null);
 
         System.out.println(shoppingCartService.save(83L, add));
     }
+
+    @Test
+    public void test() {
+        System.out.println(shoppingCartRepo.findById(1484L).orElse(null));
+    }
+
+    @Test
+    public void testSub(){
+        OrderGoodsSpec add = orderGoodsSpecService.add(607L, null, 1);
+        System.out.println(shoppingCartService.add(83L, add));
+        //System.out.println(shoppingCartService.sub(1500L, 83L));
+    }
 }