EmailSendJob.php
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
* 邮件发送任务
* @author:dc
* @time 2022/11/9 11:17
* Class EmailSendJob
* @package App\Http\Mail\Models
*/
class EmailSendJob extends Model{
const STATUS_RUNING = 1;
const STATUS_WAIT = 0;
const STATUS_SUCCESS = 2;
const STATUS_DISCARD = 3;
/**
* 新增一个job
* @param $data
* @return false|mixed
* @throws \Throwable
* @author:dc
* @time 2022/11/9 11:34
*/
public static function _insert($data){
$model = new static();
$model->email_id = $data['email_id'];// 发送邮件的id,好像要随机发送
$model->the_manage_id = $data['the_manage_id'];
$model->user_id = $data['user_id'];
if(isset($data['data']['to'])){unset($data['data']['to']);}
$model->maildata = json_encode($data['data']);
$model->title = $data['title'];
$model->total = sizeof($data['to']);
$model->send_time = $data['send_time']; // 发送时间
$model->tags = implode(',',$data['tags']??[]); // tag标签
DB::beginTransaction();
if($model->save()){
foreach ($data['to'] as $email){
$id = EmailSendJobStatu::insertGetId([
'job_id' => $model->id,
'to_email' => $email['email'],
'status' => 0,
'created_at' => date('Y-m-d H:i:s')
]);
if(!$id){
DB::rollBack();
return false;
}
}
DB::commit();
return $model->id;
}
DB::rollBack();
return false;
}
public function getStatusTextAttribute($key)
{
switch ($this->status){
case self::STATUS_WAIT:{
return '等待中';
}
case self::STATUS_RUNING:{
return '发送中';
}
case self::STATUS_SUCCESS:{
return '已完成';
}
case self::STATUS_DISCARD:{
return '废弃';
}
}
return '';
}
}