Task.php
2.2 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
<?php
namespace App\Models\Task;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\Base;
use App\Services\Facades\Upload;
class Task extends Base
{
//设置关联表名
protected $table = 'gl_task';
protected $appends = ['project_id_text', 'status_text', 'timeout'];
const STATUS_WAIT = 0;
const STATUS_DONGING = 1;
const STATUS_DOWN = 2;
public static function statusMap(){
return [
self::STATUS_WAIT => '未开始',
self::STATUS_DONGING => '进程中',
self::STATUS_DOWN => '已完成',
];
}
public function getProjectIdTextAttribute(){
if($this->project_id === null){
return '';
}
$project = ProjectLogic::instance()->getCacheInfo($this->project_id);
return $project['company'] ?? '';
}
public function getStatusTextAttribute(){
if($this->status === null){
return '';
}
return self::statusMap()[$this->status] ?? '';
}
public function setAttachmentAttribute($value){
$this->attributes['attachment'] = Upload::url2path($value);
}
public function getAttachmentAttribute($value){
return Upload::path2url($value);
}
public function getTimeoutAttribute(){
if($this->end_at === null){
return 0;
}
if($this->status != self::STATUS_DOWN && strtotime($this->end_at) < time()){
return floor((time() - strtotime($this->end_at)) / 60);
}
return 0;
}
public function owner(){
return $this->hasMany(TaskOwner::class,'task_id','id');
}
public function follow(){
return $this->hasMany(TaskFollow::class,'task_id','id');
}
public static function getNumByProjectId($project_id, $status = ''){
return self::where('project_id', $project_id)
->where(function ($query) use($status){
if($status){
if(is_array($status)){
$query->whereIn('status', $status);
}else{
$query->where('status', $status);
}
}
})->count();
}
}