Creation of a new R/MWh cost api route method.

This commit is contained in:
Djonathan 2022-06-21 16:45:05 -03:00
parent ed4a74ab77
commit 67325f063c
3 changed files with 38 additions and 74 deletions

View File

@ -14,6 +14,7 @@ use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class EconomyController extends Controller
{
@ -79,11 +80,6 @@ class EconomyController extends Controller
{
try {
$response = $this->economyContract->getCostMWhEconomy($request->all());
$res = static::checkDate($response);
dd($res);
return (new EconomyResource($response))
->response()
->setStatusCode(Response::HTTP_OK);
@ -92,58 +88,4 @@ class EconomyController extends Controller
}
}
public static function checkDate($value)
{
$start_date = current( $value);
dd($start_date);
$y = [];
foreach ($value as $val) {
$te = explode('/', $val->mes);
unset($te[0]);
$y[] = $te[1];
}
$val = collect($y)->unique();
$start_date = date_create("2021-01-01");
$end_date = date_create("2022-03-30"); // If you want to include this date, add 1 day
$interval = DateInterval::createFromDateString('1 months');
$daterange = new DatePeriod($start_date, $interval, $end_date);
$date = [];
foreach ($daterange as $date1) {
$date[] = $date1->format('m/Y');
}
$arr = collect($value)->toArray();
$i = 0;
foreach ($date as $dt) {
if (empty($arr[$i])){
$arr[] = ['mes' => $dt];
}
$i++;
}
sort($arr);
dd($arr);
// if (!in_array($dt, $arr[$i], true)) {
// $res[] = $dt;
// } else {
// $res[] = $arr[$i];
// }
dd($arr);
dd($arr, $date);
}
}

View File

@ -6,9 +6,12 @@ namespace App\Repositories\Economy;
use App\Models\Economy;
use App\Repositories\AbstractRepository;
use DateInterval;
use DatePeriod;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class EconomyRepository extends AbstractRepository implements EconomyContractInterface
{
@ -93,7 +96,7 @@ class EconomyRepository extends AbstractRepository implements EconomyContractInt
"economia.dad_estimado"
];
return $this->execute($params, $field)
$result = $this->execute($params, $field)
->whereBetween(
DB::raw("TO_DATE(economia.mes, 'YYMM')"),
[
@ -103,24 +106,44 @@ class EconomyRepository extends AbstractRepository implements EconomyContractInt
->groupBy(['mes', 'dad_estimado'])
->get();
return static::checkDate($result);
}
protected function where($query)
public static function checkDate($value): array
{
return $query->where(
DB::raw()
);
$val = collect($value)->transform(fn($item, $value) => collect(Str::of($item['mes'])->explode('/')->offsetGet(1)))->unique()->toArray();
$date_stat = current($val);
$date_end = end($val);
$start_date = date_create("{$date_stat[0]}-01-01");
$end_date = date_create("{$date_end[0]}-03-30"); // If you want to include this date, add 1 day
$interval = DateInterval::createFromDateString('1 months');
$daterange = new DatePeriod($start_date, $interval, $end_date);
$date = [];
foreach ($daterange as $date1) {
$date[] = $date1->format('m/Y');
}
$arr = collect($value)->toArray();
$i = 0;
foreach ($date as $dt) {
if (empty($arr[$i])) {
$arr[] = ['mes' => $dt];
}
$i++;
}
sort($arr);
return $arr;
}
protected function getRowField(): array
{
return [
DB::raw("TO_CHAR(TO_DATE(economia.mes, 'YYMM'), 'MM/YYYY') as mes"),
DB::raw("TRIM(TO_CHAR(economia.custo_cativo, '99999999.99')) as custo_cativo"),
DB::raw("TRIM(TO_CHAR(economia.custo_livre, '99999999.99')) as custo_livre"),
DB::raw("COALESCE(economia.economia_mensal / NULLIF(economia.custo_livre, 0), 0) as custo")
];
}
}

View File

@ -6,7 +6,6 @@ namespace App\Repositories\Pld;
use App\Models\Pld;
use App\Repositories\AbstractRepository;
use Carbon\Carbon;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;