1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace execut\import\models\queries; |
4
|
|
|
|
5
|
|
|
use execut\import\models; |
6
|
|
|
use yii\base\Exception; |
7
|
|
|
use yii\db\ActiveQuery; |
8
|
|
|
use yii\db\Expression; |
9
|
|
|
use yii\db\mysql\Schema; |
10
|
|
|
use yii\web\UploadedFile; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This is the model class for table "import_files". |
14
|
|
|
* |
15
|
|
|
* @property integer $id |
16
|
|
|
* @property string $created |
17
|
|
|
* @property string $updated |
18
|
|
|
* @property string $name |
19
|
|
|
* @property resource $content |
20
|
|
|
* @property string $md5 |
21
|
|
|
* @property string $import_files_source_id |
22
|
|
|
* @property string $use_id |
23
|
|
|
* @property UploadedFile $contentFile |
24
|
|
|
* |
25
|
|
|
* @property \execut\import\models\FilesSource $importFilesSource |
26
|
|
|
* @property \execut\import\models\User $use |
27
|
|
|
*/ |
28
|
|
|
class File extends ActiveQuery |
29
|
|
|
{ |
30
|
|
|
public function isNew() { |
31
|
|
|
return $this->byImportFilesStatuseId(models\FilesStatuse::find()->isNew()->select('id')); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function isDeleting() { |
35
|
|
|
return $this->byImportFilesStatuseId(models\FilesStatuse::find()->isDeleting()->select('id')); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function isDelete() { |
39
|
|
|
return $this->byImportFilesStatuseId(models\FilesStatuse::find()->isDelete()->select('id')); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function isLoading() { |
43
|
|
|
return $this->byImportFilesStatuseId(models\FilesStatuse::find()->isLoading()->select('id')); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function isReloading() { |
47
|
|
|
return $this->byImportFilesStatuseId(models\FilesStatuse::find()->isReloading()->select('id')); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function isForClean() { |
51
|
|
|
$keys = [models\FilesStatuse::DELETE]; |
52
|
|
|
|
53
|
|
|
return $this->byImportFilesStatuseId(models\FilesStatuse::find()->byKey($keys)->select('id')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function isForImport() { |
57
|
|
|
$keys = [models\FilesStatuse::RELOAD, models\FilesStatuse::NEW]; |
58
|
|
|
|
59
|
|
|
return $this->byImportFilesStatuseId(models\FilesStatuse::find()->byKey($keys)->select('id')); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function isNotLoadedOrStop() |
63
|
|
|
{ |
64
|
|
|
return $this->andWhere([ |
65
|
|
|
'AND', |
66
|
|
|
[ |
67
|
|
|
'NOT IN', |
68
|
|
|
'import_setting_id', |
69
|
|
|
models\File::find()->isLoading()->select('import_setting_id'), |
70
|
|
|
], |
71
|
|
|
[ |
72
|
|
|
'NOT IN', |
73
|
|
|
'import_setting_id', |
74
|
|
|
models\File::find()->isStop()->select('import_setting_id'), |
75
|
|
|
// 'AND', |
76
|
|
|
// [ |
77
|
|
|
// 'NOT IN', |
78
|
|
|
// 'import_setting_id', |
79
|
|
|
// models\File::find()->isDelete()->select('import_setting_id'), |
80
|
|
|
// ], |
81
|
|
|
// [ |
82
|
|
|
// 'NOT IN', |
83
|
|
|
// 'import_setting_id', |
84
|
|
|
// models\File::find()->isDeleting()->select('import_setting_id'), |
85
|
|
|
// ], |
86
|
|
|
], |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function byMd5($md5) { |
91
|
|
|
return $this->andWhere([ |
92
|
|
|
'md5' => $md5, |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function isStop() { |
97
|
|
|
return $this->byImportFilesStatuse_Key([models\FilesStatuse::STOP]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function isStoped() { |
101
|
|
|
return $this->byImportFilesStatuse_Key([models\FilesStatuse::STOPED]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function isCancelLoading() { |
105
|
|
|
return $this->byImportFilesStatuseId(models\FilesStatuse::find()->isNotLoading()->select('id')); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function byImportFilesStatuse_Key($key) { |
109
|
|
|
return $this->andWhere([ |
110
|
|
|
'import_files_statuse_id' => models\FilesStatuse::find()->byKey($key)->select('id'), |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function byImportFilesStatuseId($id) { |
115
|
|
|
return $this->andWhere([ |
116
|
|
|
'import_files_statuse_id' => $id, |
117
|
|
|
]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function withErrorsPercent() { |
121
|
|
|
if ($this->select === null) { |
122
|
|
|
$this->select = ['*']; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$typeCast = $this->getTypeCastFunction(); |
126
|
|
|
$this->select['errorsPercent'] = new Expression('CASE WHEN (rows_success is not null OR rows_errors is not null) AND (rows_success + rows_errors) > 0 THEN rows_errors' . $typeCast . ' / (rows_success + rows_errors) ELSE 0 END'); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function withProgress() { |
132
|
|
|
if ($this->select === null) { |
133
|
|
|
$this->select = ['*']; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$typeCast = $this->getTypeCastFunction(); |
137
|
|
|
$this->select['progress'] = new Expression('CASE WHEN rows_count > 0 THEN (rows_success + rows_errors)' . $typeCast . ' / rows_count ELSE 0 END'); |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function isOnlyFresh() { |
143
|
|
|
$modelClass = $this->modelClass; |
144
|
|
|
if ($modelClass::getDb()->schema instanceof Schema) { |
145
|
|
|
$subQuery = 'SELECT id FROM ' . $this->getPrimaryTableName() . ' GROUP BY import_setting_id ORDER BY import_setting_id, created DESC'; |
146
|
|
|
} else { |
147
|
|
|
$subQuery = 'SELECT DISTINCT ON (import_setting_id) id FROM ' . $this->getPrimaryTableName() . ' ORDER BY import_setting_id, created DESC'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this->andWhere($this->getPrimaryTableName() . '.id IN (' . $subQuery . ')'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function byEventsCount($count) { |
154
|
|
|
if ($count === '1') { |
155
|
|
|
$this->andWhere('(SELECT count(*) FROM import_settings_vs_scheduler_events WHERE import_settings_vs_scheduler_events.import_setting_id=import_files.import_setting_id) > 0'); |
156
|
|
|
} else if ($count === '0') { |
157
|
|
|
$this->andWhere('(SELECT count(*) FROM import_settings_vs_scheduler_events WHERE import_settings_vs_scheduler_events.import_setting_id=import_files.import_setting_id) = 0'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function withEventsCount() { |
164
|
|
|
$vsSql = '(SELECT count(*) FROM import_settings_vs_scheduler_events WHERE import_settings_vs_scheduler_events.import_setting_id=import_files.import_setting_id)'; |
165
|
|
|
$this->select['eventsCount'] = $vsSql; |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function byImportSettingId($id) { |
171
|
|
|
return $this->andWhere([ |
172
|
|
|
'import_setting_id' => $id, |
173
|
|
|
]); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function byId($id) { |
177
|
|
|
return $this->andWhere([ |
178
|
|
|
'id' => $id, |
179
|
|
|
]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function byHostName($name) { |
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
protected function getPhpProcessesIds() { |
187
|
|
|
exec('pidof php', $output); |
188
|
|
|
if (empty($output[0])) { |
189
|
|
|
return []; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return explode(' ', $output[0]); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function isWithoutProcess() { |
196
|
|
|
$processesIds = $this->getPhpProcessesIds(); |
197
|
|
|
return $this->andWhere([ |
198
|
|
|
'NOT IN', |
199
|
|
|
'process_id', |
200
|
|
|
$processesIds |
201
|
|
|
]); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function isInProgress() { |
205
|
|
|
return $this->andWhere([ |
206
|
|
|
'import_files_statuse_id' => models\FilesStatuse::find()->byKey([ |
207
|
|
|
models\FilesStatuse::DELETING, |
208
|
|
|
models\FilesStatuse::LOADING, |
209
|
|
|
models\FilesStatuse::STOP, |
210
|
|
|
])->select('id') |
211
|
|
|
]); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
protected function getTypeCastFunction() |
218
|
|
|
{ |
219
|
|
|
$modelClass = $this->modelClass; |
220
|
|
|
if ($modelClass::getDb()->schema instanceof Schema) { |
221
|
|
|
$typeCast = ''; |
222
|
|
|
} else { |
223
|
|
|
$typeCast = '::float'; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $typeCast; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|