Skip to content

Commit

Permalink
Add send maintainer works mail logic
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengjinghua committed Oct 17, 2016
1 parent 7c822d5 commit a311d18
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 2 deletions.
7 changes: 7 additions & 0 deletions app/Console/Commands/CalculateMaintainerWorks.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public function handle()
MaintainerLog::create($data);
}

if ($this->option('send-mail') == 'yes') {
\Artisan::call('phphub:send-maintainer-works-mail', [
'start_time' => $start_time,
'end_time' => $end_time
]);
}

$this->info('Done');
}
}
73 changes: 73 additions & 0 deletions app/Console/Commands/SendMaintainerWorksMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\User;
use App\Models\Role;
use App\Models\MaintainerLog;
use App\Jobs\SendMaintainerWorksMail;

class SendMaintainerWorksMail extends Command
{

protected $signature = 'phphub:send-maintainer-works-mail {start_time} {end_time}';
protected $description = 'Send maintainer works mail';

public function __construct()
{
parent::__construct();
}

public function handle()
{
$start_time = $this->argument('start_time');
$end_time = $this->argument('end_time');
$logs = MaintainerLog::where('start_time', $start_time)
->where('end_time', $end_time)
->get();
if (!$logs) {
return 'No data';
}

$content = $this->generateContent($logs);
$timeFrame = "{$start_time}{$end_time}";

$founders = User::byRolesName(Role::find(1)->name);
$maintainer = User::byRolesName(Role::find(2)->name);
$users = $founders->merge($maintainer);

foreach ($users as $user) {
dispatch(new SendMaintainerWorksMail($user, $timeFrame, $content));
}

$this->info('Done');
}

protected function generateContent($logs)
{
$content = "<table style=\"box-sizing: border-box; border-collapse: collapse; border-spacing: 0px; margin-top: 0px; margin-bottom: 16px; display: block; width: 637px; overflow: auto; word-break: keep-all; font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif; font-size: 16px;\">
<thead style=\"box-sizing: border-box;\">
<tr style=\"box-sizing: border-box; border-top-width: 1px; border-top-style: solid; border-top-color: rgb(204, 204, 204);\">
<th style=\"box-sizing: border-box; padding: 6px 13px; border: 1px solid rgb(221, 221, 221);\">用户</th>
<th style=\"box-sizing: border-box; padding: 6px 13px; border: 1px solid rgb(221, 221, 221); text-align: center;\">发帖数</th>
<th style=\"box-sizing: border-box; padding: 6px 13px; border: 1px solid rgb(221, 221, 221); text-align: right;\">回复数</th>
</tr>
</thead>
<tbody style=\"box-sizing: border-box;\">";
foreach ($logs as $index => $log) {
$bgColor = $index % 2 != 0 ? 'background-color: rgb(248, 248, 248);': '';
$content .= "<tr style=\"box-sizing: border-box; border-top-width: 1px; border-top-style: solid; border-top-color: rgb(204, 204, 204);" . $bgColor . "\">";

$tdStyle = 'box-sizing: border-box; padding: 6px 13px; border: 1px solid rgb(221, 221, 221); text-align: center;';
$content .= "<td style=\"{$tdStyle}\"><a href=\"".route('users.show', $log->user_id)."\" target=\"_blank\">{$log->user->name}</a></td>";
$content .= "<td style=\"{$tdStyle}\">{$log->topics_count}</td>";
$content .= "<td style=\"{$tdStyle}\">{$log->replies_count}</a></td>";

$content .= "</tr>";
}
$content .= '</tbody></table>';

return $content;
}
}
6 changes: 4 additions & 2 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ class Kernel extends ConsoleKernel
Commands\CalculateActiveUser::class,
Commands\CalculateHotTopic::class,
Commands\ClearUserData::class,
Commands\CalculateMaintainerWorks::class,
Commands\SyncUserActivedTime::class,

Commands\CalculateMaintainerWorks::class,
Commands\SendMaintainerWorksMail::class,
];

/**
Expand All @@ -41,7 +43,7 @@ protected function schedule(Schedule $schedule)

$schedule->command('backup:run --only-db')->cron('0 */4 * * * *');
$schedule->command('backup:clean')->daily()->at('00:10');
$schedule->command('phphub:calculate-maintainer-works')->mondays()->at('00:05');
$schedule->command('phphub:calculate-maintainer-works --send-mail=yes')->mondays()->at('00:05');

$schedule->command('phphub:calculate-active-user')->everyTenMinutes();
$schedule->command('phphub:calculate-hot-topic')->everyTenMinutes();
Expand Down
31 changes: 31 additions & 0 deletions app/Jobs/SendMaintainerWorksMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Jobs;

use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Models\User;

class SendMaintainerWorksMail extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;

protected $user;
protected $timeFrame;
protected $content;

public function __construct(User $user, $timeFrame, $content)
{
$this->user = $user;
$this->timeFrame = $timeFrame;
$this->content = $content;
}

public function handle()
{
return app('Phphub\Handler\EmailHandler')->sendMaintainerWorksMail($this->user, $this->timeFrame, $this->content);
}
}
15 changes: 15 additions & 0 deletions app/Phphub/Handler/EmailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ class EmailHandler
protected $reply;
protected $body;

public function sendMaintainerWorksMail(User $user, $timeFrame, $content)
{
Mail::send('emails.fake', [], function (Message $message) use ($user, $timeFrame, $content) {
$message->subject('管理员工作统计');

$message->getSwiftMessage()->setBody(new SendCloudTemplate('maintainer_works', [
'name' => $user->name,
'time_frame' => $timeFrame,
'content' => $content,
]));

$message->to($user->email);
});
}

public function sendActivateMail(User $user)
{
UserVerification::generate($user);
Expand Down

0 comments on commit a311d18

Please sign in to comment.