fix(订单中心):优惠券管理,暴露携程接口,我方核销接口逻辑实现
This commit is contained in:
+53
-10
@@ -131,12 +131,13 @@ public class CouponController {
|
||||
@RequestParam(required = false) Integer discountRate,
|
||||
@RequestParam String validStartTime,
|
||||
@RequestParam String validEndTime) {
|
||||
|
||||
// 这里需要根据实际的时间格式进行解析
|
||||
// 暂时使用当前时间,实际应该解析传入的时间字符串
|
||||
Date startTime = new Date();
|
||||
Date endTime = new Date();
|
||||
|
||||
// 幂等校验
|
||||
if (couponService.existsByUserPhoneAndCtripCouponId(userPhone, ctripCouponId)) {
|
||||
return Result.succeed(true);
|
||||
}
|
||||
// 时间解析
|
||||
Date startTime = parseDate(validStartTime);
|
||||
Date endTime = parseDate(validEndTime);
|
||||
boolean result = couponService.createCouponFromCtrip(ctripCouponId, userPhone, couponName,
|
||||
couponType, amount, minAmount, discountRate,
|
||||
startTime, endTime);
|
||||
@@ -144,13 +145,42 @@ public class CouponController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 核销携程优惠券
|
||||
* 携程取消优惠券接口
|
||||
*/
|
||||
@Operation(summary = "携程取消优惠券接口")
|
||||
@PostMapping("/ctrip/cancel")
|
||||
public Result<Boolean> cancelCouponFromCtrip(@RequestParam String userPhone) {
|
||||
List<Coupon> coupons = couponService.getAvailableCouponsByUserPhone(userPhone);
|
||||
if (coupons.isEmpty()) {
|
||||
return Result.failed("无可退优惠券");
|
||||
}
|
||||
Coupon coupon = coupons.get(0); // 默认退第一个
|
||||
coupon.setStatus(3); // 作废/退
|
||||
coupon.setCancelTime(new Date());
|
||||
couponService.updateCoupon(coupon);
|
||||
return Result.succeed(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 优化核销携程优惠券接口,先查本地券再核销
|
||||
*/
|
||||
@Operation(summary = "核销携程优惠券")
|
||||
@PostMapping("/ctrip/verify")
|
||||
public Result<Boolean> verifyCtripCoupon(@RequestParam String ctripCouponId, @RequestParam String orderNo) {
|
||||
boolean result = couponService.verifyCtripCoupon(ctripCouponId, orderNo);
|
||||
return Result.succeed(result);
|
||||
public Result<Boolean> verifyCtripCoupon(@RequestParam String userPhone, @RequestParam String orderNo) {
|
||||
List<Coupon> coupons = couponService.getAvailableCouponsByUserPhone(userPhone);
|
||||
if (coupons.isEmpty()) {
|
||||
return Result.failed("无可用优惠券");
|
||||
}
|
||||
Coupon coupon = coupons.get(0);
|
||||
boolean result = couponService.verifyCtripCoupon(coupon.getCtripCouponId(), orderNo);
|
||||
if (result) {
|
||||
coupon.setStatus(1); // 已用
|
||||
coupon.setUsedTime(new Date());
|
||||
couponService.updateCoupon(coupon);
|
||||
return Result.succeed(true);
|
||||
} else {
|
||||
return Result.failed("携程核销失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,4 +192,17 @@ public class CouponController {
|
||||
couponService.updateExpiredCoupons();
|
||||
return Result.succeed("批量更新过期优惠券成功");
|
||||
}
|
||||
|
||||
// 时间解析工具
|
||||
private Date parseDate(String dateStr) {
|
||||
try {
|
||||
return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
return new java.text.SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
|
||||
} catch (Exception ex) {
|
||||
return new Date();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,6 +104,11 @@ public class Coupon implements Serializable {
|
||||
*/
|
||||
private String usedOrderNo;
|
||||
|
||||
/**
|
||||
* 退券时间
|
||||
*/
|
||||
private Date cancelTime;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
|
||||
@@ -72,4 +72,9 @@ public interface ICouponService extends IService<Coupon> {
|
||||
* 核销携程优惠券
|
||||
*/
|
||||
boolean verifyCtripCoupon(String ctripCouponId, String orderNo);
|
||||
|
||||
/**
|
||||
* 校验手机号+携程券ID是否已存在
|
||||
*/
|
||||
boolean existsByUserPhoneAndCtripCouponId(String userPhone, String ctripCouponId);
|
||||
}
|
||||
+8
@@ -214,6 +214,14 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
||||
return baseMapper.updateCouponUsed(coupon.getId(), orderNo) > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean existsByUserPhoneAndCtripCouponId(String userPhone, String ctripCouponId) {
|
||||
return lambdaQuery()
|
||||
.eq(Coupon::getUserPhone, userPhone)
|
||||
.eq(Coupon::getCtripCouponId, ctripCouponId)
|
||||
.count() > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成优惠券编号
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user