1
This commit is contained in:
@@ -8,12 +8,15 @@ import cn.van.business.repository.ProductOrderRepository;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.jd.open.api.sdk.DefaultJdClient;
|
||||
import com.jd.open.api.sdk.JdClient;
|
||||
import com.jd.open.api.sdk.domain.kplunion.CouponService.request.get.CreateGiftCouponReq;
|
||||
import com.jd.open.api.sdk.domain.kplunion.CouponService.request.stop.StopGiftCouponReq;
|
||||
import com.jd.open.api.sdk.domain.kplunion.GoodsService.request.query.BigFieldGoodsReq;
|
||||
import com.jd.open.api.sdk.domain.kplunion.GoodsService.response.query.BigFieldGoodsResp;
|
||||
import com.jd.open.api.sdk.domain.kplunion.GoodsService.response.query.BigfieldQueryResult;
|
||||
import com.jd.open.api.sdk.domain.kplunion.OrderService.request.query.OrderRowReq;
|
||||
import com.jd.open.api.sdk.domain.kplunion.promotionbysubunioni.PromotionService.request.get.PromotionCodeReq;
|
||||
import com.jd.open.api.sdk.request.kplunion.UnionOpenOrderRowQueryRequest;
|
||||
import com.jd.open.api.sdk.request.kplunion.UnionOpenPromotionBysubunionidGetRequest;
|
||||
import com.jd.open.api.sdk.response.kplunion.UnionOpenOrderRowQueryResponse;
|
||||
import com.jd.open.api.sdk.response.kplunion.UnionOpenPromotionBysubunionidGetResponse;
|
||||
import com.jd.open.api.sdk.request.kplunion.*;
|
||||
import com.jd.open.api.sdk.response.kplunion.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@@ -32,6 +35,8 @@ import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -663,80 +668,233 @@ private StringBuilder buildStatsContent(String title, OrderStats stats) {
|
||||
}
|
||||
|
||||
// 新增礼金流程处理方法
|
||||
private void handleGiftMoneyFlow(String fromWxid, String message, UserInteractionState state) {
|
||||
if ("礼金".equals(message)) {
|
||||
state.reset(); // 重置流程
|
||||
wxUtil.sendTextMessage(fromWxid, "流程已重置,请重新开始", 1, fromWxid);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
switch (state.getCurrentStep()) {
|
||||
case STEP_PRODUCT_LINK:
|
||||
// 解析商品链接获取SKU
|
||||
String skuId = parseSkuFromUrl(message);
|
||||
Map<String, String> productInfo = queryProductInfo(skuId);
|
||||
state.getCollectedFields().put("skuId", skuId);
|
||||
state.getCollectedFields().put("productInfo", productInfo.get("name") + "\n价格:" + productInfo.get("price"));
|
||||
|
||||
state.setCurrentStep(STEP_AMOUNT);
|
||||
wxUtil.sendTextMessage(fromWxid, "商品信息:\n" + productInfo.get("name") + "\n当前价格:" + productInfo.get("price") + "\n请输入开通金额(元):", 1, fromWxid);
|
||||
break;
|
||||
|
||||
case STEP_AMOUNT:
|
||||
if (!isValidAmount(message)) {
|
||||
wxUtil.sendTextMessage(fromWxid, "金额格式错误,请重新输入", 1, fromWxid);
|
||||
return;
|
||||
}
|
||||
state.getCollectedFields().put("amount", message);
|
||||
state.setCurrentStep(STEP_QUANTITY);
|
||||
wxUtil.sendTextMessage(fromWxid, "请输入开通数量:", 1, fromWxid);
|
||||
break;
|
||||
|
||||
case STEP_QUANTITY:
|
||||
if (!isValidQuantity(message)) {
|
||||
wxUtil.sendTextMessage(fromWxid, "数量格式错误,请重新输入", 1, fromWxid);
|
||||
return;
|
||||
}
|
||||
state.getCollectedFields().put("quantity", message);
|
||||
|
||||
// 调用开通接口
|
||||
boolean result = activateGiftMoney(state.getCollectedFields().get("skuId"), Double.parseDouble(state.getCollectedFields().get("amount")), Integer.parseInt(message));
|
||||
|
||||
String response = result ? "开通成功!" : "开通失败,请稍后重试";
|
||||
wxUtil.sendTextMessage(fromWxid, response, 1, fromWxid);
|
||||
state.reset(); // 完成流程
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("礼金流程处理异常", e);
|
||||
wxUtil.sendTextMessage(fromWxid, "系统异常,流程已终止", 1, fromWxid);
|
||||
state.reset();
|
||||
}
|
||||
private void handleGiftMoneyFlow(String fromWxid, String message, UserInteractionState state) {
|
||||
if ("礼金".equals(message)) {
|
||||
state.reset();
|
||||
wxUtil.sendTextMessage(fromWxid, "流程已重置,请重新开始", 1, fromWxid);
|
||||
return;
|
||||
}
|
||||
|
||||
private String parseSkuFromUrl(String url) {
|
||||
// 实现从URL中解析SKU的逻辑
|
||||
return "123456"; // 示例返回值
|
||||
try {
|
||||
switch (state.getCurrentStep()) {
|
||||
case STEP_PRODUCT_LINK:
|
||||
// 解析商品链接获取SKU
|
||||
String skuId = parseSkuFromUrl(message);
|
||||
if (skuId == null) {
|
||||
wxUtil.sendTextMessage(fromWxid, "❌ 商品链接格式错误,请重新输入", 1, fromWxid);
|
||||
return;
|
||||
}
|
||||
|
||||
// 调用商品详情接口查询真实信息
|
||||
BigfieldQueryResult bigfieldQueryResult = queryProductInfo(skuId);
|
||||
|
||||
if (bigfieldQueryResult == null ) {
|
||||
wxUtil.sendTextMessage(fromWxid, "⚠️ 商品信息查询失败,可能链接无效", 1, fromWxid);
|
||||
return;
|
||||
}
|
||||
|
||||
BigFieldGoodsResp[] data = bigfieldQueryResult.getData();
|
||||
BigFieldGoodsResp productInfo = data[0];
|
||||
//g=自营,p=pop
|
||||
String owner = productInfo.getOwner();
|
||||
|
||||
|
||||
state.getCollectedFields().put("skuId", skuId);
|
||||
state.getCollectedFields().put("productInfo", productInfo.getBaseBigFieldInfo().getWdis());
|
||||
state.getCollectedFields().put("owner",owner);
|
||||
|
||||
state.setCurrentStep(STEP_AMOUNT);
|
||||
String prompt = String.format("商品信息:\n%s请输入开通金额(元):",
|
||||
productInfo);
|
||||
wxUtil.sendTextMessage(fromWxid, prompt, 1, fromWxid);
|
||||
break;
|
||||
|
||||
case STEP_AMOUNT:
|
||||
if (!isValidAmount(message)) {
|
||||
wxUtil.sendTextMessage(fromWxid, "❌ 金额格式错误,请输入数字(如:100.00)", 1, fromWxid);
|
||||
return;
|
||||
}
|
||||
|
||||
// 校验金额是否符合京东接口要求
|
||||
double amount = Double.parseDouble(message);
|
||||
if (amount < 1 || amount > 0.8 * Double.parseDouble(state.getCollectedFields().get("price"))) {
|
||||
wxUtil.sendTextMessage(fromWxid, "❌ 金额需≥1元且≤商品价格的80%", 1, fromWxid);
|
||||
return;
|
||||
}
|
||||
|
||||
state.getCollectedFields().put("amount", String.valueOf(amount));
|
||||
state.setCurrentStep(STEP_QUANTITY);
|
||||
wxUtil.sendTextMessage(fromWxid, "请输入开通数量(1-1000):", 1, fromWxid);
|
||||
break;
|
||||
|
||||
case STEP_QUANTITY:
|
||||
if (!isValidQuantity(message)) {
|
||||
wxUtil.sendTextMessage(fromWxid, "❌ 数量格式错误,请输入整数", 1, fromWxid);
|
||||
return;
|
||||
}
|
||||
|
||||
int quantity = Integer.parseInt(message);
|
||||
if (quantity < 1 || quantity > 1000) {
|
||||
wxUtil.sendTextMessage(fromWxid, "❌ 数量需在1-1000之间", 1, fromWxid);
|
||||
return;
|
||||
}
|
||||
|
||||
state.getCollectedFields().put("quantity", String.valueOf(quantity));
|
||||
|
||||
// 调用礼金创建接口
|
||||
skuId = state.getCollectedFields().get("skuId");
|
||||
amount = Double.parseDouble(state.getCollectedFields().get("amount"));
|
||||
quantity = Integer.parseInt(state.getCollectedFields().get("quantity"));
|
||||
owner = state.getCollectedFields().get("owner");
|
||||
|
||||
String giftKey = createGiftCoupon(skuId, amount, quantity,owner);
|
||||
if (giftKey == null) {
|
||||
wxUtil.sendTextMessage(fromWxid, "❌ 礼金创建失败,请检查商品是否符合要求", 1, fromWxid);
|
||||
state.reset();
|
||||
return;
|
||||
}
|
||||
|
||||
// 保存礼金批次ID到用户状态
|
||||
state.getCollectedFields().put("giftKey", giftKey);
|
||||
|
||||
// 返回成功信息
|
||||
wxUtil.sendTextMessage(fromWxid, "✅ 礼金创建成功!\n批次ID:" + giftKey, 1, fromWxid);
|
||||
state.reset();
|
||||
break;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("礼金流程异常", e);
|
||||
wxUtil.sendTextMessage(fromWxid, "❌ 系统异常,请稍后重试", 1, fromWxid);
|
||||
state.reset();
|
||||
}
|
||||
}
|
||||
|
||||
// 在JDUtil类中新增方法实现商品详情查询接口
|
||||
public UnionOpenGoodsBigfieldQueryResponse getUnionOpenGoodsBigfieldQueryResponse(String skuId) throws Exception {
|
||||
JdClient client = new DefaultJdClient(SERVER_URL, ACCESS_TOKEN, LPF_APP_KEY_WZ, LPF_APP_KEY_WZ);
|
||||
|
||||
UnionOpenGoodsBigfieldQueryRequest request = new UnionOpenGoodsBigfieldQueryRequest();
|
||||
BigFieldGoodsReq goodsReq = new BigFieldGoodsReq();
|
||||
goodsReq.setSkuIds(List.of(Long.parseLong(skuId)).toArray(new Long[0])); // 传入skuId集合
|
||||
/**
|
||||
* 查询域集合,不填写则查询全部,目目前支持:categoryInfo(类目信息),imageInfo(图片信息),
|
||||
* baseBigFieldInfo(基础大字段信息),bookBigFieldInfo(图书大字段信息),
|
||||
* videoBigFieldInfo(影音大字段信息),detailImages(商详图)*/
|
||||
//goodsReq.setFields(new String[]{"categoryInfo", "imageInfo", "baseBigFieldInfo"}); // 设置需要查询的字段
|
||||
goodsReq.setSceneId(2); // 设置场景ID
|
||||
|
||||
request.setGoodsReq(goodsReq);
|
||||
request.setVersion("1.0");
|
||||
request.setSignmethod("md5");
|
||||
request.setTimestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||
|
||||
return client.execute(request);
|
||||
}
|
||||
|
||||
// 修改parseSkuFromUrl方法提取SKU
|
||||
private String parseSkuFromUrl(String url) {
|
||||
Matcher m = Pattern.compile("item.jd.com/(\\d+).html").matcher(url);
|
||||
return m.find() ? m.group(1) : null;
|
||||
}
|
||||
|
||||
public BigfieldQueryResult queryProductInfo(String skuId) throws Exception {
|
||||
UnionOpenGoodsBigfieldQueryResponse response = getUnionOpenGoodsBigfieldQueryResponse(skuId);
|
||||
if (response == null || response.getQueryResult() == null) {
|
||||
return null;
|
||||
}
|
||||
BigfieldQueryResult queryResult = response.getQueryResult();
|
||||
if (queryResult.getCode() != 200) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return queryResult;
|
||||
}
|
||||
|
||||
|
||||
// 新增礼金创建方法
|
||||
public String createGiftCoupon(String skuId, double amount, int quantity,String owner) throws Exception {
|
||||
JdClient client = new DefaultJdClient(SERVER_URL, ACCESS_TOKEN, LPF_APP_KEY_WZ, LPF_APP_KEY_WZ);
|
||||
|
||||
UnionOpenCouponGiftGetRequest request = new UnionOpenCouponGiftGetRequest();
|
||||
CreateGiftCouponReq couponReq = new CreateGiftCouponReq();
|
||||
couponReq.setSkuMaterialId(skuId); // 使用SKU或链接
|
||||
couponReq.setDiscount(amount);
|
||||
couponReq.setAmount(quantity);
|
||||
//owner g=自营,p=pop
|
||||
// 自营的只能设置一天,pop的只能设置7天,默认为自营
|
||||
String startTime;
|
||||
String endTime;
|
||||
if ("pop".equals(owner)){
|
||||
startTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH"));
|
||||
endTime = LocalDateTime.now().plusDays(7).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH"));
|
||||
} else {
|
||||
startTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH"));
|
||||
endTime = LocalDateTime.now().plusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH"));
|
||||
|
||||
}
|
||||
couponReq.setReceiveStartTime(startTime);
|
||||
couponReq.setReceiveEndTime(endTime);
|
||||
couponReq.setEffectiveDays(1);
|
||||
couponReq.setIsSpu(1);
|
||||
couponReq.setExpireType(1);
|
||||
couponReq.setShare(-1);
|
||||
couponReq.setCouponTitle(skuId);
|
||||
|
||||
request.setCouponReq(couponReq);
|
||||
request.setVersion("1.0");
|
||||
request.setSignmethod("md5");
|
||||
request.setTimestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||
|
||||
UnionOpenCouponGiftGetResponse response = client.execute(request);
|
||||
if ("200".equals(response.getCode())) {
|
||||
return response.getGetResult().getData().getGiftCouponKey();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 修改activateGiftMoney方法调用真实接口
|
||||
private boolean activateGiftMoney(String skuId, double amount, int quantity) {
|
||||
try {
|
||||
String giftKey = createGiftCoupon(skuId, amount, quantity);
|
||||
if (giftKey != null) {
|
||||
// 可在此处保存礼金批次ID到用户状态
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("礼金创建失败", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 新增礼金停止方法(可选)
|
||||
public boolean stopGiftCoupon(String giftKey) throws Exception {
|
||||
JdClient client = new DefaultJdClient(SERVER_URL, LPF_APP_KEY_DG, LPF_SECRET_KEY_DG);
|
||||
|
||||
UnionOpenCouponGiftStopRequest request = new UnionOpenCouponGiftStopRequest();
|
||||
StopGiftCouponReq couponReq = new StopGiftCouponReq();
|
||||
couponReq.setGiftCouponKey(giftKey);
|
||||
|
||||
request.setCouponReq(couponReq);
|
||||
request.setVersion("1.0");
|
||||
request.setSignmethod("md5");
|
||||
request.setTimestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
||||
|
||||
UnionOpenCouponGiftStopResponse response = client.execute(request);
|
||||
return "200".equals(response.getCode());
|
||||
}
|
||||
|
||||
|
||||
|
||||
private boolean isValidAmount(String input) {
|
||||
return input.matches("^\\d+(\\.\\d{1,2})?$");
|
||||
}
|
||||
|
||||
private Map<String, String> queryProductInfo(String skuId) {
|
||||
// 调用京东商品查询API(需要实现)
|
||||
return Map.of("name", "示例商品", "price", "299.00");
|
||||
}
|
||||
|
||||
|
||||
private boolean isValidQuantity(String input) {
|
||||
return input.matches("^\\d+$");
|
||||
}
|
||||
|
||||
private boolean activateGiftMoney(String skuId, double amount, int quantity) {
|
||||
// 实现实际的开通接口调用
|
||||
return true;
|
||||
}
|
||||
|
||||
private void handleProductOrderRegistration(String fromWxid, String message, UserInteractionState state) {
|
||||
switch (state.getCurrentProductOrderStep()) {
|
||||
|
||||
Reference in New Issue
Block a user