| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace addons\WechatVideoShop\common\service;
- use addons\WechatVideoShop\common\models\WechatVideoShopRegion;
- use common\models\common\Region;
- class RegionService
- {
- public static function fix()
- {
- $regions = WechatVideoShopRegion::lists([
- 'where' => [
- ['local_id' => 0],
- ['<', 'level', 4],
- ],
- 'order' => 'id asc'
- ], false);
- foreach ($regions as $region) {
- if (!empty($region['level_one_name'])) {
- $l_province = Region::getOne([
- ['name' => $region['level_one_name']],
- ['level' => 1]
- ]);
- if (!empty($l_province)) {
- $region->local_one_id = $l_province->id;
- $region->local_one_name = $l_province->name;
- // $l_city = Region::getOne([
- // ['name' => $region['level_two_name']],
- // ['level' => 2],
- // ['parent_id' => $l_province->id],
- // ]);
- // $city_ids = Region::find()->where(['parent_id' => $l_province->id])->select('id')->column();
- $citys = Region::lists([
- 'where' => [
- ['parent_id' => $l_province->id]
- ]
- ]);
- $city_ids = array_column($citys, 'id');
- $city_names = array_column($citys, 'name', 'id');
- $name = mb_substr($region->name, 0, -1);
- if (!empty($name)) {
- $l_district = Region::getOne([
- ['like', 'name', $name],
- ['level' => 3],
- ['parent_id' => $city_ids],
- ]);
- if (!empty($l_district)) {
- // var_dump("{$region->name} - {$name} - {$l_district->name}");
- $region->local_id = $l_district->id;
- $region->local_two_name = $city_names[$l_district->parent_id]; // 取出名字
- $region->local_two_id = $l_district->parent_id;
- }
- // if (!empty($region['level_two_name'])) {
- // $l_city = Region::getOne([
- // ['name' => $region['level_two_name']],
- // ['level' => 2],
- // ['parent_id' => $l_province->id],
- // ]);
- //
- // if (!empty($l_city)) {
- // $region->local_two_id = $l_city->id;
- // $region->local_two_name = $l_city->name;
- // }
- //
- // }
- if (!$region->save()) {
- var_dump($region->getErrorMessage());
- }
- }
- } else {
- var_dump("$region->id 找不到所属省份");
- }
- }
- }
- }
- public static function newArea()
- {
- $regions = WechatVideoShopRegion::lists([
- 'where' => [
- ['local_id' => 0],
- ['>', 'local_two_id', 0],
- ['<', 'level', 4],
- // ['like', 'name', '聚集区']
- ],
- 'order' => 'id asc'
- ], false);
- $t = \Yii::$app->db->beginTransaction();
- try {
- foreach ($regions as $region) {
- $lRegion = new Region();
- $lRegion->parent_id = $region->local_two_id;
- $lRegion->name = $region->name;
- $lRegion->id = $region->id;
- $lRegion->level = $region->level;
- if (!$lRegion->save()) {
- throw new \Exception($lRegion->getErrorMessage());
- }
- $region->local_id = $lRegion->id;
- $region->local_name = $region->name;
- if (!$region->save()) throw new \Exception($region->getErrorMessage());
- }
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- var_dump($e->getMessage(), $e->getLine(), $e->getFile());
- }
- }
- }
|