ProvinceJobController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace addons\RfDevTool\backend\controllers;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use common\models\common\Provinces;
  6. use common\enums\StatusEnum;
  7. use common\models\base\SearchModel;
  8. use common\helpers\ArrayHelper;
  9. use common\helpers\TreeHelper;
  10. use addons\RfDevTool\common\models\ProvinceJob;
  11. use addons\RfDevTool\common\queues\MapJob;
  12. use addons\RfDevTool\common\queues\ProvinceJob as QueueJob;
  13. /**
  14. * Class ProvinceController
  15. * @package addons\RfDevTool\backend\controllers
  16. * @author jianyan74 <751393839@qq.com>
  17. */
  18. class ProvinceJobController extends BaseController
  19. {
  20. const URL = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/';
  21. /**
  22. * @return string
  23. * @throws \yii\web\NotFoundHttpException
  24. */
  25. public function actionIndex()
  26. {
  27. $searchModel = new SearchModel([
  28. 'model' => ProvinceJob::class,
  29. 'scenario' => 'default',
  30. 'partialMatchAttributes' => [], // 模糊查询
  31. 'defaultOrder' => [
  32. 'id' => SORT_DESC
  33. ],
  34. 'pageSize' => $this->pageSize
  35. ]);
  36. $dataProvider = $searchModel
  37. ->search(Yii::$app->request->queryParams);
  38. $dataProvider->query
  39. ->andWhere(['>=', 'status', StatusEnum::DISABLED]);
  40. return $this->render('index', [
  41. 'searchModel' => $searchModel,
  42. 'dataProvider' => $dataProvider,
  43. 'maxLevelExplain' => ProvinceJob::$maxLevelExplain,
  44. ]);
  45. }
  46. /**
  47. * @return mixed|string
  48. * @throws \yii\db\Exception
  49. */
  50. public function actionCreate()
  51. {
  52. $model = new ProvinceJob();
  53. $model = $model->loadDefaultValues();
  54. $model->year = date('Y') - 1;
  55. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  56. $queue = new QueueJob([
  57. 'baseUrl' => self::URL . $model->year . '/',
  58. 'maxLevel' => $model->max_level,
  59. 'job_id' => $model->id,
  60. ]);
  61. $model->message_id = Yii::$app->queue->push($queue);
  62. $model->save();
  63. // 插入港澳台数据
  64. $this->other();
  65. return $this->message('加入队列成功', $this->redirect(['index']));
  66. }
  67. return $this->render('create', [
  68. 'model' => $model,
  69. 'year' => ArrayHelper::numBetween(2009, date('Y')),
  70. 'maxLevelExplain' => ProvinceJob::$maxLevelExplain,
  71. ]);
  72. }
  73. /**
  74. * 抓取经纬度和拼音开头
  75. */
  76. public function actionMap()
  77. {
  78. $all = Provinces::find()
  79. ->select(['id', 'title', 'pid'])
  80. ->with(['parent'])
  81. ->asArray()
  82. ->all();
  83. foreach ($all as $item) {
  84. $title = $item['title'];
  85. isset($item['parent']['title']) && $title = $item['parent']['title'] . $title;
  86. $messageId = Yii::$app->queue->push(new MapJob([
  87. 'id' => $item['id'],
  88. 'address' => $title,
  89. ]));
  90. }
  91. return $this->message('加入队列成功', $this->redirect(['index']));
  92. }
  93. /**
  94. * @throws \yii\db\Exception
  95. */
  96. protected function other()
  97. {
  98. $path = Yii::getAlias('@addons') . '/RfDevTool/common/file/list.json';
  99. $jsonData = Json::decode(file_get_contents($path));
  100. $data = [];
  101. $Hongkong = 810000;
  102. $Macao = 820000;
  103. $Taiwan = 830000;
  104. foreach ($jsonData as $key => $datum) {
  105. if ($key == $Hongkong || $key == $Macao || $key == $Taiwan) {
  106. $data[] = [
  107. 'id' => $key,
  108. 'title' => $datum,
  109. 'level' => 1,
  110. 'pid' => 0,
  111. 'tree' => TreeHelper::defaultTreeKey(),
  112. ];
  113. } elseif ($key > $Hongkong && $key < $Macao) {
  114. // 香港
  115. for ($i = $Hongkong; $i < 820000; $i += 100) {
  116. if ($key == $i) {
  117. $data[] = [
  118. 'id' => $key,
  119. 'title' => $datum,
  120. 'level' => 2,
  121. 'pid' => $Hongkong,
  122. 'tree' => "0-$Hongkong-",
  123. ];
  124. } elseif ($key < $i + 100 && $key > $i) {
  125. $data[] = [
  126. 'id' => $key,
  127. 'title' => $datum,
  128. 'level' => 3,
  129. 'pid' => $i,
  130. 'tree' => "0-$Hongkong-$i-",
  131. ];
  132. }
  133. }
  134. } elseif ($key > $Macao && $key < $Taiwan) {
  135. // 澳门
  136. for ($i = $Macao; $i < 830000; $i += 100) {
  137. if ($key == $i) {
  138. $data[] = [
  139. 'id' => $key,
  140. 'title' => $datum,
  141. 'level' => 2,
  142. 'pid' => $Macao,
  143. 'tree' => "0-$Macao-",
  144. ];
  145. } elseif ($key < $i + 100 && $key > $i) {
  146. $data[] = [
  147. 'id' => $key,
  148. 'title' => $datum,
  149. 'level' => 3,
  150. 'pid' => $i,
  151. 'tree' => "0-$Macao-$i-",
  152. ];
  153. }
  154. }
  155. } else {
  156. // 台湾
  157. for ($i = $Taiwan; $i < 840000; $i += 100) {
  158. if ($key == $i) {
  159. $data[] = [
  160. 'id' => $key,
  161. 'title' => $datum,
  162. 'level' => 2,
  163. 'pid' => $Taiwan,
  164. 'tree' => "0-$Taiwan-",
  165. ];
  166. } elseif ($key < $i + 100 && $key > $i) {
  167. $data[] = [
  168. 'id' => $key,
  169. 'title' => $datum,
  170. 'level' => 3,
  171. 'pid' => $i,
  172. 'tree' => "0-$Taiwan-$i-",
  173. ];
  174. }
  175. }
  176. }
  177. }
  178. $command = Yii::$app->db->createCommand();
  179. $command->batchInsert(Provinces::tableName(), ['id', 'title', 'level', 'pid', 'tree'], $data)->execute();
  180. }
  181. }
粤ICP备19079148号