Parcourir la source

新增下单时店铺营业时间判断;

codingliang il y a 2 ans
Parent
commit
abf1ef8a3d

+ 45 - 0
src/main/java/com/sqx/modules/order/service/impl/AppAppOrderServiceImpl.java

@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.sqx.common.exception.SqxException;
 import com.sqx.common.utils.DateUtils;
 import com.sqx.common.utils.PageUtils;
 import com.sqx.common.utils.Result;
@@ -60,6 +61,7 @@ import java.math.BigDecimal;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.time.LocalDateTime;
+import java.time.LocalTime;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
@@ -128,8 +130,36 @@ public class AppAppOrderServiceImpl extends ServiceImpl<AppOrderDao, TbOrder> im
         return Result.error("系统繁忙,请稍后再试!");
     }
 
+    /**
+     * 判断店铺当前时间是否营业时间范围内
+     * @param shopId 店铺id
+     * @return boolean
+     */
+    private boolean isShopBusinessTime(Long shopId) {
+        GoodsShop goodsShop = goodsShopDao.selectById(shopId);
+        if (goodsShop == null) {
+            throw new SqxException("无效的店铺id");
+        }
+        String businessHours = goodsShop.getBusinessHours();
+        String lockHours = goodsShop.getLockHours();
+
+        // 营业时间和闭店时间有一个为空,则直接返回true,默认全天营业
+        if (StringUtils.isEmpty(businessHours) || StringUtils.isEmpty(lockHours)) {
+            return true;
+        }
+
+        LocalTime now = LocalTime.now();
+
+        return now.isAfter(LocalTime.parse(businessHours)) && now.isBefore(LocalTime.parse(lockHours));
+    }
+
     @Transactional
     public Result addOrder(Long userId, Long shopId, Long goodsId, Integer num, Long skuId, String skuMessage, Integer orderType) {
+        // 判断当前时间是否在店铺营业范围内
+        if (!isShopBusinessTime(shopId)) {
+            return Result.error("店铺已打烊!");
+        }
+
         //先判断该商品的库存是否足够
         GoodsShopRelevancy goodsShopRelevancy = goodsShopRelevancyDao.selectOne(new QueryWrapper<GoodsShopRelevancy>().eq("shop_id", shopId).eq("goods_id", goodsId));
         if (goodsShopRelevancy.getInventory() < num) {
@@ -210,6 +240,11 @@ public class AppAppOrderServiceImpl extends ServiceImpl<AppOrderDao, TbOrder> im
 
     @Override
     public Result shareTheBill(Long userId, Long shopId) {
+        // 判断当前时间是否在店铺营业范围内
+        if (!isShopBusinessTime(shopId)) {
+            return Result.error("店铺已打烊!");
+        }
+
         TbOrder tbOrder2 = appOrderDao.selectOne(new QueryWrapper<TbOrder>()
                 .eq("user_id", userId).eq("shop_id", shopId).eq("status", 1));
         if (tbOrder2 == null) {
@@ -303,6 +338,11 @@ public class AppAppOrderServiceImpl extends ServiceImpl<AppOrderDao, TbOrder> im
 
     @Override
     public Result joinOrder(Long userId, Long orderId, Long shopId, Long goodsId, Integer num, Long skuId, String skuMessage) {
+        // 判断当前时间是否在店铺营业范围内
+        if (!isShopBusinessTime(shopId)) {
+            return Result.error("店铺已打烊!");
+        }
+
         //先判断该商品的库存是否足够
         GoodsShopRelevancy goodsShopRelevancy = goodsShopRelevancyDao.selectOne(new QueryWrapper<GoodsShopRelevancy>()
                 .eq("shop_id", shopId).eq("goods_id", goodsId));
@@ -337,6 +377,11 @@ public class AppAppOrderServiceImpl extends ServiceImpl<AppOrderDao, TbOrder> im
     @Transactional
     @Override
     public Result buyGoods(Long userId, Long shopId, Long goodsId, Integer num, Long skuId, String skuMessage, Integer orderType) {
+        // 判断当前时间是否在店铺营业范围内
+        if (!isShopBusinessTime(shopId)) {
+            return Result.error("店铺已打烊!");
+        }
+
         //先判断该商品的库存是否足够
         GoodsShopRelevancy goodsShopRelevancy = goodsShopRelevancyDao.selectOne(new QueryWrapper<GoodsShopRelevancy>().eq("shop_id", shopId).eq("goods_id", goodsId));
         if (goodsShopRelevancy.getInventory() < num) {