Selaa lähdekoodia

fix:挂单功能添加参数校验

huangjinliang 1 viikko sitten
vanhempi
commit
4725d6155b

+ 5 - 0
fuintBackend/fuint-application/src/main/java/com/fuint/common/service/TableInfoService.java

@@ -70,4 +70,9 @@ public interface TableInfoService extends IService<MtTableInfo> {
     List<MtTableInfo> queryTableInfoListByParams(Map<String, Object> params) throws BusinessCheckException;
 
 	List<MtTableInfo> selectTableListByStoreId(Long storeId, Long categoryId);
+
+    /**
+     * 校验分类是否存在
+     */
+    boolean existCategory(Long categoryId);
 }

+ 25 - 1
fuintBackend/fuint-application/src/main/java/com/fuint/common/service/impl/CartServiceImpl.java

@@ -9,9 +9,11 @@ import com.fuint.framework.exception.BusinessCheckException;
 import com.fuint.repository.mapper.MtCartMapper;
 import com.fuint.repository.mapper.MtGoodsMapper;
 import com.fuint.repository.mapper.MtGoodsSkuMapper;
+import com.fuint.repository.mapper.MtTableInfoMapper;
 import com.fuint.repository.model.MtCart;
 import com.fuint.repository.model.MtGoods;
 import com.fuint.repository.model.MtGoodsSku;
+import com.fuint.repository.model.MtTableInfo;
 import com.fuint.utils.StringUtil;
 import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
@@ -38,6 +40,8 @@ public class CartServiceImpl extends ServiceImpl<MtCartMapper, MtCart> implement
 
     private MtGoodsSkuMapper mtGoodsSkuMapper;
 
+    private MtTableInfoMapper mtTableInfoMapper;
+
     /**
      * 切换购物车给会员
      *
@@ -310,7 +314,7 @@ public class CartServiceImpl extends ServiceImpl<MtCartMapper, MtCart> implement
     @OperationServiceLog(description = "执行挂单")
     @Transactional(rollbackFor = Exception.class)
     public MtCart setHangNo(Long cartId, String hangNo, String isVisitor) throws BusinessCheckException {
-        MtCart mtCart = mtCartMapper.selectById(cartId);
+       /* MtCart mtCart = mtCartMapper.selectById(cartId);
         if (mtCart != null) {
             mtCart.setHangNo(hangNo);
             mtCart.setUpdateTime(new Date());
@@ -318,7 +322,27 @@ public class CartServiceImpl extends ServiceImpl<MtCartMapper, MtCart> implement
             this.updateById(mtCart);
         } else {
             throw new BusinessCheckException("执行挂单失败");
+        }*/
+        // -------------------- 参数校验 --------------------
+        // 校验桌号有效性
+        LambdaQueryWrapper<MtTableInfo> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(MtTableInfo::getTableNumber, hangNo);
+        Integer tableCount = mtTableInfoMapper.selectCount(queryWrapper);
+        if (tableCount == null || tableCount == 0) {
+            throw new BusinessCheckException("执行挂单失败,桌号不存在,请确认后重试");
         }
+
+        // -------------------- 业务处理 --------------------
+        MtCart mtCart = mtCartMapper.selectById(cartId);
+        if (mtCart == null) {
+            throw new BusinessCheckException("执行挂单失败,购物车记录不存在");
+        }
+
+        // -------------------- 更新数据 --------------------
+        mtCart.setHangNo(hangNo);
+        mtCart.setUpdateTime(new Date());
+        mtCart.setIsVisitor(isVisitor);
+        this.updateById(mtCart);
         return mtCart;
     }
 }

+ 7 - 0
fuintBackend/fuint-application/src/main/java/com/fuint/common/service/impl/TableInfoServiceImpl.java

@@ -173,4 +173,11 @@ public class TableInfoServiceImpl extends ServiceImpl<MtTableInfoMapper, MtTable
                 .eq(MtTableInfo::getCategoryId, categoryId)
                 .eq(MtTableInfo::getDeleteFlag, 0));
     }
+    @Override
+    public boolean existCategory(Long categoryId) {
+        return mtTableInfoMapper.selectCount(
+                new LambdaQueryWrapper<MtTableInfo>()
+                        .eq(MtTableInfo::getId, categoryId)
+        ) > 0;
+    }
 }

+ 39 - 10
fuintBackend/fuint-application/src/main/java/com/fuint/module/backendApi/controller/BackendCashierController.java

@@ -21,6 +21,7 @@ import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
 import org.springframework.beans.BeanUtils;
 import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 import javax.servlet.http.HttpServletRequest;
 import java.lang.reflect.InvocationTargetException;
