licailing 5 år sedan
förälder
incheckning
6520be6516

+ 7 - 17
src/main/java/com/izouma/dingdong/domain/Coupon.java

@@ -1,6 +1,7 @@
 package com.izouma.dingdong.domain;
 
 import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.izouma.dingdong.converter.LongArrayConverter;
 import com.izouma.dingdong.domain.BaseEntity;
 import com.izouma.dingdong.domain.User;
 import com.izouma.dingdong.domain.backstage.Category;
@@ -13,6 +14,8 @@ import java.io.Serializable;
 import java.math.BigDecimal;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.util.HashSet;
+import java.util.Set;
 
 @EqualsAndHashCode(callSuper = true)
 @Data
@@ -33,22 +36,10 @@ public class Coupon extends BaseEntity implements Serializable {
     @ApiModelProperty(value = "金额", name = "amount")
     private BigDecimal amount;
 
-/*    @ApiModelProperty(value = "有效期", name = "ExpirationDate")
-    private LocalDateTime ExpirationDate;*/
-
     private LocalDate startDate;
 
     private LocalDate endDate;
 
-//    @ApiModelProperty(value = "是否过期", name = "isExpired")
-//    private Boolean isExpired;
-
-//    @ApiModelProperty(value = "是否已使用", name = "isUsed")
-//    private Boolean isUsed;
-//
-//    @ApiModelProperty(value = "使用时间", name = "usedTime")
-//    private LocalDateTime usedTime;
-
 /*    @ApiModelProperty(value = "使用规则", name = "rule")
     private String rule;*/
 
@@ -62,10 +53,6 @@ public class Coupon extends BaseEntity implements Serializable {
 //    @ApiModelProperty(value = "折扣", name = "discount")
 //    private Integer discount;
 
-//    @ManyToOne(fetch = FetchType.LAZY)
-//    @JoinColumn(name = "userId", insertable = false, updatable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
-//    @JsonIgnore
-//    private User user;
 
     //商家发的优惠券
     private Long merchantId;
@@ -76,5 +63,8 @@ public class Coupon extends BaseEntity implements Serializable {
 //    //用于限制手机号
 //    private String phone;
 
-    private String categoryIds;
+    //用于限制优惠券类别
+//    @ApiModelProperty(value = "类别Id")
+//    @Convert(converter = LongArrayConverter.class)
+//    private Set<Long> categoryIds = new HashSet<>();
 }

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

@@ -1,6 +1,7 @@
 package com.izouma.dingdong.domain;
 
 
+import com.izouma.dingdong.converter.LongArrayConverter;
 import com.izouma.dingdong.domain.merchant.Goods;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
@@ -33,6 +34,7 @@ public class OrderGoodsSpec extends BaseEntity implements Serializable {
 
    // private String specificationId;
 
+
     @ApiModelProperty(value = "规格", name = "specification")
     private String specification;
 

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

@@ -1,6 +1,8 @@
 package com.izouma.dingdong.domain.merchant;
 
 
+import com.izouma.dingdong.converter.LongArrayConverter;
+import com.izouma.dingdong.converter.StringArrayConverter;
 import com.izouma.dingdong.domain.BaseEntity;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
@@ -10,6 +12,9 @@ import lombok.*;
 import javax.persistence.*;
 import javax.validation.constraints.Size;
 import java.io.Serializable;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 
 
 @Data
@@ -42,7 +47,8 @@ public class MerchantClassification extends BaseEntity implements Serializable {
     @ApiModelProperty(value = "商品列表", name = "goodsList")
     private Set<Goods> goodsList;*/
 
-    private String goodsIds;
+    @Convert(converter = LongArrayConverter.class)
+    private Set<Long> goodsIds = new HashSet<>();
 
     @ApiModelProperty(value = "是否开启", name = "isOpen")
     private Boolean isOpen;

+ 4 - 0
src/main/java/com/izouma/dingdong/domain/user/Address.java

@@ -35,4 +35,8 @@ public class Address extends BaseEntity implements Serializable {
 
     @Column(nullable = false)
     private Boolean enabled = true;
+
+    public String getAddressInfo() {
+        return name + "," + phone + "," + addressName;
+    }
 }

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

@@ -12,4 +12,6 @@ public interface OrderGoodsSpecRepo extends JpaRepository<OrderGoodsSpec, Long>,
 
     List<OrderGoodsSpec> findAllByGoodsId(Long goodsId);
 
+    List<OrderGoodsSpec> findAllByShoppingCartId(Long shoppingCardId);
+
 }

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

@@ -3,9 +3,11 @@ package com.izouma.dingdong.service;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson.JSONObject;
+import com.izouma.dingdong.converter.LongArrayConverter;
 import com.izouma.dingdong.domain.OrderGoodsSpec;
 import com.izouma.dingdong.domain.merchant.Goods;
 import com.izouma.dingdong.domain.merchant.GoodsSpecification;
+import com.izouma.dingdong.enums.ApplyStatus;
 import com.izouma.dingdong.exception.BusinessException;
 import com.izouma.dingdong.repo.OrderGoodsSpecRepo;
 import com.izouma.dingdong.repo.merchant.GoodsRepo;
@@ -33,6 +35,12 @@ public class OrderGoodsSpecService {
         String spec = null;
         //String ids = null;
         Goods goods = goodsRepo.findById(goodsId).orElseThrow(new BusinessException("无商品"));
+
+        //判断商品是否通过审核
+        if (!ApplyStatus.PASS.equals(goods.getStatus())) {
+            throw new BusinessException("商品审核中或未通过");
+        }
+
         BigDecimal total = goods.getAmount();
         BigDecimal real = goods.getDiscountAmount();
 
@@ -62,7 +70,7 @@ public class OrderGoodsSpecService {
 //                } else {
 //                    ids = g.getId().toString();
 //                }
-             }
+            }
             spec = JSONObject.toJSONString(goodsSpecifications);
 
         }
@@ -77,5 +85,44 @@ public class OrderGoodsSpecService {
 
     }
 
+    public OrderGoodsSpec add(Long goodsId, String goodsSpecificationIds, Integer num) {
+        Goods goods = goodsRepo.findById(goodsId).orElseThrow(new BusinessException("无商品"));
+        //判断商品是否通过审核
+        if (!ApplyStatus.PASS.equals(goods.getStatus())) {
+            throw new BusinessException("商品审核中或未通过");
+        }
+        StringBuilder specName = null;
+        BigDecimal total = goods.getAmount();
+        BigDecimal real = goods.getDiscountAmount();
+
+        if (StrUtil.isNotBlank(goodsSpecificationIds)) {
+            LongArrayConverter longArrayConverter = new LongArrayConverter();
+            List<Long> longs = longArrayConverter.convertToEntityAttribute(goodsSpecificationIds);
+
+            for (Long l : longs) {
+                GoodsSpecification spec = goodsSpecificationRepo.findById(l).orElseThrow(new BusinessException("无规格"));
+                //价钱
+                total = total.add(spec.getAmount());
+                real = real.add(spec.getAmount());
+
+                //名字
+                if (specName != null) {
+                    specName.append(",").append(spec.getName());
+                } else {
+                    specName = new StringBuilder(spec.getName());
+                }
+            }
+        }
+        return orderGoodsSpecRepo.save(
+                OrderGoodsSpec.builder()
+                        .goodsId(goodsId)
+                        .specification(specName.toString())
+                        //.specificationId(ids)
+                        .num(num)
+                        .goodsPrice(total)
+                        .goodsRealPrice(real)
+                        .build());
+    }
+
 
 }

+ 18 - 9
src/main/java/com/izouma/dingdong/service/OrderInfoService.java

@@ -77,11 +77,12 @@ public class OrderInfoService {
         LocalTime nowTime = LocalTime.now();
 
         //是否营业时间内
-        if (startTime.compareTo(nowTime) >= 0 || endTime.compareTo(nowTime) <= 0 || !merchantSettings.getIsOpening()) {
+        if (!merchantSettings.getIsOpening()) {
+            throw new BusinessException("商家未营业");
+        } else if (!nowTime.isAfter(startTime) || nowTime.isAfter(endTime)) {
             throw new BusinessException("非营业时间");
         }
 
-
         OrderInfo orderInfo = new OrderInfo();
         BeanUtil.copyProperties(userOrderDTO, orderInfo);
         BeanUtil.copyProperties(cart, orderInfo);
@@ -93,7 +94,7 @@ public class OrderInfoService {
         //用户地址
         Address address = addressRepo.findById(userOrderDTO.getAddressId()).orElseThrow(new BusinessException("地址不存在"));
         //orderInfo.setUserAddress(address.getAddressName());
-        orderInfo.setUserAddress(JSONObject.toJSONString(address));
+        orderInfo.setUserAddress(address.getAddressInfo());
 
         // 使用优惠券(满减优惠券)
         if (ObjectUtil.isNotNull(userOrderDTO.getUserCouponId())) {
@@ -102,18 +103,21 @@ public class OrderInfoService {
             Coupon coupon = couponRepo.findById(userCoupon.getCouponId()).orElseThrow(new BusinessException("优惠券不存在"));
             //未过期,未使用
             if (coupon.getEndDate().isAfter(now.toLocalDate()) && !userCoupon.getIsUsed()) {
+                //限制商家
+                if (ObjectUtil.isNotNull(coupon.getMerchantId()) && !orderInfo.getMerchantId().equals(coupon.getMerchantId())){
+                    throw new BusinessException("指定商家优惠券");
+                }
+
                 //判断是否限制手机号
-                if (ObjectUtil.isNotNull(userCoupon.getPhone())) {
-                    if (!address.getPhone().equals(userCoupon.getPhone())) {
+                if (ObjectUtil.isNotNull(userCoupon.getPhone()) && !address.getPhone().equals(userCoupon.getPhone())) {
                         throw new BusinessException("限手机" + userCoupon.getPhone() + "使用");
-                    }
                 }
 
                 if (coupon.getFullAmount().compareTo(cart.getGoodsTotal()) <= 0) {
                     //订单金额,优惠券Id
-                    orderInfo.setPackingPrice(coupon.getAmount());
+                    orderInfo.setRedBag(coupon.getAmount());
                     orderInfo.setUserCouponId(userCoupon.getId());
-                    orderInfo.setRealAmount(orderInfo.getRealAmount().subtract(orderInfo.getPackingPrice()));
+                    orderInfo.setRealAmount(orderInfo.getRealAmount().subtract(orderInfo.getRedBag()));
 
                     userCoupon.setIsUsed(true);
                     userCoupon.setUsedTime(now);
@@ -122,13 +126,17 @@ public class OrderInfoService {
             }
         }
 
+
+        //设置状态已支付
+        orderInfo.setStatus(OrderStatus.PAID);
+
         OrderInfo save = orderInfoRepo.save(orderInfo);
 
         cart.getOrderGoodsSpecs().forEach(s -> {
                     //查库存是否足够
                     Goods goods = goodsService.buy(s.getGoodsId(), s.getNum());
                     //商品销售时间内
-                    if (goods.getStartTime().compareTo(nowTime) >= 0 || goods.getEndTime().compareTo(nowTime) <= 0) {
+                    if (goods.getStartTime().isAfter(nowTime)  || goods.getEndTime().isBefore(nowTime)) {
                         throw new BusinessException("非营业时间");
                     }
                     //查商品数据,加商品销量,减商品库存
@@ -186,6 +194,7 @@ public class OrderInfoService {
             orderInfo.setMerchantStatus(MerchantStatus.RECEIVED);
             //通知骑手 手动
             orderInfo.setRiderId(151L);
+            orderInfo.setRiderStatus(RiderStatus.RECEIVED);
 
         } else {
             orderInfo.setMerchantStatus(MerchantStatus.REJECTED);

+ 21 - 37
src/main/java/com/izouma/dingdong/service/merchant/MerchantClassificationService.java

@@ -3,6 +3,7 @@ package com.izouma.dingdong.service.merchant;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
+import com.izouma.dingdong.converter.LongArrayConverter;
 import com.izouma.dingdong.converter.StringArrayConverter;
 import com.izouma.dingdong.domain.merchant.Goods;
 import com.izouma.dingdong.domain.merchant.MerchantClassification;
@@ -14,6 +15,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
+import java.util.Set;
 
 @Service
 @AllArgsConstructor
@@ -31,7 +33,7 @@ public class MerchantClassificationService {
      */
     public MerchantClassification add(Long id, MerchantClassification classification) {
         Long merchantId = merchantService.findMerchantId(id);
-        if (ObjectUtil.isNotNull(merchantClassificationRepo.findByMerchantIdAndName(merchantId, classification.getName()))){
+        if (ObjectUtil.isNotNull(merchantClassificationRepo.findByMerchantIdAndName(merchantId, classification.getName()))) {
             throw new BusinessException("该分类已存在");
         }
 
@@ -57,12 +59,10 @@ public class MerchantClassificationService {
      */
     public List<Goods> showGoods(Long classificationId) {
         MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId).orElseThrow(new BusinessException("分类不存在"));
-
-        StringArrayConverter converter = new StringArrayConverter();
-        List<String> string = converter.convertToEntityAttribute(merchantClassification.getGoodsIds());
+        Set<Long> goodsIds = merchantClassification.getGoodsIds();
         List<Goods> goods = CollUtil.newArrayList();
-        string.forEach(s -> {
-            goods.add(goodsRepo.findById(Long.parseLong(s)).orElseThrow(new BusinessException("商品不存在")));
+        goodsIds.forEach(s -> {
+            goods.add(goodsRepo.findById(s).orElseThrow(new BusinessException("商品不存在")));
         });
         return goods;
     }
@@ -77,24 +77,14 @@ public class MerchantClassificationService {
     public MerchantClassification saveGoods(Long classificationId, String string) {
         //查找分类
         MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId).orElseThrow(new BusinessException("无分类"));
-/*        //转成String
-        StringArrayConverter converter = new StringArrayConverter();
-        String string = converter.convertToDatabaseColumn(goodId);*/
-
 
-        String goodsIds = merchantClassification.getGoodsIds();
+        Set<Long> goodsIds = merchantClassification.getGoodsIds();
+        LongArrayConverter converter = new LongArrayConverter();
+        List<Long> longs = converter.convertToEntityAttribute(string);
+        goodsIds.addAll(longs);
 
-//        StringArrayConverter converter = new StringArrayConverter();
-//        List<String> strings1 = converter.convertToEntityAttribute(string);
-//        List<String> strings = converter.convertToEntityAttribute(goodsIds);
-//        strings1.forEach(strings::contains);
+        merchantClassification.setGoodsIds(goodsIds);
 
-        if (StrUtil.isBlank(goodsIds)) {
-            merchantClassification.setGoodsIds(string);
-        } else {
-            //保存
-            merchantClassification.setGoodsIds(goodsIds + "," + string);
-        }
         return merchantClassificationRepo.save(merchantClassification);
     }
 
@@ -104,18 +94,12 @@ public class MerchantClassificationService {
     public void saveOneGoods(Long classificationId, Long goodsId) {
         //查找分类
         MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId).orElseThrow(new BusinessException("无分类"));
-        String goodsIds = merchantClassification.getGoodsIds();
-
-        StringArrayConverter converter = new StringArrayConverter();
-        List<String> strings = converter.convertToEntityAttribute(goodsIds);
-
-        if (!strings.contains(goodsId.toString())) {
-            if (StrUtil.isBlank(goodsIds)) {
-                merchantClassification.setGoodsIds(goodsId.toString());
-            } else {
-                //保存
-                merchantClassification.setGoodsIds(goodsIds + "," + goodsId);
-            }
+
+        Set<Long> ids = merchantClassification.getGoodsIds();
+
+        if (!ids.contains(goodsId)) {
+            ids.add(goodsId);
+            merchantClassification.setGoodsIds(ids);
             merchantClassificationRepo.save(merchantClassification);
         }
     }
@@ -127,10 +111,10 @@ public class MerchantClassificationService {
         //查找分类
         MerchantClassification merchantClassification = merchantClassificationRepo.findById(classificationId).orElseThrow(new BusinessException("无分类"));
 
-        //替换
-        String replaceAll = merchantClassification.getGoodsIds().replaceAll(",|''" + goodId.toString() + "''|,", "");
-        //保存
-        merchantClassification.setGoodsIds(replaceAll);
+        Set<Long> goodsIds = merchantClassification.getGoodsIds();
+        goodsIds.remove(goodId);
+        merchantClassification.setGoodsIds(goodsIds);
+
         merchantClassificationRepo.save(merchantClassification);
     }
 

+ 2 - 3
src/main/java/com/izouma/dingdong/service/merchant/MerchantService.java

@@ -93,9 +93,6 @@ public class MerchantService {
         BeanUtil.copyProperties(merchantDTO, merchant);
         merchant.setUserId(user.getId());
         merchant.setStatus(ApplyStatus.PENDING);
-        // merchant.setGoodNum(0);
-//        merchant.setBadNum(0);
-//        merchant.setMonthSales(0);
 
         merchant.setEnabled(true);
         merchant.setBlacklist(false);
@@ -104,6 +101,8 @@ public class MerchantService {
         //dto转merchantSettings实体
         BeanUtil.copyProperties(merchantDTO, merchantSettings);
         merchantSettings.setMerchantId(merchant.getId());
+        //默认不自动接单
+        merchantSettings.setAutomaticOrder(false);
 
         //新用户和首单金额未设置时
         if (ObjectUtil.isNull(merchantDTO.getNewUser())) {

+ 10 - 5
src/main/java/com/izouma/dingdong/service/user/ShoppingCartService.java

@@ -62,8 +62,12 @@ public class ShoppingCartService {
         List<OrderGoodsSpec> specs = CollUtil.newArrayList();
         specs.add(orderGoodsSpec);
 
+        //减的金额
         BigDecimal sub = orderGoodsSpec.getGoodsPrice().subtract(orderGoodsSpec.getGoodsRealPrice()).multiply(BigDecimal.valueOf(orderGoodsSpec.getNum()));
 
+        //包装费
+        BigDecimal packingPrice = goods.getPackingPrice().multiply(BigDecimal.valueOf(orderGoodsSpec.getNum()));
+
         if (ObjectUtil.isNull(shoppingCart)) {
             shoppingCart = ShoppingCart.builder()
                     .merchantId(goods.getMerchantId())
@@ -71,7 +75,7 @@ public class ShoppingCartService {
                     .goodsTotal(orderGoodsSpec.getGoodsPrice().multiply(BigDecimal.valueOf(orderGoodsSpec.getNum())))
                     .reducedAmount(sub)
                     .orderGoodsSpecs(specs)
-                    .packingPrice(goods.getPackingPrice())
+                    .packingPrice(packingPrice)
                     .fullReduction(BigDecimal.ZERO)
                     .build();
         } else {
@@ -82,7 +86,7 @@ public class ShoppingCartService {
             specs.addAll(shoppingCart.getOrderGoodsSpecs());
             shoppingCart.setOrderGoodsSpecs(specs);
             //包装费
-            shoppingCart.setPackingPrice(shoppingCart.getPackingPrice().add(goods.getPackingPrice()));
+            shoppingCart.setPackingPrice(shoppingCart.getPackingPrice().add(packingPrice));
         }
 
         //查找是不是第一次购买
@@ -127,14 +131,13 @@ public class ShoppingCartService {
             }
         }
 
-
         //商品总价+配送费+包装费
         BigDecimal total = shoppingCart.getGoodsTotal().add(shoppingCart.getDeliveryAmount()).add(shoppingCart.getPackingPrice());
 
         //需要减去的金额=首单+新用户+折扣/满减
-        BigDecimal sub2 = shoppingCart.getFirstBuy().add(shoppingCart.getNewUser()).add(shoppingCart.getFullReduction()).add(shoppingCart.getReducedAmount());
+        BigDecimal sub2 = shoppingCart.getFirstBuy().add(shoppingCart.getNewUser()).add(shoppingCart.getReducedAmount());
         //总价-首单-新用户-满减
-        BigDecimal real = total.subtract(total.subtract(sub2));
+        BigDecimal real = total.subtract(sub2);
 
         shoppingCart.setRealAmount(real);
         shoppingCart.setReducedAmount(sub2);
@@ -142,9 +145,11 @@ public class ShoppingCartService {
         shoppingCart = shoppingCartRepo.save(shoppingCart);
 
         orderGoodsSpec.setShoppingCartId(shoppingCart.getId());
+
         orderGoodsSpecRepo.save(orderGoodsSpec);
 
         return shoppingCart;
     }
 
+
 }

+ 0 - 2
src/main/java/com/izouma/dingdong/web/backstage/CategoryController.java

@@ -75,7 +75,5 @@ public class CategoryController extends BaseController {
         return categories;
     }
 
-    //public List<Category> tree()
-
 }
 

+ 2 - 1
src/main/java/com/izouma/dingdong/web/merchant/GoodsSpecificationController.java

@@ -25,6 +25,7 @@ import java.util.List;
 @RequestMapping("/goodsSpecification")
 @AllArgsConstructor
 public class GoodsSpecificationController extends BaseController {
+
     private GoodsSpecificationService goodsSpecificationService;
     private GoodsSpecificationRepo goodsSpecificationRepo;
 
@@ -32,7 +33,7 @@ public class GoodsSpecificationController extends BaseController {
 
     //@PreAuthorize("hasRole('ADMIN')")
     @PostMapping("/save")
-    @ApiOperation("商品规格添加修改")
+    @ApiOperation("商品规格添加/修改")
     public GoodsSpecification save(@RequestBody GoodsSpecification record) {
         if (record.getId() != null) {
             GoodsSpecification orig = goodsSpecificationRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));

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

@@ -1,5 +1,6 @@
 package com.izouma.dingdong.web.user;
 
+import com.izouma.dingdong.converter.LongArrayConverter;
 import com.izouma.dingdong.domain.OrderGoodsSpec;
 import com.izouma.dingdong.domain.user.ShoppingCart;
 
@@ -20,6 +21,7 @@ import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 
 @RestController
@@ -69,9 +71,9 @@ public class ShoppingCartController extends BaseController {
 
     @PostMapping("/cart")
     @ApiOperation("选择规格后就加入购物车")
-    public ShoppingCart cart(Long goodsId, List<GoodsSpecification> goodsSpecifications) {
+    public ShoppingCart cart(Long goodsId, String goodsSpecificationIds, Integer num) {
         //无规格时,goodsSpecifications可不填
-        OrderGoodsSpec orderGoodsSpec = orderGoodsSpecService.add(goodsId, goodsSpecifications);
+        OrderGoodsSpec orderGoodsSpec = orderGoodsSpecService.add(goodsId, goodsSpecificationIds, num);
         return shoppingCartService.save(SecurityUtils.getAuthenticatedUser().getId(), orderGoodsSpec);
     }
 }