自定义订单异常

This commit is contained in:
wangw 2025-11-11 11:45:05 +08:00
parent b307d03346
commit 60e36fb1d1
7 changed files with 85 additions and 49 deletions

View File

@ -3,7 +3,10 @@ package com.czg.controller.user;
import com.czg.account.dto.shopuser.ShopUserAddDTO;
import com.czg.account.dto.shopuser.ShopUserDetailDTO;
import com.czg.account.dto.shopuser.ShopUserVipCardDTO;
import com.czg.account.entity.*;
import com.czg.account.entity.MemberPointsLog;
import com.czg.account.entity.ShopInfo;
import com.czg.account.entity.ShopUser;
import com.czg.account.entity.ShopUserFlow;
import com.czg.account.service.MemberPointsLogService;
import com.czg.account.service.ShopUserFlowService;
import com.czg.account.service.UShopUserService;
@ -16,7 +19,6 @@ import com.czg.sa.StpKit;
import com.czg.utils.MyQueryWrapper;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

View File

@ -1,26 +1,14 @@
package com.czg.controller.user;
import com.czg.account.dto.shopuser.ShopUserAddDTO;
import com.czg.account.dto.shopuser.ShopUserDetailDTO;
import com.czg.account.dto.shopuser.ShopUserVipCardDTO;
import com.czg.account.entity.MemberPointsLog;
import com.czg.account.entity.ShopUser;
import com.czg.account.entity.ShopUserFlow;
import com.czg.account.service.MemberPointsLogService;
import com.czg.account.service.PointsExchangeRecordService;
import com.czg.account.service.ShopUserFlowService;
import com.czg.account.service.ShopUserService;
import com.czg.annotation.Debounce;
import com.czg.market.entity.MkShopConsumeDiscountRecord;
import com.czg.market.service.MkShopConsumeDiscountRecordService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 新客立减相关

View File

@ -113,6 +113,14 @@ public class CzgControllerAdvice {
return CzgResult.failure(ex.getCode(), ex.getMsg());
}
@ResponseBody
@ExceptionHandler(OrderValidateException.class)
@ResponseStatus(HttpStatus.OK)
public CzgResult<Object> handleOrderValidateException(OrderValidateException ex) {
log.error("订单校验异常:{}", ex.getMessage());
return CzgResult.failure(ex.getCode(), ex.getMsg());
}
/**
* 支付成功
* 支付金额为零的统一处理
@ -127,7 +135,7 @@ public class CzgControllerAdvice {
*/
@ExceptionHandler(OrderCancelException.class)
public CzgResult<Object> handleOrderCancelException() {
return CzgResult.failure(701,"订单已过期,请重新下单");
return CzgResult.failure(701, "订单已过期,请重新下单");
}
/**
@ -155,6 +163,6 @@ public class CzgControllerAdvice {
private void setErrorLog(Exception ex) {
log.error(ex.getMessage());
log.error("错误",ex);
log.error("错误", ex);
}
}

View File

@ -68,7 +68,7 @@ public class SmsPushEvent implements Serializable {
private String userId;
/**
* 0待发送 1 发送中 2发送 -1失败
* 0待发送 1 发送中 2发送 -1失败
*/
private Integer status;

View File

