Skip to content

Commit

Permalink
fix docs and improve ui importation command
Browse files Browse the repository at this point in the history
  • Loading branch information
Dilee committed May 14, 2023
1 parent 4e892aa commit 4f0436c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ composer require dileedotdev/laravel-vietnamese-administrative-units
You should publish and run the migrations with:

```bash
php artisan vendor:publish --tag="laravel-vietnamese-administrative-units-migrations"
php artisan vendor:publish --tag="vietnamese-administrative-units-migrations"
php artisan migrate
```

Expand Down
52 changes: 30 additions & 22 deletions src/Commands/ImportationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,49 @@ class ImportationCommand extends Command

public function handle(): int
{
$this->info('Importing Vietnamese administrative units excel file to database...');

$filePath = $this->argument('file') ?? __DIR__.'/../../assets/vietnamese-administrative-units.csv';

$this->info("Reading file {$filePath}...");
$this->components->info('Importing Vietnamese administrative units CSV file to database using file:');
$this->components->info($filePath);

$rows = SimpleExcelReader::create($filePath)
->getRows();

$startedAt = now();
$rows->each(function (array $row) {
[
'Tỉnh Thành Phố' => $provinceName,
'Mã TP' => $provinceCode,
'Quận Huyện' => $districtName,
'Mã QH' => $districtCode,
'Phường Xã' => $wardName,
'Mã PX' => $wardCode,
] = $row;

$province = $this->getOrCreateAndTouchProvince($provinceName, $provinceCode);
$district = $this->getOrCreateAndTouchDistrict($province, $districtName, $districtCode);
$ward = $this->getOrCreateAndTouchWard($district, $wardName, $wardCode);

$this->components->task('Inserting provinces, districts, and wards to database', function () use ($rows) {
$rows->each(function (array $row) {
[
'Tỉnh Thành Phố' => $provinceName,
'Mã TP' => $provinceCode,
'Quận Huyện' => $districtName,
'Mã QH' => $districtCode,
'Phường Xã' => $wardName,
'Mã PX' => $wardCode,
] = $row;

$province = $this->getOrCreateAndTouchProvince($provinceName, $provinceCode);
$district = $this->getOrCreateAndTouchDistrict($province, $districtName, $districtCode);
$ward = $this->getOrCreateAndTouchWard($district, $wardName, $wardCode);
});
});

Province::withTrashed()->where('updated_at', '<', $startedAt)->chunk(100, function ($provinces) {
$provinces->each->delete();
$this->components->task('Soft deleting provinces that are not existed in the CSV file', function () use ($startedAt) {
Province::withTrashed()->where('updated_at', '<', $startedAt)->chunk(100, function ($provinces) {
$provinces->each->delete();
});
});

District::withTrashed()->where('updated_at', '<', $startedAt)->chunk(100, function ($districts) {
$districts->each->delete();
$this->components->task('Soft deleting districts that are not existed in the CSV file', function () use ($startedAt) {
District::withTrashed()->where('updated_at', '<', $startedAt)->chunk(100, function ($districts) {
$districts->each->delete();
});
});

Ward::withTrashed()->where('updated_at', '<', $startedAt)->chunk(100, function ($wards) {
$wards->each->delete();
$this->components->task('Soft deleting wards that are not existed in the CSV file', function () use ($startedAt) {
Ward::withTrashed()->where('updated_at', '<', $startedAt)->chunk(100, function ($wards) {
$wards->each->delete();
});
});

return self::SUCCESS;
Expand Down

0 comments on commit 4f0436c

Please sign in to comment.