@@ -376,7 +377,6 @@ public class BackendCashierController extends BaseController {
         if (StringUtil.isEmpty(userId)) {
             isVisitor = YesOrNoEnum.YES.getKey();
         }
-
         if (StringUtil.isNotEmpty(cartIds)) {
             String[] ids = cartIds.split(",");
             if (ids.length > 0) {
@@ -403,34 +403,63 @@ public class BackendCashierController extends BaseController {
         if (accountInfo == null) {
             return getFailureResult(1001, "请先登录");
         }
+        // 获取当前店铺ID(从登录账户信息中)
         Long storeId = accountInfo.getStoreId();
-        //获取餐桌列表
-        List<MtTableInfo> tableInfoList = tableInfoService.selectTableListByStoreId(storeId,categoryId);
+        // -------------------- 新增校验:分类存在性检查 --------------------
+        if (!tableInfoService.existCategory(categoryId)) {
+//            log.warn("请求非法分类 | storeId={} categoryId={}", storeId, categoryId);
+            return getFailureResult(2001, "该餐桌分类不存在");
+        }
 
+        //获取指定分类的餐桌列表
+        //调用tableInfoService查询当前店铺下的餐桌
+        List<MtTableInfo> tableInfoList = tableInfoService.selectTableListByStoreId(storeId,categoryId);
+        // 构建返回数据列表
         List<HangUpDto> dataList = new ArrayList<>();
 
+        // 遍历每个餐桌,检查是否有挂单
 	    for (MtTableInfo mtTableInfo : tableInfoList) {
+            // 从餐桌信息中获取桌号作为挂单号
 		    String hangNo = mtTableInfo.getTableNumber();
+            //构造查询参数
 		    Map<String, Object> param = new HashMap<>();
-		    param.put("hangNo", hangNo);
-		    param.put("merchantId", accountInfo.getMerchantId());
-		    param.put("storeId", storeId);
+		    param.put("hangNo", hangNo);// 当前桌号
+		    param.put("merchantId", accountInfo.getMerchantId());// 商户ID
+		    param.put("storeId", storeId);  // 店铺ID
+            //查询关联的购物车列表(挂单记录)
 		    List<MtCart> cartList = cartService.queryCartListByParams(param);
+            //构建返回DTO对象
 		    HangUpDto dto = new HangUpDto();
+            // 设置默认值
 		    dto.setIsEmpty(true);
 		    if (!cartList.isEmpty()) {
+                // 存在挂单时的处理逻辑
+
+                //获取用户信息
                 Long userId = cartList.get(0).getUserId();
 			    String isVisitor = cartList.get(0).getIsVisitor();
-			    Map<String, Object> cartInfo = orderService.calculateCartGoods(accountInfo.getMerchantId(), userId, cartList, 0L, false, PlatformTypeEnum.PC.getCode(), OrderModeEnum.ONESELF.getKey());
-			    dto.setNum(Integer.parseInt(cartInfo.get("totalNum").toString()));
-			    dto.setAmount(new BigDecimal(cartInfo.get("totalPrice").toString()));
+                // 计算购物车商品总价和数量 调用订单服务进行计算
+			    Map<String, Object> cartInfo = orderService.calculateCartGoods(
+                        accountInfo.getMerchantId(),
+                        userId,
+                        cartList,
+                        0L,  // 使用的卡券ID 未使用优惠券
+                        false,  // isUsePoint 不使用积分
+                        PlatformTypeEnum.PC.getCode(),  // 平台类型:PC端
+                        OrderModeEnum.ONESELF.getKey()// 订单模式:自取
+                );
+                //填充数据
+			    dto.setNum(Integer.parseInt(cartInfo.get("totalNum").toString()));//数量
+			    dto.setAmount(new BigDecimal(cartInfo.get("totalPrice").toString()));//商品总价
+                // 非访客订单时获取会员信息
 			    if (isVisitor.equals(YesOrNoEnum.NO.getKey())) {
 				    MtUser userInfo = memberService.queryMemberById(userId);
 				    dto.setMemberInfo(userInfo);
 			    }
+                //设置更新时间(取第一个购物车记录的更新时间)
 			    String dateTime = DateUtil.formatDate(cartList.get(0).getUpdateTime(), "yyyy-MM-dd HH:mm:ss");
 			    dto.setDateTime(dateTime);
-			    dto.setIsEmpty(false);
+			    dto.setIsEmpty(false);// 标记为有挂单
 		    }
 		    dto.setHangNo(hangNo);
 		    dataList.add(dto);