ImportHelper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace addons\RfDevTool\common\helpers;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use yii\web\NotFoundHttpException;
  6. use common\enums\AppEnum;
  7. use common\models\common\Provinces;
  8. use common\models\rbac\AuthItem;
  9. use common\enums\WhetherEnum;
  10. /**
  11. * Class ImportHelper
  12. * @package common\helpers
  13. * @author jianyan74 <751393839@qq.com>
  14. */
  15. class ImportHelper
  16. {
  17. public static $data = '';
  18. /**
  19. * 行政区划分同步辅助
  20. *
  21. * @throws \yii\db\Exception
  22. * 数据基于:
  23. * https://github.com/modood/Administrative-divisions-of-China
  24. */
  25. public static function areas()
  26. {
  27. $data = Json::decode(static::$data);
  28. $insertData = [];
  29. $command = Yii::$app->db->createCommand();
  30. foreach ($data as $datum) {
  31. $provinceId = (string)$datum['code'] . "0000";
  32. $insertData[] = [$provinceId, $datum['name'], 0, 1, '0-'];
  33. foreach ($datum['children'] as $item) {
  34. $cityId = (string)$item['code'] . "00";
  35. $insertData[] = [$cityId, $item['name'], $provinceId, 2, '0-' . $provinceId . '-'];
  36. foreach ($item['children'] as $value) {
  37. $insertData[] = [
  38. $value['code'],
  39. $value['name'],
  40. $cityId,
  41. 3,
  42. '0-' . $provinceId . '-' . $cityId . '-'
  43. ];
  44. }
  45. }
  46. }
  47. $command->batchInsert(Provinces::tableName(), ['id', 'title', 'pid', 'level', 'tree'], $insertData)->execute();
  48. p(Json::decode(static::$data));
  49. die();
  50. }
  51. /**
  52. * @param $data
  53. * @param $app_id
  54. * @return bool
  55. * @throws NotFoundHttpException
  56. */
  57. public static function auth($data, $app_id)
  58. {
  59. if (!in_array($app_id, AppEnum::getKeys())) {
  60. throw new NotFoundHttpException('找不到应用id');
  61. }
  62. ini_set('max_execution_time', '0');
  63. AuthItem::deleteAll(['app_id' => $app_id]);
  64. $allData = [];
  65. $sortArr = [];
  66. foreach ($data as $datum) {
  67. if (!empty($datum[0])) {
  68. !isset($sortArr[$datum[2]]['id']) && $sortArr[$datum[2]]['id'] = 0;
  69. if ($datum[2] == "#") {
  70. $pid = 0;
  71. $level = 1;
  72. $tree = '0-';
  73. } else {
  74. $pid = $allData[$datum[2]]['id'];
  75. $level = $allData[$datum[2]]['level'] + 1;
  76. $tree = $allData[$datum[2]]['tree'] . $allData[$datum[2]]['id'] . '-';
  77. }
  78. $tmp = [
  79. 'title' => $datum[0],
  80. 'name' => $datum[1],
  81. 'app_id' => $app_id,
  82. 'is_addon' => WhetherEnum::DISABLED,
  83. 'pid' => $pid,
  84. 'sort' => $sortArr[$datum[2]]['id'],
  85. 'level' => $level,
  86. 'tree' => $tree,
  87. 'created_at' => time(),
  88. 'updated_at' => time(),
  89. ];
  90. $model = new AuthItem();
  91. $model->attributes = $tmp;
  92. if (!$model->save()) {
  93. p($tmp);
  94. p($model->getErrors());
  95. die();
  96. }
  97. $tmp['id'] = $model->id;
  98. $allData[$datum[1]] = $tmp;
  99. $sortArr[$datum[2]]['id']++;
  100. }
  101. }
  102. return true;
  103. }
  104. }
粤ICP备19079148号