|
|
@@ -6,7 +6,9 @@ import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.haha.common.enums.DeviceDoorStatus;
|
|
|
import com.haha.common.enums.RecognizeConsumeType;
|
|
|
import com.haha.entity.Order;
|
|
|
+import com.haha.entity.OrderGoods;
|
|
|
import com.haha.mapper.DeviceMapper;
|
|
|
+import com.haha.mapper.OrderGoodsMapper;
|
|
|
import com.haha.sdk.HahaClient;
|
|
|
import com.haha.service.HahaCallbackService;
|
|
|
import com.haha.service.OrderService;
|
|
|
@@ -19,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
@@ -33,6 +36,9 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
|
|
|
@Autowired
|
|
|
private DeviceMapper deviceMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private OrderGoodsMapper orderGoodsMapper;
|
|
|
+
|
|
|
@Autowired
|
|
|
private StringRedisTemplate stringRedisTemplate;
|
|
|
|
|
|
@@ -234,19 +240,9 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
|
|
|
|
|
|
Order existingOrder = orderService.getOrderByActivityId(activityId);
|
|
|
if (existingOrder != null) {
|
|
|
- log.info("订单已存在(开门时创建),更新订单信息 - activityId: {}, orderId: {}", activityId, existingOrder.getId());
|
|
|
+ log.info("更新订单信息 - activityId: {}, orderId: {}", activityId, existingOrder.getId());
|
|
|
updateOrderFromRecognition(existingOrder, skuListStr, resourceInfoStr, confidence, activityId);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- logRecognizeResultDetails(resultStr, skuListStr, resourceInfoStr);
|
|
|
-
|
|
|
- Order order = orderService.createOrderFromRecognition(
|
|
|
- activityId, deviceId, userId, skuListStr, resourceInfoStr, confidence);
|
|
|
-
|
|
|
- if (order != null) {
|
|
|
- log.info("AI识别订单创建成功 - 订单ID: {}", order.getId());
|
|
|
- saveOrderInfoToRedis(order, activityId);
|
|
|
+ logRecognizeResultDetails(resultStr, skuListStr, resourceInfoStr);
|
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
@@ -264,9 +260,11 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
|
|
|
String userId = (String) params.get("user_id");
|
|
|
Object orderMoney = params.get("order_money");
|
|
|
String orderDetail = (String) params.get("order_detail");
|
|
|
+ String orderName = (String) params.get("order_name");
|
|
|
+ String orderGoodsStr = (String) params.get("order_goods");
|
|
|
|
|
|
- log.info("订单信息 - 订单号: {}, 设备: {}, 活动: {}, 用户: {}, 金额: {}",
|
|
|
- orderId, deviceId, activityId, userId, orderMoney);
|
|
|
+ log.info("订单信息 - 订单号: {}, 设备: {}, 活动: {}, 用户: {}, 金额: {}, 商品: {}",
|
|
|
+ orderId, deviceId, activityId, userId, orderMoney, orderName);
|
|
|
|
|
|
Order localOrder = null;
|
|
|
if (activityId != null && !activityId.isEmpty()) {
|
|
|
@@ -289,7 +287,7 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- updateOrderFromCallback(localOrder, orderId, activityId, orderMoney, orderDetail);
|
|
|
+ updateOrderFromCallback(localOrder, orderId, activityId, orderMoney, orderDetail, orderName, orderGoodsStr);
|
|
|
saveOrderInfoToRedis(localOrder, activityId, orderId, orderMoney, orderDetail);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
@@ -349,9 +347,9 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
|
|
|
private void saveOrderInfoToRedis(Order order, String activityId) {
|
|
|
try {
|
|
|
String orderKey = ORDER_INFO_KEY + activityId;
|
|
|
-
|
|
|
+
|
|
|
stringRedisTemplate.delete(orderKey);
|
|
|
-
|
|
|
+
|
|
|
Map<String, String> orderData = new HashMap<>();
|
|
|
orderData.put("orderId", order.getId() != null ? order.getId().toString() : "");
|
|
|
orderData.put("orderNo", order.getOrderNo() != null ? order.getOrderNo() : "");
|
|
|
@@ -374,9 +372,9 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
|
|
|
private void saveOrderInfoToRedis(Order order, String activityId, String orderId, Object orderMoney, String orderDetail) {
|
|
|
try {
|
|
|
String orderKey = ORDER_INFO_KEY + activityId;
|
|
|
-
|
|
|
+
|
|
|
stringRedisTemplate.delete(orderKey);
|
|
|
-
|
|
|
+
|
|
|
Map<String, String> orderData = new HashMap<>();
|
|
|
orderData.put("orderId", orderId != null ? orderId : "");
|
|
|
orderData.put("activityId", activityId != null ? activityId : "");
|
|
|
@@ -408,6 +406,15 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据识别结果更新订单信息
|
|
|
+ *
|
|
|
+ * @param order 订单对象
|
|
|
+ * @param skuListStr 商品列表字符串
|
|
|
+ * @param resourceInfoStr 资源信息字符串
|
|
|
+ * @param confidence 置信度
|
|
|
+ * @param activityId 活动ID
|
|
|
+ */
|
|
|
private void updateOrderFromRecognition(Order order, String skuListStr, String resourceInfoStr, BigDecimal confidence, String activityId) {
|
|
|
BigDecimal totalAmount = calculateTotalAmount(skuListStr);
|
|
|
String videoUrl = parseVideoUrl(resourceInfoStr);
|
|
|
@@ -429,10 +436,16 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
|
|
|
saveOrderInfoToRedis(order, activityId);
|
|
|
}
|
|
|
|
|
|
- private void updateOrderFromCallback(Order order, String orderId, String activityId, Object orderMoney, String orderDetail) {
|
|
|
+ private void updateOrderFromCallback(Order order, String orderId, String activityId,
|
|
|
+ Object orderMoney, String orderDetail,
|
|
|
+ String orderName, String orderGoodsStr) {
|
|
|
order.setOrderNo(orderId);
|
|
|
order.setActivityId(activityId);
|
|
|
|
|
|
+ if (orderName != null && !orderName.isEmpty()) {
|
|
|
+ order.setOrderName(orderName);
|
|
|
+ }
|
|
|
+
|
|
|
if (orderDetail != null) {
|
|
|
order.setItems(orderDetail);
|
|
|
}
|
|
|
@@ -444,11 +457,63 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
|
|
|
boolean updated = orderService.updateById(order);
|
|
|
if (updated) {
|
|
|
log.info("订单信息更新成功: {}", orderId);
|
|
|
+
|
|
|
+ saveOrderGoods(order.getId(), orderId, activityId, orderGoodsStr, order.getDeviceId(), order.getUserId());
|
|
|
} else {
|
|
|
log.error("订单信息更新失败: {}", orderId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void saveOrderGoods(Long orderId, String orderNo, String activityId,
|
|
|
+ String orderGoodsStr, String deviceId, Long userId) {
|
|
|
+ if (orderGoodsStr == null || orderGoodsStr.isEmpty()) {
|
|
|
+ log.info("订单商品信息为空,跳过保存");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ JSONArray goodsArray = JSON.parseArray(orderGoodsStr);
|
|
|
+ if (goodsArray == null || goodsArray.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < goodsArray.size(); i++) {
|
|
|
+ JSONObject goodsJson = goodsArray.getJSONObject(i);
|
|
|
+ OrderGoods goods = new OrderGoods();
|
|
|
+ goods.setOrderId(orderId);
|
|
|
+ goods.setOrderNo(orderNo);
|
|
|
+ goods.setActivityId(activityId);
|
|
|
+ goods.setDeviceId(deviceId);
|
|
|
+ goods.setUserId(userId);
|
|
|
+ goods.setCreateTime(LocalDateTime.now());
|
|
|
+
|
|
|
+ goods.setProductId(goodsJson.getLong("id"));
|
|
|
+ goods.setCode(goodsJson.getString("code"));
|
|
|
+ goods.setBarCode(goodsJson.getString("bar_code"));
|
|
|
+ goods.setProductName(goodsJson.getString("product_name"));
|
|
|
+ goods.setPic(goodsJson.getString("pic"));
|
|
|
+ goods.setProductNum(goodsJson.getInteger("product_num"));
|
|
|
+
|
|
|
+ if (goodsJson.get("money") != null) {
|
|
|
+ goods.setMoney(new BigDecimal(goodsJson.getString("money")));
|
|
|
+ }
|
|
|
+ if (goodsJson.get("origin_price") != null) {
|
|
|
+ goods.setOriginPrice(goodsJson.getBigDecimal("origin_price"));
|
|
|
+ }
|
|
|
+ if (goodsJson.get("price") != null) {
|
|
|
+ goods.setPrice(goodsJson.getBigDecimal("price"));
|
|
|
+ }
|
|
|
+
|
|
|
+ orderGoodsMapper.insert(goods);
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("订单商品保存成功 - orderId: {}, 商品数量: {}", orderId, goodsArray.size());
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("保存订单商品失败 - orderId: {}", orderId, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private Order fetchRecognitionAndCreateOrder(String activityId, String deviceId, String userId) {
|
|
|
try {
|
|
|
Map<String, Object> recognizeResult = hahaClient.getOrderApi().getRecognizeResult(null, activityId);
|
|
|
@@ -499,6 +564,13 @@ public class HahaCallbackServiceImpl implements HahaCallbackService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 记录识别结果详情
|
|
|
+ *
|
|
|
+ * @param resultStr 识别结果字符串
|
|
|
+ * @param skuListStr 商品列表字符串
|
|
|
+ * @param resourceInfoStr 资源信息字符串
|
|
|
+ */
|
|
|
private void logRecognizeResultDetails(String resultStr, String skuListStr, String resourceInfoStr) {
|
|
|
if (resultStr != null && !resultStr.isEmpty()) {
|
|
|
JSONObject result = JSON.parseObject(resultStr);
|