diff --git a/app/Models/Domain.php b/app/Models/Domain.php index b3b10ad..e2f572c 100644 --- a/app/Models/Domain.php +++ b/app/Models/Domain.php @@ -2,6 +2,7 @@ namespace App\Models; +use AgliPanci\LaravelCase\Facades\CaseBuilder; use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -113,20 +114,22 @@ class Domain extends Model ->values(); } - /** - * Get the latest message in each echomail area - * - * @return Collection - */ - public function latest_echomail_message(): Collection + public function echoarea_stats(): Collection { + $dt = Carbon::now()->startOfday(); + $case = CaseBuilder::whenRaw("datetime >= '?'",$dt->subDay()->format('Y-m-d'))->thenRaw("'day'") + ->whenRaw("datetime >= '?'",$dt->subDays(7)->format('Y-m-d'))->thenRaw("'week'") + ->whenRaw("datetime >= '?'",$dt->subMonth()->format('Y-m-d'))->thenRaw("'month'") + ->elseRaw("'all'"); + return Echoarea::cacheFor(self::CACHE_TIME) - ->select([ - 'echoareas.*',DB::raw('max(datetime) as last_message') - ]) - ->leftJoin('echomails',['echomails.echoarea_id'=>'echoareas.id']) + ->select(['echoareas.id','name','description','active',DB::raw('count(echoareas.id) AS count'),DB::raw('max(datetime) as last_message')]) + ->selectRaw($case->toRaw().' AS stats') + ->join('echomails',['echomails.echoarea_id'=>'echoareas.id']) ->where('domain_id',$this->id) ->groupBy('echoareas.id') + ->groupBy('echoareas.name') + ->groupBy('stats') ->orderBy('echoareas.name') ->get(); } diff --git a/app/Models/Echoarea.php b/app/Models/Echoarea.php index 840f387..1b9a73e 100644 --- a/app/Models/Echoarea.php +++ b/app/Models/Echoarea.php @@ -70,7 +70,7 @@ class Echoarea extends Model /* METHODS */ - public function messages_count(int $period): int + public function messages_count(int $period=NULL): int { $eo = Echomail::cacheFor(self::CACHE_TIME) ->where('echoarea_id',$this->id); @@ -87,6 +87,10 @@ class Echoarea extends Model case 30: // month $eo->where('datetime','>=',$dt->subMonth()); break; + + case NULL: // all + break; + default: return 0; } diff --git a/resources/views/domain/view.blade.php b/resources/views/domain/view.blade.php index 4d71175..dd60039 100644 --- a/resources/views/domain/view.blade.php +++ b/resources/views/domain/view.blade.php @@ -34,7 +34,7 @@ - Messages + Messages Echoarea @@ -44,19 +44,21 @@ Day Week Month + Total - @foreach ($o->latest_echomail_message() as $oo) + @foreach ($o->echoarea_stats()->groupBy('id') as $oo) - {{ $oo->name }} - {{ $oo->description }} - {{ $oo->last_message ? $oo->last_message->format('Y-m-d H:i') : '-' }} - {{ $oo->active ? 'Active' : 'Archive' }} - {{ number_format($oo->messages_count(1)) }} - {{ number_format($oo->messages_count(7)) }} - {{ number_format($oo->messages_count(30)) }} + {{ $x->name }} + {{ $x->description }} + {{ $x->last_message ? $x->last_message->format('Y-m-d H:i') : '-' }} + {{ $x->active ? 'Active' : 'Archive' }} + {{ number_format($oo->where('stats','day')->pop()?->count) }} + {{ number_format($oo->where('stats','week')->pop()?->count) }} + {{ number_format($oo->where('stats','month')->pop()?->count) }} + {{ number_format($oo->where('stats','all')->pop()?->count) }} @endforeach