-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModMinsk115Cli.php
404 lines (311 loc) · 14.1 KB
/
ModMinsk115Cli.php
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
use \Core2\Mod\Minsk115;
require_once DOC_ROOT . 'core2/inc/classes/Common.php';
require_once 'classes/autoload.php';
/**
* @property \ModMinsk115Controller $modMinsk115
*/
class ModMinsk115Cli extends Common {
/**
* Синхронизация заявок
* @return void
* @throws Zend_Config_Exception
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function syncRequests() {
$config = $this->getModuleConfig('minsk115');
if ( ! $config?->app?->login ||
! $config?->app?->pass ||
! $config?->app?->token
) {
return;
}
$cabinet115 = new OpenDataWorld\Cabinet115(
$config->app->login,
$config->app->pass,
$config->app->token,
);
try {
$orders = $cabinet115->getOrders();
} catch (\Exception $e) {
if (strpos($e->getMessage(), 'SSL_ERROR_SYSCALL') === false) {
throw $e;
} else {
$orders = [];
}
}
$statuses = [
'Новая заявка' => 'new',
'В работе' => 'in_process',
'На контроле' => 'active',
'Заявка закрыта' => 'closed',
'Отклонено' => 'closed',
];
foreach ($orders as $order) {
if (empty($order['id_request'])) {
continue;
}
$order_row = $this->modMinsk115->dataMinsk115Orders->getRowByExtId($order['id_request']);
if ( ! $order_row) {
$order_row = $this->modMinsk115->dataMinsk115Orders->createRow([
'ext_id' => $order['id_request'],
'ext_city_id' => $order['id_city'],
'status' => $statuses[$order['status']] ?? 'new',
'nmbr' => $order['cc_id'] ?? '',
'subject' => $order['subject'] ?? '',
'user_comment' => $order['user_comment'] ?? '',
'result_text' => $order['org_comment'] ?? '',
'address' => $order['address'] ?? '',
'lat' => $order['lat'] ?? '',
'lng' => $order['lng'] ?? '',
'rating' => $order['rating'] ?? 0,
]);
} else {
$order_status = $statuses[$order['status']] ?? 'new';
$is_order_closed = $order_row->status != 'closed' && $order_status == 'closed';
$order_row->ext_city_id = $order['id_city'];
$order_row->nmbr = $order['cc_id'] ?? '';
$order_row->status = $order_status;
$order_row->subject = $order['subject'] ?? '';
$order_row->result_text = $order['org_comment'] ?? '';
$order_row->address = $order['address'] ?? '';
$order_row->lat = $order['lat'] ?? '';
$order_row->lng = $order['lng'] ?? '';
$order_row->rating = $order['rating'] ?? 0;
}
$order_row->save();
if ( ! empty($is_order_closed) && $order_row->author_id) {
$author = $this->modMinsk115->dataMinsk115Authors->find($order_row->author_id)->current();
if ( ! empty($author)) {
$title = $order_row->subject ?: $order_row->user_comment;
$comment = $this->modMinsk115->dataMinsk115OrdersComments->getRowLastByOrderId($order_row->id);
(new Minsk115\Authors\Model())
->sendMessageText($author, implode("\n", [
'Ваша заявка была закрыта',
"Описание заявки {$title}",
"Комментарий исполнителя: {$comment->comment}",
]));
}
}
$user_images = explode(':', $order['user_images'] ?? '');
foreach ($user_images as $user_image_id) {
if ($user_image_id) {
$image = $this->modMinsk115->dataMinsk115OrdersFiles->getRowsByExtId($user_image_id);
if (empty($image)) {
$image_content = $cabinet115->getImageContent($user_image_id);
$this->modMinsk115->dataMinsk115OrdersFiles->createPhotoByContent($order_row->id, $image_content, [
'ext_id' => $user_image_id,
'fieldid' => 'photo_org',
'status' => 'completed',
]);
}
}
}
$org_images = explode(':', $order['org_images'] ?? '');
foreach ($org_images as $org_image_id) {
if ($org_image_id) {
$image = $this->modMinsk115->dataMinsk115OrdersFiles->getRowsByExtId($org_image_id);
if (empty($image)) {
$image_content = $cabinet115->getImageContent($org_image_id);
$this->modMinsk115->dataMinsk115OrdersFiles->createPhotoByContent($order_row->id, $image_content, [
'ext_id' => $org_image_id,
'fieldid' => 'photo_org',
'status' => 'completed',
]);
}
}
}
}
}
/**
* Синхронизация комментариев по заявкам
* @return void
* @throws Zend_Config_Exception
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function syncRequestsComments() {
$config = $this->getModuleConfig('minsk115');
if ( ! $config?->app?->login ||
! $config?->app?->pass ||
! $config?->app?->token
) {
return;
}
$cabinet115 = new OpenDataWorld\Cabinet115(
$config->app->login,
$config->app->pass,
$config->app->token,
);
$where = [];
$where[] = "(status != 'closed' OR DATE_FORMAT(date_last_update, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d'))";
$where[] = "ext_id IS NOT NULL";
$orders = $this->modMinsk115->dataMinsk115Orders->fetchAll($where);
$statuses = [
'Заявка успешно прошла модерацию' => 'new',
'Новая заявка' => 'new',
'Заявка закрыта' => 'closed',
'В план текущего ремонта' => 'plan',
'moderate_115' => 'moderate_115',
];
foreach ($orders as $order) {
if (empty($order->ext_id)) {
continue;
}
try {
$comments = $cabinet115->getOrderComments((int)$order->ext_id);
} catch (\Exception $e) {
if (strpos($e->getMessage(), 'SSL_ERROR_SYSCALL') === false) {
throw $e;
} else {
$comments = [];
}
}
foreach ($comments as $comment) {
if ( ! isset($comment['id_ral'])) {
continue;
}
$order_history = $this->modMinsk115->dataMinsk115OrdersComments->getRowByOrderIdExtId((int)$order->id, (int)$comment['id_ral']);
if ( ! $order_history) {
$status = '';
$emergency_mark = null;
$date_event = null;
$comment_text = $comment['comments'] ?? '';
if ($comment_text) {
if ($comment['id_ral'] != -2) {
if (preg_match('~.*Заявка успешно прошла модерацию.*~', $comment_text, $math)) {
$status = 'Заявка успешно прошла модерацию';
} elseif (preg_match('~.*Статус заявки: ([^\.]*)~', $comment_text, $math)) {
$status = $math[1] ?? '';
}
} else {
$status = 'moderate_115';
}
if (preg_match('~.*Отметка аварийной службы: ([^\.]*)~', $comment_text, $math)) {
$emergency_mark = $math[1] ?? '';
}
}
if ( ! empty($comment['date_created'])) {
$date_event = date('Y-m-d H:i:s', strtotime($comment['date_created']));
}
$order_history = $this->modMinsk115->dataMinsk115OrdersComments->createRow([
'order_id' => $order->id,
'ext_id' => $comment['id_ral'],
'status' => $statuses[$status] ?? $status,
'creator' => trim($comment['creator'] ?? ''),
'emergency_mark' => $emergency_mark ?: '',
'comment' => trim($comment_text),
'date_event' => $date_event ?? null,
]);
$order_history->save();
}
}
}
}
/**
* Общение с телеграм ботом
* @throws Exception
*/
public function answerBot() {
$config = $this->getModuleConfig('minsk115');
$token = $config?->bot?->tg?->token;
$bot = new Minsk115\Common\Bot($token, [
'bot_username' => $config?->bot?->tg?->bot_username ?? '',
'download_dir' => $config?->bot?->tg?->download_dir ?: $this->config->temp,
'log_dir' => $config?->bot?->tg?->log_dir ?: dirname(realpath($this->config?->log?->path)),
]);
$seconds = 55;
$start = time();
while ($seconds >= (time() - $start)) {
try {
$messages = $bot->getMessages();
if ( ! empty($messages)) {
foreach ($messages as $chat_id => $chat) {
if ( ! empty($chat['messages']) && ! empty($chat['user_id'])) {
try {
$communicate = new Minsk115\Common\Communicate($chat_id, $bot);
$communicate->processAnswer($chat['messages']);
$answer_messages = $communicate->getMessages();
} catch (\Exception $e) {
$answer_messages = [];
$answer_messages[] = new Minsk115\Common\Bot\Message('text', "Что-то пошло не так.\n {$e->getMessage()}");
$this->sendErrorMessage("Ошибка при попытке обработать телеграм сообщение пользователя", $e);
}
foreach ($answer_messages as $answer_message) {
$bot->sendMessage($chat_id, $answer_message);
}
}
}
}
// break;
} catch (\Exception $e) {
echo $e->getMessage() . "\n";
}
usleep(300000);
}
}
/**
* Напоминание о не законченных заявках (созданных вчера)
* @return void
*/
public function notifyDraftOrders(): void {
$orders = $this->db->fetchAll("
SELECT mo.id,
mo.author_id
FROM mod_minsk115_orders AS mo
WHERE mo.status = 'draft'
AND DATE_ADD(DATE_FORMAT(mo.date_created, '%Y-%m-%d'), INTERVAL 1 DAY) = DATE_FORMAT(NOW(), '%Y-%m-%d')
GROUP BY mo.author_id
");
if ( ! empty($orders)) {
foreach ($orders as $order) {
if ($order['author_id']) {
$author = $this->modMinsk115->dataMinsk115Authors->find($order['author_id'])->current();
$text = implode("\n", [
"Ваша заявка не была завершена. Для продолжения нажмите на /new"
]);
(new Minsk115\Authors\Model())
->sendMessageText($author, $text);
}
}
}
}
/**
* Обработка фотографий в заявках
* @return false
* @throws \ImagickException
* @throws Zend_Db_Adapter_Exception
* @throws Zend_Exception
* @cron_skip
*/
public function processingPhoto(): bool {
$where = [];
$where[] = "status = 'processing'";
$where[] = "fieldid = 'photo'";
$where[] = "DATE_ADD(date_last_update, INTERVAL 5 MINUTE) < NOW()";
$this->modMinsk115->dataMinsk115OrdersFiles->update([
'status' => 'pending',
], $where);
$photo_processing = $this->modMinsk115->dataMinsk115OrdersFiles->getRowByStatus('processing');
if ($photo_processing) {
return false;
}
$model = new Minsk115\Index\Model();
$count = 0;
do {
$photo = $this->modMinsk115->dataMinsk115OrdersFiles->getRowByStatus('pending');
if ( ! empty($photo)) {
$model->photoProcessing($photo->id);
$photo_processing = $this->modMinsk115->dataMinsk115OrdersFiles->getRowByStatus('processing');
if ($photo_processing && $photo_processing->id != $photo->id) {
break;
}
}
$count++;
if ($count > 100) {
break;
}
} while ( ! empty($photo));
return true;
}
}