init
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Setup\Patch\Data;
|
||||
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Setup\ModuleDataSetupInterface;
|
||||
use Magento\Framework\Setup\Patch\DataPatchInterface;
|
||||
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
|
||||
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
|
||||
use Magento\Directory\Model\ResourceModel\Region as RegionResourceModel;
|
||||
use \Magento\Framework\Filesystem\Driver\File;
|
||||
use Magento\Framework\Module\Dir as ModuleDir;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class UpdateDirectoryRegion implements DataPatchInterface, PatchRevertableInterface
|
||||
{
|
||||
protected RegionCollectionFactory $regionCollection;
|
||||
protected RegionResourceModel $regionResourceModel;
|
||||
protected File $file;
|
||||
protected ModuleDir $moduleDir;
|
||||
protected ModuleDataSetupInterface $moduleDataSetup;
|
||||
protected ResourceConnection $resourceConnection;
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
public function __construct(
|
||||
RegionCollectionFactory $regionCollection,
|
||||
RegionResourceModel $regionResourceModel,
|
||||
File $file,
|
||||
ModuleDir $moduleDir,
|
||||
ModuleDataSetupInterface $moduleDataSetup,
|
||||
ResourceConnection $resourceConnection,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->regionCollection = $regionCollection;
|
||||
$this->regionResourceModel = $regionResourceModel;
|
||||
$this->file = $file;
|
||||
$this->moduleDir = $moduleDir;
|
||||
$this->moduleDataSetup = $moduleDataSetup;
|
||||
$this->resourceConnection = $resourceConnection;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RegionResourceModel\Collection
|
||||
*/
|
||||
protected function getChineseRegion() {
|
||||
return $this->regionCollection->create()
|
||||
->addFieldToFilter('country_id', 'CN')
|
||||
->addFieldToSelect("*")
|
||||
->load();
|
||||
}
|
||||
|
||||
protected function getDataPath() {
|
||||
return $this->moduleDir->getDir('Stemcell_ChineseDirectory').'/data/pcas-code.json';
|
||||
}
|
||||
|
||||
public static function getDependencies()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function getAliases()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function apply()
|
||||
{
|
||||
$data = $this->file->fileGetContents($this->getDataPath());
|
||||
$data = json_decode($data, true);
|
||||
$regions = $this->getChineseRegion();
|
||||
$connection = $this->moduleDataSetup->getConnection();
|
||||
foreach ($data as $region) {
|
||||
$cities = $region['children'];
|
||||
/* @var $directoryRegion \Magento\Directory\Model\Region */
|
||||
$directoryRegion = $regions->addFieldToFilter('region_id', $region['region_id'])->getFirstItem();
|
||||
|
||||
if ($directoryRegion->getId()) {
|
||||
$connection->update(
|
||||
$this->resourceConnection->getTableName('directory_country_region'),
|
||||
['code' => $region['code'], 'default_name' => $region['name']],
|
||||
'region_id='.$region['region_id']
|
||||
);
|
||||
$bind = ['locale' => 'zh_Hans_CN', 'region_id' => $region['region_id'], 'name' => $region['name']];
|
||||
$connection->insert($this->resourceConnection->getTableName('directory_country_region_name'), $bind);
|
||||
foreach ($cities as $city) {
|
||||
$bind = ['code' => intval($city['code']), 'parent_code' => intval($region['code']), 'name' => $city['name']];
|
||||
$connection->insert($this->resourceConnection->getTableName('directory_country_city'), $bind);
|
||||
$districts = $city['children'];
|
||||
foreach ($districts as $district) {
|
||||
$towns = $district['children'];
|
||||
$bind = ['code' => intval($district['code']), 'parent_code' => intval($city['code']), 'name' => $district['name']];
|
||||
$connection->insert($this->resourceConnection->getTableName('directory_country_district'), $bind);
|
||||
foreach ($towns as $town) {
|
||||
$bind = ['code' => intval($town['code']), 'parent_code' => intval($district['code']), 'name' => $town['name']];
|
||||
$connection->insert($this->resourceConnection->getTableName('directory_country_town'), $bind);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$temp = $region;
|
||||
unset($temp['children']);
|
||||
$this->logger->warning($region['name'].' 在directory_country_region中没有对应的数据', $temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
$connection = $this->moduleDataSetup->getConnection();
|
||||
$connection->delete($this->resourceConnection->getTableName('directory_country_region_name'), 'locale=zh_Hans_CN');
|
||||
$connection->dropTable($this->resourceConnection->getTableName('directory_country_city'));
|
||||
$connection->dropTable($this->resourceConnection->getTableName('directory_country_district'));
|
||||
$connection->dropTable($this->resourceConnection->getTableName('directory_country_town'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user