connectionName = (string) config('upstream_mirrors.connection', 'business_mirror'); $this->connection = $database->connection($this->connectionName); } /** * 读取每家供应商在目标月份或之前最近一期的结算方式。 * * @param list $suppliers * @return array */ public function latestMethods(array $suppliers, string $month): array { $month = trim($month); $suppliers = array_values(array_unique(array_filter( array_map(static fn (mixed $supplier): string => trim((string) $supplier), $suppliers), static fn (string $supplier): bool => $supplier !== '', ))); if ($suppliers === [] || preg_match('/^\d{4}-(0[1-9]|1[0-2])$/D', $month) !== 1 || ! Schema::connection($this->connectionName)->hasTable(self::TABLE)) { return []; } $rows = $this->connection->table(self::TABLE) ->whereIn('supplier', $suppliers) ->where('Month', '<=', $month) ->whereNotNull('settleway') ->where('settleway', '<>', '') ->orderByDesc('Month') ->orderByDesc('id') ->get(['supplier', 'settleway', 'Month']); $methods = []; foreach ($rows as $row) { $supplier = trim((string) ($row->supplier ?? '')); if ($supplier === '' || isset($methods[$supplier])) { continue; } $settlementMethod = trim((string) ($row->settleway ?? '')); if ($settlementMethod === '') { continue; } $methods[$supplier] = [ 'settlement_method' => $settlementMethod, 'month' => trim((string) ($row->Month ?? '')), ]; } return $methods; } }