feat: 新增优惠券使用接口
This commit is contained in:
@@ -3,6 +3,7 @@ package com.chaozhanggui.system.cashierservice.controller;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.chaozhanggui.system.cashierservice.entity.dto.CreateOrderDTO;
|
||||
import com.chaozhanggui.system.cashierservice.entity.dto.UseCouponDTO;
|
||||
import com.chaozhanggui.system.cashierservice.redis.RedisCst;
|
||||
import com.chaozhanggui.system.cashierservice.service.CartService;
|
||||
import com.chaozhanggui.system.cashierservice.service.OrderService;
|
||||
@@ -41,6 +42,17 @@ public class OrderController {
|
||||
// return orderService.createOrder(shopTable.getTableId(),shopTable.getShopId(),shopTable.getUserId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择优惠券
|
||||
* @param userId 用户od
|
||||
* @param useCouponDTO 优惠券信息
|
||||
*/
|
||||
@PostMapping("/useCoupon")
|
||||
public Result useCoupon(@RequestHeader("id") Integer userId, @RequestBody UseCouponDTO useCouponDTO) {
|
||||
useCouponDTO.setUserId(userId);
|
||||
return cartService.useCoupon(useCouponDTO);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 订单详情
|
||||
|
||||
@@ -24,4 +24,8 @@ public class CreateOrderDTO {
|
||||
private List<UserCouponInfoDTO> userCouponInfos = new ArrayList<>();
|
||||
// 使用的积分抵扣数量
|
||||
private Integer pointsNum;
|
||||
|
||||
// 是否只修改优惠券信息
|
||||
private boolean onlyChangeCoupon = false;
|
||||
private Integer orderId;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.chaozhanggui.system.cashierservice.entity.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class UseCouponDTO {
|
||||
@NotNull(message = "店铺id不为空")
|
||||
private Integer shopId;
|
||||
@NotNull(message = "订单id不为空")
|
||||
private Integer orderId;
|
||||
private Integer userId;
|
||||
@Valid
|
||||
@NotEmpty(message = "请选择优惠券信息")
|
||||
private List<UserCouponInfoDTO> userCouponInfos = new ArrayList<>();
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.chaozhanggui.system.cashierservice.mapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.chaozhanggui.system.cashierservice.entity.TbOrderInfo;
|
||||
import com.chaozhanggui.system.cashierservice.entity.TbShopInfo;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
@@ -12,6 +13,14 @@ import com.chaozhanggui.system.cashierservice.entity.TbShopInfo;
|
||||
*/
|
||||
public interface MpOrderInfoMapper extends BaseMapper<TbOrderInfo> {
|
||||
|
||||
/**
|
||||
* 根据shopId和orderId查询数据
|
||||
* @param shopId 店铺数据
|
||||
* @param orderId 订单id
|
||||
* @return 订单信息
|
||||
*/
|
||||
@Select("select * from tb_order_info where shop_id=#{shopId} and id=#{orderId}")
|
||||
TbOrderInfo selectByShopIdAndId(Integer shopId, Integer orderId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -861,35 +861,39 @@ public class CartService {
|
||||
}
|
||||
|
||||
|
||||
private void updateCartAndStock(List<TbCashierCart> newAddCashierCarts, TbOrderInfo orderInfo, ShopEatTypeInfoDTO shopEatTypeInfoDTO) {
|
||||
private void updateCartAndStock(List<TbCashierCart> newAddCashierCarts, TbOrderInfo orderInfo,
|
||||
ShopEatTypeInfoDTO shopEatTypeInfoDTO, boolean onlyChangeCoupon) {
|
||||
// 更新购物车记录的orderId
|
||||
for (TbCashierCart cashierCart : newAddCashierCarts) {
|
||||
if (!"-999".equals(cashierCart.getProductId())) {
|
||||
TbProduct product = productMapper.selectById(Integer.valueOf(cashierCart.getProductId()));
|
||||
TbProductSku productSku = productSkuMapper.selectByPrimaryKey(Integer.valueOf(cashierCart.getSkuId()));
|
||||
if (!onlyChangeCoupon) {
|
||||
if (!"-999".equals(cashierCart.getProductId())) {
|
||||
TbProduct product = productMapper.selectById(Integer.valueOf(cashierCart.getProductId()));
|
||||
TbProductSku productSku = productSkuMapper.selectByPrimaryKey(Integer.valueOf(cashierCart.getSkuId()));
|
||||
|
||||
log.info("下单,开始校验库存预警,购物车id:{}", cashierCart.getId());
|
||||
CompletableFuture.runAsync(() -> checkWarnLineAndSendMsg(productSku, product, cashierCart.getNumber()));
|
||||
// 已经添加的商品,修改数量
|
||||
updateStock(cashierCart);
|
||||
log.info("下单,开始校验库存预警,购物车id:{}", cashierCart.getId());
|
||||
CompletableFuture.runAsync(() -> checkWarnLineAndSendMsg(productSku, product, cashierCart.getNumber()));
|
||||
// 已经添加的商品,修改数量
|
||||
updateStock(cashierCart);
|
||||
}
|
||||
|
||||
cashierCart.setStatus("pending".equals(orderInfo.getStatus()) ? "refund" : cashierCart.getStatus());
|
||||
// 先付费模式,结束购物车状态
|
||||
if (!shopEatTypeInfoDTO.isDineInAfter() || StrUtil.isBlank(shopEatTypeInfoDTO.getTableId())) {
|
||||
cashierCart.setStatus("final");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
cashierCart.setOrderId(String.valueOf(orderInfo.getId()));
|
||||
cashierCart.setUpdatedAt(System.currentTimeMillis());
|
||||
cashierCart.setStatus("pending".equals(orderInfo.getStatus()) ? "refund" : cashierCart.getStatus());
|
||||
if (cashierCart.getPlaceNum() == null) {
|
||||
cashierCart.setPlaceNum(orderInfo.getPlaceNum());
|
||||
}
|
||||
|
||||
// 先付费模式,结束购物车状态
|
||||
if (!shopEatTypeInfoDTO.isDineInAfter() || StrUtil.isBlank(shopEatTypeInfoDTO.getTableId())) {
|
||||
cashierCart.setStatus("final");
|
||||
}
|
||||
mpCashierCartMapper.updateById(cashierCart);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateDetailAndPrint(TbOrderInfo orderInfo, OrderPriceDTO priceDTO, ShopEatTypeInfoDTO shopEatTypeInfoDTO) {
|
||||
private void updateDetailAndPrint(TbOrderInfo orderInfo, OrderPriceDTO priceDTO, ShopEatTypeInfoDTO shopEatTypeInfoDTO, boolean onlyChangeCoupon) {
|
||||
// 添加订单详细数据
|
||||
Integer orderId = orderInfo.getId();
|
||||
for (TbOrderDetail orderDetail : priceDTO.getOrderDetailList()) {
|
||||
@@ -904,11 +908,11 @@ public class CartService {
|
||||
}
|
||||
|
||||
// 菜品票
|
||||
if (!priceDTO.getNewOrderDetailList().isEmpty() && shopEatTypeInfoDTO.isDineInAfter()) {
|
||||
if (!priceDTO.getNewOrderDetailList().isEmpty() && shopEatTypeInfoDTO.isDineInAfter() && !onlyChangeCoupon) {
|
||||
mQUtils.printDishesTicket(orderInfo.getId(), false, priceDTO.getNewOrderDetailList().toArray(new TbOrderDetail[0]));
|
||||
}
|
||||
|
||||
if (!priceDTO.getRemoveOrderDetailIds().isEmpty()) {
|
||||
if (!priceDTO.getRemoveOrderDetailIds().isEmpty() && !onlyChangeCoupon) {
|
||||
// 退单票
|
||||
orderDetailService.removeByIds(priceDTO.getRemoveOrderDetailIds());
|
||||
if (shopEatTypeInfoDTO.isDineInAfter()) {
|
||||
@@ -983,10 +987,8 @@ public class CartService {
|
||||
private TbOrderInfo createOrderInfoWithCoupon(CreateOrderDTO createOrderDTO, OrderPriceDTO priceDTO, ShopEatTypeInfoDTO eatTypeInfoDTO,
|
||||
TbOrderInfo orderInfo, TbCashierCart seatCart, TbShopUser shopUser, TbShopTable shopTable, OrderCouponInfoDTO couponInfoDTO) {
|
||||
Integer placeNum = getCurrentPlaceNum(eatTypeInfoDTO.getTableId(), eatTypeInfoDTO.getShopInfo().getId(), eatTypeInfoDTO);
|
||||
boolean isFirst = false;
|
||||
// 修改订单信息
|
||||
if (orderInfo == null) {
|
||||
isFirst = true;
|
||||
String orderNo = generateOrderNumber(null);
|
||||
orderInfo = new TbOrderInfo();
|
||||
orderInfo.setOrderNo(orderNo);
|
||||
@@ -1036,7 +1038,7 @@ public class CartService {
|
||||
}
|
||||
|
||||
private OrderPriceDTO createOrderDetailWithCoupon(List<TbCashierCart> fullCashierCarts, Integer orderId,
|
||||
Integer shopId, ShopEatTypeInfoDTO shopEatTypeInfoDTO) {
|
||||
Integer shopId, ShopEatTypeInfoDTO shopEatTypeInfoDTO, boolean onlyChangeCoupon) {
|
||||
OrderPriceDTO priceDTO = new OrderPriceDTO();
|
||||
|
||||
List<Integer> cartIds = fullCashierCarts.stream().map(TbCashierCart::getId).collect(Collectors.toList());
|
||||
@@ -1067,8 +1069,6 @@ public class CartService {
|
||||
cashierCart.setPackFee(BigDecimal.ZERO);
|
||||
}
|
||||
|
||||
cashierCart.setStatus(shopEatTypeInfoDTO.isDineInAfter() ? "create" : "final");
|
||||
|
||||
// 已经退款和使用优惠券的商品不进行统计金额
|
||||
if (!"return".equals(cashierCart.getStatus()) && cashierCart.getUserCouponId() == null) {
|
||||
priceDTO.setTotalAmount(priceDTO.getTotalAmount().add(cashierCart.getTotalAmount()));
|
||||
@@ -1106,7 +1106,10 @@ public class CartService {
|
||||
orderDetail.setProductName(cashierCart.getName());
|
||||
orderDetail.setShopId(Integer.valueOf(cashierCart.getShopId()));
|
||||
orderDetail.setPackAmount(cashierCart.getPackFee());
|
||||
orderDetail.setStatus(TableConstant.CashierCart.Status.RETURN.equalsVals(cashierCart.getStatus()) ? cashierCart.getStatus() : "unpaid");
|
||||
if (!onlyChangeCoupon) {
|
||||
cashierCart.setStatus(shopEatTypeInfoDTO.isDineInAfter() ? "create" : "final");
|
||||
orderDetail.setStatus(TableConstant.CashierCart.Status.RETURN.equalsVals(cashierCart.getStatus()) ? cashierCart.getStatus() : "unpaid");
|
||||
}
|
||||
orderDetail.setUseType(cashierCart.getUseType());
|
||||
orderDetail.setProductImg(cashierCart.getCoverImg());
|
||||
orderDetail.setCartId(cashierCart.getId());
|
||||
@@ -1349,7 +1352,12 @@ public class CartService {
|
||||
ShopEatTypeInfoDTO shopEatTypeInfoDTO = shopUtils.checkEatModel(tableId, shopId);
|
||||
|
||||
// 查询购物车数据并获取台桌信息
|
||||
List<TbCashierCart> cashierCartList = cashierCartService.selectByShopEatTypeInfo(shopEatTypeInfoDTO, orderDTO.getUserId());
|
||||
List<TbCashierCart> cashierCartList;
|
||||
if (orderDTO.isOnlyChangeCoupon()) {
|
||||
cashierCartList = cashierCartService.selectByOrderId(orderDTO.getShopId(), orderDTO.getOrderId());
|
||||
}else {
|
||||
cashierCartList = cashierCartService.selectByShopEatTypeInfo(shopEatTypeInfoDTO, orderDTO.getUserId());
|
||||
}
|
||||
if (cashierCartList.isEmpty()) {
|
||||
return Result.fail("购物车为空");
|
||||
}
|
||||
@@ -1384,7 +1392,7 @@ public class CartService {
|
||||
}
|
||||
|
||||
// 创建订单详情数据
|
||||
OrderPriceDTO detailPriceDTO = createOrderDetailWithCoupon(cartInfoDTO.getCashierCarts(), cartInfoDTO.getOrderId(), orderDTO.getShopId(), shopEatTypeInfoDTO);
|
||||
OrderPriceDTO detailPriceDTO = createOrderDetailWithCoupon(cartInfoDTO.getCashierCarts(), cartInfoDTO.getOrderId(), orderDTO.getShopId(), shopEatTypeInfoDTO, orderDTO.isOnlyChangeCoupon());
|
||||
detailPriceDTO.setProductDiscountAmount(discountAmount);
|
||||
|
||||
// 是否是第一次创建订单
|
||||
@@ -1411,15 +1419,17 @@ public class CartService {
|
||||
}
|
||||
|
||||
// 修改订单详情并打票
|
||||
updateDetailAndPrint(orderInfo, detailPriceDTO, shopEatTypeInfoDTO);
|
||||
updateDetailAndPrint(orderInfo, detailPriceDTO, shopEatTypeInfoDTO, orderDTO.isOnlyChangeCoupon());
|
||||
|
||||
// 修改购物车状态和库存
|
||||
updateCartAndStock(cartInfoDTO.getNewCashierCarts(), orderInfo, shopEatTypeInfoDTO);
|
||||
updateCartAndStock(cartInfoDTO.getNewCashierCarts(), orderInfo, shopEatTypeInfoDTO, orderDTO.isOnlyChangeCoupon());
|
||||
|
||||
// 推送耗材信息
|
||||
pushConsMsg(orderInfo, cartInfoDTO.getNewCashierCarts());
|
||||
if (!orderDTO.isOnlyChangeCoupon()) {
|
||||
// 推送耗材信息
|
||||
pushConsMsg(orderInfo, cartInfoDTO.getNewCashierCarts());
|
||||
|
||||
updateTableState(shopEatTypeInfoDTO, orderInfo, cartInfoDTO.getCashierCarts(), cartInfoDTO.getSeatCart());
|
||||
updateTableState(shopEatTypeInfoDTO, orderInfo, cartInfoDTO.getCashierCarts(), cartInfoDTO.getSeatCart());
|
||||
}
|
||||
|
||||
// 消耗优惠券
|
||||
if (!orderDTO.getUserCouponInfos().isEmpty() && couponInfo != null) {
|
||||
@@ -1428,8 +1438,10 @@ public class CartService {
|
||||
|
||||
mpOrderInfoMapper.updateById(orderInfo);
|
||||
|
||||
String tableCartKey = RedisCst.getTableCartKey(shopId, shopEatTypeInfoDTO.isOpenDineIn() ? tableId : null, Integer.valueOf(userId));
|
||||
redisUtil.deleteByKey(tableCartKey);
|
||||
if (!orderDTO.isOnlyChangeCoupon()) {
|
||||
String tableCartKey = RedisCst.getTableCartKey(shopId, shopEatTypeInfoDTO.isOpenDineIn() ? tableId : null, Integer.valueOf(userId));
|
||||
redisUtil.deleteByKey(tableCartKey);
|
||||
}
|
||||
return Result.successWithData(orderInfo);
|
||||
} catch (Exception e) {
|
||||
log.info("长链接错误 createOrder{}", e.getMessage());
|
||||
@@ -1573,4 +1585,24 @@ public class CartService {
|
||||
// 所有订单信息
|
||||
return cashierCartList;
|
||||
}
|
||||
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Result useCoupon(UseCouponDTO useCouponDTO) {
|
||||
// 获取订单信息
|
||||
TbOrderInfo orderInfo = mpOrderInfoMapper.selectByShopIdAndId(useCouponDTO.getShopId(), useCouponDTO.getOrderId());
|
||||
if (!"unpaid".equals(orderInfo.getStatus())) {
|
||||
return Result.fail("订单正在支付中,请稍后再试");
|
||||
}
|
||||
|
||||
CreateOrderDTO orderDTO = new CreateOrderDTO();
|
||||
orderDTO.setShopId(Integer.valueOf(orderInfo.getShopId()));
|
||||
orderDTO.setTableId(orderInfo.getTableId());
|
||||
orderDTO.setRemark(orderInfo.getRemark());
|
||||
orderDTO.setSendType(orderInfo.getSendType());
|
||||
orderDTO.setUserId(Integer.valueOf(orderInfo.getUserId()));
|
||||
orderDTO.setUserCouponInfos(useCouponDTO.getUserCouponInfos());
|
||||
orderDTO.setOnlyChangeCoupon(true);
|
||||
return createOrder(orderDTO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,4 +15,5 @@ public interface TbCashierCartService extends IService<TbCashierCart> {
|
||||
*/
|
||||
List<TbCashierCart> selectByShopEatTypeInfo(ShopEatTypeInfoDTO shopEatTypeInfoDTO, Integer userId);
|
||||
|
||||
List<TbCashierCart> selectByOrderId(Integer shopId, Integer orderId);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.chaozhanggui.system.cashierservice.service.impl;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.chaozhanggui.system.cashierservice.constant.TableConstant;
|
||||
import com.chaozhanggui.system.cashierservice.entity.TbCashierCart;
|
||||
import com.chaozhanggui.system.cashierservice.entity.TbShopTable;
|
||||
import com.chaozhanggui.system.cashierservice.entity.dto.ShopEatTypeInfoDTO;
|
||||
@@ -45,6 +46,13 @@ public class TbCashierCartServiceImpl extends ServiceImpl<MpCashierCartMapper, T
|
||||
// 所有订单信息
|
||||
return list(queryWrapper.orderByAsc(TbCashierCart::getTotalAmount));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TbCashierCart> selectByOrderId(Integer shopId, Integer orderId) {
|
||||
return list(new LambdaQueryWrapper<TbCashierCart>()
|
||||
.eq(TbCashierCart::getOrderId, orderId)
|
||||
.eq(TbCashierCart::getShopId, shopId));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user