1
This commit is contained in:
@@ -74,8 +74,54 @@ public class ProductOrder {
|
|||||||
@Temporal(TemporalType.TIMESTAMP)
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
@Column(name = "cashback_time")
|
@Column(name = "cashback_time")
|
||||||
private Date cashbackTime;
|
private Date cashbackTime;
|
||||||
|
//收货信息
|
||||||
|
@Column(name = "recipient_name")
|
||||||
|
private String recipientName;
|
||||||
|
@Column(name = "recipient_phone")
|
||||||
|
private String recipientPhone;
|
||||||
|
@Column(name = "recipient_address")
|
||||||
|
private String recipientAddress;
|
||||||
|
|
||||||
// Getters and Setters
|
public Boolean getReviewed() {
|
||||||
|
return isReviewed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReviewed(Boolean reviewed) {
|
||||||
|
isReviewed = reviewed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean getCashbackReceived() {
|
||||||
|
return isCashbackReceived;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCashbackReceived(Boolean cashbackReceived) {
|
||||||
|
isCashbackReceived = cashbackReceived;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRecipientName() {
|
||||||
|
return recipientName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRecipientName(String recipientName) {
|
||||||
|
this.recipientName = recipientName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRecipientPhone() {
|
||||||
|
return recipientPhone;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRecipientPhone(String recipientPhone) {
|
||||||
|
this.recipientPhone = recipientPhone;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRecipientAddress() {
|
||||||
|
return recipientAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRecipientAddress(String recipientAddress) {
|
||||||
|
this.recipientAddress = recipientAddress;
|
||||||
|
}
|
||||||
|
// Getters and Setters
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取主键ID。
|
* 获取主键ID。
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package cn.van.business.repository;
|
||||||
|
|
||||||
|
import cn.van.business.model.jd.ProductOrder;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface ProductOrderRepository extends JpaRepository<ProductOrder, Long> {
|
||||||
|
}
|
||||||
@@ -2,7 +2,9 @@ package cn.van.business.util;
|
|||||||
|
|
||||||
|
|
||||||
import cn.van.business.model.jd.OrderRow;
|
import cn.van.business.model.jd.OrderRow;
|
||||||
|
import cn.van.business.model.jd.ProductOrder;
|
||||||
import cn.van.business.repository.OrderRowRepository;
|
import cn.van.business.repository.OrderRowRepository;
|
||||||
|
import cn.van.business.repository.ProductOrderRepository;
|
||||||
import com.alibaba.fastjson2.util.DateUtils;
|
import com.alibaba.fastjson2.util.DateUtils;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.jd.open.api.sdk.DefaultJdClient;
|
import com.jd.open.api.sdk.DefaultJdClient;
|
||||||
@@ -74,6 +76,7 @@ public class JDUtil {
|
|||||||
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
private final StringRedisTemplate redisTemplate;
|
private final StringRedisTemplate redisTemplate;
|
||||||
private final OrderRowRepository orderRowRepository;
|
private final OrderRowRepository orderRowRepository;
|
||||||
|
private final ProductOrderRepository productOrderRepository;
|
||||||
private final WXUtil wxUtil;
|
private final WXUtil wxUtil;
|
||||||
private final OrderUtil orderUtil;
|
private final OrderUtil orderUtil;
|
||||||
private static final String INTERACTION_STATE_PREFIX = "interaction_state:";
|
private static final String INTERACTION_STATE_PREFIX = "interaction_state:";
|
||||||
@@ -81,30 +84,37 @@ public class JDUtil {
|
|||||||
|
|
||||||
// 构造函数中注入StringRedisTemplate
|
// 构造函数中注入StringRedisTemplate
|
||||||
@Autowired
|
@Autowired
|
||||||
public JDUtil(StringRedisTemplate redisTemplate, OrderRowRepository orderRowRepository, WXUtil wxUtil, OrderUtil orderUtil) {
|
public JDUtil(StringRedisTemplate redisTemplate,
|
||||||
|
ProductOrderRepository productOrderRepository,OrderRowRepository orderRowRepository, WXUtil wxUtil, OrderUtil orderUtil) {
|
||||||
this.redisTemplate = redisTemplate;
|
this.redisTemplate = redisTemplate;
|
||||||
this.orderRowRepository = orderRowRepository;
|
this.orderRowRepository = orderRowRepository;
|
||||||
|
this.productOrderRepository = productOrderRepository;
|
||||||
this.wxUtil = wxUtil;
|
this.wxUtil = wxUtil;
|
||||||
this.orderUtil = orderUtil;
|
this.orderUtil = orderUtil;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 定义一个内部类来存储用户交互状态
|
// 定义一个内部类来存储用户交互状态
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
private static class UserInteractionState {
|
private static class UserInteractionState {
|
||||||
private String lastInteractionTime;
|
private String lastInteractionTime;
|
||||||
private String currentState;
|
private String currentState;
|
||||||
|
private Map<String, String> collectedFields; // 用于存储收集到的字段值
|
||||||
|
private String currentField; // 当前正在询问的字段
|
||||||
|
|
||||||
public UserInteractionState() {
|
public UserInteractionState() {
|
||||||
this.lastInteractionTime = LocalDateTime.now().format(DATE_TIME_FORMATTER);
|
this.lastInteractionTime = LocalDateTime.now().format(DATE_TIME_FORMATTER);
|
||||||
this.currentState = "INIT";
|
this.currentState = "INIT";
|
||||||
}
|
this.collectedFields = new HashMap<>();
|
||||||
|
this.currentField = null;
|
||||||
public void updateLastInteractionTime() {
|
|
||||||
this.lastInteractionTime = LocalDateTime.now().format(DATE_TIME_FORMATTER);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void updateLastInteractionTime() {
|
||||||
|
this.lastInteractionTime = LocalDateTime.now().format(DATE_TIME_FORMATTER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 添加ObjectMapper来序列化和反序列化UserInteractionState
|
// 添加ObjectMapper来序列化和反序列化UserInteractionState
|
||||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
@@ -1172,107 +1182,108 @@ public void sendOrderToWxByOrderDefault(String order, String fromWxid) {
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* */
|
* */
|
||||||
@Scheduled(fixedRate = 60000) // 每分钟执行一次
|
@Scheduled(fixedRate = 60000) // 每分钟执行一次
|
||||||
public void cleanUpTimeoutStates() {
|
public void cleanUpTimeoutStates() {
|
||||||
LocalDateTime now = LocalDateTime.now();
|
LocalDateTime now = LocalDateTime.now();
|
||||||
redisTemplate.keys(INTERACTION_STATE_PREFIX + "*").forEach(key -> {
|
redisTemplate.keys(INTERACTION_STATE_PREFIX + "*").forEach(key -> {
|
||||||
String stateJson = redisTemplate.opsForValue().get(key);
|
|
||||||
try {
|
|
||||||
UserInteractionState state = objectMapper.readValue(stateJson, UserInteractionState.class);
|
|
||||||
LocalDateTime lastInteractionTime = LocalDateTime.parse(state.getLastInteractionTime(), DATE_TIME_FORMATTER);
|
|
||||||
if (ChronoUnit.MINUTES.between(lastInteractionTime, now) > TIMEOUT_MINUTES) {
|
|
||||||
redisTemplate.delete(key);
|
|
||||||
logger.debug("Deleted timeout state for key: {}", key);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("Error parsing interaction state: {}", e.getMessage());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handleUserInteraction(String fromWxid, String message) {
|
|
||||||
String key = INTERACTION_STATE_PREFIX + fromWxid;
|
|
||||||
String stateJson = redisTemplate.opsForValue().get(key);
|
String stateJson = redisTemplate.opsForValue().get(key);
|
||||||
UserInteractionState state;
|
try {
|
||||||
if (stateJson == null) {
|
UserInteractionState state = objectMapper.readValue(stateJson, UserInteractionState.class);
|
||||||
state = new UserInteractionState();
|
LocalDateTime lastInteractionTime = LocalDateTime.parse(state.getLastInteractionTime(), DATE_TIME_FORMATTER);
|
||||||
logger.debug("New interaction state created for user: {}", fromWxid);
|
if (ChronoUnit.MINUTES.between(lastInteractionTime, now) > TIMEOUT_MINUTES) {
|
||||||
} else {
|
redisTemplate.delete(key);
|
||||||
try {
|
logger.debug("Deleted timeout state for key: {}", key);
|
||||||
state = objectMapper.readValue(stateJson, UserInteractionState.class);
|
}
|
||||||
// 检查是否超时
|
} catch (Exception e) {
|
||||||
LocalDateTime now = LocalDateTime.now();
|
logger.error("Error parsing interaction state: {}", e.getMessage());
|
||||||
LocalDateTime lastInteractionTime = LocalDateTime.parse(state.getLastInteractionTime(), DATE_TIME_FORMATTER);
|
}
|
||||||
if (ChronoUnit.MINUTES.between(lastInteractionTime, now) > TIMEOUT_MINUTES) {
|
});
|
||||||
redisTemplate.delete(key);
|
}
|
||||||
logger.debug("Deleted timeout state for user: {}", fromWxid);
|
|
||||||
state = new UserInteractionState();
|
|
||||||
}
|
public void handleUserInteraction(String fromWxid, String message) {
|
||||||
} catch (Exception e) {
|
String key = INTERACTION_STATE_PREFIX + fromWxid;
|
||||||
logger.error("Error parsing interaction state: {}", e.getMessage());
|
String stateJson = redisTemplate.opsForValue().get(key);
|
||||||
|
UserInteractionState state;
|
||||||
|
if (stateJson == null) {
|
||||||
|
state = new UserInteractionState();
|
||||||
|
logger.debug("New interaction state created for user: {}", fromWxid);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
state = objectMapper.readValue(stateJson, UserInteractionState.class);
|
||||||
|
// 检查是否超时
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
LocalDateTime lastInteractionTime = LocalDateTime.parse(state.getLastInteractionTime(), DATE_TIME_FORMATTER);
|
||||||
|
if (ChronoUnit.MINUTES.between(lastInteractionTime, now) > TIMEOUT_MINUTES) {
|
||||||
|
redisTemplate.delete(key);
|
||||||
|
logger.debug("Deleted timeout state for user: {}", fromWxid);
|
||||||
state = new UserInteractionState();
|
state = new UserInteractionState();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
state.updateLastInteractionTime();
|
|
||||||
|
|
||||||
switch (state.getCurrentState()) {
|
|
||||||
case "INIT":
|
|
||||||
if ("订单".equals(message)) {
|
|
||||||
//1,查询消毒柜订单;2,输入新的订单;3,修改订单
|
|
||||||
String sb = """
|
|
||||||
请选择您要执行的操作:
|
|
||||||
1,查询订单
|
|
||||||
2,输入新的订单
|
|
||||||
3,修改订单""";
|
|
||||||
wxUtil.sendTextMessage(fromWxid, sb, 1, fromWxid);
|
|
||||||
state.setCurrentState("DISINFECTANT_CABINET");
|
|
||||||
logger.debug("User {} entered DISINFECTANT_CABINET state", fromWxid);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "DISINFECTANT_CABINET":
|
|
||||||
switch (message) {
|
|
||||||
case "1":
|
|
||||||
// 查询消毒柜订单的逻辑
|
|
||||||
String sb = "";
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
wxUtil.sendTextMessage(fromWxid, sb, 1, fromWxid);
|
|
||||||
state.setCurrentState("INIT");
|
|
||||||
logger.debug("User {} queried disinfectant cabinet orders", fromWxid);
|
|
||||||
break;
|
|
||||||
case "2":
|
|
||||||
// 输入新的订单的逻辑
|
|
||||||
wxUtil.sendTextMessage(fromWxid, "输入新的订单的逻辑", 1, fromWxid);
|
|
||||||
state.setCurrentState("INIT");
|
|
||||||
logger.debug("User {} input new disinfectant cabinet order", fromWxid);
|
|
||||||
break;
|
|
||||||
case "3":
|
|
||||||
// 修改订单的逻辑
|
|
||||||
wxUtil.sendTextMessage(fromWxid, "修改订单的逻辑", 1, fromWxid);
|
|
||||||
state.setCurrentState("INIT");
|
|
||||||
logger.debug("User {} modified disinfectant cabinet order", fromWxid);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
wxUtil.sendTextMessage(fromWxid, "无效的选择,请重新选择", 1, fromWxid);
|
|
||||||
logger.debug("User {} made invalid choice: {}", fromWxid, message);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
wxUtil.sendTextMessage(fromWxid, "无效的状态,请重新开始对话", 1, fromWxid);
|
|
||||||
state.setCurrentState("INIT");
|
|
||||||
logger.debug("User {} reset to INIT state due to invalid state", fromWxid);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
redisTemplate.opsForValue().set(key, objectMapper.writeValueAsString(state), TIMEOUT_MINUTES, TimeUnit.MINUTES);
|
|
||||||
logger.debug("Saved interaction state for user {}: {}", fromWxid, state);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Error saving interaction state: {}", e.getMessage());
|
logger.error("Error parsing interaction state: {}", e.getMessage());
|
||||||
|
state = new UserInteractionState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
state.updateLastInteractionTime();
|
||||||
|
|
||||||
|
switch (state.getCurrentState()) {
|
||||||
|
case "INIT":
|
||||||
|
if ("登记".equals(message)) {
|
||||||
|
// 开始登记新的订单
|
||||||
|
state.setCurrentState("DISINFECTANT_CABINET");
|
||||||
|
state.setCurrentField("orderId");
|
||||||
|
wxUtil.sendTextMessage(fromWxid, "请输入订单号:", 1, fromWxid);
|
||||||
|
logger.debug("User {} entered DISINFECTANT_CABINET state", fromWxid);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "DISINFECTANT_CABINET":
|
||||||
|
if ("退出".equals(message)) {
|
||||||
|
state.setCurrentState("INIT");
|
||||||
|
wxUtil.sendTextMessage(fromWxid, "退出登记", 1, fromWxid);
|
||||||
|
logger.debug("User {} exited DISINFECTANT_CABINET state", fromWxid);
|
||||||
|
} else {
|
||||||
|
state.getCollectedFields().put(state.getCurrentField(), message);
|
||||||
|
if (state.getCurrentField().equals("orderId")) {
|
||||||
|
state.setCurrentField("recipientName");
|
||||||
|
wxUtil.sendTextMessage(fromWxid, "请输入收件人姓名:", 1, fromWxid);
|
||||||
|
} else if (state.getCurrentField().equals("recipientName")) {
|
||||||
|
state.setCurrentField("recipientPhone");
|
||||||
|
wxUtil.sendTextMessage(fromWxid, "请输入收件人电话:", 1, fromWxid);
|
||||||
|
} else if (state.getCurrentField().equals("recipientPhone")) {
|
||||||
|
// 所有字段收集完毕,保存订单
|
||||||
|
saveProductOrder(state.getCollectedFields());
|
||||||
|
state.setCurrentState("INIT");
|
||||||
|
wxUtil.sendTextMessage(fromWxid, "订单已登记", 1, fromWxid);
|
||||||
|
logger.debug("User {} completed order registration", fromWxid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
wxUtil.sendTextMessage(fromWxid, "无效的状态,请重新开始对话", 1, fromWxid);
|
||||||
|
state.setCurrentState("INIT");
|
||||||
|
logger.debug("User {} reset to INIT state due to invalid state", fromWxid);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
redisTemplate.opsForValue().set(key, objectMapper.writeValueAsString(state), TIMEOUT_MINUTES, TimeUnit.MINUTES);
|
||||||
|
logger.debug("Saved interaction state for user {}: {}", fromWxid, state);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error saving interaction state: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void saveProductOrder(Map<String, String> fields) {
|
||||||
|
// 创建 ProductOrder 对象并保存到数据库
|
||||||
|
ProductOrder productOrder = new ProductOrder();
|
||||||
|
productOrder.setOrderId(fields.get("orderId"));
|
||||||
|
productOrder.setOrderTime(new Date());
|
||||||
|
productOrder.setRecipientName(fields.get("recipientName"));
|
||||||
|
// 设置其他字段...
|
||||||
|
|
||||||
|
// 保存到数据库
|
||||||
|
productOrderRepository.save(productOrder);
|
||||||
|
logger.debug("Saved product order: {}", productOrder);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user