-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathOrderUuidManager.php
204 lines (187 loc) · 6.09 KB
/
OrderUuidManager.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
<?php
/**
* Copyright 2023 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
*/
declare(strict_types=1);
namespace Magento\SalesOrdersDataExporter\Model;
use Generator;
use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface;
use Magento\DataExporter\Uuid\UuidManager;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Query\BatchIteratorFactory;
class OrderUuidManager
{
/**
* @var array[]
*/
private $mapTypes = [
'order' => [
'table' => 'sales_order',
'id_column' => 'entity_id',
'link_column' => 'entity_id'
],
'order_item' => [
'table' => 'sales_order_item',
'id_column' => 'item_id',
'link_column' => 'order_id'
],
'credit_memo' => [
'table' => 'sales_creditmemo',
'id_column' => 'entity_id',
'link_column' => 'order_id'
],
'order_shipment' => [
'table' => 'sales_shipment',
'id_column' => 'entity_id',
'link_column' => 'order_id'
]
];
/**
* @var UuidManager
*/
private $uuidManager;
/**
* @var ResourceConnection
*/
private $resourceConnection;
/**
* @var BatchIteratorFactory
*/
private $batchIteratorFactory;
/**
* @var CommerceDataExportLoggerInterface
*/
private $logger;
/**
* @param ResourceConnection $resourceConnection
* @param UuidManager $uuidManager
* @param BatchIteratorFactory $batchIteratorFactory
* @param CommerceDataExportLoggerInterface $logger
*/
public function __construct(
ResourceConnection $resourceConnection,
UuidManager $uuidManager,
BatchIteratorFactory $batchIteratorFactory,
CommerceDataExportLoggerInterface $logger
) {
$this->uuidManager = $uuidManager;
$this->resourceConnection = $resourceConnection;
$this->batchIteratorFactory = $batchIteratorFactory;
$this->logger = $logger;
}
/**
* Assign uuids to order entities for the given date range
*
* @param int $batchSize
* @param string|null $from
* @param string|null $to
* @param string $state
* @return int
*/
public function assignByDate(
int $batchSize,
?string $from = null,
?string $to = null,
string $state = ''
): int {
$updatedEntities = 0;
foreach ($this->getOrders($batchSize, $from, $to, $state) as $type => $entityIds) {
$this->uuidManager->assignBulk($entityIds, $type);
$updatedEntities += count($entityIds);
}
return $updatedEntities;
}
/**
* Assign uuids to order entities for the given order ids
*
* @param int $batchSize
* @param array $orderIds
* @return int
*/
public function assignByOrderIds(int $batchSize, array $orderIds): int
{
$updatedEntities = 0;
foreach ($this->getOrders($batchSize, orderIds: $orderIds) as $type => $entityIds) {
$this->uuidManager->assignBulk($entityIds, $type);
$updatedEntities += count($entityIds);
}
return $updatedEntities;
}
/**
* Retrieve affected entity types and its ids
*
* @param int $batchSize
* @param string|null $from
* @param string|null $to
* @param string $state
* @param array $orderIds
* @return Generator
*/
private function getOrders(
int $batchSize,
?string $from = null,
?string $to = null,
string $state = '',
array $orderIds = []
): Generator {
$mapTypes = array_map(function ($type) {
$type['table'] = $this->resourceConnection->getTableName($type['table']);
return $type;
}, $this->mapTypes);
$connection = $this->resourceConnection->getConnection();
$uuidTableName = $this->resourceConnection->getTableName('data_exporter_uuid');
$orderTableName = $mapTypes['order']['table'];
foreach ($mapTypes as $type => $data) {
$select = $connection->select()
->from(
['order' => $orderTableName],
[]
)
->joinInner(
['child' => $data['table']],
"order.entity_id = child.{$data['link_column']}",
"child.{$data['id_column']} AS entity_id"
)
->joinLeft(
['uuid' => $uuidTableName],
"child.{$data['id_column']} = uuid.entity_id and uuid.type = '{$type}'",
[]
)
->where('uuid.uuid IS NULL');
if (!empty($state)) {
$select->where('order.state = ?', $state);
}
if (!empty($from)) {
$select->where('order.created_at >= ?', $from);
}
if (!empty($to)) {
$select->where('order.created_at <= ?', $to);
}
if (!empty($orderIds)) {
$select->where('order.entity_id IN (?)', $orderIds);
}
$iterator = $this->batchIteratorFactory->create(
[
'select' => $select,
'batchSize' => $batchSize,
'correlationName' => 'child',
'rangeField' => $data['id_column'],
'rangeFieldAlias' => 'entity_id'
]
);
foreach ($iterator as $batchSelect) {
yield $type => $connection->fetchCol($batchSelect);
}
}
}
}