Skip to content

Commit 608047e

Browse files
authored
MDEE-394: implement assign uuids by order ids. (#236)
* MDEE-394: implement assign uuids by order ids.
1 parent b8813fc commit 608047e

2 files changed

Lines changed: 207 additions & 108 deletions

File tree

SalesOrdersDataExporter/Console/Command/Link.php

Lines changed: 12 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,44 @@
88
namespace Magento\SalesOrdersDataExporter\Console\Command;
99

1010
use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface;
11-
use Magento\DataExporter\Uuid\UuidManager;
12-
use Magento\Framework\App\ResourceConnection;
1311
use Magento\Framework\Console\Cli;
14-
use Magento\Framework\DB\Query\BatchIteratorFactory;
12+
use Magento\SalesOrdersDataExporter\Model\OrderUuidManager;
13+
use Symfony\Component\Console\Command\Command;
1514
use Symfony\Component\Console\Input\InputInterface;
1615
use Symfony\Component\Console\Input\InputOption;
1716
use Symfony\Component\Console\Output\OutputInterface;
1817

1918
/**
2019
* Command provides possibility to full export system configuration
2120
*/
22-
class Link extends \Symfony\Component\Console\Command\Command
21+
class Link extends Command
2322
{
2423
private const COMMAND_NAME = 'commerce-data-export:orders:link';
2524
private const OPTION_STATE = 'state';
2625
private const OPTION_FROM = 'from';
2726
private const OPTION_TO = 'to';
2827
private const OPTION_BATCH_SIZE = 'batch-size';
2928

30-
private $uuidManager;
31-
private $resourceConnection;
32-
private $batchIteratorFactory;
33-
34-
private $mapTypes = [
35-
'order' => [
36-
'table' => 'sales_order',
37-
'id_column' => 'entity_id',
38-
'link_column' => 'entity_id'
39-
],
40-
'order_item' => [
41-
'table' => 'sales_order_item',
42-
'id_column' => 'item_id',
43-
'link_column' => 'order_id'
44-
],
45-
'credit_memo' => [
46-
'table' => 'sales_creditmemo',
47-
'id_column' => 'entity_id',
48-
'link_column' => 'order_id'
49-
],
50-
'order_shipment' => [
51-
'table' => 'sales_shipment',
52-
'id_column' => 'entity_id',
53-
'link_column' => 'order_id'
54-
]
55-
];
29+
/**
30+
* @var OrderUuidManager
31+
*/
32+
private $orderUuidManager;
5633

5734
/**
5835
* @var CommerceDataExportLoggerInterface
5936
*/
6037
private $logger;
6138

6239
/**
63-
* @param ResourceConnection $resourceConnection
64-
* @param UuidManager $uuidManager
65-
* @param BatchIteratorFactory $batchIteratorFactory
40+
* @param OrderUuidManager $orderUuidManager
41+
* @param CommerceDataExportLoggerInterface $logger
6642
*/
6743
public function __construct(
68-
ResourceConnection $resourceConnection,
69-
UuidManager $uuidManager,
70-
BatchIteratorFactory $batchIteratorFactory,
44+
OrderUuidManager $orderUuidManager,
7145
CommerceDataExportLoggerInterface $logger
7246
) {
73-
$this->uuidManager = $uuidManager;
74-
$this->resourceConnection = $resourceConnection;
75-
$this->batchIteratorFactory = $batchIteratorFactory;
7647
$this->logger = $logger;
48+
$this->orderUuidManager = $orderUuidManager;
7749
parent::__construct();
7850
}
7951

@@ -175,74 +147,6 @@ public function assignUuidsToOrderEntities(
175147
string $to = null,
176148
string $state = ''
177149
): int {
178-
$updatedEntities = 0;
179-
foreach ($this->getOrders($state, $batchSize, $from, $to) as $type => $entityIds) {
180-
$this->uuidManager->assignBulk($entityIds, $type);
181-
$updatedEntities += count($entityIds);
182-
}
183-
return $updatedEntities;
184-
}
185-
186-
/**
187-
* @param string $state
188-
* @param int $batchSize
189-
* @param string|null $from
190-
* @param string|null $to
191-
* @return \Generator
192-
*/
193-
private function getOrders(string $state, int $batchSize, string $from = null, string $to = null): \Generator
194-
{
195-
196-
$mapTypes = array_map(function ($type) {
197-
$type['table'] = $this->resourceConnection->getTableName($type['table']);
198-
return $type;
199-
}, $this->mapTypes);
200-
201-
$connection = $this->resourceConnection->getConnection();
202-
$uuidTableName = $this->resourceConnection->getTableName('data_exporter_uuid');
203-
$orderTableName = $mapTypes['order']['table'];
204-
205-
foreach ($mapTypes as $type => $data) {
206-
$select = $connection->select()
207-
->from(
208-
['order' => $orderTableName],
209-
[]
210-
)
211-
->joinInner(
212-
['child' => $data['table']],
213-
"order.entity_id = child.{$data['link_column']}",
214-
"child.{$data['id_column']} AS entity_id"
215-
)
216-
->joinLeft(
217-
['uuid' => $uuidTableName],
218-
"child.{$data['id_column']} = uuid.entity_id and uuid.type = '{$type}'",
219-
[]
220-
)
221-
->where('uuid.uuid IS NULL');
222-
223-
if (!empty($state)) {
224-
$select->where('order.state = ?', $state);
225-
}
226-
if (!empty($from)) {
227-
$select->where('order.created_at >= ?', $from);
228-
}
229-
if (!empty($to)) {
230-
$select->where('order.created_at <= ?', $to);
231-
}
232-
233-
$iterator = $this->batchIteratorFactory->create(
234-
[
235-
'select' => $select,
236-
'batchSize' => $batchSize,
237-
'correlationName' => 'child',
238-
'rangeField' => $data['id_column'],
239-
'rangeFieldAlias' => 'entity_id'
240-
]
241-
);
242-
243-
foreach ($iterator as $batchSelect) {
244-
yield $type => $connection->fetchCol($batchSelect);
245-
}
246-
}
150+
return $this->orderUuidManager->assignByDate($batchSize, $from, $to, $state);
247151
}
248152
}
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\SalesOrdersDataExporter\Model;
10+
11+
use Generator;
12+
use Magento\DataExporter\Model\Logging\CommerceDataExportLoggerInterface;
13+
use Magento\DataExporter\Uuid\UuidManager;
14+
use Magento\Framework\App\ResourceConnection;
15+
use Magento\Framework\DB\Query\BatchIteratorFactory;
16+
17+
class OrderUuidManager
18+
{
19+
/**
20+
* @var array[]
21+
*/
22+
private $mapTypes = [
23+
'order' => [
24+
'table' => 'sales_order',
25+
'id_column' => 'entity_id',
26+
'link_column' => 'entity_id'
27+
],
28+
'order_item' => [
29+
'table' => 'sales_order_item',
30+
'id_column' => 'item_id',
31+
'link_column' => 'order_id'
32+
],
33+
'credit_memo' => [
34+
'table' => 'sales_creditmemo',
35+
'id_column' => 'entity_id',
36+
'link_column' => 'order_id'
37+
],
38+
'order_shipment' => [
39+
'table' => 'sales_shipment',
40+
'id_column' => 'entity_id',
41+
'link_column' => 'order_id'
42+
]
43+
];
44+
45+
/**
46+
* @var UuidManager
47+
*/
48+
private $uuidManager;
49+
50+
/**
51+
* @var ResourceConnection
52+
*/
53+
private $resourceConnection;
54+
55+
/**
56+
* @var BatchIteratorFactory
57+
*/
58+
private $batchIteratorFactory;
59+
60+
/**
61+
* @var CommerceDataExportLoggerInterface
62+
*/
63+
private $logger;
64+
65+
/**
66+
* @param ResourceConnection $resourceConnection
67+
* @param UuidManager $uuidManager
68+
* @param BatchIteratorFactory $batchIteratorFactory
69+
* @param CommerceDataExportLoggerInterface $logger
70+
*/
71+
public function __construct(
72+
ResourceConnection $resourceConnection,
73+
UuidManager $uuidManager,
74+
BatchIteratorFactory $batchIteratorFactory,
75+
CommerceDataExportLoggerInterface $logger
76+
) {
77+
$this->uuidManager = $uuidManager;
78+
$this->resourceConnection = $resourceConnection;
79+
$this->batchIteratorFactory = $batchIteratorFactory;
80+
$this->logger = $logger;
81+
}
82+
83+
/**
84+
* Assign uuids to order entities for the given date range
85+
*
86+
* @param int $batchSize
87+
* @param string|null $from
88+
* @param string|null $to
89+
* @param string $state
90+
* @return int
91+
*/
92+
public function assignByDate(
93+
int $batchSize,
94+
string $from = null,
95+
string $to = null,
96+
string $state = ''
97+
): int {
98+
$updatedEntities = 0;
99+
foreach ($this->getOrders($batchSize, $from, $to, $state) as $type => $entityIds) {
100+
$this->uuidManager->assignBulk($entityIds, $type);
101+
$updatedEntities += count($entityIds);
102+
}
103+
return $updatedEntities;
104+
}
105+
106+
/**
107+
* Assign uuids to order entities for the given order ids
108+
*
109+
* @param int $batchSize
110+
* @param array $orderIds
111+
* @return int
112+
*/
113+
public function assignByOrderIds(int $batchSize, array $orderIds): int
114+
{
115+
$updatedEntities = 0;
116+
foreach ($this->getOrders($batchSize, orderIds: $orderIds) as $type => $entityIds) {
117+
$this->uuidManager->assignBulk($entityIds, $type);
118+
$updatedEntities += count($entityIds);
119+
}
120+
return $updatedEntities;
121+
}
122+
123+
/**
124+
* Retrieve affected entity types and its ids
125+
*
126+
* @param int $batchSize
127+
* @param string|null $from
128+
* @param string|null $to
129+
* @param string $state
130+
* @param array $orderIds
131+
* @return Generator
132+
*/
133+
private function getOrders(
134+
int $batchSize,
135+
string $from = null,
136+
string $to = null,
137+
string $state = '',
138+
array $orderIds = []
139+
): Generator {
140+
$mapTypes = array_map(function ($type) {
141+
$type['table'] = $this->resourceConnection->getTableName($type['table']);
142+
return $type;
143+
}, $this->mapTypes);
144+
145+
$connection = $this->resourceConnection->getConnection();
146+
$uuidTableName = $this->resourceConnection->getTableName('data_exporter_uuid');
147+
$orderTableName = $mapTypes['order']['table'];
148+
149+
foreach ($mapTypes as $type => $data) {
150+
$select = $connection->select()
151+
->from(
152+
['order' => $orderTableName],
153+
[]
154+
)
155+
->joinInner(
156+
['child' => $data['table']],
157+
"order.entity_id = child.{$data['link_column']}",
158+
"child.{$data['id_column']} AS entity_id"
159+
)
160+
->joinLeft(
161+
['uuid' => $uuidTableName],
162+
"child.{$data['id_column']} = uuid.entity_id and uuid.type = '{$type}'",
163+
[]
164+
)
165+
->where('uuid.uuid IS NULL');
166+
167+
if (!empty($state)) {
168+
$select->where('order.state = ?', $state);
169+
}
170+
if (!empty($from)) {
171+
$select->where('order.created_at >= ?', $from);
172+
}
173+
if (!empty($to)) {
174+
$select->where('order.created_at <= ?', $to);
175+
}
176+
if (!empty($orderIds)) {
177+
$select->where('order.entity_id IN (?)', $orderIds);
178+
}
179+
180+
$iterator = $this->batchIteratorFactory->create(
181+
[
182+
'select' => $select,
183+
'batchSize' => $batchSize,
184+
'correlationName' => 'child',
185+
'rangeField' => $data['id_column'],
186+
'rangeFieldAlias' => 'entity_id'
187+
]
188+
);
189+
190+
foreach ($iterator as $batchSelect) {
191+
yield $type => $connection->fetchCol($batchSelect);
192+
}
193+
}
194+
}
195+
}

0 commit comments

Comments
 (0)