add PLD
This commit is contained in:
parent
2fd0c7bf70
commit
2335dc6540
@ -16,4 +16,5 @@ class Pld extends Model implements Auditing
|
||||
protected $table = "pld";
|
||||
|
||||
protected $guarded = ['id'];
|
||||
|
||||
}
|
||||
|
||||
@ -22,19 +22,12 @@ class PldRepository extends AbstractRepository implements PldContractInterface
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
private function execute($fields, $params): Builder
|
||||
private function execute($fields, $params = []): Builder
|
||||
{
|
||||
$query = $this->model->select($fields);
|
||||
|
||||
$query = static::getFilterBuilder($params)->applyFilter($query);
|
||||
|
||||
if (!empty($params)) {
|
||||
if ($params['field'] === 'mes_ref' && $params['value']) {
|
||||
$query = $query->where(
|
||||
DB::raw("TO_CHAR(TO_DATE(pld.{$params['field']}, 'YYMM'), 'MM/YYYY')"),
|
||||
$params['type'],
|
||||
$params['value']);
|
||||
}
|
||||
$query = static::getFilterBuilder($params)->applyFilter($query);
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
@ -51,43 +44,113 @@ class PldRepository extends AbstractRepository implements PldContractInterface
|
||||
DB::raw("SUM(pld.valor) as value")
|
||||
];
|
||||
|
||||
$params = ["type" => "=", "field" => 'mes_ref', "value" => Carbon::now()->format('m/Y')];
|
||||
|
||||
return $this->execute($fields, $params)
|
||||
return $this->execute($fields)
|
||||
->where( DB::raw("TO_CHAR(TO_DATE(pld.mes_ref, 'YYMM'), 'MM/YYYY')"), '=', Carbon::now()->format('m/Y'))
|
||||
->groupBy(['submarket', 'year_month', 'year_month_formatted'])
|
||||
->get();
|
||||
}
|
||||
|
||||
public function getListConsumption($params)
|
||||
public function getListConsumption($params): Collection|array
|
||||
{
|
||||
// TODO: Implement getListConsumption() method.
|
||||
$fields = [
|
||||
'pld.mes_ref as year_month',
|
||||
DB::raw("TO_CHAR(TO_DATE(pld.mes_ref, 'YYMM'), 'MM/YYYY') as year_month_formatted"),
|
||||
DB::raw('pld_norte.value as norte'),
|
||||
DB::raw('pld_sul.value as sul'),
|
||||
DB::raw('pld_nordeste.value as nordeste'),
|
||||
DB::raw('pld_sudeste.value as sudeste'),
|
||||
];
|
||||
|
||||
$sql = DB::table('pld')
|
||||
->select([
|
||||
'submercado as submarket',
|
||||
'mes_ref as year_month',
|
||||
DB::raw('SUM(valor) as value')
|
||||
])
|
||||
->where('pld.submercado', '=', 'NORTE')
|
||||
->groupBy('submarket', 'year_month');
|
||||
|
||||
$sql2 = DB::table('pld')
|
||||
->select([
|
||||
'submercado as submarket',
|
||||
'mes_ref as year_month',
|
||||
DB::raw('SUM(valor) as value')
|
||||
])
|
||||
->where('pld.submercado', '=', 'SUL')
|
||||
->groupBy('submarket', 'year_month');
|
||||
|
||||
$sql3 = DB::table('pld')
|
||||
->select([
|
||||
'submercado as submarket',
|
||||
'mes_ref as year_month',
|
||||
DB::raw('SUM(valor) as value')
|
||||
])
|
||||
->where('pld.submercado', '=', 'NORDESTE')
|
||||
->groupBy('submarket', 'year_month');
|
||||
|
||||
$sql4 = DB::table('pld')
|
||||
->select([
|
||||
'submercado as submarket',
|
||||
'mes_ref as year_month',
|
||||
DB::raw('SUM(valor) as value')
|
||||
])
|
||||
->where('pld.submercado', '=', 'SUDESTE')
|
||||
->groupBy('submarket', 'year_month');
|
||||
|
||||
|
||||
return $this->model->select($fields)->joinSub($sql, 'pld_norte', function ($join) {
|
||||
$join->on('pld.mes_ref', '=', 'pld_norte.year_month');
|
||||
})->joinSub($sql2, 'pld_sul', function ($join) {
|
||||
$join->on('pld.mes_ref', '=', 'pld_sul.year_month');
|
||||
})->joinSub($sql3, 'pld_nordeste', function ($join) {
|
||||
$join->on('pld.mes_ref', '=', 'pld_nordeste.year_month');
|
||||
})->joinSub($sql4, 'pld_sudeste', function ($join) {
|
||||
$join->on('pld.mes_ref', '=', 'pld_sudeste.year_month');
|
||||
})->distinct()->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BindingResolutionException
|
||||
*/
|
||||
public function getConsumptionByDaily($params): Collection|array
|
||||
public function getConsumptionByDaily($params, $field = "mes_ref"): Collection|array
|
||||
{
|
||||
$fields = static::getRowField();
|
||||
|
||||
$i = 0;
|
||||
foreach ($params['filters'] as $param) {
|
||||
if ($param['field'] === $field) {
|
||||
$params['filters'][$i]['field'] = "TO_CHAR(TO_DATE(pld.{$param['field']}, 'YYMM'), 'MM/YYYY')";
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
|
||||
return $this->execute($fields, $params)->get();
|
||||
|
||||
}
|
||||
|
||||
public function getConsumptionBySchedule($params)
|
||||
public function getConsumptionBySchedule($params, $field = "dia_num")
|
||||
{
|
||||
$fields = static::getRowField();
|
||||
|
||||
return $this->execute($fields, $params)->toSql();
|
||||
$i = 0;
|
||||
foreach ($params['filters'] as $param) {
|
||||
if ($param['field'] === $field) {
|
||||
$params['filters'][$i]['field'] = "(date('1899-12-30') + interval '1' day * pld.{$param['field']})";
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
|
||||
return $this->execute($fields, $params)->get();
|
||||
}
|
||||
|
||||
protected static function getRowField(): array
|
||||
{
|
||||
return [
|
||||
DB::raw("TO_CHAR((date('1899-12-30') + interval '1' day * pld.dia_num), 'DD') as day_formatted"),
|
||||
'hora as hour',
|
||||
DB::raw("(date('1899-12-30') + interval '1' day * pld.dia_num) as day_calc"),
|
||||
'pld.submercado as submarket',
|
||||
'pld.mes_ref as year_month',
|
||||
DB::raw("TO_CHAR(TO_DATE(pld.mes_ref, 'YYMM'), 'MM/YYYY') as year_month"),
|
||||
DB::raw("TO_CHAR(TO_DATE(pld.mes_ref, 'YYMM'), 'MM/YYYY') as year_month_formatted"),
|
||||
];
|
||||
}
|
||||
|
||||
@ -24,8 +24,10 @@ class FilterQueryBuilder extends EntityJson implements IFilterBuilder
|
||||
}
|
||||
}
|
||||
|
||||
$builder->limit($this->limit);
|
||||
$builder->offset($this->offset);
|
||||
if (!empty($this->limit) && !empty($this->offset)){
|
||||
$builder->limit($this->limit);
|
||||
$builder->offset($this->offset);
|
||||
}
|
||||
|
||||
foreach ($this->getOrder() as $order) {
|
||||
$builder->orderBy($order->getField(), $order->getDirection());
|
||||
|
||||
@ -21,7 +21,7 @@ Route::prefix('auth')->group(function (){
|
||||
|
||||
Route::middleware(['auth:sanctum', 'verified'])->group(function () {
|
||||
|
||||
Route::get('pld/overview', [\App\Http\Controllers\PldController::class, 'overviewByRegion']);
|
||||
Route::post('pld/overview', [\App\Http\Controllers\PldController::class, 'overviewByRegion']);
|
||||
Route::post('pld/list', [\App\Http\Controllers\PldController::class, 'listConsumption']);
|
||||
Route::post('pld/daily', [\App\Http\Controllers\PldController::class, 'consumptionByDaily']);
|
||||
Route::post('pld/schedule', [\App\Http\Controllers\PldController::class, 'consumptionBySchedule']);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user