Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
b24integrationstattable.php
1<?php
3
8
25class B24integrationStatTable extends Main\Entity\DataManager
26{
27 public static function getTableName()
28 {
29 return 'b_sale_b24integration_stat';
30 }
31
35 public static function getMap()
36 {
37 return [
38 new Main\Entity\IntegerField('ID', ['primary' => true, 'autocomplete' => true]),
39 new Main\Entity\EnumField('ENTITY_TYPE_ID', [
40 'required' => true,
41 'values' => [
43 ]]),
44 new Main\Entity\IntegerField('ENTITY_ID', ['required' => true]),
45 new Main\Entity\DatetimeField('DATE_UPDATE'),
46 new Main\Entity\DatetimeField('TIMESTAMP_X', ['default_value' => new Main\Type\DateTime()]),
47 new Main\Entity\IntegerField('PROVIDER_ID', ['required' => true]),
48 new Main\Entity\StringField("CURRENCY", ['required' => true]),
49 new Main\Entity\EnumField("STATUS", [
50 'required' => true,
51 'values' => [
55 ]]),
56 new Main\Entity\StringField("XML_ID", ['required' => true]),
57 new Main\Entity\FloatField("AMOUNT", ['required' => true]),
58 ];
59 }
60
61 protected static function upsertPrepareParams(array $data)
62 {
63 $entityTypeID = isset($data['ENTITY_TYPE_ID']) ? (int)$data['ENTITY_TYPE_ID'] : \CCrmOwnerType::Undefined;
64 $entityID = isset($data['ENTITY_ID']) ? (int)$data['ENTITY_ID'] : 0;
65 $providerID = isset($data['PROVIDER_ID']) ? (int)$data['PROVIDER_ID'] : 0;
66 $dateUpdate = isset($data['DATE_UPDATE']) ? $data['DATE_UPDATE']: null;
67 $currency = isset($data['CURRENCY']) ? $data['CURRENCY'] : null;
68 $status = isset($data['STATUS']) ? $data['STATUS'] : null;
69 $xmlId = isset($data['XML_ID']) ? $data['XML_ID'] : null;
70 $amount = isset($data['AMOUNT']) ? (double)$data['AMOUNT'] : 0.0;
71
72 $now = Main\Type\DateTime::createFromTimestamp(time() + \CTimeZone::GetOffset());
73
74 $fields = [
75 'ENTITY_TYPE_ID' => $entityTypeID,
76 'ENTITY_ID' => $entityID,
77 'DATE_UPDATE' => $dateUpdate,
78 'TIMESTAMP_X' => $now,
79 'PROVIDER_ID' => $providerID,
80 'CURRENCY' => $currency,
81 'STATUS' => $status,
82 'XML_ID' => $xmlId,
83 'AMOUNT' => $amount,
84 ];
85
86 return $fields;
87 }
88
96 public static function upsert(array $data)
97 {
98 $result = new Main\Entity\AddResult();
99 $connection = Main\Application::getConnection();
100
101 static::checkFields($result, null, $data);
102 if($result->isSuccess() == false)
103 {
104 return $result;
105 }
106
107 $updateFields = $insertFields = static::upsertPrepareParams($data);
108
109 $queries = $connection->getSqlHelper()->prepareMerge(
110 static::getTableName(),
111 [
112 'ENTITY_TYPE_ID',
113 'ENTITY_ID'
114 ],
115 $insertFields,
116 $updateFields
117 );
118
119 foreach($queries as $query)
120 {
121 $connection->queryExecute($query);
122 }
123
124 $result->setId(
125 $connection->getInsertedId());
126
127 return $result;
128 }
129
138 public static function modify(array $items)
139 {
140 $connection = Main\Application::getConnection();
141 $sqlHelper = $connection->getSqlHelper();
142
143 $r = static::checkModifyFields($items);
144 if($r->isSuccess() == false)
145 {
146 return $r;
147 }
148
149 $names = [
150 'ENTITY_TYPE_ID',
151 'ENTITY_ID',
152 'DATE_UPDATE',
153 'TIMESTAMP_X',
154 'PROVIDER_ID',
155 'CURRENCY',
156 'STATUS',
157 'XML_ID',
158 'AMOUNT'
159 ];
160
161 foreach ($names as $name)
162 {
163 $duplicate[] = $name.' = VALUES('.$name.')';
164 }
165
166 foreach ($items as $item)
167 {
168 $fields = static::upsertPrepareParams($item);
169
170 $valuesData = [
171 $fields['ENTITY_TYPE_ID'],
172 $fields['ENTITY_ID'],
173 $sqlHelper->convertToDbDateTime($fields['DATE_UPDATE']),
174 $sqlHelper->convertToDbDateTime($fields['TIMESTAMP_X']),
175 $fields['PROVIDER_ID'],
176 '\''.$sqlHelper->forSql($fields['CURRENCY']).'\'',
177 '\''.$sqlHelper->forSql($fields['STATUS']).'\'',
178 '\''.$sqlHelper->forSql($fields['XML_ID']).'\'',
179 '\''.$fields['AMOUNT'].'\''
180 ];
181 $values[] = '('.implode(',', $valuesData).')';
182 }
183
184 $query = '
185 INSERT INTO '.static::getTableName().'
186 ('.implode(', ', $names).')
187 VALUES '.implode(',', $values).'
188 ON DUPLICATE KEY UPDATE
189 '.implode(', ', $duplicate).'
190 ';
191
192 Application::getConnection()->query($query);
193
194 return $r;
195 }
196
203 static protected function checkModifyFields(array $list)
204 {
205 $result = new AddResult();
206 $error = [];
207
208 foreach ($list as $k=>$fields)
209 {
210 $r = new AddResult();
211
212 static::checkFields($r, null, $fields);
213 if($r->isSuccess() == false)
214 {
215 $error[] = '['.$k.'] '.implode(', ', $r->getErrorMessages()).'.';
216 }
217 }
218
219 if(count($error)>0)
220 {
221 $result->addError(new Main\Error(implode(', ', $error)));
222 }
223
224 return $result;
225 }
226}
static getConnection($name="")