抽离礼金
This commit is contained in:
@@ -186,13 +186,15 @@ public void sendOrderToWxByOrderDefault(String order, String fromWxid) {
|
|||||||
logger.info("执行 sendOrderToWxByOrderDefault 方法,order: {}, fromWxid: {}", order, fromWxid);
|
logger.info("执行 sendOrderToWxByOrderDefault 方法,order: {}, fromWxid: {}", order, fromWxid);
|
||||||
|
|
||||||
if ("礼".equals(order)) {
|
if ("礼".equals(order)) {
|
||||||
// 确保缓存中有消息内容
|
// 设置状态为等待文案输入
|
||||||
String cachedMessage = cacheMap.get("giftMessage" + fromWxid);
|
String key = INTERACTION_STATE_PREFIX + fromWxid;
|
||||||
if (cachedMessage == null || cachedMessage.trim().isEmpty()) {
|
UserInteractionState state = loadOrCreateState(key);
|
||||||
wxUtil.sendTextMessage(fromWxid, "未找到商品链接,请重新输入包含商品链接的消息。", 1, fromWxid, false);
|
state.setCurrentState(UserInteractionState.ProcessState.GIFT_MONEY_FLOW);
|
||||||
return;
|
state.setCurrentStep(UserInteractionState.GiftMoneyStep.STEP_WAIT_FOR_CONTENT);
|
||||||
}
|
saveState(key, state);
|
||||||
handleGiftCommand(fromWxid);
|
|
||||||
|
// 提示用户输入文案
|
||||||
|
wxUtil.sendTextMessage(fromWxid, "请输入包含商品链接的文案:", 1, fromWxid, false);
|
||||||
} else {
|
} else {
|
||||||
handleUserInteraction(fromWxid, order);
|
handleUserInteraction(fromWxid, order);
|
||||||
}
|
}
|
||||||
@@ -200,6 +202,7 @@ public void sendOrderToWxByOrderDefault(String order, String fromWxid) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理“礼”指令,直接进入礼金创建流程
|
* 处理“礼”指令,直接进入礼金创建流程
|
||||||
*/
|
*/
|
||||||
@@ -264,80 +267,47 @@ private void handleGiftCommand(String fromWxid) {
|
|||||||
*/
|
*/
|
||||||
private void processGiftMoneyFlow(String wxid, String message, UserInteractionState state) {
|
private void processGiftMoneyFlow(String wxid, String message, UserInteractionState state) {
|
||||||
try {
|
try {
|
||||||
// 获取缓存的商品信息
|
// 提取文案中的 u.jd.com 链接
|
||||||
String cachedData = cacheMap.get("validProducts" + wxid);
|
List<String> urls = extractUJDUrls(message);
|
||||||
if (cachedData == null) {
|
if (urls.isEmpty()) {
|
||||||
wxUtil.sendTextMessage(wxid, "数据丢失,请重新开始流程", 1, wxid, false);
|
wxUtil.sendTextMessage(wxid, "未找到有效的商品链接,请重新输入包含商品链接的文案。", 1, wxid, false);
|
||||||
resetState(wxid, state);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<GoodsQueryResult> validProducts = JSON.parseArray(cachedData, GoodsQueryResult.class);
|
// 查询商品信息,筛选有效商品
|
||||||
int currentIndex = Integer.parseInt(state.getCollectedFields().get("productIndex"));
|
List<GoodsQueryResult> validProducts = new ArrayList<>();
|
||||||
int totalProducts = Integer.parseInt(state.getCollectedFields().get("totalProducts"));
|
for (String url : urls) {
|
||||||
|
try {
|
||||||
// 根据当前步骤处理
|
GoodsQueryResult productInfo = queryProductInfoByUJDUrl(url);
|
||||||
switch (state.getCurrentStep()) {
|
if (productInfo != null && productInfo.getCode() == 200 && productInfo.getTotalCount() > 0) {
|
||||||
case STEP_AMOUNT:
|
validProducts.add(productInfo);
|
||||||
if (!isValidAmount(message)) {
|
}
|
||||||
wxUtil.sendTextMessage(wxid, "金额格式错误,请输入1-50元", 1, wxid, false);
|
} catch (Exception e) {
|
||||||
return;
|
logger.error("查询商品信息失败:{}", url, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
state.getCollectedFields().put("amount", message);
|
|
||||||
state.setCurrentStep(UserInteractionState.GiftMoneyStep.STEP_QUANTITY);
|
|
||||||
wxUtil.sendTextMessage(wxid, "请输入数量(1-100):", 1, wxid, false);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case STEP_QUANTITY:
|
if (validProducts.isEmpty()) {
|
||||||
if (!isValidQuantity(message)) {
|
wxUtil.sendTextMessage(wxid, "未找到有效的商品信息,请检查链接是否正确。", 1, wxid, false);
|
||||||
wxUtil.sendTextMessage(wxid, "数量格式错误,请输入1-100的整数", 1, wxid, false);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 记录数量
|
// 缓存商品信息
|
||||||
state.getCollectedFields().put("quantity", message);
|
cacheMap.put("validProducts" + wxid, JSON.toJSONString(validProducts));
|
||||||
|
|
||||||
// 开始循环创建礼金
|
// 更新状态
|
||||||
double amount = Double.parseDouble(state.getCollectedFields().get("amount"));
|
state.setCurrentStep(UserInteractionState.GiftMoneyStep.STEP_AMOUNT);
|
||||||
int quantity = Integer.parseInt(message);
|
state.getCollectedFields().clear();
|
||||||
|
state.getCollectedFields().put("productIndex", "0"); // 当前处理的商品索引
|
||||||
StringBuilder result = new StringBuilder();
|
state.getCollectedFields().put("totalProducts", String.valueOf(validProducts.size())); // 总商品数
|
||||||
for (int i = currentIndex; i < totalProducts; i++) {
|
|
||||||
GoodsQueryResult product = validProducts.get(i);
|
|
||||||
JSONObject productInfo = JSONObject.parseObject(JSON.toJSONString(product.getData()[0]));
|
|
||||||
|
|
||||||
String skuId = productInfo.getString("materialUrl");
|
|
||||||
String owner = productInfo.getString("owner");
|
|
||||||
String skuName = productInfo.getString("skuName");
|
|
||||||
|
|
||||||
// 调用礼金创建接口
|
|
||||||
String giftKey = createGiftCoupon(skuId, amount, quantity, owner, skuName);
|
|
||||||
if (giftKey != null) {
|
|
||||||
result.append("商品:").append(skuName).append("\n")
|
|
||||||
.append("礼金创建成功,批次ID:").append(giftKey).append("\n\n");
|
|
||||||
} else {
|
|
||||||
result.append("商品:").append(skuName).append("\n")
|
|
||||||
.append("礼金创建失败,请稍后重试。\n\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 更新当前索引
|
|
||||||
state.getCollectedFields().put("productIndex", String.valueOf(i + 1));
|
|
||||||
saveState(INTERACTION_STATE_PREFIX + wxid, state);
|
saveState(INTERACTION_STATE_PREFIX + wxid, state);
|
||||||
}
|
|
||||||
|
|
||||||
// 返回结果
|
// 提示用户输入金额
|
||||||
wxUtil.sendTextMessage(wxid, "礼金创建完成:\n" + result.toString(), 1, wxid, false);
|
wxUtil.sendTextMessage(wxid, "请输入开通金额(1-50元,支持小数点后两位):", 1, wxid, false);
|
||||||
resetState(wxid, state);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
wxUtil.sendTextMessage(wxid, "流程异常,请重新开始", 1, wxid, false);
|
|
||||||
resetState(wxid, state);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("礼金创建流程异常 - 用户: {}", wxid, e);
|
logger.error("处理礼金文案异常 - 用户: {}", wxid, e);
|
||||||
wxUtil.sendTextMessage(wxid, "处理异常,请重试", 1, wxid, false);
|
wxUtil.sendTextMessage(wxid, "处理请求时发生异常,请重试", 1, wxid, false);
|
||||||
resetState(wxid, state);
|
resetState(wxid, state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -903,7 +873,7 @@ private void processGiftMoneyFlow(String wxid, String message, UserInteractionSt
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("流程处理异常 - 用户: {}, 状态: {}", fromWxid, state, e);
|
logger.error("流程处理异常 - 用户: {}, 状态: {}", fromWxid, state, e);
|
||||||
resetState(fromWxid, state);
|
resetState(fromWxid, state);
|
||||||
wxUtil.sendTextMessage(fromWxid, "处理异常,请重新开始", 1, fromWxid, false);
|
wxUtil.sendTextMessage(fromWxid, "处理异常,请重试", 1, fromWxid, false);
|
||||||
} finally {
|
} finally {
|
||||||
saveState(key, state);
|
saveState(key, state);
|
||||||
}
|
}
|
||||||
@@ -1556,7 +1526,9 @@ public HashMap<String, List<String>> generatePromotionContent(String message) {
|
|||||||
INIT, GIFT_MONEY_FLOW, PRODUCT_PROMOTION
|
INIT, GIFT_MONEY_FLOW, PRODUCT_PROMOTION
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
public enum GiftMoneyStep {
|
public enum GiftMoneyStep {
|
||||||
|
STEP_WAIT_FOR_CONTENT(3),
|
||||||
STEP_CONFIRM_GIFT(0), // 显式指定序号
|
STEP_CONFIRM_GIFT(0), // 显式指定序号
|
||||||
STEP_AMOUNT(1), STEP_QUANTITY(2);
|
STEP_AMOUNT(1), STEP_QUANTITY(2);
|
||||||
private final int code;
|
private final int code;
|
||||||
@@ -1565,10 +1537,6 @@ public HashMap<String, List<String>> generatePromotionContent(String message) {
|
|||||||
this.code = code;
|
this.code = code;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCode() {
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user