diff --git a/cash-api/order-server/src/main/java/com/czg/controller/admin/AdminOrderController.java b/cash-api/order-server/src/main/java/com/czg/controller/admin/AdminOrderController.java index f67d59e3c..f552e3a6e 100644 --- a/cash-api/order-server/src/main/java/com/czg/controller/admin/AdminOrderController.java +++ b/cash-api/order-server/src/main/java/com/czg/controller/admin/AdminOrderController.java @@ -1,7 +1,6 @@ package com.czg.controller.admin; -import com.czg.order.dto.OrderInfoAddDTO; -import com.czg.order.dto.OrderInfoQueryDTO; +import com.czg.order.dto.*; import com.czg.order.entity.OrderInfo; import com.czg.order.service.OrderInfoService; import com.czg.order.vo.OrderInfoVo; @@ -11,6 +10,7 @@ import com.czg.utils.AssertUtil; import com.czg.utils.ServletUtil; import com.mybatisflex.core.paginate.Page; import jakarta.annotation.Resource; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -40,7 +40,7 @@ public class AdminOrderController { } @PostMapping("/createOrder") - public CzgResult createOrder(@RequestBody OrderInfoAddDTO addDto) { + public CzgResult createOrder(@Validated @RequestBody OrderInfoAddDTO addDto) { AssertUtil.isNull(addDto.getUserId(), "请选择下单用户后使用"); addDto.setPlatformType(ServletUtil.getHeaderIgnoreCase(ServletUtil.getRequest(), "platformType")); addDto.setStaffId(StpKit.USER.getLoginIdAsLong()); @@ -48,4 +48,23 @@ public class AdminOrderController { addDto.setOrderType("cash"); return CzgResult.success(orderInfoService.createOrder(addDto)); } + +// @PostMapping("/updateOrder") +// public CzgResult updateOrder(@Validated @RequestBody OrderInfoUpDTO upDTO) { +// AssertUtil.isListEmpty(upDTO.getDetailList(), "订单详情不能为空"); +// orderInfoService.updateOrder(upDTO); +// return CzgResult.success(); +// } + + /** + * 订单全额退款 只传订单id + * 部分退款 传参refundDetailMap {"详情id":"数量","详情id":"数量"} + * + * @param refundDTO + * @return + */ +// @PostMapping("/refundOrder") +// public CzgResult refundOrder(@Validated @RequestBody OrderInfoRefundDTO refundDTO) { +// return orderInfoService.refundOrder(refundDTO); +// } } diff --git a/cash-common/cash-common-service/src/main/java/com/czg/account/dto/ShopCouponDTO.java b/cash-common/cash-common-service/src/main/java/com/czg/account/dto/ShopCouponDTO.java index 7be5e6ba4..6254cdb5d 100644 --- a/cash-common/cash-common-service/src/main/java/com/czg/account/dto/ShopCouponDTO.java +++ b/cash-common/cash-common-service/src/main/java/com/czg/account/dto/ShopCouponDTO.java @@ -121,6 +121,12 @@ public class ShopCouponDTO implements Serializable { */ private BigDecimal discountAmount; + /** + * 商品id + */ + private Long proId; + private String proName; + /** * 描述 */ diff --git a/cash-common/cash-common-service/src/main/java/com/czg/order/dto/CheckOrderPay.java b/cash-common/cash-common-service/src/main/java/com/czg/order/dto/CheckOrderPay.java new file mode 100644 index 000000000..1b655b1d2 --- /dev/null +++ b/cash-common/cash-common-service/src/main/java/com/czg/order/dto/CheckOrderPay.java @@ -0,0 +1,88 @@ + +package com.czg.order.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.math.BigDecimal; +import java.util.List; + +/** + * 订单表 实体类。 + * + * @author ww + * @since 2025-02-13 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class CheckOrderPay implements Serializable { + + private Long orderId; + + private Long userId; + + /** + * 用餐人数 + */ + private Integer seatNum; + + /** + * 订单原金额(包含打包费+餐位费) 不含折扣价格 + */ + private BigDecimal originAmount; + + /** + * 折扣比例(计算时 向上取整保留 两位小数) + */ + private BigDecimal discountRatio; + /** + * 手动优惠金额 + */ + private BigDecimal discountAmount; + + /** + * 商品优惠券抵扣金额 + */ + private BigDecimal productCouponDiscountAmount; + /** + * 满减优惠券抵扣金额 + */ + private BigDecimal fullCouponDiscountAmount; + /** + * 用户使用的卡券 + */ + private List couponList; + + /** + * 订单金额 + */ + private BigDecimal orderAmount; + /** + * 抹零金额 减免多少钱 + */ + private BigDecimal roundAmount; + + /** + * 积分抵扣金额(tb_points_basic_setting表) + */ + private BigDecimal pointsDiscountAmount; + + /** + * 使用的积分数量 (扣除各类折扣 enable_deduction后使用) + */ + private Integer pointsNum; + + + public Integer getSeatNum() { + return seatNum == null ? 0 : seatNum; + } + + public Integer getPointsNum() { + return pointsNum == null ? 0 : pointsNum; + } +} diff --git a/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoAddDTO.java b/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoAddDTO.java index 28e7b0b03..e69ba7ec4 100644 --- a/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoAddDTO.java +++ b/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoAddDTO.java @@ -10,7 +10,6 @@ import lombok.NoArgsConstructor; import java.io.Serializable; import java.math.BigDecimal; -import java.util.List; /** * 订单表 实体类。 @@ -47,47 +46,6 @@ public class OrderInfoAddDTO implements Serializable { */ private BigDecimal originAmount; - /** - * 折扣比例(计算时 向上取整保留 两位小数) - */ - private BigDecimal discountRatio; - /** - * 手动优惠金额 - */ - private BigDecimal discountAmount; - - /** - * 商品优惠券抵扣金额 - */ - private BigDecimal productCouponDiscountAmount; - /** - * 满减优惠券抵扣金额 - */ - private BigDecimal fullCouponDiscountAmount; - /** - * 用户使用的卡券 - */ - private List couponList; - - /** - * 订单金额 - */ - private BigDecimal orderAmount; - /** - * 抹零金额 减免多少钱 - */ - private BigDecimal roundAmount; - - /** - * 积分抵扣金额(tb_points_basic_setting表) - */ - private BigDecimal pointsDiscountAmount; - - /** - * 使用的积分数量 (扣除各类折扣 enable_deduction后使用) - */ - private Integer pointsNum; - /** * 台桌Id */ @@ -109,6 +67,13 @@ public class OrderInfoAddDTO implements Serializable { * APP APP管理端 */ private String platformType; + /** + * 支付模式: + * 后付费 after-pay + * 先付费 before-pay + * 无桌码 no-table + */ + private String payMode; /** * 用餐模式 堂食 dine-in 外带 take-out 外卖 take-away @@ -116,12 +81,6 @@ public class OrderInfoAddDTO implements Serializable { @NotBlank(message = "用餐模式不能为空") private String dineMode; - /** - * 支付模式: afterPay 后付费 normal 正常模式 buyer 挂账模式 - */ - @NotBlank(message = "付款模式不能为空") - private String payMode; - /** * 备注 */ @@ -152,8 +111,4 @@ public class OrderInfoAddDTO implements Serializable { public String getDineMode() { return StrUtil.isBlank(dineMode) ? "dine-in" : dineMode; } - - public Integer getPointsNum() { - return pointsNum == null ? 0 : pointsNum; - } } diff --git a/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoDTO.java b/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoDTO.java index efb1b2adb..0526ed41a 100644 --- a/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoDTO.java +++ b/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoDTO.java @@ -108,7 +108,7 @@ public class OrderInfoDTO implements Serializable { /** * 折扣比例 */ - private String discountRatio; + private BigDecimal discountRatio; /** * 打包费 @@ -143,7 +143,10 @@ public class OrderInfoDTO implements Serializable { private String dineMode; /** - * 支付模式: afterPay 后付费 normal 正常模式 buyer 挂账模式 + * 支付模式: + * 后付费 after-pay + * 先付费 before-pay + * 无桌码 no-table */ private String payMode; diff --git a/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoRefundDTO.java b/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoRefundDTO.java new file mode 100644 index 000000000..735ac9950 --- /dev/null +++ b/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoRefundDTO.java @@ -0,0 +1,62 @@ + +package com.czg.order.dto; + +import cn.hutool.core.util.StrUtil; +import com.czg.order.entity.OrderDetail; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.math.BigDecimal; +import java.util.List; + +/** + * 退单Dto。 + * + * @author ww + * @since 2025-02-13 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class OrderInfoRefundDTO implements Serializable { + + @NotNull(message = "订单Id不能为空") + private Long orderId; + + /** + * 退单总金额 + */ + @NotNull(message = "退单金额不能为空") + private BigDecimal refundAmount; + + /** + * 退款金额 默认为 退单数量*单价 的和 + * 整单退款 为订单orderAmount + * 如果自定义退款金额 则金额不进行校验 直接退款 + */ + private boolean modify; + + private String refundReason; + + /** + * 退单明细 + * id: orderDetailId + * num: 退单数量 + * returnAmount: 退单金额 + */ + private List refundDetails; + + public String getRefundReason() { + if (StrUtil.isNotBlank(refundReason)) { + refundReason = refundReason.trim().replaceAll(" ", ","); + return refundReason; + } else { + return ""; + } + } +} diff --git a/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoUpDTO.java b/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoUpDTO.java new file mode 100644 index 000000000..bf8e274a1 --- /dev/null +++ b/cash-common/cash-common-service/src/main/java/com/czg/order/dto/OrderInfoUpDTO.java @@ -0,0 +1,32 @@ + +package com.czg.order.dto; + +import com.czg.order.entity.OrderDetail; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.util.List; + +/** + * 订单表 实体类。 + * + * @author ww + * @since 2025-02-13 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class OrderInfoUpDTO implements Serializable { + + @NotNull(message = "订单Id不能为空") + private Long orderId; + + private Integer seatNum; + + private List detailList; +} diff --git a/cash-common/cash-common-service/src/main/java/com/czg/order/entity/OrderDetail.java b/cash-common/cash-common-service/src/main/java/com/czg/order/entity/OrderDetail.java index 2fe578f07..9995e11c3 100644 --- a/cash-common/cash-common-service/src/main/java/com/czg/order/entity/OrderDetail.java +++ b/cash-common/cash-common-service/src/main/java/com/czg/order/entity/OrderDetail.java @@ -1,5 +1,6 @@ package com.czg.order.entity; +import cn.hutool.core.util.StrUtil; import com.mybatisflex.annotation.Column; import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.KeyType; @@ -157,4 +158,15 @@ public class OrderDetail implements Serializable { @Column(onInsertValue = "now()", onUpdateValue = "now()") private LocalDateTime updateTime; + public BigDecimal getRefundNum() { + return refundNum == null ? BigDecimal.ZERO : refundNum; + } + + public BigDecimal getReturnAmount() { + return returnAmount == null ? BigDecimal.ZERO : returnAmount; + } + + public String getRefundRemark() { + return StrUtil.isBlank(refundRemark) ? "" : refundRemark + " "; + } } diff --git a/cash-common/cash-common-service/src/main/java/com/czg/order/entity/OrderInfo.java b/cash-common/cash-common-service/src/main/java/com/czg/order/entity/OrderInfo.java index 4c82ae061..696ba4470 100644 --- a/cash-common/cash-common-service/src/main/java/com/czg/order/entity/OrderInfo.java +++ b/cash-common/cash-common-service/src/main/java/com/czg/order/entity/OrderInfo.java @@ -113,7 +113,7 @@ public class OrderInfo implements Serializable { /** * 折扣比例 */ - private String discountRatio; + private BigDecimal discountRatio; /** * 打包费 @@ -148,7 +148,10 @@ public class OrderInfo implements Serializable { private String dineMode; /** - * 支付模式: afterPay 后付费 normal 正常模式 buyer 挂账模式 + * 支付模式: + * 后付费 after-pay + * 先付费 before-pay + * 无桌码 no-table */ private String payMode; @@ -164,6 +167,7 @@ public class OrderInfo implements Serializable { private String payType; /** + * OrderStatusEnums 枚举类 * 状态: unpaid-待支付;in-production 制作中;wait-out 待取餐;;done-订单完成;refunding-申请退单;refund-退单;part-refund 部分退单;cancelled-取消订单 */ private String status; @@ -258,47 +262,8 @@ public class OrderInfo implements Serializable { */ private Integer isDel; - public BigDecimal getOriginAmount() { - return originAmount == null ? BigDecimal.ZERO : originAmount; - } - public BigDecimal getRoundAmount() { - return roundAmount == null ? BigDecimal.ZERO : roundAmount; - } - - public BigDecimal getOrderAmount() { - return orderAmount == null ? BigDecimal.ZERO : orderAmount; - } - - public BigDecimal getPointsDiscountAmount() { - return pointsDiscountAmount == null ? BigDecimal.ZERO : pointsDiscountAmount; - } - - public Integer getPointsNum() { - return pointsNum == null ? 0 : pointsNum; - } - - public BigDecimal getProductCouponDiscountAmount() { - return productCouponDiscountAmount == null ? BigDecimal.ZERO : productCouponDiscountAmount; - } - - public String getCouponInfoList() { - return StrUtil.isBlank(couponInfoList) ? "" : couponInfoList; - } - - public BigDecimal getFullCouponDiscountAmount() { - return fullCouponDiscountAmount == null ? BigDecimal.ZERO : fullCouponDiscountAmount; - } - - public BigDecimal getDiscountAmount() { - return discountAmount == null ? BigDecimal.ZERO : discountAmount; - } - - public String getDiscountRatio() { - return discountRatio == null ? "" : discountRatio + ","; - } - - public BigDecimal getPackFee() { - return packFee == null ? BigDecimal.ZERO : packFee; + public String getRefundRemark() { + return StrUtil.isBlank(refundRemark) ? "" : refundRemark + " "; } } diff --git a/cash-common/cash-common-service/src/main/java/com/czg/order/service/OrderInfoService.java b/cash-common/cash-common-service/src/main/java/com/czg/order/service/OrderInfoService.java index 659d6240d..5ca37db79 100644 --- a/cash-common/cash-common-service/src/main/java/com/czg/order/service/OrderInfoService.java +++ b/cash-common/cash-common-service/src/main/java/com/czg/order/service/OrderInfoService.java @@ -1,11 +1,11 @@ package com.czg.order.service; import com.alibaba.fastjson2.JSONObject; -import com.czg.order.dto.OrderInfoAddDTO; -import com.czg.order.dto.OrderInfoQueryDTO; +import com.czg.order.dto.*; import com.czg.order.entity.OrderInfo; import com.czg.order.enums.PayEnums; import com.czg.order.vo.OrderInfoVo; +import com.czg.resp.CzgResult; import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.service.IService; import jakarta.validation.constraints.NotBlank; @@ -25,6 +25,12 @@ public interface OrderInfoService extends IService { Page getOrderByPage(OrderInfoQueryDTO param); OrderInfo createOrder(OrderInfoAddDTO param); +// void updateOrder(OrderInfoUpDTO upDTO); + + BigDecimal checkOrderPay(CheckOrderPay param); + + CzgResult refundOrder(OrderInfoRefundDTO param); + void payCallBackOrder(@NotBlank String orderNo, @NotNull JSONObject resultJson); void refundCallBackOrder(@NotBlank String orderNo, @NotNull JSONObject resultJson); diff --git a/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/ProductServiceImpl.java b/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/ProductServiceImpl.java index 31234cc23..faa7f2993 100644 --- a/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/ProductServiceImpl.java +++ b/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/ProductServiceImpl.java @@ -1,10 +1,10 @@ package com.czg.service.account.service.impl; -import com.mybatisflex.spring.service.impl.ServiceImpl; import com.czg.account.entity.Product; import com.czg.account.service.ProductService; import com.czg.service.account.mapper.ProductMapper; -import org.springframework.stereotype.Service; +import com.mybatisflex.spring.service.impl.ServiceImpl; +import org.apache.dubbo.config.annotation.DubboService; /** * 商品 服务层实现。 @@ -12,7 +12,7 @@ import org.springframework.stereotype.Service; * @author zs * @since 2025-02-20 */ -@Service +@DubboService public class ProductServiceImpl extends ServiceImpl implements ProductService{ } diff --git a/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/ShopCouponServiceImpl.java b/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/ShopCouponServiceImpl.java index 163e31ed1..87da97b92 100644 --- a/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/ShopCouponServiceImpl.java +++ b/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/ShopCouponServiceImpl.java @@ -2,14 +2,17 @@ package com.czg.service.account.service.impl; import cn.hutool.core.bean.BeanUtil; import com.czg.account.dto.ShopCouponDTO; +import com.czg.account.entity.Product; import com.czg.account.entity.ShopActivateCouponRecord; import com.czg.account.entity.ShopCoupon; +import com.czg.account.service.ProductService; import com.czg.account.service.ShopActivateCouponRecordService; import com.czg.account.service.ShopCouponService; import com.czg.sa.StpKit; import com.czg.service.account.mapper.ShopCouponMapper; import com.mybatisflex.spring.service.impl.ServiceImpl; import jakarta.annotation.Resource; +import org.apache.dubbo.config.annotation.DubboReference; import org.apache.dubbo.config.annotation.DubboService; import java.util.List; @@ -25,13 +28,22 @@ public class ShopCouponServiceImpl extends ServiceImpl getList(Long shopId, Integer type, Integer status) { - return queryChain().select().eq(ShopCoupon::getShopId, shopId) + List coupons = queryChain().select().eq(ShopCoupon::getShopId, shopId) .eq(ShopCoupon::getType, type) .eq(ShopCoupon::getStatus, status) .orderBy(ShopCoupon::getCreateTime).desc().listAs(ShopCouponDTO.class); + coupons.forEach(coupon -> { + if (coupon.getProId() != null) { + Product product = productService.getById(coupon.getProId()); + coupon.setProName(product.getName()); + } + }); + return coupons; } @Override @@ -51,13 +63,14 @@ public class ShopCouponServiceImpl extends ServiceImpl find(Long shopUserId, Integer status) { - return couponRecordService.queryChain() + List list = couponRecordService.queryChain() .eq(ShopActivateCouponRecord::getShopId, StpKit.USER.getShopId()) .eq(ShopActivateCouponRecord::getShopUserId, shopUserId) .eq(ShopActivateCouponRecord::getStatus, status) .orderBy(ShopActivateCouponRecord::getStatus).asc() .orderBy(ShopActivateCouponRecord::getCreateTime).desc() .select().list(); + return list; } @Override diff --git a/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderInfoServiceImpl.java b/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderInfoServiceImpl.java index 5b3a467cc..0a91a99e8 100644 --- a/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderInfoServiceImpl.java +++ b/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderInfoServiceImpl.java @@ -15,8 +15,7 @@ import com.czg.config.RedisCst; import com.czg.entity.notify.CzgPayNotifyDTO; import com.czg.entity.notify.CzgRefundNotifyDTO; import com.czg.enums.ShopUserFlowBizEnum; -import com.czg.order.dto.OrderInfoAddDTO; -import com.czg.order.dto.OrderInfoQueryDTO; +import com.czg.order.dto.*; import com.czg.order.entity.OrderDetail; import com.czg.order.entity.OrderInfo; import com.czg.order.entity.OrderPayment; @@ -27,6 +26,8 @@ import com.czg.order.service.OrderInfoService; import com.czg.order.service.OrderPaymentService; import com.czg.order.vo.OrderDetailSmallVO; import com.czg.order.vo.OrderInfoVo; +import com.czg.resp.CzgResult; +import com.czg.sa.StpKit; import com.czg.service.RedisService; import com.czg.service.order.enums.OrderStatusEnums; import com.czg.service.order.mapper.OrderInfoMapper; @@ -123,6 +124,7 @@ public class OrderInfoServiceImpl extends ServiceImpl 0 && !pointSetting.getEnableDeduction().equals(1)) { - throw new ValidateException("生成订单失败,该店铺未开启积分抵扣"); - } + UserInfo userInfo = userInfoService.getById(param.getUserId()); AssertUtil.isNull(userInfo, "生成订单失败,用户信息不存在"); - ShopUser shopUser = shopUserService.getShopUserInfo(param.getShopId(), param.getUserId()); - if (!shopUser.getIsVip().equals(1) && pointSetting.getDeductionGroup().contains("vip")) { - throw new ValidateException("生成订单失败,该店铺仅会员可使用积分抵扣"); - } ShopTable table = shopTableService.getOneByTableCode(param.getShopId(), param.getTableCode()); - AssertUtil.isNull(table, "生成订单失败,桌台信息不存在"); + if (table == null) { + param.setPayMode("no-table"); + } else { + if ("before".equals(shopInfo.getRegisterType())) { + param.setPayMode("before-pay"); + } else if ("after".equals(shopInfo.getRegisterType())) { + param.setPayMode("after-pay"); + } else { + param.setPayMode("未知"); + } + } + //获取商品信息 计算金额 需要传入优惠券 减去优惠券 + List orderDetails = cartService.getCartByTableCode(param.getTableCode(), param.isVipPrice() ? 1 : 0, param.getPlaceNum()); + //总打包费 + BigDecimal packAmount = BigDecimal.ZERO; + //总商品支付金额 不包含打包费 用来计算后续 + BigDecimal totalAmount = BigDecimal.ZERO; + processOrderDetails(orderDetails, packAmount, totalAmount); + if (packAmount.compareTo(param.getPackFee()) != 0) { + throw new ValidateException("生成订单失败,打包费不正确"); + } + //总金额 + if (totalAmount.add(param.getPackFee()).compareTo(param.getOriginAmount()) != 0) { + throw new ValidateException("生成订单失败,订单金额不正确"); + } + //生成订单 + OrderInfo orderInfo = initOrderInfo(param, shopInfo, table); + orderDetailService.createOrderDetails(orderInfo.getId(), orderDetails); + return orderInfo; + } + +// @Override +// @Transactional +// public void updateOrder(OrderInfoUpDTO upDTO) { +// OrderInfo orderInfo = getById(upDTO.getOrderId()); +// AssertUtil.isNull(orderInfo, "订单不存在"); +// ShopInfo shopInfo = shopInfoService.getById(orderInfo.getShopId()); +// AssertUtil.isNull(shopInfo, "店铺不存在"); +// BigDecimal totalAmount = BigDecimal.ZERO; +// BigDecimal packAmount = BigDecimal.ZERO; +// List collect = upDTO.getDetailList().stream().map(OrderDetail::getId).toList(); +// List orderDetails = detailService.queryChain().eq(OrderDetail::getOrderId, orderInfo.getId()).select().list(); +// List updateList = new ArrayList<>(); +// List removeList = new ArrayList<>(); +// for (OrderDetail orderDetail : orderDetails) { +// if (!collect.contains(orderDetail.getId())) { +// removeList.add(orderDetail.getId()); +// break; +// } +// OrderDetail detail = new OrderDetail(); +// detail.setId(orderDetail.getId()); +// detail.setNum(orderDetail.getNum()); +// detail.setPackNumber(orderDetail.getPackNumber()); +// detail.setIsWaitCall(orderDetail.getIsWaitCall()); +// detail.setRemark(orderDetail.getRemark()); +// detail.setPayAmount(orderDetail.getNum().multiply(detail.getPrice())); +// packAmount = packAmount.add(detail.getPackAmount().multiply(detail.getPackNumber())); +// totalAmount = totalAmount.add(detail.getPayAmount()); +// } +// detailService.updateBatch(updateList); +// detailService.removeByIds(removeList); +// orderInfo.setPackFee(packAmount); +// orderInfo.setOriginAmount(totalAmount.add(orderInfo.getSeatAmount())); +// +// orderInfo.setSeatNum(upDTO.getSeatNum()); +// orderInfo.setSeatAmount(shopInfo.getTableFee().multiply(new BigDecimal(upDTO.getSeatNum()))); +// updateById(orderInfo); +// } + + @Override + public BigDecimal checkOrderPay(CheckOrderPay param) { + OrderInfo orderInfo = getById(param.getOrderId()); + Long shopId = StpKit.USER.getShopId(); + PointsBasicSetting pointSetting = pointsBasicService.getById(shopId); + if (param.getPointsNum() > 0 && !pointSetting.getEnableDeduction().equals(1)) { + throw new ValidateException("生成支付订单失败,该店铺未开启积分抵扣"); + } + UserInfo userInfo = userInfoService.getById(param.getUserId()); + AssertUtil.isNull(userInfo, "生成支付订单失败,用户信息不存在"); + ShopUser shopUser = shopUserService.getShopUserInfo(shopId, param.getUserId()); + if (!shopUser.getIsVip().equals(1) && pointSetting.getDeductionGroup().contains("vip")) { + throw new ValidateException("生成支付订单失败,该店铺仅会员可使用积分抵扣"); + } if (param.getDiscountRatio().compareTo(BigDecimal.ZERO) <= 0 && param.getDiscountRatio().compareTo(BigDecimal.ONE) > 0) { - throw new ValidateException("生成订单失败,折扣比例不正确"); + throw new ValidateException("生成支付订单失败,折扣比例不正确"); } //商品券 <商品id,数量> Map prodCouponMap = new HashMap<>(); @@ -162,63 +236,166 @@ public class OrderInfoServiceImpl extends ServiceImpl orderDetails = cartService.getCartByTableCode(table.getTableCode(), param.isVipPrice() ? 1 : 0, param.getPlaceNum()); - //总打包费 - BigDecimal packAmount = BigDecimal.ZERO; + List orderDetails = orderDetailService.queryChain().eq(OrderDetail::getOrderId, param.getOrderId()).select().list(); //商品优惠券金额 BigDecimal prodCouponAmount = BigDecimal.ZERO; //总商品支付金额 不包含打包费 用来计算后续 BigDecimal totalAmount = BigDecimal.ZERO; - processOrderDetails(orderDetails, prodCouponMap, prodCouponAmount, packAmount, totalAmount); - if (packAmount.compareTo(param.getPackFee()) != 0) { - throw new ValidateException("生成订单失败,打包费不正确"); - } + processOrderDetails2(orderDetails, prodCouponMap, prodCouponAmount, totalAmount); if (prodCouponAmount.compareTo(param.getProductCouponDiscountAmount()) != 0) { - throw new ValidateException("生成订单失败,商品优惠券优惠金额不正确"); + throw new ValidateException("生成支付订单失败,商品优惠券优惠金额不正确"); } //折扣金额 如 9折 计算 为 订单金额*0.9 向上取整 BigDecimal newTotalAmount = BigDecimal.ZERO; if (param.getDiscountRatio().compareTo(BigDecimal.ONE) != 0) { newTotalAmount = totalAmount.multiply(param.getDiscountRatio()).setScale(2, RoundingMode.UP); if (param.getDiscountAmount().compareTo(totalAmount.subtract(newTotalAmount)) != 0) { - throw new ValidateException("生成订单失败,折扣金额不正确"); + throw new ValidateException("生成支付订单失败,折扣金额不正确"); } } //满减券 校验 if (newTotalAmount.compareTo(fullAmount) < 0) { - throw new ValidateException("生成订单失败,满减券不满足条件"); + throw new ValidateException("生成支付订单失败,满减券不满足条件"); } //减去满减优惠券 newTotalAmount = newTotalAmount.subtract(discountAmount); //积分抵扣 金额范围校验 抵扣金额校验 if (param.getPointsNum() > 0) { if (pointSetting.getMinPaymentAmount().compareTo(newTotalAmount) > 0) { - throw new ValidateException("生成订单失败,未满足积分抵扣最低门槛"); + throw new ValidateException("生成支付订单失败,未满足积分抵扣最低门槛"); } if (pointSetting.getMaxDeductionRatio().multiply(newTotalAmount).compareTo(param.getPointsDiscountAmount()) < 0) { - throw new ValidateException("生成订单失败,积分抵扣金额已超出最大抵扣金额"); + throw new ValidateException("生成支付订单失败,积分抵扣金额已超出最大抵扣金额"); } BigDecimal pointAmount = new BigDecimal(param.getPointsNum()).divide(new BigDecimal(pointSetting.getEquivalentPoints()), 2, RoundingMode.DOWN); if (pointAmount.compareTo(param.getPointsDiscountAmount()) != 0) { - throw new ValidateException("生成订单失败,积分抵扣金额不正确"); + throw new ValidateException("生成支付订单失败,积分抵扣金额不正确"); } } newTotalAmount = newTotalAmount.subtract(param.getPointsDiscountAmount()); //抹零 newTotalAmount = newTotalAmount.subtract(param.getRoundAmount()); //校验最终金额(订单金额 (扣除各类折扣)+打包费 餐位费) - newTotalAmount = newTotalAmount.add(param.getPackFee()).add(new BigDecimal(param.getSeatNum()).multiply(shopInfo.getTableFee())); + newTotalAmount = newTotalAmount.add(orderInfo.getPackFee()).add(orderInfo.getSeatAmount()); if (newTotalAmount.compareTo(param.getOrderAmount()) != 0) { - throw new ValidateException("生成订单失败,订单金额不正确"); + throw new ValidateException("生成支付订单失败,订单金额不正确"); } //生成订单 //discount_info 所有折扣 几折 折扣金额 满减金额 优惠券金额 积分抵扣金额 抹零 - OrderInfo orderInfo = initOrderInfo(param, shopInfo, table); - orderDetailService.createOrderDetails(orderInfo.getId(), orderDetails); + upOrderPayInfo(orderInfo, param); + //更新优惠券信息 + orderDetailService.updateBatch(orderDetails); //券消耗 couponService.use(param.getCouponList(), shopUser.getId(), orderInfo.getId()); - return orderInfo; + return newTotalAmount; } + @Override + @Transactional + public CzgResult refundOrder(OrderInfoRefundDTO param) { + OrderInfo orderInfo = getById(param.getOrderId()); + //退款总额 + BigDecimal refundAmountTotal = BigDecimal.ZERO; + boolean isPay = true; + if (orderInfo.getStatus().equals(OrderStatusEnums.UNPAID.getCode())) { + isPay = false; + } + if (CollUtil.isNotEmpty(param.getRefundDetails())) { + for (OrderDetail refundDetail : param.getRefundDetails()) { + AssertUtil.isNull(refundDetail.getNum(), "退单数量不能为空"); + AssertUtil.isNull(refundDetail.getReturnAmount(), "退单金额不能为空"); + //退款金额 + BigDecimal refundAmount; + //退款数量 + BigDecimal refNum; + OrderDetail orderDetail = detailService.getById(refundDetail.getId()); + //可退数量=订单数量-退单数量-退菜数量 + BigDecimal returnNum = orderDetail.getNum().subtract(orderDetail.getRefundNum()).subtract(orderDetail.getReturnNum()); + if (returnNum.compareTo(BigDecimal.ZERO) <= 0 || returnNum.compareTo(refundDetail.getNum()) < 0) { + throw new ValidateException("退单失败,可退数量不足"); + } + //可退数量 大于优惠券数量 + if (returnNum.compareTo(orderDetail.getCouponNum()) > 0) { + //实际计算金额的数量 + refNum = returnNum.subtract(orderDetail.getCouponNum()); + if (refundDetail.getNum().compareTo(refNum) > 0) { + refundAmount = (refNum).multiply(orderDetail.getPrice()); + refNum = refundDetail.getNum(); + } else { + refNum = refundDetail.getNum(); + refundAmount = refundDetail.getNum().multiply(orderDetail.getPrice()); + if (param.isModify()) { + refundAmount = refundDetail.getReturnAmount(); + } else { + if (refundAmount.compareTo(refundDetail.getReturnAmount()) != 0) { + throw new ValidateException("退单失败," + refundDetail.getProductName() + "退单金额不正确"); + } + } + } + } else { + if (param.isModify()) { + refundAmount = refundDetail.getReturnAmount(); + refNum = refundDetail.getNum(); + } else { + refNum = refundDetail.getNum(); + refundAmount = BigDecimal.ZERO; + if (refundAmount.compareTo(refundDetail.getReturnAmount()) != 0) { + throw new ValidateException("退单失败," + refundDetail.getProductName() + "退单金额不正确"); + } + } + } + refundAmountTotal = refundAmountTotal.add(refundAmount); + if (refundDetail.getReturnAmount().compareTo(refundDetail.getNum().multiply(orderDetail.getPrice())) != 0) { + throw new ValidateException("退单失败," + refundDetail.getProductName() + "退单金额不正确"); + } + if (isPay) { + orderDetail.setRefundNum(orderDetail.getRefundNum().add(refNum)); + orderDetail.setReturnAmount(orderDetail.getReturnAmount().add(refundDetail.getReturnAmount())); + if (orderDetail.getReturnAmount().compareTo(orderDetail.getPayAmount()) > 0) { + throw new ValidateException("退单失败," + refundDetail.getProductName() + "退单金额不正确"); + } + } else { + orderDetail.setReturnNum(orderDetail.getRefundNum().add(refNum)); + } + refundDetail.setRefundRemark(refundDetail.getRefundRemark() + param.getRefundReason()); + orderDetail.setPayAmount((orderDetail.getNum().subtract(orderDetail.getRefundNum()).subtract(orderDetail.getReturnNum()) + .subtract(orderDetail.getCouponNum())).multiply(orderDetail.getPrice())); + } + } else { + refundAmountTotal = param.getRefundAmount(); + } + if (param.isModify()) { + orderInfo.setOrderAmount(orderInfo.getOrderAmount().subtract(param.getRefundAmount())); + if (orderInfo.getOrderAmount().compareTo(BigDecimal.ZERO) < 0) { + throw new ValidateException("退单失败,订单金额不正确"); + } + } + if (isPay) { + orderInfo.setRefundAmount(orderInfo.getRefundAmount().add(refundAmountTotal)); + orderInfo.setRefundRemark(orderInfo.getRefundRemark() + param.getRefundReason()); + //退钱 refundAmountTotal + + } else { + + } + return null; + } + + /** + * 填充 优惠券使用数量 以及 付款金额 + * + * @param orderDetails 订单详情 需要回填 + * @param packAmount 打包费 + * @param totalAmount 最终总金额 + */ + private void processOrderDetails(List orderDetails, BigDecimal packAmount, BigDecimal totalAmount) { + for (OrderDetail detail : orderDetails) { + if (detail.getPackNumber().compareTo(BigDecimal.ZERO) > 0 && detail.getPackAmount().compareTo(BigDecimal.ZERO) > 0) { + packAmount = packAmount.add(detail.getPackAmount().multiply(detail.getPackNumber())); + } + detail.setPayAmount(detail.getNum().multiply(detail.getPrice())); + totalAmount = totalAmount.add(detail.getPayAmount()); + } + } /** * 填充 优惠券使用数量 以及 付款金额 @@ -226,17 +403,13 @@ public class OrderInfoServiceImpl extends ServiceImpl * @param prodCouponAmount 商品券优惠金额 商品单价*优惠数量 的总和 - * @param packAmount 打包费 * @param totalAmount 最终总金额(没加打包费 餐位费) 去除优惠券金额后的 */ - private void processOrderDetails(List orderDetails, Map prodCouponMap, - BigDecimal prodCouponAmount, BigDecimal packAmount, BigDecimal totalAmount) { + private void processOrderDetails2(List orderDetails, Map prodCouponMap, + BigDecimal prodCouponAmount, BigDecimal totalAmount) { Map> detailMap = new HashMap<>(); for (OrderDetail detail : orderDetails) { detailMap.computeIfAbsent(detail.getProductId(), k -> new ArrayList<>()).add(detail); - if (detail.getPackNumber().compareTo(BigDecimal.ZERO) > 0 && detail.getPackAmount().compareTo(BigDecimal.ZERO) > 0) { - packAmount = packAmount.add(detail.getPackAmount().multiply(detail.getPackNumber())); - } } List resultList = new ArrayList<>(); for (Map.Entry> entry : detailMap.entrySet()) { @@ -269,31 +442,32 @@ public class OrderInfoServiceImpl extends ServiceImpl prodCouponMap, BigDecimal fullAmount, BigDecimal discountAmount, OrderInfoAddDTO param) { + private void checkCoupon(Map prodCouponMap, BigDecimal fullAmount, BigDecimal discountAmount, CheckOrderPay param) { if (CollUtil.isNotEmpty(param.getCouponList())) { //校验优惠券 List records = couponRecordService.list(QueryWrapper.create() .where(ShopActivateCouponRecord::getId).in(param.getCouponList()) .and(ShopActivateCouponRecord::getStatus).eq(0)); if (CollUtil.isEmpty(records)) { - throw new ValidateException("生成订单失败,优惠券信息不存在"); + throw new ValidateException("生成支付订单失败,优惠券信息不存在"); } else if (records.size() != param.getCouponList().size()) { - throw new ValidateException("生成订单失败,优惠券信息不正确"); + throw new ValidateException("生成支付订单失败,优惠券信息不正确"); } boolean isFullMinus = false; for (ShopActivateCouponRecord record : records) { if (record.getType().equals(1)) { if (isFullMinus) { - throw new ValidateException("生成订单失败,满减券仅可使用一张"); + throw new ValidateException("生成支付订单失败,满减券仅可使用一张"); } fullAmount = record.getFullAmount(); discountAmount = record.getDiscountAmount(); @@ -304,7 +478,7 @@ public class OrderInfoServiceImpl extends ServiceImpl 0) { @@ -491,4 +670,19 @@ public class OrderInfoServiceImpl extends ServiceImpl