store->save(self::DATASET, $outTrackId, [ 'supplier' => $supplier, 'month' => $month, 'push_basis' => $pushBasis, 'qty' => round($qty, 4), 'amount' => round($amount, 2), 'status' => self::STATUS_SENDING, 'requested_by' => $requestedBy, 'requested_at' => $this->timestamp(), ], $companyId, $requestedBy, 'dingtalk_card_request'); return $this->find($outTrackId, $companyId) ?? []; } public function markSent(string $outTrackId, int $companyId): void { $this->merge($outTrackId, $companyId, [ 'status' => self::STATUS_PENDING, 'sent_at' => $this->timestamp(), 'send_error' => null, ], 'dingtalk_card_sent'); } public function markFailed(string $outTrackId, int $companyId, string $message): void { $this->merge($outTrackId, $companyId, [ 'status' => self::STATUS_FAILED, 'failed_at' => $this->timestamp(), 'send_error' => mb_strimwidth($message, 0, 500, '…', 'UTF-8'), ], 'dingtalk_card_failed'); } /** @return array{record:array,newly_confirmed:bool} */ public function confirm( string $outTrackId, int $companyId, ?string $dingTalkUserId, string $corpId = '', string $source = 'dingtalk_card', ): array { $record = $this->find($outTrackId, $companyId); if ($record === null) { throw new InvalidArgumentException('确认卡片不存在。'); } if (($record['status'] ?? '') === self::STATUS_CONFIRMED) { return ['record' => $record, 'newly_confirmed' => false]; } $changes = [ 'status' => self::STATUS_CONFIRMED, 'confirmed_at' => $this->timestamp(), 'confirmed_source' => $source, ]; if ($dingTalkUserId !== null && trim($dingTalkUserId) !== '') { $changes['confirmed_by_dingtalk_user_id'] = trim($dingTalkUserId); } if ($corpId !== '') { $changes['dingtalk_corp_id'] = $corpId; } $confirmed = array_merge($record, $changes); unset($confirmed['_action_key']); $this->store->save( self::DATASET, $outTrackId, $confirmed, $companyId, null, 'dingtalk_card_callback', ); return [ 'record' => $this->find($outTrackId, $companyId) ?? $confirmed, 'newly_confirmed' => true, ]; } /** @return array|null */ public function find(string $outTrackId, int $companyId): ?array { return $this->store->findRow(self::DATASET, $outTrackId, $companyId); } /** @return array|null */ public function latest( string $supplier, string $month, string $pushBasis, int $companyId, ): ?array { return $this->store->findLatestRow(self::DATASET, [ 'supplier' => $supplier, 'month' => $month, 'push_basis' => $pushBasis, ], $companyId); } public function companyIdFromOutTrackId(string $outTrackId): ?int { if (preg_match('/^supplier-summary-c(\d+)-[a-f0-9]{16,128}$/', strtolower($outTrackId), $matches) !== 1) { return null; } $companyId = (int) $matches[1]; return $companyId > 0 ? $companyId : null; } /** @return array */ public function statusPayload(?array $record): array { $status = trim((string) ($record['status'] ?? '')); if (! in_array($status, [ self::STATUS_SENDING, self::STATUS_PENDING, self::STATUS_CONFIRMED, self::STATUS_FAILED, ], true)) { return [ 'status' => 'none', 'label' => '当前账期尚未推送确认卡片', 'sent_at' => '', 'confirmed_at' => '', 'qty' => null, 'amount' => null, 'push_basis' => '', ]; } $labels = [ self::STATUS_SENDING => '确认卡片正在推送', self::STATUS_PENDING => '等待厂家确认', self::STATUS_CONFIRMED => '厂家已确认', self::STATUS_FAILED => '确认卡片推送失败', ]; return [ 'status' => $status, 'label' => $labels[$status], 'sent_at' => (string) ($record['sent_at'] ?? ''), 'confirmed_at' => (string) ($record['confirmed_at'] ?? ''), 'qty' => isset($record['qty']) ? (float) $record['qty'] : null, 'amount' => isset($record['amount']) ? (float) $record['amount'] : null, 'push_basis' => (string) ($record['push_basis'] ?? ''), ]; } private function merge( string $outTrackId, int $companyId, array $changes, string $source, ): void { $record = $this->find($outTrackId, $companyId); if ($record === null) { return; } $record = array_merge($record, $changes); unset($record['_action_key']); $this->store->save(self::DATASET, $outTrackId, $record, $companyId, null, $source); } private function timestamp(): string { $timezone = trim((string) config('app.platform_timezone', '')); return now($timezone !== '' ? $timezone : 'Asia/Shanghai')->format('Y-m-d H:i:s'); } }