| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace App\Services;
- use App\Models\InhabitantClassification;
- use App\Models\Unit;
- use App\Models\UnitInhabitantClassification;
- use Illuminate\Database\Eloquent\Collection;
- class InhabitantClassificationService
- {
- public function getAll(): Collection
- {
- return InhabitantClassification::orderBy('id')->get();
- }
- public function getSelectList(): Collection
- {
- return InhabitantClassification::orderBy('id')
- ->get(['id', 'description', 'start', 'end', 'tbr_percentage']);
- }
- public function findById(int $id): ?InhabitantClassification
- {
- return InhabitantClassification::find($id);
- }
- public function create(array $data): InhabitantClassification
- {
- $model = InhabitantClassification::create($data);
- $this->propagateToAllUnits($model);
- return $model;
- }
- public function update(int $id, array $data): ?InhabitantClassification
- {
- $model = $this->findById($id);
- if (!$model) {
- return null;
- }
- $oldStart = $model->start;
- $oldEnd = $model->end;
- $oldTbrPercentage = $model->tbr_percentage;
- $oldDescription = $model->description;
- $model->update($data);
- $updated = $model->fresh();
- $this->propagateUpdateToUnits($updated, [
- 'description' => $oldDescription,
- 'start' => $oldStart,
- 'end' => $oldEnd,
- 'tbr_percentage' => $oldTbrPercentage,
- ]);
- return $updated;
- }
- public function delete(int $id): bool
- {
- $model = $this->findById($id);
- if (!$model) {
- return false;
- }
- return $model->delete();
- }
- //
- private function propagateToAllUnits(InhabitantClassification $parent): void
- {
- $existingUnitIds = UnitInhabitantClassification::where('start', $parent->start)
- ->where('end', $parent->end)
- ->pluck('unit_id');
- Unit::whereNotIn('id', $existingUnitIds)
- ->each(function (Unit $unit) use ($parent) {
- UnitInhabitantClassification::create([
- 'unit_id' => $unit->id,
- 'description' => $parent->description,
- 'start' => $parent->start,
- 'end' => $parent->end,
- 'tbr_percentage' => $parent->tbr_percentage,
- ]);
- });
- }
- private function propagateUpdateToUnits(InhabitantClassification $parent, array $old): void
- {
- // propaga a atualizacao para as classificacoes de unidades que tenham os mesmos valores antigos
- UnitInhabitantClassification::where('start', $old['start'])
- ->where('end', $old['end'])
- ->where('tbr_percentage', $old['tbr_percentage'])
- ->where('description', $old['description'])
- ->update([
- 'description' => $parent->description,
- 'start' => $parent->start,
- 'end' => $parent->end,
- 'tbr_percentage' => $parent->tbr_percentage,
- ]);
- // garante que todas as unidades tenham a classificacao, criando para as que ainda nao tem
- $coveredUnitIds = UnitInhabitantClassification::where(function ($q) use ($parent, $old) {
- $q->where(fn($q) => $q->where('start', $parent->start)->where('end', $parent->end))
- ->orWhere(fn($q) => $q->where('start', $old['start'])->where('end', $old['end']));
- })
- ->pluck('unit_id')
- ->unique();
- Unit::whereNotIn('id', $coveredUnitIds)
- ->each(function (Unit $unit) use ($parent) {
- UnitInhabitantClassification::create([
- 'unit_id' => $unit->id,
- 'description' => $parent->description,
- 'start' => $parent->start,
- 'end' => $parent->end,
- 'tbr_percentage' => $parent->tbr_percentage,
- ]);
- });
- }
- }
|