| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 | <?php/** * Created by PhpStorm. * User: Administrator * Date: 2019/4/23 * Time: 19:17 */namespace App\Console\Commands\Transfer;use App\Models\SubsiteSysMessage;use Illuminate\Console\Command;use App\Transfer\Article;use App\Transfer\ArticleProperty;use App\Transfer\ArticleCategory;use App\Models\SubsiteArticle;use App\Models\Article as newArticle;use App\Models\ArticleCategory as newArticleCategory;use App\Models\ArticleProperty as newArticleProperty;use App\Models\ArticleCategoryRole;class ArticleCommand extends Command{    protected $signature = 'aix:transfer-article';    protected $description = 'add the transfer-article data';    /**     * ArticleCommand constructor.     */    public function __construct()    {        parent::__construct();    }    public function handle()    {        newArticleCategory::withTrashed()->forceDelete();        newArticleProperty::withTrashed()->forceDelete();        newArticle::withTrashed()->forceDelete();        SubsiteArticle::truncate();        ArticleCategoryRole::truncate();        //导入分类        $categories = ArticleCategory::orderBy('id', 'asc')->get();        $cate_count = 0;        $cate_last_id = 0;        if ($categories->isNotEmpty()) {            $cate_data = [];            foreach ($categories as $k => $v) {                $time = date('Y-m-d H:i:s', time());                $cate_data = array(                    'id' => $v->id,                    'category_name' => $v->categoryname,                    'parent_id' => $v->parentid,                    'list_order' => $v->category_order,                    'title' => $v->title,                    'keywords' => $v->keywords,                    'description' => $v->description,                    'admin_set' => $v->admin_set,                    'created_at' => $time,                    'updated_at' => $time                );                if (newArticleCategory::insert($cate_data)) {                    $cate_count++;                    $cate_last_id = $v->id;                } else {                    $this->info('导入新闻分类' . $v->id . '失败');                }            }        }        $this->info("导入新闻分类:" . $cate_count . '条,最后导入的新闻分类id是:' . $cate_last_id);        //导入属性        $properties = ArticleProperty::orderBy('id', 'asc')->get();        $pro_count = 0;        $pro_last_id = 0;        if ($properties->isNotEmpty()) {            $pro_data = [];            foreach ($properties as $key => $val) {                $ptime = date('Y-m-d H:i:s', time());                $pro_data = array(                    'id' => $val->id,                    'category_name' => $val->categoryname,                    'list_order' => $val->category_order,                    'admin_set' => $val->admin_set,                    'created_at' => $ptime,                    'updated_at' => $ptime                );                if (newArticleProperty::insert($pro_data)) {                    $pro_count++;                    $pro_last_id = $val->id;                } else {                    $this->info('导入新闻属性' . $val->id . '失败');                }            }        }        $this->info("导入新闻属性:" . $pro_count . '条,最后导入的新闻属性id是:' . $pro_last_id);        //导入新闻        $articles = Article::orderBy('id', 'asc')->get();        $article_count = 0;        $article_last_id = 0;        if ($articles->isNotEmpty()) {            $article_data = [];            foreach ($articles as $n => $article) {                $content = htmlspecialchars_decode($article->content);                $match_str = '/data/upload';                if (strpos($content, $match_str) >= 0) {                    $content = str_replace($match_str, '/storage/old', $content);                }                /*$content = htmlspecialchars_decode($article->content);                $match_str = 'class="ke-insertfile" href="';                $content = substr_replace($content, 'old', (int)(28+strpos($content, $match_str)), 0);*/                $atime = date('Y-m-d H:i:s', $article->addtime);                $article_data = array(                    'id' => $article->id,                    'type_id' => $article->type_id,                    'parent_id' => $article->parentid,                    'title' => $article->title,                    'content' => $content,                    'tit_color' => $article->tit_color,                    'tit_b' => $article->tit_b,                    'small_img' => !empty($article->Small_img) ? 'old/images/' . $article->Small_img : '',                    'is_display' => $article->is_display,                    'released_at' => $article->addtime,                    'list_order' => $article->article_order == 255 ? 0 : $article->article_order,                    'author' => $article->author,                    'source' => $article->source,                    'property_id' => $article->focos,                    'is_url' => $article->is_url,                    'seo_keywords' => $article->seo_keywords,                    'seo_description' => $article->seo_description,                    'click' => $article->click,                    'subsite_id' => $article->subsite_id,                    'robot' => $article->robot,                    'created_at' => $atime,                    'updated_at' => $atime                );                if (newArticle::insert($article_data)) {                    $subsite_data = array(                        'article_id' => $article->id,                        'subsite_id' => $article->subsite_id?$article->subsite_id:0,                        'created_at' => $atime,                        'updated_at' => $atime,                    );                    SubsiteArticle::insert($subsite_data);                    $article_count++;                    $article_last_id = $article->id;                } else {                    $this->info('导入新闻' . $article->id . '失败');                }            }        }        $this->info("导入新闻:" . $article_count . '条,最后导入的新闻id是:' . $article_last_id);    }}
 |