$mainId, 'orderId' => $orderId, 'orderSn' => (string)$orderSn, 'printSn' => (string)$printSn, ], JSON_UNESCAPED_UNICODE); Yii::$app->redis->executeCommand('SETEX', [$mapKey, self::MAP_TTL, $mapData]); $logKey = self::logCacheKey($mainId, $orderId); $list = self::decodeList(Yii::$app->redis->executeCommand('GET', [$logKey])); $list[] = [ 'orderSn' => (string)$orderSn, 'printSn' => (string)$printSn, 'feieOrderId' => $feieOrderId, 'printTime' => 0, 'printTimeText' => '', 'status' => 0, ]; Yii::$app->redis->executeCommand('SETEX', [$logKey, self::CACHE_TTL, json_encode($list, JSON_UNESCAPED_UNICODE)]); return true; } /** * 飞鹅回调更新打印时间与状态 */ public static function updateByCallback($feieOrderId, $stime, $status, $rawCallback = []) { $feieOrderId = trim((string)$feieOrderId); if ($feieOrderId === '') { return false; } $mapRaw = Yii::$app->redis->executeCommand('GET', [self::mapCacheKey($feieOrderId)]); if (empty($mapRaw)) { Yii::warning('飞鹅打印回调未找到映射: ' . $feieOrderId, __METHOD__); return false; } $map = json_decode($mapRaw, true); if (empty($map['mainId']) || empty($map['orderId'])) { return false; } $logKey = self::logCacheKey($map['mainId'], $map['orderId']); $list = self::decodeList(Yii::$app->redis->executeCommand('GET', [$logKey])); $stime = intval($stime); $updated = false; foreach ($list as &$item) { if (($item['feieOrderId'] ?? '') === $feieOrderId) { $item['printTime'] = $stime; $item['printTimeText'] = $stime > 0 ? date('Y-m-d H:i:s', $stime) : ''; $item['status'] = intval($status); if (!empty($rawCallback)) { $item['callbackInfo'] = $rawCallback; } $updated = true; break; } } unset($item); if (!$updated) { $list[] = [ 'orderSn' => (string)($map['orderSn'] ?? ''), 'printSn' => (string)($map['printSn'] ?? ''), 'feieOrderId' => $feieOrderId, 'printTime' => $stime, 'printTimeText' => $stime > 0 ? date('Y-m-d H:i:s', $stime) : '', 'status' => intval($status), 'callbackInfo' => $rawCallback, ]; } Yii::$app->redis->executeCommand('SETEX', [$logKey, self::CACHE_TTL, json_encode($list, JSON_UNESCAPED_UNICODE)]); return true; } /** * 读取订单打印日志,按 printTime 降序 */ public static function getList($mainId, $orderId) { $logKey = self::logCacheKey($mainId, $orderId); $list = self::decodeList(Yii::$app->redis->executeCommand('GET', [$logKey])); usort($list, function ($a, $b) { $timeA = intval($a['printTime'] ?? 0); $timeB = intval($b['printTime'] ?? 0); if ($timeA === $timeB) { return strcmp($b['feieOrderId'] ?? '', $a['feieOrderId'] ?? ''); } return $timeB <=> $timeA; }); return $list; } /** * 解析 Redis 中的 JSON 数组 */ protected static function decodeList($raw) { if (empty($raw)) { return []; } $list = json_decode($raw, true); return is_array($list) ? $list : []; } }