connection = $databases->connection( (string) config('upstream_mirrors.connection', 'business_mirror'), ); } /** * @return list */ public function synchronizeBankAccounts(string $companyName, ?string $companyPlan): array { $companyName = trim($companyName); if ($companyName === '') { return []; } $companyPlan = trim((string) $companyPlan); $desiredPlan = $companyPlan !== '' ? $companyPlan : null; $desiredKeep = FinanceLedgerAccountShouldKeep::fromCompanyPlan($desiredPlan); return $this->connection->transaction(function () use ( $companyName, $desiredPlan, $desiredKeep, ): array { $accounts = $this->connection->table('fin_accounts') ->where('company_name', $companyName) ->orderBy('id') ->lockForUpdate() ->get([ 'id', 'account_short', 'account_type', 'company_plan', 'account_should_keep', ]); if ($accounts->isEmpty()) { return []; } $overrides = $this->connection->table('fin_account_type_overrides') ->whereIn('account_short', $accounts->pluck('account_short')->all()) ->pluck('account_type', 'account_short'); $companyPlanChanges = []; $personalCardChanges = []; foreach ($accounts as $account) { $accountShort = trim((string) $account->account_short); $accountType = $overrides->has($accountShort) ? (string) $overrides->get($accountShort) : (string) ($account->account_type ?? ''); $currentPlan = trim((string) ($account->company_plan ?? '')); $currentKeep = trim((string) ($account->account_should_keep ?? '')); if (FinanceLedgerAccountType::isAlwaysRetained($accountType)) { if ($currentPlan === '保留' && $currentKeep === FinanceLedgerAccountShouldKeep::YES) { continue; } $personalCardChanges[] = [ 'id' => (int) $account->id, 'account_short' => $accountShort, 'company_plan' => '保留', 'account_should_keep' => FinanceLedgerAccountShouldKeep::YES, ]; continue; } if (! FinanceLedgerAccountType::companyPlanDrivesKeep($accountType)) { continue; } $targetPlan = (string) ($desiredPlan ?? ''); $targetKeep = (string) ($desiredKeep ?? ''); if ($currentPlan === $targetPlan && $currentKeep === $targetKeep) { continue; } $companyPlanChanges[] = [ 'id' => (int) $account->id, 'account_short' => $accountShort, 'company_plan' => $targetPlan, 'account_should_keep' => $targetKeep, ]; } if ($companyPlanChanges === [] && $personalCardChanges === []) { return []; } $now = now(); if ($companyPlanChanges !== []) { $this->connection->table('fin_accounts') ->whereIn('id', array_column($companyPlanChanges, 'id')) ->update([ 'company_plan' => $desiredPlan, 'account_should_keep' => $desiredKeep, 'updated_at' => $now, ]); } if ($personalCardChanges !== []) { $this->connection->table('fin_accounts') ->whereIn('id', array_column($personalCardChanges, 'id')) ->update([ 'company_plan' => '保留', 'account_should_keep' => FinanceLedgerAccountShouldKeep::YES, 'updated_at' => $now, ]); } $changes = [...$companyPlanChanges, ...$personalCardChanges]; return array_map( static fn (array $row): array => [ 'account_short' => $row['account_short'], 'company_plan' => $row['company_plan'], 'account_should_keep' => $row['account_should_keep'], 'company_plan_label' => $row['company_plan'] !== '' ? $row['company_plan'] : '未填写', 'account_should_keep_label' => FinanceLedgerAccountShouldKeep::LABELS[ $row['account_should_keep'] ] ?? '未填写', ], $changes, ); }); } }