(eid int64, orderId string, status int, transactionId string, payTime int64)
| 276 | } |
| 277 | |
| 278 | func createOrUpdateOrderFromCacheWithTime(eid int64, orderId string, status int, transactionId string, payTime int64) (*model.Order, error) { |
| 279 | // Lock to prevent concurrent operations |
| 280 | orderMutex.Lock() |
| 281 | defer orderMutex.Unlock() |
| 282 | |
| 283 | // Check if order exists in database |
| 284 | dbOrder, err := model.GetOrderByOrderId(eid, orderId) |
| 285 | if err != nil { |
| 286 | // Order not found in database, check cache |
| 287 | cachedOrder, found := getCachedOrder(orderId) |
| 288 | if !found { |
| 289 | return nil, fmt.Errorf("order not found in database or cache: %s", orderId) |
| 290 | } |
| 291 | |
| 292 | xlog.Info("Order found in cache:", orderId) |
| 293 | // Create order from cached data |
| 294 | cachedOrder.Status = status |
| 295 | if transactionId != "" { |
| 296 | cachedOrder.TransactionId = transactionId |
| 297 | } |
| 298 | |
| 299 | if status == model.OrderStatusPaid && payTime > 0 { |
| 300 | cachedOrder.PayTime = payTime |
| 301 | xlog.Info("Setting payment time from WeChat:", time.UnixMilli(payTime).Format(time.RFC3339)) |
| 302 | } else if status == model.OrderStatusPaid { |
| 303 | cachedOrder.PayTime = time.Now().UTC().UnixMilli() |
| 304 | } |
| 305 | |
| 306 | if err = cachedOrder.Create(); err != nil { |
| 307 | return nil, fmt.Errorf("create order from cache error: %v", err) |
| 308 | } |
| 309 | xlog.Info("Successfully created order from cache with status:", status) |
| 310 | |
| 311 | // Remove from cache |
| 312 | removeCachedOrder(orderId) |
| 313 | xlog.Info("Removed order from cache") |
| 314 | |
| 315 | return cachedOrder, nil |
| 316 | } |
| 317 | |
| 318 | // Order exists, update status |
| 319 | if status == model.OrderStatusPaid { |
| 320 | if payTime > 0 { |
| 321 | err = model.UpdateOrderPaidWithTime(eid, orderId, transactionId, payTime) |
| 322 | dbOrder.PayTime = payTime |
| 323 | } else { |
| 324 | err = model.UpdateOrderPaid(eid, orderId, transactionId) |
| 325 | dbOrder.PayTime = time.Now().UTC().UnixMilli() |
| 326 | } |
| 327 | dbOrder.Status = model.OrderStatusPaid |
| 328 | dbOrder.TransactionId = transactionId |
| 329 | } else { |
| 330 | err = model.UpdateOrderStatus(eid, orderId, status) |
| 331 | dbOrder.Status = status |
| 332 | } |
| 333 | |
| 334 | if err != nil { |
| 335 | return nil, fmt.Errorf("update order status error: %v", err) |
no test coverage detected