0 ? $weight : null; } /** 出库报表只有两位小数;原始值必须属于同一出库单、单号且与报表舍入结果一致。 */ public function outboundRows(array $rows): array { if ($rows === [] || ! Schema::connection($this->connection())->hasTable(self::RAW_TABLE)) return $rows; $ids = array_values(array_unique(array_filter(array_column($rows, 'io_id')))); $raw = []; foreach (array_chunk($ids, 500) as $chunk) { foreach (DB::connection($this->connection())->table(self::RAW_TABLE)->whereIn('io_id', $chunk) ->orderByDesc('Id')->get(['Id', 'io_id', 'l_id', 'weight', 'status']) as $row) { $raw[(string) $row->io_id] ??= $row; } } foreach ($rows as &$row) { $source = $raw[(string) ($row['io_id'] ?? '')] ?? null; if (! $source || ! array_key_exists('weight', $row)) continue; if (ltrim(trim((string) $source->l_id), '@') !== ltrim(trim((string) ($row['l_id'] ?? '')), '@')) continue; if (in_array(strtolower(trim((string) $source->status)), ['delete', 'deleted', 'cancelled', 'canceled', 'cancel', '作废'], true)) continue; $precise = self::positive($source->weight); if ($precise !== null && is_numeric($row['weight']) && abs(round($precise, 2) - (float) $row['weight']) < 0.000001) { $row['weight'] = $precise; } } unset($row); return $rows; } /** 按发货月份+单号+快递取确认值,防止单号复用;不对上游执行写入。 */ public function corrections(array $rows): array { if ($rows === [] || ! Schema::connection($this->connection())->hasTable(self::CORRECTIONS)) return []; $groups = []; foreach ($rows as $row) { $month = substr((string) ($row['ship_time'] ?? ''), 0, 7); $waybill = ltrim(trim((string) ($row['express_no'] ?? '')), '@'); if ($waybill !== '' && preg_match('/^\d{4}-\d{2}$/', $month)) $groups[$month][$waybill] = true; } $out = []; foreach ($groups as $month => $waybills) { foreach (array_chunk(array_keys($waybills), 500) as $chunk) { foreach (DB::connection($this->connection())->table(self::CORRECTIONS) ->where('ship_month', $month)->whereIn('express_no', $chunk)->get() as $correction) { $out[$month . '|' . $correction->express_no . '|' . $correction->carrier] = (array) $correction; } } } return $out; } public function correction(array $row, array $corrections): ?array { $key = substr((string) ($row['ship_time'] ?? ''), 0, 7) . '|' . ltrim(trim((string) ($row['express_no'] ?? '')), '@') . '|' . trim((string) ($row['express_name1'] ?? '')); return $corrections[$key] ?? null; } /** 整包重量只写首行;已确认数据在重新同步后仍保留。 */ public function shipmentRows(array $rows): array { $corrections = $this->corrections($rows); $seen = []; foreach ($rows as &$row) { $correction = $this->correction($row, $corrections); if ($correction === null) continue; foreach (['receiver_state', 'receiver_city', 'receiver_district'] as $field) { if (($correction[$field] ?? null) !== null) $row[$field] = $correction[$field]; } if ($correction['online_weight'] === null) continue; $id = $row['source_id']; $row['online_weight'] = isset($seen[$id]) ? null : (float) $correction['online_weight']; $seen[$id] = true; } unset($row); return $rows; } }