This commit is contained in:
Leo
2025-03-02 14:39:31 +08:00
parent 41fae55b3f
commit d8e97e7e2b
3 changed files with 174 additions and 110 deletions

View File

@@ -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。

View File

@@ -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> {
}

View File

@@ -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,29 +84,36 @@ 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() { public void updateLastInteractionTime() {
this.lastInteractionTime = LocalDateTime.now().format(DATE_TIME_FORMATTER); 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,8 +1182,8 @@ 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); String stateJson = redisTemplate.opsForValue().get(key);
@@ -1188,9 +1198,10 @@ public void sendOrderToWxByOrderDefault(String order, String fromWxid) {
logger.error("Error parsing interaction state: {}", e.getMessage()); logger.error("Error parsing interaction state: {}", e.getMessage());
} }
}); });
} }
public void handleUserInteraction(String fromWxid, String message) {
public void handleUserInteraction(String fromWxid, String message) {
String key = INTERACTION_STATE_PREFIX + fromWxid; String key = INTERACTION_STATE_PREFIX + fromWxid;
String stateJson = redisTemplate.opsForValue().get(key); String stateJson = redisTemplate.opsForValue().get(key);
UserInteractionState state; UserInteractionState state;
@@ -1217,46 +1228,34 @@ public void sendOrderToWxByOrderDefault(String order, String fromWxid) {
switch (state.getCurrentState()) { switch (state.getCurrentState()) {
case "INIT": case "INIT":
if ("订单".equals(message)) { if ("登记".equals(message)) {
//1查询消毒柜订单2输入新的订单3修改订单 // 开始登记新的订单
String sb = """
请选择您要执行的操作:
1查询订单
2输入新的订单
3修改订单""";
wxUtil.sendTextMessage(fromWxid, sb, 1, fromWxid);
state.setCurrentState("DISINFECTANT_CABINET"); state.setCurrentState("DISINFECTANT_CABINET");
state.setCurrentField("orderId");
wxUtil.sendTextMessage(fromWxid, "请输入订单号:", 1, fromWxid);
logger.debug("User {} entered DISINFECTANT_CABINET state", fromWxid); logger.debug("User {} entered DISINFECTANT_CABINET state", fromWxid);
} }
break; break;
case "DISINFECTANT_CABINET": case "DISINFECTANT_CABINET":
switch (message) { if ("退出".equals(message)) {
case "1":
// 查询消毒柜订单的逻辑
String sb = "";
wxUtil.sendTextMessage(fromWxid, sb, 1, fromWxid);
state.setCurrentState("INIT"); state.setCurrentState("INIT");
logger.debug("User {} queried disinfectant cabinet orders", fromWxid); wxUtil.sendTextMessage(fromWxid, "退出登记", 1, fromWxid);
break; logger.debug("User {} exited DISINFECTANT_CABINET state", fromWxid);
case "2": } else {
// 输入新的订单的逻辑 state.getCollectedFields().put(state.getCurrentField(), message);
wxUtil.sendTextMessage(fromWxid, "输入新的订单的逻辑", 1, fromWxid); 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"); state.setCurrentState("INIT");
logger.debug("User {} input new disinfectant cabinet order", fromWxid); wxUtil.sendTextMessage(fromWxid, "订单已登记", 1, fromWxid);
break; logger.debug("User {} completed order registration", fromWxid);
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; break;
default: default:
@@ -1272,7 +1271,19 @@ public void sendOrderToWxByOrderDefault(String order, String fromWxid) {
} catch (Exception e) { } catch (Exception e) {
logger.error("Error saving interaction state: {}", e.getMessage()); 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);
}