mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-11 18:42:54 +00:00
perf: 优化任务到期前后邮件提醒
This commit is contained in:
parent
9bc56f5d17
commit
601d037201
@ -1264,55 +1264,4 @@ class ProjectTask extends AbstractModel
|
|||||||
//
|
//
|
||||||
return $task;
|
return $task;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 预超期任务提醒
|
|
||||||
* @param $task
|
|
||||||
*/
|
|
||||||
public static function overdueRemindEmail($task)
|
|
||||||
{
|
|
||||||
$ownerIds = ProjectTaskUser::whereTaskId($task['id'])->whereOwner(1)->pluck('userid')->toArray();
|
|
||||||
$users = User::whereIn('userid', $ownerIds)->get();
|
|
||||||
if (!$users) {
|
|
||||||
throw new ApiException("ProjectTask::overdueRemindEmail--没有负责人");
|
|
||||||
}
|
|
||||||
$type = $task['end_at'] < Carbon::now() ? 2 : 1;
|
|
||||||
$setting = Base::setting('emailSetting');
|
|
||||||
$hours = floatval($setting['task_remind_hours']);
|
|
||||||
$hours2 = floatval($setting['task_remind_hours2']);
|
|
||||||
$time = $type === 1 ? $hours : $hours2;
|
|
||||||
UserEmailVerification::initMailConfig();
|
|
||||||
foreach ($users as $user) {
|
|
||||||
/** @var User $user */
|
|
||||||
if (ProjectTaskMailLog::whereTaskId($task['id'])->whereUserid($user->userid)->whereType($type)->whereIsSend(1)->exists()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$email = $user->email;
|
|
||||||
$isSend = 1;
|
|
||||||
try {
|
|
||||||
$emailContent = [
|
|
||||||
'name' => $task['name'],
|
|
||||||
'time' => $time,
|
|
||||||
'type' => $type
|
|
||||||
];
|
|
||||||
Mail::send('taskOverdueRemind', $emailContent, function ($m) use ($email) {
|
|
||||||
$m->from(Config::get("mail.mailers.smtp.username"), env('APP_NAME'));
|
|
||||||
$m->to($email);
|
|
||||||
$m->subject("任务提醒");
|
|
||||||
});
|
|
||||||
} catch (\Exception $e) {
|
|
||||||
$isSend = 0;
|
|
||||||
\Log::error($email . '--邮箱发送报错:', [$e->getMessage()]);
|
|
||||||
}
|
|
||||||
$logData = [
|
|
||||||
'userid' => $user->userid,
|
|
||||||
'task_id' => $task['id'],
|
|
||||||
'email' => $email,
|
|
||||||
'type' => $type,
|
|
||||||
'is_send' => $isSend,
|
|
||||||
];
|
|
||||||
$emailLog = ProjectTaskMailLog::whereTaskId($task['id'])->whereUserid($user->userid)->whereType($type)->first();
|
|
||||||
ProjectTaskMailLog::createInstance($logData, $emailLog->id ?? null)->save();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
|||||||
* @property string|null $email 电子邮箱
|
* @property string|null $email 电子邮箱
|
||||||
* @property int|null $type 提醒类型:1第一次任务提醒,2第二次任务超期提醒
|
* @property int|null $type 提醒类型:1第一次任务提醒,2第二次任务超期提醒
|
||||||
* @property int|null $is_send 邮件发送是否成功:0否,1是
|
* @property int|null $is_send 邮件发送是否成功:0否,1是
|
||||||
|
* @property string|null $send_error 邮件发送错误详情
|
||||||
* @property \Illuminate\Support\Carbon|null $created_at
|
* @property \Illuminate\Support\Carbon|null $created_at
|
||||||
* @property \Illuminate\Support\Carbon|null $updated_at
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
||||||
* @property \Illuminate\Support\Carbon|null $deleted_at
|
* @property \Illuminate\Support\Carbon|null $deleted_at
|
||||||
@ -28,6 +29,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
|||||||
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereEmail($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereEmail($value)
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereId($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereId($value)
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereIsSend($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereIsSend($value)
|
||||||
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereSendError($value)
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereTaskId($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereTaskId($value)
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereType($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereType($value)
|
||||||
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereUpdatedAt($value)
|
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTaskMailLog whereUpdatedAt($value)
|
||||||
|
|||||||
@ -1,11 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace App\Tasks;
|
namespace App\Tasks;
|
||||||
|
|
||||||
use App\Models\ProjectTask;
|
use App\Models\ProjectTask;
|
||||||
|
use App\Models\ProjectTaskMailLog;
|
||||||
|
use App\Models\User;
|
||||||
use App\Module\Base;
|
use App\Module\Base;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Guanguans\Notify\Factory;
|
||||||
|
use Guanguans\Notify\Messages\EmailMessage;
|
||||||
|
|
||||||
@error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
|
@error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
|
||||||
|
|
||||||
@ -22,35 +25,92 @@ class OverdueRemindEmailTask extends AbstractTask
|
|||||||
if ($setting['notice'] === 'open') {
|
if ($setting['notice'] === 'open') {
|
||||||
$hours = floatval($setting['task_remind_hours']);
|
$hours = floatval($setting['task_remind_hours']);
|
||||||
$hours2 = floatval($setting['task_remind_hours2']);
|
$hours2 = floatval($setting['task_remind_hours2']);
|
||||||
$taskLists1 = [];
|
|
||||||
$taskLists2 = [];
|
|
||||||
if ($hours > 0) {
|
if ($hours > 0) {
|
||||||
$taskLists1 = ProjectTask::whereNull('complete_at')
|
ProjectTask::whereNull('complete_at')
|
||||||
->where('end_at', '>=', Carbon::now()->addMinutes($hours * 60 - 3)->rawFormat('Y-m-d H:i:s'))
|
|
||||||
->where('end_at', '<=', Carbon::now()->addMinutes($hours * 60 + 3)->rawFormat('Y-m-d H:i:s'))
|
|
||||||
->whereNull('archived_at')
|
->whereNull('archived_at')
|
||||||
->take(100)
|
->whereBetween("end_at", [
|
||||||
->get()
|
Carbon::now()->addMinutes($hours * 60 - 3),
|
||||||
->toArray();
|
Carbon::now()->addMinutes($hours * 60 + 3)
|
||||||
|
])->chunkById(100, function ($tasks) {
|
||||||
|
/** @var ProjectTask $task */
|
||||||
|
foreach ($tasks as $task) {
|
||||||
|
$this->overdueBeforeAfterEmail($task, true);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if ($hours2 > 0) {
|
if ($hours2 > 0) {
|
||||||
$taskLists2 = ProjectTask::whereNull('complete_at')
|
ProjectTask::whereNull('complete_at')
|
||||||
->where('end_at', '>=', Carbon::now()->subMinutes($hours2 * 60 + 3)->rawFormat('Y-m-d H:i:s'))
|
|
||||||
->where('end_at', '<=', Carbon::now()->subMinutes($hours2 * 60 - 3)->rawFormat('Y-m-d H:i:s'))
|
|
||||||
->whereNull('archived_at')
|
->whereNull('archived_at')
|
||||||
->take(100)
|
->whereBetween("end_at", [
|
||||||
->get()
|
Carbon::now()->addMinutes($hours2 * 60 + 3),
|
||||||
->toArray();
|
Carbon::now()->addMinutes($hours2 * 60 - 3)
|
||||||
}
|
])->chunkById(100, function ($tasks) {
|
||||||
$taskLists = array_merge($taskLists1, $taskLists2);
|
/** @var ProjectTask $task */
|
||||||
$ids = [];
|
foreach ($tasks as $task) {
|
||||||
foreach ($taskLists as $task) {
|
$this->overdueBeforeAfterEmail($task, false);
|
||||||
if (!in_array($task->id, $ids)) {
|
}
|
||||||
$ids[] = $task->id;
|
});
|
||||||
ProjectTask::overdueRemindEmail($task);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 过期前、超期后提醒
|
||||||
|
* @param ProjectTask $task
|
||||||
|
* @param $isBefore
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function overdueBeforeAfterEmail(ProjectTask $task, $isBefore)
|
||||||
|
{
|
||||||
|
$userids = $task->taskUser->where('owner', 1)->pluck('userid')->toArray();
|
||||||
|
if (empty($userids)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$users = User::whereIn('userid', $userids)->get();
|
||||||
|
if (empty($users)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$setting = Base::setting('emailSetting');
|
||||||
|
$hours = floatval($setting['task_remind_hours']);
|
||||||
|
$hours2 = floatval($setting['task_remind_hours2']);
|
||||||
|
if ($isBefore) {
|
||||||
|
$subject = "任务提醒";
|
||||||
|
$content = "<p>用户您好, " . env('APP_NAME') . " 任务到期提醒。</p><p>您有一个任务【{{$task->name}}】还有{{$hours}}小时即将超时,请及时处理</p>";
|
||||||
|
} else {
|
||||||
|
$subject = "任务过期提醒";
|
||||||
|
$content = "<p>用户您好, " . env('APP_NAME') . " 任务到期提醒。</p><p>您的任务【{{$task->name}}】已经超时{{$hours2}}小时,请及时处理</p>";
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var User $user */
|
||||||
|
foreach ($users as $user) {
|
||||||
|
$data = [
|
||||||
|
'type' => $isBefore ? 1 : 2,
|
||||||
|
'userid' => $user->userid,
|
||||||
|
'task_id' => $task->id,
|
||||||
|
];
|
||||||
|
$emailLog = ProjectTaskMailLog::where($data)->first();
|
||||||
|
if ($emailLog) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (!Base::isEmail($user->email)) {
|
||||||
|
throw new \Exception("User email '{$user->email}' address error");
|
||||||
|
}
|
||||||
|
Factory::mailer()
|
||||||
|
->setDsn("smtp://{$setting['account']}:{$setting['password']}@{$setting['smtp_server']}:{$setting['port']}?verify_peer=0")
|
||||||
|
->setMessage(EmailMessage::create()
|
||||||
|
->from(env('APP_NAME', 'Task') . " <{$setting['account']}>")
|
||||||
|
->to($user->email)
|
||||||
|
->subject($subject)
|
||||||
|
->html($content))
|
||||||
|
->send();
|
||||||
|
$data['is_send'] = 1;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$data['send_error'] = $e->getMessage();
|
||||||
|
}
|
||||||
|
$data['email'] = $user->email;
|
||||||
|
ProjectTaskMailLog::createInstance($data)->save();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class AddProjectTaskMailLogsSendError extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('project_task_mail_logs', function (Blueprint $table) {
|
||||||
|
if (!Schema::hasColumn('project_task_mail_logs', 'send_error')) {
|
||||||
|
$table->text('send_error')->nullable()->after('is_send')->comment('邮件发送错误详情');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('project_task_mail_logs', function (Blueprint $table) {
|
||||||
|
$table->dropColumn("send_error");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,19 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="{{ app()->getLocale() }}">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<p>用户您好, {{env('APP_NAME') }} 任务到期提醒。</p>
|
|
||||||
@if ($type == 1)
|
|
||||||
<p>
|
|
||||||
您有一个任务【{{$name}}】还有{{$time}}小时即将超时,请及时处理
|
|
||||||
</p>
|
|
||||||
@else
|
|
||||||
<p>
|
|
||||||
您的任务【{{$name}}】已经超时{{$time}}小时,请及时处理
|
|
||||||
</p>
|
|
||||||
@endif
|
|
||||||
</body>
|
|
||||||
<body>
|
|
||||||
Loading…
x
Reference in New Issue
Block a user