1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
message.php
См. документацию.
1<?php
2namespace Bitrix\MessageService\Internal\Entity;
3
4use Bitrix\Main\Application;
5use Bitrix\Main\ORM\Data\DataManager;
6use Bitrix\Main\ORM\Fields\ArrayField;
7use Bitrix\Main\ORM\Fields\DatetimeField;
8use Bitrix\Main\ORM\Fields\EnumField;
9use Bitrix\Main\ORM\Fields\ExpressionField;
10use Bitrix\Main\ORM\Fields\IntegerField;
11use Bitrix\Main\ORM\Fields\StringField;
12use Bitrix\Main\ORM\Fields\TextField;
13use Bitrix\Main\ORM\Fields\Validators\LengthValidator;
14use Bitrix\Main\Type\DateTime;
15use Bitrix\MessageService\Internal\Entity\Message\SuccessExec;
16
34{
38 public static function getTableName()
39 {
40 return 'b_messageservice_message';
41 }
42
46 public static function getMap()
47 {
48 return [
49 'ID' =>
50 (new IntegerField('ID', []))
51 ->configurePrimary(true)
52 ->configureAutocomplete(true)
53 ,
54 'TYPE' =>
55 (new StringField('TYPE', [
56 'validation' => [__CLASS__, 'validateType']
57 ]))
58 ->configureRequired(true)
59 ,
60 'SENDER_ID' =>
61 (new StringField('SENDER_ID', [
62 'validation' => [__CLASS__, 'validateSenderId']
63 ]))
64 ->configureRequired(true)
65 ,
66 'AUTHOR_ID' => (new IntegerField('AUTHOR_ID',
67 []
68 ))
69 ->configureDefaultValue(0),
70 'MESSAGE_FROM' =>
71 (new StringField('MESSAGE_FROM', [
72 'validation' => [__CLASS__, 'validateMessageFrom']
73 ]))
74 ,
75 'MESSAGE_TO' =>
76 (new StringField('MESSAGE_TO', [
77 'validation' => [__CLASS__, 'validateMessageTo']
78 ]))
79 ->configureRequired(true)
80 ,
81 'MESSAGE_HEADERS' =>
82 (new ArrayField('MESSAGE_HEADERS', []))
83 ->configureSerializationPhp()
84 ,
85 'MESSAGE_BODY' =>
86 (new TextField('MESSAGE_BODY', []))
87 ->configureRequired(true)
88 ,
89 'DATE_INSERT' =>
90 (new DatetimeField('DATE_INSERT'))
91 ->configureDefaultValue(static fn() => new DateTime())
92 ,
93 'DATE_EXEC' =>
94 (new DatetimeField('DATE_EXEC', []))
95 ,
96 'NEXT_EXEC' =>
97 (new DatetimeField('NEXT_EXEC', []))
98 ,
99 'SUCCESS_EXEC' =>
100 (new EnumField('SUCCESS_EXEC', []))
101 ->configureValues([
106 ])
107 ->configureDefaultValue(SuccessExec::NO)
108 ,
109 'EXEC_ERROR' =>
110 (new StringField('EXEC_ERROR', [
111 'validation' => [__CLASS__, 'validateExecError']
112 ]))
113 ,
114 'STATUS_ID' =>
115 (new IntegerField('STATUS_ID', []))
116 ->configureDefaultValue(0)
117 ,
118 'EXTERNAL_ID' =>
119 (new StringField('EXTERNAL_ID', [
120 'validation' => [__CLASS__, 'validateExternalId']
121 ]))
122 ,
123 'EXTERNAL_STATUS' =>
124 (new StringField('EXTERNAL_STATUS', [
125 'validation' => [__CLASS__, 'validateExternalStatus']
126 ]))
127 ,
128 'CLUSTER_GROUP' =>
129 (new IntegerField('CLUSTER_GROUP', []))
130 ,
131 ];
132 }
133
134 public static function getByExternalId(string $senderId, string $externalId, ?string $from = null)
135 {
136 return MessageTable::getList([
137 'filter' => [
138 '=SENDER_ID' => $senderId,
139 '=EXTERNAL_ID' => $externalId,
140 ],
141 'limit' => 1
142 ]);
143 }
144
152 public static function updateStatusId(int $id, int $newStatusId): bool
153 {
154 $connection = Application::getConnection();
155 $tableName = static::getTableName();
156
157 $connection->query("
158 UPDATE
159 {$tableName}
160 SET
161 STATUS_ID = {$newStatusId}
162 WHERE
163 ID = $id
164 AND STATUS_ID != {$newStatusId}
165 ");
166
167 return $connection->getAffectedRowsCount() === 1;
168 }
169
170 public static function updateMessageStatuses($id, $newInternalStatusId, $newExternalStatus): bool
171 {
172 $connection = Application::getConnection();
173 $tableName = static::getTableName();
174
175 $newExternalStatus = $connection->getSqlHelper()->forSql($newExternalStatus);
176
177 $connection->query("
178 UPDATE
179 {$tableName}
180 SET
181 STATUS_ID = {$newInternalStatusId},
182 EXTERNAL_STATUS = '{$newExternalStatus}'
183 WHERE
184 ID = {$id}
185 AND STATUS_ID < {$newInternalStatusId}
186 ");
187
188 return $connection->getAffectedRowsCount() === 1;
189 }
190
191 public static function getDailyCount($senderId, $fromId): int
192 {
193 $today = (new DateTime)->setTime(0, 0, 0);
194
195 return self::getCount([
196 '=SUCCESS_EXEC' => 'Y',
197 '>=DATE_EXEC' => $today,
198 '=SENDER_ID' => $senderId,
199 '=MESSAGE_FROM' => $fromId,
200 ]);
201 }
202
203 public static function getAllDailyCount(): array
204 {
205 $today = (new DateTime)->setTime(0, 0, 0);
206
208 'runtime' => [new ExpressionField('CNT', 'COUNT(*)')],
209 'select' => [
210 'SENDER_ID', 'MESSAGE_FROM', 'CNT'
211 ],
212 'filter' => [
213 '=SUCCESS_EXEC' => 'Y',
214 '>=DATE_EXEC' => $today,
215 ],
216 'group' => ['SENDER_ID', 'MESSAGE_FROM'],
217 ]);
218
219 $counts = [];
220 while ($row = $result->fetch())
221 {
222 $id = $row['SENDER_ID'] .':'. $row['MESSAGE_FROM'];
223 $counts[$id] = (int)$row['CNT'];
224 }
225
226 return $counts;
227 }
228
229 public static function returnDeferredToQueue($senderId, $fromId): bool
230 {
231 $connection = Application::getConnection();
232 $helper = $connection->getSqlHelper();
233
234 $senderId = $helper->forSql((string)$senderId);
235 $fromId = $helper->forSql((string)$fromId);
236
237 $connection->queryExecute("
238 UPDATE b_messageservice_message
239 SET NEXT_EXEC = NULL
240 WHERE
241 SUCCESS_EXEC = 'N'
242 AND NEXT_EXEC IS NOT NULL
243 AND SENDER_ID = '{$senderId}'
244 AND MESSAGE_FROM = '{$fromId}'
245 ");
246
247 return true;
248 }
249
255 public static function validateType(): array
256 {
257 return [
258 new LengthValidator(null, 30),
259 ];
260 }
261
267 public static function validateSenderId(): array
268 {
269 return [
270 new LengthValidator(null, 50),
271 ];
272 }
273
279 public static function validateMessageFrom(): array
280 {
281 return [
282 new LengthValidator(null, 260),
283 ];
284 }
285
291 public static function validateMessageTo(): array
292 {
293 return [
294 new LengthValidator(null, 50),
295 ];
296 }
297
303 public static function validateExecError(): array
304 {
305 return [
306 new LengthValidator(null, 255),
307 ];
308 }
309
315 public static function validateExternalId(): array
316 {
317 return [
318 new LengthValidator(null, 128),
319 ];
320 }
321
327 public static function validateExternalStatus(): array
328 {
329 return [
330 new LengthValidator(null, 128),
331 ];
332 }
333
334 public static function onAfterAdd(\Bitrix\Main\ORM\Event $event)
335 {
337 }
338
339 public static function onAfterUpdate(\Bitrix\Main\ORM\Event $event)
340 {
342 }
343
344 public static function onAfterDelete(\Bitrix\Main\ORM\Event $event)
345 {
347 }
348
349 protected static function clearQueueCache(): void
350 {
351 $cache = \Bitrix\Main\Data\Cache::createInstance();
352 $cache->clean(\Bitrix\MessageService\Queue::CACHE_HAS_MESSAGES_ID, \Bitrix\MessageService\Queue::CACHE_HAS_MESSAGES_DIR);
353 }
354}
$connection
Определения actionsdefinitions.php:38
Определения event.php:5
static getList(array $parameters=array())
Определения datamanager.php:431
static getCount($filter=array(), array $cache=array())
Определения datamanager.php:516
static getByExternalId(string $senderId, string $externalId, ?string $from=null)
Определения message.php:134
static onAfterAdd(\Bitrix\Main\ORM\Event $event)
Определения message.php:334
static returnDeferredToQueue($senderId, $fromId)
Определения message.php:229
static onAfterDelete(\Bitrix\Main\ORM\Event $event)
Определения message.php:344
static updateStatusId(int $id, int $newStatusId)
Определения message.php:152
static getDailyCount($senderId, $fromId)
Определения message.php:191
static updateMessageStatuses($id, $newInternalStatusId, $newExternalStatus)
Определения message.php:170
static onAfterUpdate(\Bitrix\Main\ORM\Event $event)
Определения message.php:339
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$event
Определения prolog_after.php:141