Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
numeratorsequence.php
1<?php
3
5use Bitrix\Main\Entity\DataManager;
6use Bitrix\Main\Entity\IntegerField;
7use Bitrix\Main\Entity\StringField;
10
28class NumeratorSequenceTable extends DataManager
29{
33 public static function getTableName()
34 {
35 return 'b_numerator_sequence';
36 }
37
42 public static function getMap()
43 {
44 return [
45 (new IntegerField('NUMERATOR_ID'))
46 ->configureRequired(true)
47 ->configurePrimary(true)
48 ,
49 (new StringField('KEY'))
50 ->configureRequired(true)
51 ->configurePrimary(true)
52 ,
53 (new StringField('TEXT_KEY'))
54 ->configureRequired(true)
55 ,
56 (new IntegerField('NEXT_NUMBER')),
57 (new IntegerField('LAST_INVOCATION_TIME'))
58 ->configureRequired(true)
59 ,
60 ];
61 }
62
71 public static function updateSettings($numeratorId, $numberHash, $fields, $whereNextNumber = null)
72 {
73 if ($whereNextNumber)
74 {
76 $helper = $conn->getSqlHelper();
77 $update = $helper->prepareUpdate(static::getTableName(), $fields);
78 $query = 'UPDATE ' . $helper->quote(static::getTableName())
79 . ' SET ' . $update[0]
80 . ' WHERE NUMERATOR_ID = ' . intval($numeratorId)
81 . ' AND ' . $helper->quote('KEY') . ' = ' . $helper->convertToDbString(md5($numberHash))
82 . ' AND NEXT_NUMBER = ' . intval($whereNextNumber);
83 $conn->query($query);
84
85 return $conn->getAffectedRowsCount();
86 }
87 $res = static::update([
88 'NUMERATOR_ID' => intval($numeratorId),
89 'KEY' => md5($numberHash),
90 ], $fields);
91 if ($res->isSuccess())
92 {
93 return $res->getAffectedRowsCount();
94 }
95
96 return 0;
97 }
98
103 public static function deleteByNumeratorId($id)
104 {
105 $result = new Result();
106
107 $list = static::getList([
108 'filter' => [
109 '=NUMERATOR_ID' => (int)$id,
110 ]
111 ]);
112 while($row = $list->fetchObject())
113 {
114 $deleteResult = $row->delete();
115 if (!$deleteResult->isSuccess())
116 {
117 $result->addErrors($deleteResult->getErrors());
118 }
119 }
120
121 return $result;
122 }
123
132 public static function getSettings($numeratorId, $numberHash)
133 {
134 $sequenceSettings = static::getList([
135 'select' => ['NEXT_NUMBER', 'LAST_INVOCATION_TIME'],
136 'filter' => ['=NUMERATOR_ID' => $numeratorId, '=KEY' => md5($numberHash),],
137 ])->fetch();
138 return $sequenceSettings;
139 }
140
149 public static function setSettings($numeratorId, $numberHash, $defaultNumber, $lastInvocationTime)
150 {
151 try
152 {
153 $result = static::add([
154 'NUMERATOR_ID' => $numeratorId,
155 'KEY' => md5($numberHash),
156 'TEXT_KEY' => mb_substr($numberHash, 0, 50),
157 'LAST_INVOCATION_TIME' => $lastInvocationTime,
158 'NEXT_NUMBER' => $defaultNumber,
159 ]);
160 if ($result->isSuccess())
161 {
162 return $result->getData();
163 }
164 return [];
165 }
166 catch (SqlQueryException $exc)
167 {
168 if (mb_stripos($exc->getMessage(), "Duplicate entry") !== false)
169 {
170 return [];
171 }
172 throw $exc;
173 }
174 }
175}
static getConnection($name="")
static setSettings($numeratorId, $numberHash, $defaultNumber, $lastInvocationTime)
static updateSettings($numeratorId, $numberHash, $fields, $whereNextNumber=null)