feat: consolidar desenvolvimentos do portal InvestMoz
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Services;
|
||||
|
||||
use App\Models\Opportunity;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
@@ -70,13 +71,89 @@ final class ProactiveSupply
|
||||
// Exact repeated observations are retained in storage, counted separately.
|
||||
$total = (clone $base)->whereNull('o.duplicate_of')->count();
|
||||
$duplicates = (clone $base)->whereNotNull('o.duplicate_of')->count();
|
||||
$rows = (clone $base)->whereNull('o.duplicate_of')->orderBy('o.name')->orderBy('o.id')->offset(($page - 1) * 25)->limit(25)->get(['o.id', 'o.entity_key', 'o.name', 'o.province', 'o.district', 'o.territory_id', 'o.territory_resolution', 'o.cae', 'o.activity', 'o.sector', 'o.source_date', 'o.sheet', 'o.row_number', 'o.source_hash', 'o.identity_conflict', 's.name as source_name', 's.period as source_period', 's.imported_at']);
|
||||
$rows = (clone $base)->whereNull('o.duplicate_of')->orderBy('o.name')->orderBy('o.id')->offset(($page - 1) * 25)->limit(25)->get(['o.id', 'o.name', 'o.province', 'o.district', 'o.territory_id', 'o.territory_resolution', 'o.cae', 'o.activity', 'o.sector', 'o.source_date', 'o.sheet', 'o.row_number', 'o.source_hash', 'o.identity_conflict', 's.name as source_name', 's.period as source_period', 's.imported_at']);
|
||||
$coverage = DB::table('proactive_supply_observations')->selectRaw("count(*) FILTER (WHERE territory_id IS NULL) AS unmapped, count(*) FILTER (WHERE territory_resolution='province_only_district_unknown') AS province_only")->first();
|
||||
|
||||
return ['installed' => true, 'rows' => $rows, 'total' => $total, 'duplicate_sightings' => $duplicates, 'page' => $page, 'per_page' => 25, 'sectors' => $sectors, 'territory_coverage' => $coverage, 'limitations' => self::LIMITATIONS, 'status' => $total ? 'Registos documentais encontrados; não equivalem a empresas únicas ou oferta actual confirmada.' : 'Informação insuficiente nesta base e nestes filtros.'];
|
||||
});
|
||||
}
|
||||
|
||||
/** Review all retained observations, including those that cannot enter a territorial query.
|
||||
* @param array<string,mixed> $input
|
||||
* @return array<string,mixed> */
|
||||
public function review(User $actor, array $input, ?Opportunity $record = null): array
|
||||
{
|
||||
$this->authorize($actor, $record);
|
||||
$filters = Validator::make($input, [
|
||||
'source_hash' => ['nullable', 'string', 'regex:/^[a-f0-9]{64}$/D'],
|
||||
'observation_id' => ['nullable', 'string', 'regex:/^[a-f0-9]{64}$/D'],
|
||||
'q' => ['nullable', 'string', 'max:120'],
|
||||
'status' => ['nullable', Rule::in(['all', 'unmapped', 'ambiguous', 'territory_unavailable', 'identity_conflict', 'repeated', 'unclassified', 'unnamed'])],
|
||||
'page' => ['nullable', 'integer', 'min:1', 'max:100000'],
|
||||
])->validate();
|
||||
$filters['status'] = $filters['status'] ?? 'unmapped';
|
||||
$filters['page'] = (int) ($filters['page'] ?? 1);
|
||||
if (! Schema::hasTable('proactive_supply_observations') || ! Schema::hasTable('proactive_supply_sources')) {
|
||||
return ['installed' => false, 'filters' => $filters, 'sources' => [], 'coverage' => null, 'rows' => [], 'total' => null, 'per_page' => 25,
|
||||
'limitations' => self::LIMITATIONS, 'status' => 'Base interna ainda não carregada neste ambiente.'];
|
||||
}
|
||||
|
||||
return $this->read($actor, function () use ($filters): array {
|
||||
$sources = DB::table('proactive_supply_sources')->orderBy('name')->orderBy('sha256')->get(['sha256', 'name', 'period', 'limitations', 'imported_at']);
|
||||
if (! empty($filters['source_hash']) && ! $sources->contains('sha256', $filters['source_hash'])) {
|
||||
throw ValidationException::withMessages(['source_hash' => 'A origem escolhida não está disponível nesta base interna.']);
|
||||
}
|
||||
$base = DB::table('proactive_supply_observations as o')->join('proactive_supply_sources as s', 's.sha256', '=', 'o.source_hash')
|
||||
->leftJoinSub(app(InvestmentGeography::class)->publicReferences()->select('id', 'name', 'location_type'), 'territory', 'territory.id', '=', 'o.territory_id');
|
||||
if (! empty($filters['source_hash'])) {
|
||||
$base->where('o.source_hash', $filters['source_hash']);
|
||||
}
|
||||
$counts = (clone $base)->selectRaw("count(*) AS observations,
|
||||
count(*) FILTER (WHERE o.duplicate_of IS NULL) AS distinct_observations,
|
||||
count(*) FILTER (WHERE o.duplicate_of IS NOT NULL) AS repeated,
|
||||
count(*) FILTER (WHERE o.territory_id IS NULL) AS unmapped,
|
||||
count(*) FILTER (WHERE o.territory_resolution='ambiguous') AS ambiguous,
|
||||
count(*) FILTER (WHERE o.territory_id IS NOT NULL AND territory.id IS NULL) AS territory_unavailable,
|
||||
count(*) FILTER (WHERE o.identity_conflict) AS identity_conflict,
|
||||
count(*) FILTER (WHERE btrim(o.activity)='') AS unclassified,
|
||||
count(*) FILTER (WHERE btrim(o.name) IN ('','Denominação não indicada')) AS unnamed")->first();
|
||||
$coverage = array_map(fn ($value) => (int) $value, (array) $counts);
|
||||
$this->reviewStatus($base, $filters['status']);
|
||||
if (! empty($filters['observation_id'])) {
|
||||
$base->where('o.id', $filters['observation_id']);
|
||||
}
|
||||
if (! empty($filters['q'])) {
|
||||
$term = str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], mb_strtolower(Str::ascii(trim($filters['q']))));
|
||||
$base->whereRaw("translate(lower(concat_ws(' ',o.name,o.province,o.district,o.cae,o.activity)),'áàâãéêíóôõúç','aaaaeeiooouc') LIKE ? ESCAPE '\\'", ['%'.$term.'%']);
|
||||
}
|
||||
$total = (clone $base)->count();
|
||||
$filters['page'] = min($filters['page'], max(1, (int) ceil($total / 25)));
|
||||
$rows = $base->orderBy('o.source_hash')->orderBy('o.sheet')->orderBy('o.row_number')->orderBy('o.id')
|
||||
->offset(($filters['page'] - 1) * 25)->limit(25)
|
||||
->get(['o.id', 'o.name', 'o.province', 'o.district', 'o.territory_id', 'o.territory_resolution', 'o.cae', 'o.activity', 'o.sector',
|
||||
'o.source_date', 'o.sheet', 'o.row_number', 'o.source_hash', 'o.identity_conflict', 'o.duplicate_of',
|
||||
's.name as source_name', 's.period as source_period', 's.imported_at', 'territory.name as canonical_territory', 'territory.location_type as canonical_type']);
|
||||
|
||||
return ['installed' => true, 'filters' => $filters, 'sources' => $sources, 'coverage' => $coverage, 'rows' => $rows,
|
||||
'total' => $total, 'per_page' => 25, 'limitations' => self::LIMITATIONS,
|
||||
'status' => $total ? 'Observações administrativas para conferência; não são empresas únicas nem prova de actividade actual.' : 'Informação insuficiente nesta base e nestes filtros.'];
|
||||
});
|
||||
}
|
||||
|
||||
private function reviewStatus(Builder $query, string $status): void
|
||||
{
|
||||
match ($status) {
|
||||
'unmapped' => $query->whereNull('o.territory_id'),
|
||||
'ambiguous' => $query->where('o.territory_resolution', 'ambiguous'),
|
||||
'territory_unavailable' => $query->whereNotNull('o.territory_id')->whereNull('territory.id'),
|
||||
'identity_conflict' => $query->where('o.identity_conflict', true),
|
||||
'repeated' => $query->whereNotNull('o.duplicate_of'),
|
||||
'unclassified' => $query->whereRaw("btrim(o.activity)=''"),
|
||||
'unnamed' => $query->whereRaw("btrim(o.name) IN ('','Denominação não indicada')"),
|
||||
default => $query,
|
||||
};
|
||||
}
|
||||
|
||||
/** Resolve server-side source lineage; a caller cannot fabricate an internal observation.
|
||||
* @param array<array-key, mixed> $input
|
||||
* @return array<array-key, mixed>
|
||||
|
||||
Reference in New Issue
Block a user