-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
512 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/.idea | ||
/vendor | ||
/node_modules | ||
composer.lock | ||
composer.phar | ||
phpunit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "kjmtrue/vietnam-zone", | ||
"description": "VietNam Zone", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "KimTrien", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"require": { | ||
"illuminate/support": "^5.8|^6|^7", | ||
"illuminate/database": "^5.8|^6|^7", | ||
"illuminate/console": "^5.8|^6|^7", | ||
"guzzlehttp/guzzle": "^6.3.1|^7.0", | ||
"maatwebsite/excel": "^3.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Kjmtrue\\VietnamZone\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Kjmtrue\\VietnamZone\\ServiceProvider" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
return [ | ||
'tables' => [ | ||
'provinces' => 'provinces', | ||
'districts' => 'districts', | ||
'wards' => 'wards', | ||
], | ||
|
||
'columns' => [ | ||
'name' => 'name', | ||
'gso_id' => 'gso_id', | ||
'province_id' => 'province_id', | ||
'district_id' => 'district_id', | ||
], | ||
]; |
33 changes: 33 additions & 0 deletions
33
database/migrations/2020_01_01_000001_create_provinces_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateProvincesTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create(config('vietnam-zone.tables.provinces'), function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string('name'); | ||
$table->string('gso_id'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists(config('vietnam-zone.tables.provinces')); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
database/migrations/2020_01_01_000002_create_districts_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateDistrictsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create(config('vietnam-zone.tables.districts'), function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string(config('vietnam-zone.columns.name')); | ||
$table->string(config('vietnam-zone.columns.gso_id')); | ||
$table->unsignedBigInteger(config('vietnam-zone.columns.province_id')); | ||
$table->timestamps(); | ||
|
||
$table->foreign(config('vietnam-zone.columns.province_id')) | ||
->references('id') | ||
->on(config('vietnam-zone.tables.provinces')) | ||
->cascadeOnDelete(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists(config('vietnam-zone.tables.districts')); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
database/migrations/2020_01_01_000003_create_wards_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateWardsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create(config('vietnam-zone.tables.wards'), function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string(config('vietnam-zone.columns.name')); | ||
$table->string(config('vietnam-zone.columns.gso_id')); | ||
$table->unsignedBigInteger(config('vietnam-zone.columns.district_id')); | ||
$table->timestamps(); | ||
|
||
$table->foreign(config('vietnam-zone.columns.district_id')) | ||
->references('id') | ||
->on(config('vietnam-zone.tables.districts')) | ||
->cascadeOnDelete(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists(config('vietnam-zone.tables.wards')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
## VietNam Zone | ||
|
||
Database đơn vị hành chính của Việt Nam | ||
|
||
Dữ liệu được lấy trực tiếp từ Tổng Cục Thống Kê Việt Nam. | ||
|
||
Đảm bảo luôn luôn là dữ liệu mới nhất và chính xác nhất. | ||
|
||
## Cài đặt | ||
|
||
```shell | ||
composer require kjmtrue/vietnam-zone | ||
``` | ||
|
||
#### Copy file config và migration | ||
|
||
```shell | ||
php artisan vendor:publish --provider="Kjmtrue\VietnamZone\ServiceProvider" | ||
``` | ||
|
||
#### Chỉnh sửa config và migration nếu bạn cần tuỳ biến cho dự án | ||
|
||
1. Đổi tên bảng | ||
|
||
Mở file `config/vietnam-zone.php` chỉnh các cấu hình sau: | ||
|
||
```php | ||
'tables' => [ | ||
'provinces' => 'provinces', | ||
'districts' => 'districts', | ||
'wards' => 'wards', | ||
], | ||
``` | ||
|
||
2. Đổi tên column | ||
|
||
Mở file `config/vietnam-zone.php` chỉnh các cấu hình sau: | ||
|
||
```php | ||
'columns' => [ | ||
'name' => 'name', | ||
'gso_id' => 'gso_id', | ||
'province_id' => 'province_id', | ||
'district_id' => 'district_id', | ||
], | ||
``` | ||
|
||
3. Thêm column | ||
|
||
Mở các file migration sau và tuỳ chỉnh theo ý thích | ||
|
||
```shell | ||
database/migrations/2020_01_01_000001_create_provinces_table.php | ||
database/migrations/2020_01_01_000002_create_districts_table.php | ||
database/migrations/2020_01_01_000003_create_wards_table.php | ||
``` | ||
|
||
#### Chạy migration | ||
|
||
```shell | ||
php artisan migrate | ||
``` | ||
|
||
#### Download và import dữ liệu vào database | ||
|
||
```shell | ||
php artisan vietnamzone:download | ||
``` | ||
|
||
#### Todo | ||
|
||
- [ ] Command cập nhật dữ liệu | ||
- [ ] Download file trực tiếp từu website tổng cục thống kê |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Kjmtrue\VietnamZone\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\File; | ||
use Kjmtrue\VietnamZone\Downloader; | ||
use Kjmtrue\VietnamZone\Imports\VienamZoneImport; | ||
use Maatwebsite\Excel\Facades\Excel; | ||
|
||
class DownloadCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'vietnamzone:download'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'VietNam Zone Download Data'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle() | ||
{ | ||
$this->info('Downloading...'); | ||
|
||
$tmpFile = app(Downloader::class)->downloadFile(); | ||
|
||
$this->info('Importing...'); | ||
|
||
Excel::import(new VienamZoneImport(), $tmpFile); | ||
|
||
File::delete($tmpFile); | ||
|
||
$this->info('Completed'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Kjmtrue\VietnamZone\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
|
||
class UpdateCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'vietnamzone:update'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'VietNam Zone Update Data'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle() | ||
{ | ||
$this->info('Updating...'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Kjmtrue\VietnamZone; | ||
|
||
use GuzzleHttp\Client; | ||
|
||
class Downloader | ||
{ | ||
const DOWNLOAD_URL = 'https://github.com/kjmtrue/vietnam-zone/raw/database/vietnam-zone.xls'; | ||
|
||
/** | ||
* Download database VietNam Zone | ||
* | ||
* @return string|null | ||
* @throws \GuzzleHttp\Exception\GuzzleException | ||
*/ | ||
public function downloadFile() | ||
{ | ||
$client = new Client([ | ||
'verify' => false | ||
]); | ||
|
||
$res = $client->get(self::DOWNLOAD_URL, [ | ||
'save_to' => storage_path('vietnam-zone.xls') | ||
]); | ||
|
||
return $res->getStatusCode() == 200 ? storage_path('vietnam-zone.xls') : null; | ||
} | ||
} |
Oops, something went wrong.