Initial commit
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Concerns\SortsLists;
|
||||
use App\Models\Engine;
|
||||
use App\Models\Manufacturer;
|
||||
use App\Models\Pump;
|
||||
use App\Services\PumpService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Katalog agregatow (legacy site205 / site205E) - "agregat" = pompa + przypisane
|
||||
* silniki (seg_pump_engine_rel). Uklad lista -> szczegoly (jak pozostale katalogi).
|
||||
*/
|
||||
class AggregateController extends Controller
|
||||
{
|
||||
use SortsLists;
|
||||
|
||||
private const MCF_ALL = 100;
|
||||
|
||||
/** Sortowalne kolumny listy: klucz nagłówka => kolumna/alias SQL. */
|
||||
private const SORTABLE = [
|
||||
'name' => 'PUP_NAME',
|
||||
'flow' => 'PUP_Q_N',
|
||||
'mcf' => 'm.MCF_NAME',
|
||||
'engines' => 'engines_count',
|
||||
];
|
||||
|
||||
public function __construct(private readonly PumpService $pumps) {}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$by = $request->query('by', 'name');
|
||||
$q = trim((string) $request->query('q', ''));
|
||||
$mcf = (int) $request->query('mcf', self::MCF_ALL);
|
||||
|
||||
[$sort, $dir] = $this->sortParams($request);
|
||||
|
||||
$aggregates = Pump::query()
|
||||
->visible()->with('manufacturer')->where('PUP_Q_N', '>', 0)
|
||||
->select('seg_pumps.*')
|
||||
->selectSub(
|
||||
DB::connection('softspm')->table('seg_pump_engine_rel')
|
||||
->whereColumn('RPE_PUP_ID', 'seg_pumps.PUP_ID')->selectRaw('count(*)'),
|
||||
'engines_count'
|
||||
)
|
||||
->when($q !== '' && $by === 'name', fn ($w) => $w->where('PUP_NAME', 'like', '%'.$q.'%'))
|
||||
->when($q !== '' && $by === 'flow', function ($w) use ($q) {
|
||||
$v = (float) str_replace(',', '.', $q);
|
||||
$w->whereBetween('PUP_Q_N', [$v - $v * 0.1, $v + $v * 0.1]);
|
||||
})
|
||||
->when($mcf !== self::MCF_ALL, fn ($w) => $w->where('PUP_MCF_ID', $mcf))
|
||||
// Sortowanie po producencie wymaga joinu (relacja nie jest w FROM).
|
||||
->when($sort === 'mcf', fn ($w) => $w
|
||||
->leftJoin('seg_manufacturers as m', 'm.MCF_ID', '=', 'seg_pumps.PUP_MCF_ID'))
|
||||
->when($sort !== null, fn ($w) => $w->orderBy(self::SORTABLE[$sort], $dir))
|
||||
->orderBy('PUP_Q_N')->orderBy('PUP_NAME')
|
||||
->paginate(20)->withQueryString();
|
||||
|
||||
return view('catalog.aggregates.index', [
|
||||
'aggregates' => $aggregates,
|
||||
'manufacturers' => Manufacturer::orderBy('MCF_NAME')->pluck('MCF_NAME', 'MCF_ID'),
|
||||
'by' => $by, 'q' => $q, 'mcf' => $mcf, 'mcfAll' => self::MCF_ALL,
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(Request $request, Pump $pump)
|
||||
{
|
||||
$pump->load('manufacturer');
|
||||
|
||||
$h = $this->pumps->curve($pump, 'H');
|
||||
$eta = $this->pumps->curve($pump, 'ETA');
|
||||
$hDrawable = $this->pumps->curveDrawable($h, 0);
|
||||
$etaDrawable = $this->pumps->curveDrawable($eta, 1);
|
||||
|
||||
$engines = $this->assignedEngines($pump->PUP_ID);
|
||||
$engineId = (int) $request->query('engine') ?: (int) ($engines->first()->ENG_ID ?? 0);
|
||||
$engine = $engineId && $engines->firstWhere('ENG_ID', $engineId)
|
||||
? Engine::with('manufacturer')->find($engineId)
|
||||
: null;
|
||||
|
||||
return view('catalog.aggregates.show', [
|
||||
'pump' => $pump,
|
||||
'engines' => $engines,
|
||||
'engine' => $engine,
|
||||
'h' => $h, 'eta' => $eta,
|
||||
'hDrawable' => $hDrawable, 'etaDrawable' => $etaDrawable,
|
||||
'hPlot' => $hDrawable ? $this->pumps->plot($h) : null,
|
||||
'etaPlot' => $etaDrawable ? $this->pumps->plot($eta) : null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(Pump $pump)
|
||||
{
|
||||
$allEngines = Engine::query()->visible()->with('manufacturer')
|
||||
->orderBy('ENG_POWER')->orderBy('ENG_NAME')->get();
|
||||
|
||||
$assigned = DB::connection('softspm')->table('seg_pump_engine_rel')
|
||||
->where('RPE_PUP_ID', $pump->PUP_ID)->pluck('RPE_ENG_ID')->all();
|
||||
|
||||
return view('catalog.aggregates.edit', [
|
||||
'pump' => $pump->load('manufacturer'),
|
||||
'allEngines' => $allEngines,
|
||||
'assigned' => array_map('intval', $assigned),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, Pump $pump)
|
||||
{
|
||||
$engineIds = collect($request->input('engines', []))->map(fn ($v) => (int) $v)->filter()->unique();
|
||||
|
||||
DB::connection('softspm')->transaction(function () use ($pump, $engineIds) {
|
||||
DB::connection('softspm')->table('seg_pump_engine_rel')->where('RPE_PUP_ID', $pump->PUP_ID)->delete();
|
||||
foreach ($engineIds as $eid) {
|
||||
DB::connection('softspm')->table('seg_pump_engine_rel')
|
||||
->insert(['RPE_PUP_ID' => $pump->PUP_ID, 'RPE_ENG_ID' => $eid]);
|
||||
}
|
||||
});
|
||||
|
||||
return redirect()->route('aggregates.show', $pump)
|
||||
->with('status', 'Zaktualizowano silniki dla pompy "'.$pump->PUP_NAME.'".');
|
||||
}
|
||||
|
||||
private function assignedEngines(int $pumpId)
|
||||
{
|
||||
return DB::connection('softspm')->table('seg_pump_engine_rel as r')
|
||||
->join('seg_engines as e', 'e.ENG_ID', '=', 'r.RPE_ENG_ID')
|
||||
->leftJoin('seg_manufacturers as m', 'm.MCF_ID', '=', 'e.ENG_MCF_ID')
|
||||
->where('r.RPE_PUP_ID', $pumpId)
|
||||
->select('e.ENG_ID', 'e.ENG_NAME', 'e.ENG_VOLTAGE', 'e.ENG_POWER', 'e.ENG_ETA', 'm.MCF_NAME')
|
||||
->orderBy('e.ENG_POWER')->orderBy('e.ENG_NAME')->get();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user