新增创建订单 切换就餐模式接口

This commit is contained in:
2024-09-29 15:45:48 +08:00
parent 70ad132780
commit 095773f62a
6 changed files with 87 additions and 31 deletions

View File

@@ -1,14 +1,17 @@
package com.chaozhanggui.system.cashierservice.controller; package com.chaozhanggui.system.cashierservice.controller;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.chaozhanggui.system.cashierservice.entity.TbShopTable; import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.entity.dto.OrderDto; import com.chaozhanggui.system.cashierservice.annotation.LimitSubmit;
import com.chaozhanggui.system.cashierservice.redis.RedisCst;
import com.chaozhanggui.system.cashierservice.service.CartService;
import com.chaozhanggui.system.cashierservice.service.OrderService; import com.chaozhanggui.system.cashierservice.service.OrderService;
import com.chaozhanggui.system.cashierservice.sign.Result; import com.chaozhanggui.system.cashierservice.sign.Result;
import com.chaozhanggui.system.cashierservice.util.Utils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
import java.util.Map; import java.util.Map;
@@ -19,19 +22,24 @@ import java.util.Map;
@RequestMapping("/order") @RequestMapping("/order")
public class OrderController { public class OrderController {
@Resource private final OrderService orderService;
private OrderService orderService; private final CartService cartService;
private final StringRedisTemplate stringRedisTemplate;
public OrderController(OrderService orderService, CartService cartService, StringRedisTemplate stringRedisTemplate) {
this.orderService = orderService;
this.cartService = cartService;
this.stringRedisTemplate = stringRedisTemplate;
}
/** /**
* 添加订单 * 添加订单
* @return * @return
*/ */
@PostMapping("/creatOrder") @PostMapping("/creatOrder")
public Result createOrder(@RequestBody OrderDto shopTable){ public Result createOrder(@RequestBody JSONObject jsonObject){
if (shopTable.getTableId() == null){ return Utils.runFunAndCheckKey(() -> cartService.createOrder(jsonObject), stringRedisTemplate, RedisCst.getLockKey("CREATE_ORDER_KEY"));
return Result.fail("台桌号有误"); // return orderService.createOrder(shopTable.getTableId(),shopTable.getShopId(),shopTable.getUserId());
}
return orderService.createOrder(shopTable.getTableId(),shopTable.getShopId(),shopTable.getUserId());
} }
/** /**

View File

@@ -4,6 +4,7 @@ package com.chaozhanggui.system.cashierservice.controller;
import cn.hutool.core.util.ObjectUtil; 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.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.entity.TbCashierCart;
import com.chaozhanggui.system.cashierservice.entity.dto.ChoseCountDTO; import com.chaozhanggui.system.cashierservice.entity.dto.ChoseCountDTO;
import com.chaozhanggui.system.cashierservice.entity.dto.ChoseEatModelDTO; import com.chaozhanggui.system.cashierservice.entity.dto.ChoseEatModelDTO;
import com.chaozhanggui.system.cashierservice.entity.dto.QuerySpecDTO; import com.chaozhanggui.system.cashierservice.entity.dto.QuerySpecDTO;
@@ -16,6 +17,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
@CrossOrigin(origins = "*") @CrossOrigin(origins = "*")
@@ -107,17 +111,21 @@ public class ProductController {
* @return 餐位费信息 * @return 餐位费信息
*/ */
@PostMapping("/choseCount") @PostMapping("/choseCount")
public Result choseCount( public Result choseCount(@Validated @RequestBody ChoseCountDTO choseCountDTO) {
@Validated @RequestBody ChoseCountDTO choseCountDTO
) {
return Result.success(CodeEnum.SUCCESS, productService.choseCount(choseCountDTO)); return Result.success(CodeEnum.SUCCESS, productService.choseCount(choseCountDTO));
} }
@PostMapping("/choseEatModel") @PostMapping("/choseEatModel")
public Result choseEatModel( public Result choseEatModel(@Validated @RequestBody ChoseEatModelDTO choseEatModelDTO) {
@Validated @RequestBody ChoseEatModelDTO choseEatModelDTO List<TbCashierCart> cashierCartList = cartService.choseEatModel(choseEatModelDTO);
) { BigDecimal amount = BigDecimal.ZERO;
return Result.success(CodeEnum.SUCCESS, productService.choseEatModel(choseEatModelDTO)); for (TbCashierCart item : cashierCartList) {
amount = amount.add(item.getTotalAmount());
}
HashMap<String, Object> data = new HashMap<>();
data.put("amount", amount);
data.put("info", cashierCartList);
return Result.success(CodeEnum.SUCCESS, data);
} }
@PostMapping("cleanCart") @PostMapping("cleanCart")

View File

@@ -2,6 +2,7 @@ package com.chaozhanggui.system.cashierservice.entity.dto;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@@ -12,5 +13,8 @@ public class ChoseEatModelDTO {
private Integer shopId; private Integer shopId;
private String tableId; private String tableId;
@NotNull @NotNull
private Integer userId; @Max(1)
@Min(0)
// 0切换店内 1切换外带
private Integer type;
} }

View File

@@ -39,7 +39,9 @@ import java.util.concurrent.ConcurrentHashMap;
public class PushToAppChannelHandlerAdapter extends NettyChannelHandlerAdapter { public class PushToAppChannelHandlerAdapter extends NettyChannelHandlerAdapter {
/** /**
* [tableID-shopId, [userId, ctx]] * 台桌下单为: TAKEOUT_TABLE_CART:shopId:tableID
* 店内自取为: DINE_IN_TABLE_CART:shopId:userId
* [shopId:tableID, [userId, ctx]]
*/ */
private static Map<String, ConcurrentHashMap<String, ChannelHandlerContext>> webSocketMap = new HashMap<>(); private static Map<String, ConcurrentHashMap<String, ChannelHandlerContext>> webSocketMap = new HashMap<>();
@@ -239,7 +241,7 @@ public class PushToAppChannelHandlerAdapter extends NettyChannelHandlerAdapter {
@Async @Async
public void AppSendInfo(String message, String redisKey,String userId, boolean userFlag) { public void AppSendInfo(String message, String redisKey,String userId, boolean userFlag) {
log.info("netty连接 发送消息 tableId:{} userId:{} userFlag:{} message:{}",redisKey,userId,userFlag, JSONUtil.toJSONString(message)); log.info("netty连接 发送消息 key:{} userId:{} userFlag:{} message:{}",redisKey,userId,userFlag, JSONUtil.toJSONString(message));
log.info("当前已连接队列信息: {}", webSocketMap); log.info("当前已连接队列信息: {}", webSocketMap);
if (userFlag) { if (userFlag) {
if (webSocketMap.containsKey(redisKey)) { if (webSocketMap.containsKey(redisKey)) {

View File

@@ -14,6 +14,7 @@ import com.chaozhanggui.system.cashierservice.dao.*;
import com.chaozhanggui.system.cashierservice.entity.*; import com.chaozhanggui.system.cashierservice.entity.*;
import com.chaozhanggui.system.cashierservice.entity.Enum.*; import com.chaozhanggui.system.cashierservice.entity.Enum.*;
import com.chaozhanggui.system.cashierservice.entity.dto.ChoseCountDTO; import com.chaozhanggui.system.cashierservice.entity.dto.ChoseCountDTO;
import com.chaozhanggui.system.cashierservice.entity.dto.ChoseEatModelDTO;
import com.chaozhanggui.system.cashierservice.entity.dto.ShopEatTypeInfoDTO; import com.chaozhanggui.system.cashierservice.entity.dto.ShopEatTypeInfoDTO;
import com.chaozhanggui.system.cashierservice.exception.MsgException; import com.chaozhanggui.system.cashierservice.exception.MsgException;
import com.chaozhanggui.system.cashierservice.mapper.*; import com.chaozhanggui.system.cashierservice.mapper.*;
@@ -723,7 +724,7 @@ public class CartService {
} }
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void createOrder(JSONObject jsonObject) { public Result createOrder(JSONObject jsonObject) {
try { try {
JSONObject responseData = new JSONObject(); JSONObject responseData = new JSONObject();
@@ -795,7 +796,7 @@ public class CartService {
responseData.put("msg", "购物车为空"); responseData.put("msg", "购物车为空");
responseData.put("data", new ArrayList<>()); responseData.put("data", new ArrayList<>());
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(responseData.toString(), tableCartKey, jsonObject.getString("userId"), true); PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(responseData.toString(), tableCartKey, jsonObject.getString("userId"), true);
return; return Result.fail("购物车为空");
} }
// 就餐人数 // 就餐人数
@@ -819,9 +820,9 @@ public class CartService {
// 设置餐位费 // 设置餐位费
TbShopInfo shopInfo = mpShopInfoMapper.selectById(shopId); TbShopInfo shopInfo = mpShopInfoMapper.selectById(shopId);
if (!shopEatTypeInfoDTO.isTakeout() && shopInfo.getIsTableFee() != null && shopInfo.getIsTableFee() == 0 if (!shopEatTypeInfoDTO.isTakeout() && shopInfo.getIsTableFee() != null && shopInfo.getIsTableFee() == 0
&& seatNum < 1) { && (seatNum < 1 || cashierCartList.size() < 2)) {
log.info("消息推送"); log.info("消息推送");
responseData.put("msg", "请选择就餐人数"); responseData.put("msg", "购物车为空");
if (shopTable.getMaxCapacity() < seatNum) { if (shopTable.getMaxCapacity() < seatNum) {
responseData.put("msg", "当前台桌最大人数为: " + shopTable.getMaxCapacity()); responseData.put("msg", "当前台桌最大人数为: " + shopTable.getMaxCapacity());
} }
@@ -829,7 +830,7 @@ public class CartService {
responseData.put("type", jsonObject.getString("type")); responseData.put("type", jsonObject.getString("type"));
responseData.put("data", ""); responseData.put("data", "");
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(responseData.toString(), tableCartKey, jsonObject.getString("userId"), true); PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(responseData.toString(), tableCartKey, jsonObject.getString("userId"), true);
return; return Result.fail(responseData.getString("msg"));
} }
@@ -1014,7 +1015,7 @@ public class CartService {
responseData.put("data", ""); responseData.put("data", "");
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(responseData.toString(), tableCartKey, jsonObject.getString("userId"), true); PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(responseData.toString(), tableCartKey, jsonObject.getString("userId"), true);
log.info("消息推送"); log.info("消息推送");
return; return Result.fail("优惠券已售空");
} }
if (N.gt(systemCoupons.getCouponsAmount(), totalAmount)) { if (N.gt(systemCoupons.getCouponsAmount(), totalAmount)) {
log.info("开始处理订单"); log.info("开始处理订单");
@@ -1024,7 +1025,7 @@ public class CartService {
responseData.put("data", ""); responseData.put("data", "");
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(responseData.toString(), tableCartKey, jsonObject.getString("userId"), true); PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(responseData.toString(), tableCartKey, jsonObject.getString("userId"), true);
log.info("消息推送"); log.info("消息推送");
return; return Result.fail("订单金额小于优惠价金额");
} }
totalAmount = totalAmount.add(systemCoupons.getCouponsPrice()).subtract(systemCoupons.getCouponsAmount()); totalAmount = totalAmount.add(systemCoupons.getCouponsPrice()).subtract(systemCoupons.getCouponsAmount());
originAmount = originAmount.add(systemCoupons.getCouponsPrice()); originAmount = originAmount.add(systemCoupons.getCouponsPrice());
@@ -1064,7 +1065,7 @@ public class CartService {
responseData.put("data", ""); responseData.put("data", "");
PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(responseData.toString(), tableCartKey, jsonObject.getString("userId"), true); PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(responseData.toString(), tableCartKey, jsonObject.getString("userId"), true);
log.info("消息推送"); log.info("消息推送");
return; return Result.fail("订单正在支付中,请稍后再试");
} }
orderInfo.setUpdatedAt(System.currentTimeMillis()); orderInfo.setUpdatedAt(System.currentTimeMillis());
orderInfo.setSettlementAmount(totalAmount); orderInfo.setSettlementAmount(totalAmount);
@@ -1135,11 +1136,10 @@ public class CartService {
} }
// 去除餐位费信息 // 去除餐位费信息
cashierCartList = cashierCartList.stream().filter(item -> !"-999".equals(item.getProductId())).collect(Collectors.toList());
List<TbActivateOutRecord> outRecords = new ArrayList<>(); List<TbActivateOutRecord> outRecords = new ArrayList<>();
for (TbCashierCart cashierCart : cashierCartList) { for (TbCashierCart cashierCart : cashierCartList) {
if (cashierCart.getIsVip().equals((byte) 1)) { if (!cashierCart.getProductId().equals("-999") && cashierCart.getIsVip().equals((byte) 1)) {
List<TbActivateInRecord> actInRecords = activateInRecordService.queryAllByVipIdAndShopIdAndProId( List<TbActivateInRecord> actInRecords = activateInRecordService.queryAllByVipIdAndShopIdAndProId(
Integer.valueOf(tbShopUser.getId()), Integer.valueOf(orderInfo.getShopId()), Integer.valueOf(cashierCart.getProductId())); Integer.valueOf(tbShopUser.getId()), Integer.valueOf(orderInfo.getShopId()), Integer.valueOf(cashierCart.getProductId()));
Integer totalNumber = cashierCart.getTotalNumber(); Integer totalNumber = cashierCart.getTotalNumber();
@@ -1169,6 +1169,9 @@ public class CartService {
mpCashierCartMapper.updateById(cashierCart); mpCashierCartMapper.updateById(cashierCart);
} }
cashierCartList = cashierCartList.stream().filter(item -> !"-999".equals(item.getProductId())).collect(Collectors.toList());
// 删除旧的餐位费信息 // 删除旧的餐位费信息
if (shopEatTypeInfoDTO.isTakeout() && seatCartInfo != null) { if (shopEatTypeInfoDTO.isTakeout() && seatCartInfo != null) {
cashierCartMapper.deleteByPrimaryKey(seatCartInfo.getId()); cashierCartMapper.deleteByPrimaryKey(seatCartInfo.getId());
@@ -1228,10 +1231,13 @@ public class CartService {
} }
}); });
return Result.successWithData(orderInfo);
} catch (Exception e) { } catch (Exception e) {
log.info("长链接错误 createOrder{}", e.getMessage()); log.info("长链接错误 createOrder{}", e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
return Result.fail("失败");
} }
private TbOrderInfo getOrder(BigDecimal totalAmount, BigDecimal packAMount, private TbOrderInfo getOrder(BigDecimal totalAmount, BigDecimal packAMount,
@@ -1578,4 +1584,33 @@ public class CartService {
} }
} }
public List<TbCashierCart> choseEatModel(ChoseEatModelDTO choseEatModelDTO) {
Integer userId = TokenUtil.getUserId();
List<TbCashierCart> cashierCartList;
if (choseEatModelDTO.getType() == 1) {
// 查询购物车所有信息
LambdaQueryWrapper<TbCashierCart> queryWrapper = new LambdaQueryWrapper<TbCashierCart>()
.eq(TbCashierCart::getShopId, choseEatModelDTO.getShopId())
.gt(TbCashierCart::getCreatedAt, DateUtil.offsetDay(DateUtil.date(), -1).getTime())
.isNull(TbCashierCart::getOrderId)
.ne(TbCashierCart::getProductId, "-999")
.eq(TbCashierCart::getStatus, "create");
// 外带模式
queryWrapper.eq(TbCashierCart::getUserId, userId);
cashierCartList = mpCashierCartMapper.selectList(queryWrapper);
}else {
cashierCartList = new ArrayList<>();
String tableCartKey = RedisCst.getTableCartKey(choseEatModelDTO.getShopId().toString(), choseEatModelDTO.getTableId(), userId);
String message = redisUtil.getMessage(tableCartKey);
if (StrUtil.isNotBlank(message)) {
JSONObject.parseArray(message).forEach(item -> {
TbCashierCart cart = JSONObject.parseObject(item.toString(), TbCashierCart.class);
cashierCartList.add(cart);
});
}
}
// 所有订单信息
return cashierCartList;
}
} }

View File

@@ -994,7 +994,6 @@ public class ProductService {
return mpCashierCartMapper.update(null, new LambdaUpdateWrapper<TbCashierCart>() return mpCashierCartMapper.update(null, new LambdaUpdateWrapper<TbCashierCart>()
.eq(TbCashierCart::getShopId, choseTableDTO.getShopId()) .eq(TbCashierCart::getShopId, choseTableDTO.getShopId())
.eq(TbCashierCart::getUserId, choseTableDTO.getUserId())
.isNull(TbCashierCart::getUseType) .isNull(TbCashierCart::getUseType)
.eq(TbCashierCart::getStatus, "create") .eq(TbCashierCart::getStatus, "create")
.set(TbCashierCart::getUseType, shopEatTypeInfoDTO.getUseType())) ; .set(TbCashierCart::getUseType, shopEatTypeInfoDTO.getUseType())) ;