67 lines
3.7 KiB
PHP
67 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\DevelopmentCorridor;
|
|
use App\Models\EconomicCluster;
|
|
use App\Models\InvestmentDossier;
|
|
use App\Models\Project;
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Str;
|
|
|
|
class InvestmentPortfolioSeeder extends Seeder
|
|
{
|
|
public function run(): void
|
|
{
|
|
$data = json_decode(File::get(database_path('data/investment-portfolio.json')), true, 512, JSON_THROW_ON_ERROR);
|
|
DB::transaction(function () use ($data) {
|
|
DB::select('select pg_advisory_xact_lock(250925, 130000)');
|
|
$ids = [];
|
|
foreach (['projects' => [Project::class, 'project_id', 'draft', '6.1 / 6.6'],
|
|
'clusters' => [EconomicCluster::class, 'cluster_id', 'planned', '4.5.2'],
|
|
'corridors' => [DevelopmentCorridor::class, 'corridor_id', 'proposed', '4.3.4']] as $kind => [$model, $foreign, $status, $section]) {
|
|
foreach ($data[$kind] as $item) {
|
|
// Never overwrite a manually edited record or silently take over its code.
|
|
$record = $model::withTrashed()->where('code', $item['code'])->first();
|
|
if ($record && ($record->getAttribute('source_system') !== 'upgrade-v4' || $record->trashed())) {
|
|
throw new \RuntimeException('Portfolio code collision: '.$item['code']);
|
|
}
|
|
$record ??= $model::create(['code' => $item['code'], 'name' => $item['name'], 'status' => $status,
|
|
'source_system' => 'upgrade-v4', 'external_id' => $item['code'],
|
|
...($kind === 'clusters' ? ['sector' => $item['name']] : ['description' => $item['description'] ?? $item['focus']])]);
|
|
$ids[$item['code']] = $record->id;
|
|
InvestmentDossier::withTrashed()->firstOrCreate([$foreign => $record->id], [
|
|
'title' => $item['name'], 'source' => $data['source'].' — '.$section,
|
|
'content' => [...$item, 'notice' => $data['notice']], 'publication_status' => 'draft',
|
|
]);
|
|
}
|
|
}
|
|
foreach (['clusters' => ['cluster_projects', 'cluster_id'], 'corridors' => ['corridor_projects', 'corridor_id']] as $kind => [$table, $foreign]) {
|
|
foreach ($data[$kind] as $item) {
|
|
preg_match_all('/\bA\d{2}\b/', $item['anchors'], $matches);
|
|
foreach (array_unique($matches[0]) as $code) {
|
|
$key = [$foreign => $ids[$item['code']], 'project_id' => $ids[$code]];
|
|
if (! DB::table($table)->where($key)->exists()) {
|
|
DB::table($table)->insert([...$key, 'id' => (string) Str::uuid(), 'source_system' => 'upgrade-v4',
|
|
...($kind === 'clusters' ? ['role' => 'anchor'] : [])]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Only unambiguous correspondences in section 4.3.4; no inferred mining anchors.
|
|
$matrix = ['COR-01' => [1, 2, 3, 4, 6], 'COR-02' => [1, 2, 3, 5], 'COR-03' => [1, 2, 3, 6],
|
|
'COR-04' => [1, 3], 'COR-05' => [4, 7], 'COR-06' => [2, 3, 7]];
|
|
foreach ($matrix as $corridor => $clusters) {
|
|
foreach ($clusters as $cluster) {
|
|
$key = ['corridor_id' => $ids[$corridor], 'cluster_id' => $ids[sprintf('CLU-%02d', $cluster)]];
|
|
if (! DB::table('corridor_clusters')->where($key)->exists()) {
|
|
DB::table('corridor_clusters')->insert([...$key, 'id' => (string) Str::uuid(), 'source_system' => 'upgrade-v4']);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|