@ -0,0 +1,39 @@
package com.czg.exception;
import com.czg.resp.CzgRespCode;
import java.io.Serial;
/**
* @author ww
* @description
*/
public class OrderValidateException extends RuntimeException{
@Serial
private static final long serialVersionUID = 1L;
private int code;
private String msg;
public OrderValidateException(String msg) {
super(msg);
this.code = CzgRespCode.FAILURE.getCode();
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}

View File

@ -5,9 +5,7 @@ import cn.hutool.poi.excel.ExcelWriter;
import com.czg.account.entity.ShopUserFlow;
import com.czg.account.service.ShopInfoService;
import com.czg.account.service.ShopUserFlowService;
import com.czg.account.service.ShopUserService;
import com.czg.account.vo.ShopUserFlowVO;
import com.czg.account.vo.SysUserDetailVO;
import com.czg.service.account.mapper.ShopUserFlowMapper;
import com.czg.utils.PageUtil;
import com.github.pagehelper.PageHelper;

View File

@ -23,6 +23,7 @@ import com.czg.enums.ShopUserFlowBizEnum;
import com.czg.exception.ApiNotPrintException;
import com.czg.exception.CzgException;
import com.czg.exception.OrderCancelException;
import com.czg.exception.OrderValidateException;
import com.czg.market.dto.MkDiscountActivityDTO;
import com.czg.market.entity.MkDiscountThreshold;
import com.czg.market.entity.MkShopCouponRecord;
@ -342,17 +343,17 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
ShopInfo shopInfo = shopInfoService.getById(orderInfo.getShopId());
AssertUtil.isNull(shopInfo, "生成订单失败,店铺信息不存在");
if (param.isVipPrice() && !shopInfo.getIsMemberPrice().equals(1)) {
throw new ValidateException("生成订单失败,该店铺不支持使用会员价");
throw new OrderValidateException("生成订单失败,该店铺不支持使用会员价");
}
if (param.getOrderAmount().compareTo(BigDecimal.ZERO) < 0) {
throw new ValidateException("生成订单失败订单金额不能小于0");
throw new OrderValidateException("生成订单失败订单金额不能小于0");
}
log.info("订单信息:{},优惠信息:{}", JSONObject.toJSONString(orderInfo), JSONObject.toJSONString(param));
Long shopId = orderInfo.getShopId();
AssertUtil.isNull(shopId, "生成支付订单失败,订单信息异常");
//霸王餐不参与满减
if (param.isFreeDine() && param.getDiscountActId() != null) {
throw new ValidateException("生成订单失败,霸王餐不能享受满减活动");
throw new OrderValidateException("生成订单失败,霸王餐不能享受满减活动");
}
MkDiscountActivityDTO discountAct = null;
// 满减活动校验
@ -368,30 +369,30 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
boolean usePointsDeduction = param.getPointsNum() > 0 || param.getPointsDiscountAmount().compareTo(BigDecimal.ZERO) > 0;
if (usePointsDeduction) {
if (pointSetting == null || !pointSetting.getEnableDeduction().equals(1)) {
throw new ValidateException("生成支付订单失败,该店铺未开启积分抵扣");
throw new OrderValidateException("生成支付订单失败,该店铺未开启积分抵扣");
}
if (param.getUserId() == null) {
throw new ValidateException("生成支付订单失败,请选择用户后再使用积分抵扣");
throw new OrderValidateException("生成支付订单失败,请选择用户后再使用积分抵扣");
}
//霸王餐
if (param.isFreeDine() && !param.isWithPoints()) {
throw new ValidateException("生成支付订单失败,霸王餐不支持积分抵扣");
throw new OrderValidateException("生成支付订单失败,霸王餐不支持积分抵扣");
}
} else {
if (param.getPointsNum() != 0 || param.getPointsDiscountAmount().compareTo(BigDecimal.ZERO) != 0) {
throw new ValidateException("生成支付订单失败,积分抵扣数量或金额不正确");
throw new OrderValidateException("生成支付订单失败,积分抵扣数量或金额不正确");
}
}
if (param.getPointsNum() > 0 && (!param.isFreeDine() || param.isWithPoints())) {
if (pointSetting == null || !pointSetting.getEnableDeduction().equals(1)) {
throw new ValidateException("生成支付订单失败,该店铺未开启积分抵扣");
throw new OrderValidateException("生成支付订单失败,该店铺未开启积分抵扣");
}
if (param.getUserId() == null) {
throw new ValidateException("生成支付订单失败,请选择用户后再使用积分抵扣");
throw new OrderValidateException("生成支付订单失败,请选择用户后再使用积分抵扣");
}
} else if (param.getPointsDiscountAmount().compareTo(BigDecimal.ZERO) > 0) {
throw new ValidateException("生成支付订单失败,已使用积分抵扣,积分数量不正确");
throw new OrderValidateException("生成支付订单失败,已使用积分抵扣,积分数量不正确");
}
ShopUser shopUser = null;
if (param.getUserId() != null) {
@ -399,14 +400,14 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
AssertUtil.isNull(userInfo, "生成支付订单失败,用户信息不存在");
shopUser = shopUserService.getShopUserInfo(shopId, param.getUserId());
if (param.getPointsNum() > 0 && !shopUser.getIsVip().equals(1) && pointSetting.getDeductionGroup().contains("vip")) {
throw new ValidateException("生成支付订单失败,该店铺仅会员可使用积分抵扣");
throw new OrderValidateException("生成支付订单失败,该店铺仅会员可使用积分抵扣");
}
orderInfo.setUserId(userInfo.getId());
}
//会员价校验
if (param.isVipPrice() && (shopUser == null || shopUser.getIsMemberPrice() == null
|| shopUser.getIsMemberPrice() == 0 || shopUser.getIsVip().equals(0))) {
throw new ValidateException("生成支付订单失败,仅会员可使用会员价");
throw new OrderValidateException("生成支付订单失败,仅会员可使用会员价");
}
List<OrderDetail> orderDetails = orderDetailService.queryChain().eq(OrderDetail::getOrderId, param.getOrderId()).select().list();
@ -430,7 +431,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
param.isAllPack(), param.getUserAllPack(), param.isVipPrice());
if (totalAmount.getPrice().compareTo(param.getOriginAmount()) != 0) {
log.info("订单原价不正确:订单原价:{},传递为:{}", totalAmount.getPrice(), param.getOriginAmount());
throw new ValidateException("生成支付订单失败,订单原金额不正确");
throw new OrderValidateException("生成支付订单失败,订单原金额不正确");
}
//优惠券部分 目前规则 每个券只能用一张
@ -450,7 +451,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
if (param.getOriginAmount().subtract(param.getProductCouponDiscountAmount()).subtract(param.getOtherCouponDiscountAmount()).compareTo(newTotalAmount) != 0) {
log.info("其它优惠券金额不正确:满减金额为:{},折扣金额为:{},买一赠一金额为:{},第二件半价券金额为:{} 传递为:{}",
fullReductionAmount.getPrice(), rateAmount.getPrice(), oneGiftAmount.getPrice(), twoHalfAmount.getPrice(), param.getOtherCouponDiscountAmount());
throw new ValidateException("生成支付订单失败,其它优惠券金额不正确");
throw new OrderValidateException("生成支付订单失败,其它优惠券金额不正确");
}
if (newTotalAmount.compareTo(BigDecimal.ZERO) <= 0) {
newTotalAmount = BigDecimal.ZERO;
@ -477,7 +478,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
BigDecimal discountActAmount = calculateDiscountActAmount(orderDetails, discountAct, packAmount.getPrice(), orderInfo.getSeatAmount());
if (discountActAmount.compareTo(param.getDiscountActAmount()) != 0) {
log.info("满减活动金额不正确:传递为:{},计算为:{}", param.getDiscountActAmount(), discountActAmount);
throw new ValidateException("生成支付订单失败,满减活动金额不正确");
throw new OrderValidateException("生成支付订单失败,满减活动金额不正确");
}
newTotalAmount = newTotalAmount.subtract(discountActAmount);
@ -491,7 +492,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
discountAmount = discountAmount.setScale(2, RoundingMode.HALF_UP);
if (discountAmount.compareTo(param.getVipDiscountAmount()) != 0) {
log.info("会员整单折扣金额不正确:传递为:{},计算为:{}", param.getVipDiscountAmount(), discountAmount);
throw new ValidateException("生成支付订单失败,会员整单折扣金额不正确");
throw new OrderValidateException("生成支付订单失败,会员整单折扣金额不正确");
}
newTotalAmount = newTotalAmount.subtract(discountAmount);
}
@ -499,14 +500,14 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
//积分抵扣 金额范围校验 抵扣金额校验
if (param.getPointsNum() > 0) {
if (pointSetting.getMinPaymentAmount().compareTo(newTotalAmount) > 0) {
throw new ValidateException("生成支付订单失败,未满足积分抵扣最低门槛");
throw new OrderValidateException("生成支付订单失败,未满足积分抵扣最低门槛");
}
if (pointSetting.getMaxDeductionRatio().multiply(newTotalAmount).compareTo(param.getPointsDiscountAmount()) < 0) {
throw new ValidateException("生成支付订单失败,积分抵扣金额已超出最大抵扣金额");
throw new OrderValidateException("生成支付订单失败,积分抵扣金额已超出最大抵扣金额");
}
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 OrderValidateException("生成支付订单失败,积分抵扣金额不正确");
}
newTotalAmount = newTotalAmount.subtract(param.getPointsDiscountAmount());
}
@ -529,7 +530,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
orderInfo.getId(), newTotalAmount, totalAmount.getPrice(), tempAmount.getPrice(), packAmount.getPrice(), orderInfo.getSeatAmount(),
param.getOrderAmount(), param.getProductCouponDiscountAmount(), param.getOtherCouponDiscountAmount(), param.getNewCustomerDiscountAmount(),
discountActAmount, param.getDiscountAmount(), param.getPointsDiscountAmount(), param.getRoundAmount());
throw new ValidateException("生成支付订单失败,支付金额不正确");
throw new OrderValidateException("生成支付订单失败,支付金额不正确");
}
orderInfo.setPackFee(packAmount.getPrice());
//生成订单 //discount_info 所有折扣 几折 折扣金额 满减金额 优惠券金额 积分抵扣金额 抹零
@ -607,7 +608,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
couponRecordQuery.ne(MkShopCouponRecord::getIsDel, 1);
List<MkShopCouponRecord> list = couponRecordService.list(couponRecordQuery);
if (CollUtil.isEmpty(list) || param.getCouponList().size() > list.size()) {
throw new ValidateException("生成支付订单失败,优惠券不可用");
throw new OrderValidateException("生成支付订单失败,优惠券不可用");
}
Map<Integer, MkShopCouponRecord> couponRecordMap = list.stream().collect(Collectors.toMap(MkShopCouponRecord::getType, t -> t));
AtomicBoolean otherCouponShare = new AtomicBoolean(true);
@ -636,7 +637,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
"price_asc".equals(coupon.getUseRule()), prodCouponAmount);
} else if (type == 4 || type == 6) {//4-第二件半价券 6-买一送一券
if (!otherCouponShare.get()) {
throw new ValidateException("生成支付订单失败,商品券与其它券不可共用");
throw new OrderValidateException("生成支付订单失败,商品券与其它券不可共用");
}
boolean isAllFoods = true;
List<Long> couponFoodIds = new ArrayList<>();
@ -653,7 +654,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
}
} else if (type == 1 || type == 3) {//1-满减券 3-折扣券
if (!otherCouponShare.get()) {
throw new ValidateException("生成支付订单失败,商品券与其它券不可共用");
throw new OrderValidateException("生成支付订单失败,商品券与其它券不可共用");
}
//计算门槛
boolean isAllFoods = true;
@ -681,7 +682,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
}
if (prodCouponAmount.getPrice().compareTo(param.getProductCouponDiscountAmount()) != 0) {
log.info("支付计算金额不正确:商品券抵扣金额为:{},传递为:{}", prodCouponAmount.getPrice(), param.getProductCouponDiscountAmount());
throw new ValidateException("生成支付订单失败,商品优惠券优惠金额不正确");
throw new OrderValidateException("生成支付订单失败,商品优惠券优惠金额不正确");
}
}
@ -939,10 +940,10 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
} else {
if (orderDetail.getIsTimeDiscount() == 1) {
if (limitRate == null || (limitRate.getFoodType() == 2 && CollUtil.isEmpty(limitRate.getFoodIds()))) {
throw new CzgException("限时折扣使用失败,商品范围不正确");
throw new OrderValidateException("限时折扣使用失败,商品范围不正确");
}
if (limitRate.getFoodType() == 2 && !limitRate.getFoodIds().contains(orderDetail.getProductId())) {
throw new CzgException("限时折扣使用失败,商品" + orderDetail.getProductName() + "不享受限时折扣");
throw new OrderValidateException("限时折扣使用失败,商品" + orderDetail.getProductName() + "不享受限时折扣");
}
if (orderDetail.getPrice().compareTo(BigDecimal.ZERO) == 0) {
orderDetail.setUnitPrice(orderDetail.getPrice());