RegionService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace addons\WechatVideoShop\common\service;
  3. use addons\WechatVideoShop\common\models\WechatVideoShopRegion;
  4. use common\models\common\Region;
  5. class RegionService
  6. {
  7. public static function fix()
  8. {
  9. $regions = WechatVideoShopRegion::lists([
  10. 'where' => [
  11. ['local_id' => 0],
  12. ['<', 'level', 4],
  13. ],
  14. 'order' => 'id asc'
  15. ], false);
  16. foreach ($regions as $region) {
  17. if (!empty($region['level_one_name'])) {
  18. $l_province = Region::getOne([
  19. ['name' => $region['level_one_name']],
  20. ['level' => 1]
  21. ]);
  22. if (!empty($l_province)) {
  23. $region->local_one_id = $l_province->id;
  24. $region->local_one_name = $l_province->name;
  25. // $l_city = Region::getOne([
  26. // ['name' => $region['level_two_name']],
  27. // ['level' => 2],
  28. // ['parent_id' => $l_province->id],
  29. // ]);
  30. // $city_ids = Region::find()->where(['parent_id' => $l_province->id])->select('id')->column();
  31. $citys = Region::lists([
  32. 'where' => [
  33. ['parent_id' => $l_province->id]
  34. ]
  35. ]);
  36. $city_ids = array_column($citys, 'id');
  37. $city_names = array_column($citys, 'name', 'id');
  38. $name = mb_substr($region->name, 0, -1);
  39. if (!empty($name)) {
  40. $l_district = Region::getOne([
  41. ['like', 'name', $name],
  42. ['level' => 3],
  43. ['parent_id' => $city_ids],
  44. ]);
  45. if (!empty($l_district)) {
  46. // var_dump("{$region->name} - {$name} - {$l_district->name}");
  47. $region->local_id = $l_district->id;
  48. $region->local_two_name = $city_names[$l_district->parent_id]; // 取出名字
  49. $region->local_two_id = $l_district->parent_id;
  50. }
  51. // if (!empty($region['level_two_name'])) {
  52. // $l_city = Region::getOne([
  53. // ['name' => $region['level_two_name']],
  54. // ['level' => 2],
  55. // ['parent_id' => $l_province->id],
  56. // ]);
  57. //
  58. // if (!empty($l_city)) {
  59. // $region->local_two_id = $l_city->id;
  60. // $region->local_two_name = $l_city->name;
  61. // }
  62. //
  63. // }
  64. if (!$region->save()) {
  65. var_dump($region->getErrorMessage());
  66. }
  67. }
  68. } else {
  69. var_dump("$region->id 找不到所属省份");
  70. }
  71. }
  72. }
  73. }
  74. public static function newArea()
  75. {
  76. $regions = WechatVideoShopRegion::lists([
  77. 'where' => [
  78. ['local_id' => 0],
  79. ['>', 'local_two_id', 0],
  80. ['<', 'level', 4],
  81. // ['like', 'name', '聚集区']
  82. ],
  83. 'order' => 'id asc'
  84. ], false);
  85. $t = \Yii::$app->db->beginTransaction();
  86. try {
  87. foreach ($regions as $region) {
  88. $lRegion = new Region();
  89. $lRegion->parent_id = $region->local_two_id;
  90. $lRegion->name = $region->name;
  91. $lRegion->id = $region->id;
  92. $lRegion->level = $region->level;
  93. if (!$lRegion->save()) {
  94. throw new \Exception($lRegion->getErrorMessage());
  95. }
  96. $region->local_id = $lRegion->id;
  97. $region->local_name = $region->name;
  98. if (!$region->save()) throw new \Exception($region->getErrorMessage());
  99. }
  100. $t->commit();
  101. } catch (\Exception $e) {
  102. $t->rollBack();
  103. var_dump($e->getMessage(), $e->getLine(), $e->getFile());
  104. }
  105. }
  106. }