概況欄の時間表記が狂っていたため、フォーマットの担当をDBからPHPに移した

This commit is contained in:
shibafu 2017-09-09 02:10:51 +09:00
parent be2cf3328a
commit 12d06dc88b
8 changed files with 82 additions and 18 deletions

13
app/Facades/Formatter.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Formatter extends Facade
{
protected static function getFacadeAccessor()
{
return \App\Utilities\Formatter::class;
}
}

View File

@ -52,15 +52,15 @@ SQL
// 概況欄のデータ取得 // 概況欄のデータ取得
$summary = DB::select(<<<'SQL' $summary = DB::select(<<<'SQL'
SELECT SELECT
to_char(avg(span), 'FMDDD日 FMHH24時間 FMMI分') AS average, avg(span) AS average,
to_char(max(span), 'FMDDD日 FMHH24時間 FMMI分') AS longest, max(span) AS longest,
to_char(min(span), 'FMDDD日 FMHH24時間 FMMI分') AS shortest, min(span) AS shortest,
to_char(sum(span), 'FMDDD日 FMHH24時間 FMMI分') AS total_times, sum(span) AS total_times,
count(*) AS total_checkins count(*) AS total_checkins
FROM FROM
( (
SELECT SELECT
ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC) AS span extract(epoch from ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)) AS span
FROM FROM
ejaculations ejaculations
WHERE WHERE

View File

@ -48,15 +48,15 @@ SQL
// 概況欄のデータ取得 // 概況欄のデータ取得
$summary = DB::select(<<<'SQL' $summary = DB::select(<<<'SQL'
SELECT SELECT
to_char(avg(span), 'FMDDD日 FMHH24時間 FMMI分') AS average, avg(span) AS average,
to_char(max(span), 'FMDDD日 FMHH24時間 FMMI分') AS longest, max(span) AS longest,
to_char(min(span), 'FMDDD日 FMHH24時間 FMMI分') AS shortest, min(span) AS shortest,
to_char(sum(span), 'FMDDD日 FMHH24時間 FMMI分') AS total_times, sum(span) AS total_times,
count(*) AS total_checkins count(*) AS total_checkins
FROM FROM
( (
SELECT SELECT
ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC) AS span extract(epoch from ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)) AS span
FROM FROM
ejaculations ejaculations
WHERE WHERE

View File

@ -0,0 +1,31 @@
<?php
namespace App\Providers;
use App\Utilities\Formatter;
use Illuminate\Support\ServiceProvider;
class FormatterServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(Formatter::class, function ($app) {
return new Formatter();
});
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace App\Utilities;
class Formatter
{
/**
* 通算秒数を日数と時分にフォーマットします。
* @param int|float $value 通算秒数
* @return string "xx日 xx時間 xx分" 形式でフォーマットされた文字列
*/
public function formatInterval($value)
{
$days = floor($value / 86400);
$hours = floor($value % 86400 / 3600);
$minutes = floor($value % 3600 / 60);
return "{$days}{$hours}時間 {$minutes}";
}
}

View File

@ -226,6 +226,7 @@ return [
'Validator' => Illuminate\Support\Facades\Validator::class, 'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class, 'View' => Illuminate\Support\Facades\View::class,
'Formatter' => App\Facades\Formatter::class,
], ],
]; ];

View File

@ -31,10 +31,10 @@
@if (isset($summary) && $summary[0]->total_checkins > 0) @if (isset($summary) && $summary[0]->total_checkins > 0)
<hr> <hr>
<p class="valign-wrapper"><i class="material-icons">assessment</i><b>概況</b></p> <p class="valign-wrapper"><i class="material-icons">assessment</i><b>概況</b></p>
<p>平均記録: {{ $summary[0]->average }}</p> <p>平均記録: {{ Formatter::formatInterval($summary[0]->average) }}</p>
<p>最長記録: {{ $summary[0]->longest }}</p> <p>最長記録: {{ Formatter::formatInterval($summary[0]->longest) }}</p>
<p>最短記録: {{ $summary[0]->shortest }}</p> <p>最短記録: {{ Formatter::formatInterval($summary[0]->shortest) }}</p>
<p>合計時間: {{ $summary[0]->total_times }}</p> <p>合計時間: {{ Formatter::formatInterval($summary[0]->total_times) }}</p>
<p>通算回数: {{ $summary[0]->total_checkins }}</p> <p>通算回数: {{ $summary[0]->total_checkins }}</p>
@endif @endif
</div> </div>

View File

@ -39,10 +39,10 @@
@if (isset($summary) && $summary[0]->total_checkins > 0) @if (isset($summary) && $summary[0]->total_checkins > 0)
<hr> <hr>
<p class="valign-wrapper"><i class="material-icons">assessment</i><b>概況</b></p> <p class="valign-wrapper"><i class="material-icons">assessment</i><b>概況</b></p>
<p>平均記録: {{ $summary[0]->average }}</p> <p>平均記録: {{ Formatter::formatInterval($summary[0]->average) }}</p>
<p>最長記録: {{ $summary[0]->longest }}</p> <p>最長記録: {{ Formatter::formatInterval($summary[0]->longest) }}</p>
<p>最短記録: {{ $summary[0]->shortest }}</p> <p>最短記録: {{ Formatter::formatInterval($summary[0]->shortest) }}</p>
<p>合計時間: {{ $summary[0]->total_times }}</p> <p>合計時間: {{ Formatter::formatInterval($summary[0]->total_times) }}</p>
<p>通算回数: {{ $summary[0]->total_checkins }}</p> <p>通算回数: {{ $summary[0]->total_checkins }}</p>
@endif @endif
</div> </div>