ArticleCommand.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/4/23
  6. * Time: 19:17
  7. */
  8. namespace App\Console\Commands\Transfer;
  9. use App\Models\SubsiteSysMessage;
  10. use Illuminate\Console\Command;
  11. use App\Transfer\Article;
  12. use App\Transfer\ArticleProperty;
  13. use App\Transfer\ArticleCategory;
  14. use App\Models\SubsiteArticle;
  15. use App\Models\Article as newArticle;
  16. use App\Models\ArticleCategory as newArticleCategory;
  17. use App\Models\ArticleProperty as newArticleProperty;
  18. use App\Models\ArticleCategoryRole;
  19. class ArticleCommand extends Command
  20. {
  21. protected $signature = 'aix:transfer-article';
  22. protected $description = 'add the transfer-article data';
  23. /**
  24. * ArticleCommand constructor.
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. public function handle()
  31. {
  32. newArticleCategory::withTrashed()->forceDelete();
  33. newArticleProperty::withTrashed()->forceDelete();
  34. newArticle::withTrashed()->forceDelete();
  35. SubsiteArticle::truncate();
  36. ArticleCategoryRole::truncate();
  37. //导入分类
  38. $categories = ArticleCategory::orderBy('id', 'asc')->get();
  39. $cate_count = 0;
  40. $cate_last_id = 0;
  41. if ($categories->isNotEmpty()) {
  42. $cate_data = [];
  43. foreach ($categories as $k => $v) {
  44. $time = date('Y-m-d H:i:s', time());
  45. $cate_data = array(
  46. 'id' => $v->id,
  47. 'category_name' => $v->categoryname,
  48. 'parent_id' => $v->parentid,
  49. 'list_order' => $v->category_order,
  50. 'title' => $v->title,
  51. 'keywords' => $v->keywords,
  52. 'description' => $v->description,
  53. 'admin_set' => $v->admin_set,
  54. 'created_at' => $time,
  55. 'updated_at' => $time
  56. );
  57. if (newArticleCategory::insert($cate_data)) {
  58. $cate_count++;
  59. $cate_last_id = $v->id;
  60. } else {
  61. $this->info('导入新闻分类' . $v->id . '失败');
  62. }
  63. }
  64. }
  65. $this->info("导入新闻分类:" . $cate_count . '条,最后导入的新闻分类id是:' . $cate_last_id);
  66. //导入属性
  67. $properties = ArticleProperty::orderBy('id', 'asc')->get();
  68. $pro_count = 0;
  69. $pro_last_id = 0;
  70. if ($properties->isNotEmpty()) {
  71. $pro_data = [];
  72. foreach ($properties as $key => $val) {
  73. $ptime = date('Y-m-d H:i:s', time());
  74. $pro_data = array(
  75. 'id' => $val->id,
  76. 'category_name' => $val->categoryname,
  77. 'list_order' => $val->category_order,
  78. 'admin_set' => $val->admin_set,
  79. 'created_at' => $ptime,
  80. 'updated_at' => $ptime
  81. );
  82. if (newArticleProperty::insert($pro_data)) {
  83. $pro_count++;
  84. $pro_last_id = $val->id;
  85. } else {
  86. $this->info('导入新闻属性' . $val->id . '失败');
  87. }
  88. }
  89. }
  90. $this->info("导入新闻属性:" . $pro_count . '条,最后导入的新闻属性id是:' . $pro_last_id);
  91. //导入新闻
  92. $articles = Article::orderBy('id', 'asc')->get();
  93. $article_count = 0;
  94. $article_last_id = 0;
  95. if ($articles->isNotEmpty()) {
  96. $article_data = [];
  97. foreach ($articles as $n => $article) {
  98. $content = htmlspecialchars_decode($article->content);
  99. $match_str = '/data/upload';
  100. if (strpos($content, $match_str) >= 0) {
  101. $content = str_replace($match_str, '/storage/old', $content);
  102. }
  103. /*$content = htmlspecialchars_decode($article->content);
  104. $match_str = 'class="ke-insertfile" href="';
  105. $content = substr_replace($content, 'old', (int)(28+strpos($content, $match_str)), 0);*/
  106. $atime = date('Y-m-d H:i:s', $article->addtime);
  107. $article_data = array(
  108. 'id' => $article->id,
  109. 'type_id' => $article->type_id,
  110. 'parent_id' => $article->parentid,
  111. 'title' => $article->title,
  112. 'content' => $content,
  113. 'tit_color' => $article->tit_color,
  114. 'tit_b' => $article->tit_b,
  115. 'small_img' => !empty($article->Small_img) ? 'old/images/' . $article->Small_img : '',
  116. 'is_display' => $article->is_display,
  117. 'released_at' => $article->addtime,
  118. 'list_order' => $article->article_order == 255 ? 0 : $article->article_order,
  119. 'author' => $article->author,
  120. 'source' => $article->source,
  121. 'property_id' => $article->focos,
  122. 'is_url' => $article->is_url,
  123. 'seo_keywords' => $article->seo_keywords,
  124. 'seo_description' => $article->seo_description,
  125. 'click' => $article->click,
  126. 'subsite_id' => $article->subsite_id,
  127. 'robot' => $article->robot,
  128. 'created_at' => $atime,
  129. 'updated_at' => $atime
  130. );
  131. if (newArticle::insert($article_data)) {
  132. $subsite_data = array(
  133. 'article_id' => $article->id,
  134. 'subsite_id' => $article->subsite_id?$article->subsite_id:0,
  135. 'created_at' => $atime,
  136. 'updated_at' => $atime,
  137. );
  138. SubsiteArticle::insert($subsite_data);
  139. $article_count++;
  140. $article_last_id = $article->id;
  141. } else {
  142. $this->info('导入新闻' . $article->id . '失败');
  143. }
  144. }
  145. }
  146. $this->info("导入新闻:" . $article_count . '条,最后导入的新闻id是:' . $article_last_id);
  147. }
  148. }