fix(订单中心):优惠券管理,暴露携程接口,我方核销接口逻辑实现
This commit is contained in:
+53
-10
@@ -131,12 +131,13 @@ public class CouponController {
|
|||||||
@RequestParam(required = false) Integer discountRate,
|
@RequestParam(required = false) Integer discountRate,
|
||||||
@RequestParam String validStartTime,
|
@RequestParam String validStartTime,
|
||||||
@RequestParam String validEndTime) {
|
@RequestParam String validEndTime) {
|
||||||
|
// 幂等校验
|
||||||
// 这里需要根据实际的时间格式进行解析
|
if (couponService.existsByUserPhoneAndCtripCouponId(userPhone, ctripCouponId)) {
|
||||||
// 暂时使用当前时间,实际应该解析传入的时间字符串
|
return Result.succeed(true);
|
||||||
Date startTime = new Date();
|
}
|
||||||
Date endTime = new Date();
|
// 时间解析
|
||||||
|
Date startTime = parseDate(validStartTime);
|
||||||
|
Date endTime = parseDate(validEndTime);
|
||||||
boolean result = couponService.createCouponFromCtrip(ctripCouponId, userPhone, couponName,
|
boolean result = couponService.createCouponFromCtrip(ctripCouponId, userPhone, couponName,
|
||||||
couponType, amount, minAmount, discountRate,
|
couponType, amount, minAmount, discountRate,
|
||||||
startTime, endTime);
|
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 = "核销携程优惠券")
|
@Operation(summary = "核销携程优惠券")
|
||||||
@PostMapping("/ctrip/verify")
|
@PostMapping("/ctrip/verify")
|
||||||
public Result<Boolean> verifyCtripCoupon(@RequestParam String ctripCouponId, @RequestParam String orderNo) {
|
public Result<Boolean> verifyCtripCoupon(@RequestParam String userPhone, @RequestParam String orderNo) {
|
||||||
boolean result = couponService.verifyCtripCoupon(ctripCouponId, orderNo);
|
List<Coupon> coupons = couponService.getAvailableCouponsByUserPhone(userPhone);
|
||||||
return Result.succeed(result);
|
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();
|
couponService.updateExpiredCoupons();
|
||||||
return Result.succeed("批量更新过期优惠券成功");
|
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 String usedOrderNo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 退券时间
|
||||||
|
*/
|
||||||
|
private Date cancelTime;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建时间
|
* 创建时间
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -72,4 +72,9 @@ public interface ICouponService extends IService<Coupon> {
|
|||||||
* 核销携程优惠券
|
* 核销携程优惠券
|
||||||
*/
|
*/
|
||||||
boolean verifyCtripCoupon(String ctripCouponId, String orderNo);
|
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;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成优惠券编号
|
* 生成优惠券编号
|
||||||
*/
|
*/
|
||||||
|
|||||||
+133
@@ -0,0 +1,133 @@
|
|||||||
|
# 携程优惠券对接接口文档
|
||||||
|
|
||||||
|
本文件描述我方对接携程的优惠券相关接口,包括创建、取消、核销等业务场景。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 创建用户优惠券接口
|
||||||
|
|
||||||
|
- **接口地址**:`POST /coupon/ctrip/create`
|
||||||
|
- **用途**:携程下单后回调,创建用户优惠券(手机号+携程券ID唯一,幂等)。
|
||||||
|
- **请求参数**:
|
||||||
|
|
||||||
|
| 参数名 | 类型 | 必填 | 说明 |
|
||||||
|
|----------------|-----------|------|------------------------|
|
||||||
|
| ctripCouponId | string | 是 | 携程券ID(唯一) |
|
||||||
|
| userPhone | string | 是 | 用户手机号 |
|
||||||
|
| couponName | string | 是 | 优惠券名称 |
|
||||||
|
| couponType | integer | 是 | 优惠券类型 |
|
||||||
|
| amount | number | 是 | 优惠券面额 |
|
||||||
|
| minAmount | number | 否 | 使用门槛金额 |
|
||||||
|
| discountRate | integer | 否 | 折扣率 |
|
||||||
|
| validStartTime | string | 是 | 有效期开始时间(yyyy-MM-dd或yyyy-MM-dd HH:mm:ss)|
|
||||||
|
| validEndTime | string | 是 | 有效期结束时间(yyyy-MM-dd或yyyy-MM-dd HH:mm:ss)|
|
||||||
|
|
||||||
|
- **返回示例**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 0,
|
||||||
|
"data": true,
|
||||||
|
"resp_msg": "success"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 取消用户优惠券接口
|
||||||
|
|
||||||
|
- **接口地址**:`POST /coupon/ctrip/cancel`
|
||||||
|
- **用途**:携程退券回调,取消用户优惠券(默认退第一个未用券)。
|
||||||
|
- **请求参数**:
|
||||||
|
|
||||||
|
| 参数名 | 类型 | 必填 | 说明 |
|
||||||
|
|-----------|--------|------|-----------|
|
||||||
|
| userPhone | string | 是 | 用户手机号 |
|
||||||
|
|
||||||
|
- **返回示例**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 0,
|
||||||
|
"data": true,
|
||||||
|
"resp_msg": "success"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
如无可退券:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 1,
|
||||||
|
"data": null,
|
||||||
|
"resp_msg": "无可退优惠券"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 核销用户优惠券接口
|
||||||
|
|
||||||
|
- **接口地址**:`POST /coupon/ctrip/verify`
|
||||||
|
- **用途**:用户下单时,先查本地券,再调用携程核销接口,核销成功后本地状态设为已用。
|
||||||
|
- **请求参数**:
|
||||||
|
|
||||||
|
| 参数名 | 类型 | 必填 | 说明 |
|
||||||
|
|-----------|--------|------|-----------|
|
||||||
|
| userPhone | string | 是 | 用户手机号 |
|
||||||
|
| orderNo | string | 是 | 订单号 |
|
||||||
|
|
||||||
|
- **返回示例**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 0,
|
||||||
|
"data": true,
|
||||||
|
"resp_msg": "success"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
如无可用券或核销失败:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"code": 1,
|
||||||
|
"data": null,
|
||||||
|
"resp_msg": "无可用优惠券" // 或 "携程核销失败"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 业务说明
|
||||||
|
|
||||||
|
- **幂等性**:所有接口均需保证幂等,创建时手机号+携程券ID唯一。
|
||||||
|
- **状态同步**:本地与携程券状态需保持一致。
|
||||||
|
- **时间格式**:支持`yyyy-MM-dd`或`yyyy-MM-dd HH:mm:ss`。
|
||||||
|
- **安全性**:建议所有接口加签名校验。
|
||||||
|
- **返回结构**:统一为 `{ code, data, resp_msg }`,便于携程方解析。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 示例请求
|
||||||
|
|
||||||
|
### 创建优惠券
|
||||||
|
```http
|
||||||
|
POST /coupon/ctrip/create
|
||||||
|
Content-Type: application/x-www-form-urlencoded
|
||||||
|
|
||||||
|
ctripCouponId=CT123456&userPhone=13800000000&couponName=xxx&couponType=1&amount=100&validStartTime=2024-05-01&validEndTime=2024-06-01
|
||||||
|
```
|
||||||
|
|
||||||
|
### 取消优惠券
|
||||||
|
```http
|
||||||
|
POST /coupon/ctrip/cancel
|
||||||
|
Content-Type: application/x-www-form-urlencoded
|
||||||
|
|
||||||
|
userPhone=13800000000
|
||||||
|
```
|
||||||
|
|
||||||
|
### 核销优惠券
|
||||||
|
```http
|
||||||
|
POST /coupon/ctrip/verify
|
||||||
|
Content-Type: application/x-www-form-urlencoded
|
||||||
|
|
||||||
|
userPhone=13800000000&orderNo=ORDER123
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
如需OpenAPI YAML、签名校验、错误码说明等补充,请联系技术支持。
|
||||||
Reference in New Issue
Block a user