Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
message.php
1<?php
3
14
32{
36 public static function getTableName()
37 {
38 return 'b_messageservice_message';
39 }
40
44 public static function getMap()
45 {
46 return [
47 'ID' =>
48 (new IntegerField('ID', []))
49 ->configurePrimary(true)
50 ->configureAutocomplete(true)
51 ,
52 'TYPE' =>
53 (new StringField('TYPE', [
54 'validation' => [__CLASS__, 'validateType']
55 ]))
56 ->configureRequired(true)
57 ,
58 'SENDER_ID' =>
59 (new StringField('SENDER_ID', [
60 'validation' => [__CLASS__, 'validateSenderId']
61 ]))
62 ->configureRequired(true)
63 ,
64 'AUTHOR_ID' => (new IntegerField('AUTHOR_ID',
65 []
66 ))
67 ->configureDefaultValue(0),
68 'MESSAGE_FROM' =>
69 (new StringField('MESSAGE_FROM', [
70 'validation' => [__CLASS__, 'validateMessageFrom']
71 ]))
72 ,
73 'MESSAGE_TO' =>
74 (new StringField('MESSAGE_TO', [
75 'validation' => [__CLASS__, 'validateMessageTo']
76 ]))
77 ->configureRequired(true)
78 ,
79 'MESSAGE_HEADERS' =>
80 (new ArrayField('MESSAGE_HEADERS', []))
81 ->configureSerializationPhp()
82 ,
83 'MESSAGE_BODY' =>
84 (new TextField('MESSAGE_BODY', []))
85 ->configureRequired(true)
86 ,
87 'DATE_INSERT' =>
88 (new DatetimeField('DATE_INSERT', []))
89 ,
90 'DATE_EXEC' =>
91 (new DatetimeField('DATE_EXEC', []))
92 ,
93 'NEXT_EXEC' =>
94 (new DatetimeField('NEXT_EXEC', []))
95 ,
96 'SUCCESS_EXEC' =>
97 (new StringField('SUCCESS_EXEC', []))
98 ->configureDefaultValue('N')
99 ,
100 'EXEC_ERROR' =>
101 (new StringField('EXEC_ERROR', [
102 'validation' => [__CLASS__, 'validateExecError']
103 ]))
104 ,
105 'STATUS_ID' =>
106 (new IntegerField('STATUS_ID', []))
107 ->configureDefaultValue(0)
108 ,
109 'EXTERNAL_ID' =>
110 (new StringField('EXTERNAL_ID', [
111 'validation' => [__CLASS__, 'validateExternalId']
112 ]))
113 ,
114 'EXTERNAL_STATUS' =>
115 (new StringField('EXTERNAL_STATUS', [
116 'validation' => [__CLASS__, 'validateExternalStatus']
117 ]))
118 ,
119 'CLUSTER_GROUP' =>
120 (new IntegerField('CLUSTER_GROUP', []))
121 ,
122 ];
123 }
124
125 public static function getByExternalId(string $senderId, string $externalId, ?string $from = null)
126 {
127 return MessageTable::getList([
128 'filter' => [
129 '=SENDER_ID' => $senderId,
130 '=EXTERNAL_ID' => $externalId,
131 ],
132 'limit' => 1
133 ]);
134 }
135
143 public static function updateStatusId(int $id, int $newStatusId): bool
144 {
145 $connection = Application::getConnection();
146 $tableName = static::getTableName();
147
148 $connection->query("
149 UPDATE
150 {$tableName}
151 SET
152 STATUS_ID = {$newStatusId}
153 WHERE
154 ID = $id
155 AND STATUS_ID != {$newStatusId}
156 ");
157
158 return $connection->getAffectedRowsCount() === 1;
159 }
160
161 public static function updateMessageStatuses($id, $newInternalStatusId, $newExternalStatus): bool
162 {
163 $connection = Application::getConnection();
164 $tableName = static::getTableName();
165
166 $newExternalStatus = $connection->getSqlHelper()->forSql($newExternalStatus);
167
168 $connection->query("
169 UPDATE
170 {$tableName}
171 SET
172 STATUS_ID = {$newInternalStatusId},
173 EXTERNAL_STATUS = '{$newExternalStatus}'
174 WHERE
175 ID = {$id}
176 AND STATUS_ID < {$newInternalStatusId}
177 ");
178
179 return $connection->getAffectedRowsCount() === 1;
180 }
181
182 public static function getDailyCount($senderId, $fromId): int
183 {
184 $today = (new DateTime)->setTime(0, 0, 0);
185
186 return self::getCount([
187 '=SUCCESS_EXEC' => 'Y',
188 '>=DATE_EXEC' => $today,
189 '=SENDER_ID' => $senderId,
190 '=MESSAGE_FROM' => $fromId,
191 ]);
192 }
193
194 public static function getAllDailyCount(): array
195 {
196 $today = (new DateTime)->setTime(0, 0, 0);
197
198 $result = self::getList([
199 'runtime' => [new ExpressionField('CNT', 'COUNT(*)')],
200 'select' => [
201 'SENDER_ID', 'MESSAGE_FROM', 'CNT'
202 ],
203 'filter' => [
204 '=SUCCESS_EXEC' => 'Y',
205 '>=DATE_EXEC' => $today,
206 ],
207 'group' => ['SENDER_ID', 'MESSAGE_FROM'],
208 ]);
209
210 $counts = [];
211 while ($row = $result->fetch())
212 {
213 $id = $row['SENDER_ID'] .':'. $row['MESSAGE_FROM'];
214 $counts[$id] = (int)$row['CNT'];
215 }
216
217 return $counts;
218 }
219
220 public static function returnDeferredToQueue($senderId, $fromId): bool
221 {
222 $connection = Application::getConnection();
223 $helper = $connection->getSqlHelper();
224
225 $senderId = $helper->forSql((string)$senderId);
226 $fromId = $helper->forSql((string)$fromId);
227
228 $connection->queryExecute("
229 UPDATE b_messageservice_message
230 SET NEXT_EXEC = NULL
231 WHERE
232 SUCCESS_EXEC = 'N'
233 AND NEXT_EXEC IS NOT NULL
234 AND SENDER_ID = '{$senderId}'
235 AND MESSAGE_FROM = '{$fromId}'
236 ");
237
238 return true;
239 }
240
246 public static function validateType(): array
247 {
248 return [
249 new LengthValidator(null, 30),
250 ];
251 }
252
258 public static function validateSenderId(): array
259 {
260 return [
261 new LengthValidator(null, 50),
262 ];
263 }
264
270 public static function validateMessageFrom(): array
271 {
272 return [
273 new LengthValidator(null, 260),
274 ];
275 }
276
282 public static function validateMessageTo(): array
283 {
284 return [
285 new LengthValidator(null, 50),
286 ];
287 }
288
294 public static function validateExecError(): array
295 {
296 return [
297 new LengthValidator(null, 255),
298 ];
299 }
300
306 public static function validateExternalId(): array
307 {
308 return [
309 new LengthValidator(null, 128),
310 ];
311 }
312
318 public static function validateExternalStatus(): array
319 {
320 return [
321 new LengthValidator(null, 128),
322 ];
323 }
324}
static getConnection($name="")
static getList(array $parameters=array())
static getCount($filter=array(), array $cache=array())
static getByExternalId(string $senderId, string $externalId, ?string $from=null)
Definition message.php:125
static returnDeferredToQueue($senderId, $fromId)
Definition message.php:220
static updateStatusId(int $id, int $newStatusId)
Definition message.php:143
static updateMessageStatuses($id, $newInternalStatusId, $newExternalStatus)
Definition message.php:161