diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/OrderController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/OrderController.java index 07d6dbf..b31672d 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/OrderController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/OrderController.java @@ -1,14 +1,17 @@ package com.chaozhanggui.system.cashierservice.controller; import cn.hutool.core.util.ObjectUtil; -import com.chaozhanggui.system.cashierservice.entity.TbShopTable; -import com.chaozhanggui.system.cashierservice.entity.dto.OrderDto; +import com.alibaba.fastjson.JSONObject; +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.sign.Result; +import com.chaozhanggui.system.cashierservice.util.Utils; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.*; -import javax.annotation.Resource; import java.io.IOException; import java.text.ParseException; import java.util.Map; @@ -19,19 +22,24 @@ import java.util.Map; @RequestMapping("/order") public class OrderController { - @Resource - private OrderService orderService; + private final 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 */ @PostMapping("/creatOrder") - public Result createOrder(@RequestBody OrderDto shopTable){ - if (shopTable.getTableId() == null){ - return Result.fail("台桌号有误"); - } - return orderService.createOrder(shopTable.getTableId(),shopTable.getShopId(),shopTable.getUserId()); + public Result createOrder(@RequestBody JSONObject jsonObject){ + return Utils.runFunAndCheckKey(() -> cartService.createOrder(jsonObject), stringRedisTemplate, RedisCst.getLockKey("CREATE_ORDER_KEY")); +// return orderService.createOrder(shopTable.getTableId(),shopTable.getShopId(),shopTable.getUserId()); } /** diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/controller/ProductController.java b/src/main/java/com/chaozhanggui/system/cashierservice/controller/ProductController.java index 8032b54..f63025f 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/controller/ProductController.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/controller/ProductController.java @@ -4,6 +4,7 @@ package com.chaozhanggui.system.cashierservice.controller; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; 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.ChoseEatModelDTO; 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.web.bind.annotation.*; +import java.math.BigDecimal; +import java.util.HashMap; +import java.util.List; import java.util.Map; @CrossOrigin(origins = "*") @@ -107,17 +111,21 @@ public class ProductController { * @return 餐位费信息 */ @PostMapping("/choseCount") - public Result choseCount( - @Validated @RequestBody ChoseCountDTO choseCountDTO - ) { + public Result choseCount(@Validated @RequestBody ChoseCountDTO choseCountDTO) { return Result.success(CodeEnum.SUCCESS, productService.choseCount(choseCountDTO)); } @PostMapping("/choseEatModel") - public Result choseEatModel( - @Validated @RequestBody ChoseEatModelDTO choseEatModelDTO - ) { - return Result.success(CodeEnum.SUCCESS, productService.choseEatModel(choseEatModelDTO)); + public Result choseEatModel(@Validated @RequestBody ChoseEatModelDTO choseEatModelDTO) { + List cashierCartList = cartService.choseEatModel(choseEatModelDTO); + BigDecimal amount = BigDecimal.ZERO; + for (TbCashierCart item : cashierCartList) { + amount = amount.add(item.getTotalAmount()); + } + HashMap data = new HashMap<>(); + data.put("amount", amount); + data.put("info", cashierCartList); + return Result.success(CodeEnum.SUCCESS, data); } @PostMapping("cleanCart") diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/ChoseEatModelDTO.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/ChoseEatModelDTO.java index f27de02..6760671 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/ChoseEatModelDTO.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/ChoseEatModelDTO.java @@ -2,6 +2,7 @@ package com.chaozhanggui.system.cashierservice.entity.dto; import lombok.Data; +import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; @@ -12,5 +13,8 @@ public class ChoseEatModelDTO { private Integer shopId; private String tableId; @NotNull - private Integer userId; + @Max(1) + @Min(0) + // 0切换店内 1切换外带 + private Integer type; } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/netty/PushToAppChannelHandlerAdapter.java b/src/main/java/com/chaozhanggui/system/cashierservice/netty/PushToAppChannelHandlerAdapter.java index a0144a4..3a065d5 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/netty/PushToAppChannelHandlerAdapter.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/netty/PushToAppChannelHandlerAdapter.java @@ -39,7 +39,9 @@ import java.util.concurrent.ConcurrentHashMap; 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> webSocketMap = new HashMap<>(); @@ -239,7 +241,7 @@ public class PushToAppChannelHandlerAdapter extends NettyChannelHandlerAdapter { @Async 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); if (userFlag) { if (webSocketMap.containsKey(redisKey)) { diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java index f8c3081..e28d0a1 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/CartService.java @@ -14,6 +14,7 @@ import com.chaozhanggui.system.cashierservice.dao.*; import com.chaozhanggui.system.cashierservice.entity.*; import com.chaozhanggui.system.cashierservice.entity.Enum.*; 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.exception.MsgException; import com.chaozhanggui.system.cashierservice.mapper.*; @@ -723,7 +724,7 @@ public class CartService { } @Transactional(rollbackFor = Exception.class) - public void createOrder(JSONObject jsonObject) { + public Result createOrder(JSONObject jsonObject) { try { JSONObject responseData = new JSONObject(); @@ -795,7 +796,7 @@ public class CartService { responseData.put("msg", "购物车为空"); responseData.put("data", new ArrayList<>()); 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); if (!shopEatTypeInfoDTO.isTakeout() && shopInfo.getIsTableFee() != null && shopInfo.getIsTableFee() == 0 - && seatNum < 1) { + && (seatNum < 1 || cashierCartList.size() < 2)) { log.info("消息推送"); - responseData.put("msg", "请选择就餐人数"); + responseData.put("msg", "购物车为空"); if (shopTable.getMaxCapacity() < seatNum) { responseData.put("msg", "当前台桌最大人数为: " + shopTable.getMaxCapacity()); } @@ -829,7 +830,7 @@ public class CartService { responseData.put("type", jsonObject.getString("type")); responseData.put("data", ""); 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", ""); PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(responseData.toString(), tableCartKey, jsonObject.getString("userId"), true); log.info("消息推送"); - return; + return Result.fail("优惠券已售空"); } if (N.gt(systemCoupons.getCouponsAmount(), totalAmount)) { log.info("开始处理订单"); @@ -1024,7 +1025,7 @@ public class CartService { responseData.put("data", ""); PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(responseData.toString(), tableCartKey, jsonObject.getString("userId"), true); log.info("消息推送"); - return; + return Result.fail("订单金额小于优惠价金额"); } totalAmount = totalAmount.add(systemCoupons.getCouponsPrice()).subtract(systemCoupons.getCouponsAmount()); originAmount = originAmount.add(systemCoupons.getCouponsPrice()); @@ -1064,7 +1065,7 @@ public class CartService { responseData.put("data", ""); PushToAppChannelHandlerAdapter.getInstance().AppSendInfo(responseData.toString(), tableCartKey, jsonObject.getString("userId"), true); log.info("消息推送"); - return; + return Result.fail("订单正在支付中,请稍后再试"); } orderInfo.setUpdatedAt(System.currentTimeMillis()); orderInfo.setSettlementAmount(totalAmount); @@ -1135,11 +1136,10 @@ public class CartService { } // 去除餐位费信息 - cashierCartList = cashierCartList.stream().filter(item -> !"-999".equals(item.getProductId())).collect(Collectors.toList()); List outRecords = new ArrayList<>(); for (TbCashierCart cashierCart : cashierCartList) { - if (cashierCart.getIsVip().equals((byte) 1)) { + if (!cashierCart.getProductId().equals("-999") && cashierCart.getIsVip().equals((byte) 1)) { List actInRecords = activateInRecordService.queryAllByVipIdAndShopIdAndProId( Integer.valueOf(tbShopUser.getId()), Integer.valueOf(orderInfo.getShopId()), Integer.valueOf(cashierCart.getProductId())); Integer totalNumber = cashierCart.getTotalNumber(); @@ -1169,6 +1169,9 @@ public class CartService { mpCashierCartMapper.updateById(cashierCart); } + cashierCartList = cashierCartList.stream().filter(item -> !"-999".equals(item.getProductId())).collect(Collectors.toList()); + + // 删除旧的餐位费信息 if (shopEatTypeInfoDTO.isTakeout() && seatCartInfo != null) { cashierCartMapper.deleteByPrimaryKey(seatCartInfo.getId()); @@ -1228,10 +1231,13 @@ public class CartService { } }); + return Result.successWithData(orderInfo); } catch (Exception e) { log.info("长链接错误 createOrder{}", e.getMessage()); e.printStackTrace(); } + + return Result.fail("失败"); } private TbOrderInfo getOrder(BigDecimal totalAmount, BigDecimal packAMount, @@ -1578,4 +1584,33 @@ public class CartService { } } + public List choseEatModel(ChoseEatModelDTO choseEatModelDTO) { + Integer userId = TokenUtil.getUserId(); + List cashierCartList; + if (choseEatModelDTO.getType() == 1) { + // 查询购物车所有信息 + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper() + .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; + } } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java index 1a87606..06dd6c3 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/ProductService.java @@ -994,7 +994,6 @@ public class ProductService { return mpCashierCartMapper.update(null, new LambdaUpdateWrapper() .eq(TbCashierCart::getShopId, choseTableDTO.getShopId()) - .eq(TbCashierCart::getUserId, choseTableDTO.getUserId()) .isNull(TbCashierCart::getUseType) .eq(TbCashierCart::getStatus, "create") .set(TbCashierCart::getUseType, shopEatTypeInfoDTO.getUseType())) ;