ImportPoemDataToMysql.php 13 KB
<?php

namespace App\Console\Commands;

use App\Models\Author;
use App\Models\Poetry;
use App\Models\Verse;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class ImportPoemDataToMysql extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'import:poem';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @throws
     * @return int
     */
    public function handle()
    {
        Author::query()->truncate();
        Poetry::query()->truncate();
        Verse::query()->truncate();

        // 写入楚辞
        $this->createChuCiData();

        // 写入诗经
        $this->createShiJingData();

        // 宋词
        $this->createSongCiData();

        // 唐诗
        $this->createTangShiData();

        // 道德经
        $this->createDaoDeJingData();

        return 0;
    }

    public function createChuCiData()
    {
        try{
            $chuci = json_decode(Storage::disk('public')->get('PoemData/chuci.json'),true);

            $author = new Author;
            $author->name = '屈原';
            $author->dynasty = '春秋战国';
            $author->introduce = '屈原是属于春秋战国时期楚国的人,芈姓,屈氏,名平,字原,又自云名正则,字灵均,战国时期楚国诗人、政治家,是中国历史上一位伟大的爱国诗人,中国浪漫主义文学的奠基人,“楚辞”的创立者和代表作家,开辟了“香草美人”的传统,被誉为“楚辞之祖”。';
            $author->save();

            $author = new Author;
            $author->name = '宋玉';
            $author->dynasty = '战国';
            $author->introduce = '宋玉(公元前298年—公元前222年),楚国诗人,宋国公族后裔,生于楚国,曾事楚顷襄王,为楚国士大夫。战国著名辞赋家,宋玉与唐勒,景差齐名,传世作品有《九辩》等。所谓“下里巴人”、“阳春白雪”、“曲高和寡”便说的是他,典故皆他而来。始皇帝二十五年己卯,公元前222年因病去世享年七十六岁。';
            $author->save();

            $author = new Author;
            $author->name = '景差';
            $author->dynasty = '战国';
            $author->introduce = '景差[cuō](前290年—前223年),芈姓,景氏,名差,战国时期楚国辞赋家。后于屈原,与宋玉、唐勒同时以赋见称,主要作品有楚辞《大招》。';
            $author->save();

            $author = new Author;
            $author->name = '贾谊';
            $author->dynasty = '战国';
            $author->introduce = '贾谊(公元前200年~公元前168年),汉族,洛阳(今河南省洛阳市)人,西汉初年著名政论家、文学家,世称贾生。贾谊少有才名,十八岁时,以善文为郡人所称。文帝时任博士,迁太中大夫,受大臣周勃、灌婴排挤,谪为长沙王太傅,故后世亦称贾长沙、贾太傅。三年后被召回长安,为梁怀王太傅。梁怀王坠马而死,贾谊深自歉疚,抑郁而亡,时仅三十三岁。司马迁对屈原、贾谊都寄予同情,为二人写了一篇合传,后世因而往往把贾谊与屈原并称为“屈贾”。';
            $author->save();

            $author = new Author;
            $author->name = '淮南小山';
            $author->dynasty = '西汉';
            $author->introduce = '淮南小山是西汉淮南王刘安的一部分门客的共称。今仅存辞赋《招隐士》 1篇。《汉书·艺文志》著录“淮南王群臣赋四十四篇”,《招隐士》当是其中仅存的1篇。此篇始见于东汉王逸的《楚辞章句》,题为淮南小山作,然而萧统《文选》则题刘安作。关于文章写作的背景,说法也不一。王逸说是小山之徒“闵伤屈原”之作,王夫之《楚辞通释》说是淮南小山“为淮南王召致山谷潜伏之士”而作,而不少研究者则以为是淮南小山思念淮南王的作品。';
            $author->save();

            $author = new Author;
            $author->name = '东方朔';
            $author->dynasty = '西汉';
            $author->introduce = '东方朔(约前161年—前93年?),字曼倩,平原郡厌次县人,西汉时期著名文学家。';
            $author->save();

            $author = new Author;
            $author->name = '庄忌';
            $author->dynasty = '西汉';
            $author->introduce = '庄忌(约前188-前105)西汉辞赋家。会稽吴(今苏州吴县)人。因避汉明帝刘庄讳改名严忌,与邹阳、枚乘等唱和,是梁孝王门下著名辞赋家。作品仅存《哀时命》一篇。 此赋感叹屈原生不逢时,空怀壮志而不得伸。是咏屈赋中的佳品。此赋纯属骚体,保持了由贾谊开创的西汉早期骚赋所具有的特点。感情真挚,篇幅短小精悍,是咏屈赋中的佳品。';
            $author->save();

            $author = new Author;
            $author->name = '王褒';
            $author->dynasty = '西汉';
            $author->introduce = '王褒(前90年—前51年),字子渊,别号桐柏真人,蜀郡资中(今四川省资阳市雁江区昆仑乡墨池坝村)人。西汉时期辞赋家,与扬雄并称“渊云”。';
            $author->save();

            $author = new Author;
            $author->name = '刘向';
            $author->dynasty = '西汉';
            $author->introduce = '刘向(公元前77年~公元前6年),原名刘更生,字子政,沛郡丰邑(今江苏省徐州市)人。汉朝宗室大臣、文学家,楚元王刘交(汉高祖刘邦异母弟)之玄孙,阳城侯刘德之子,经学家刘歆之父,中国目录学鼻祖。';
            $author->save();

            $author = new Author;
            $author->name = '王逸';
            $author->dynasty = '东汉';
            $author->introduce = '王逸,字叔师,生卒年不详,南郡宜城(今湖北省襄阳市宜城县)人,东汉著名文学家。官至豫州刺史、豫章太守。所作《楚辞章句》,是《楚辞》中最早的完整注本,颇为后世楚辞学者所重。又作有赋、诔、书、论等21篇,及《汉诗》123篇,今多散佚,仅存《九思》,为哀悼屈原而作。明人辑有《王叔师集》';
            $author->save();


            foreach ($chuci as $item) {
                $author = Author::query()->where('name',$item['author'])->first();

                $poetry = new Poetry();
                $poetry->name = $item['title'];
                $poetry->subname = $item['section'];
                $poetry->author_id = $author->id;
                $poetry->save();

                foreach ($item['content'] as $value) {
                    $verse = new Verse();
                    $verse->poetry_id = $poetry->id;
                    $verse->stanza = $value;
                    $verse->annotate = '';
                    $verse->en = '';
                    $verse->save();
                }
            }
        }catch (\Exception $exception){
            dd($exception->getMessage() . 'Line:' . $exception->getLine());
        }

        return true;
    }

    public function createShiJingData()
    {
        try{
            DB::beginTransaction();
            $author = new Author;
            $author->name = '不详';
            $author->dynasty = '西周至春秋';
            $author->introduce = '《诗经》的作者佚名,绝大部分已经无法考证,传为尹吉甫采集、孔子编订。《诗经》在先秦时期称为《诗》,或取其整数称《诗三百》。';
            $author->save();

            $shijing = json_decode(Storage::disk('public')->get('PoemData/shijing.json'),true);

            foreach ($shijing as $item) {

                $poetry = new Poetry();
                $poetry->name = $item['title'];
                $poetry->subname = $item['section'];
                $poetry->alias = $item['chapter'];
                $poetry->author_id = $author->id;
                $poetry->save();

                foreach ($item['content'] as $value) {
                    $verse = new Verse();
                    $verse->poetry_id = $poetry->id;
                    $verse->stanza = $value;
                    $verse->annotate = '';
                    $verse->en = '';
                    $verse->save();
                }
            }
            DB::commit();
        }catch (\Exception $exception){
            DB::rollBack();
            dd($exception->getMessage() . 'Line:' . $exception->getLine());
        }

        return true;
    }

    public function createSongCiData()
    {
        try{
            $songauthor = json_decode(Storage::disk('public')->get('PoemData/author.song.json'),true);

            foreach ($songauthor as $item) {
                $author = new Author();
                $author->name = $item['name'];
                $author->introduce = $item['description'];
                $author->save();
            }
            $songci = json_decode(Storage::disk('public')->get('PoemData/songci.json'),true);

            foreach ($songci as $item) {
                $author = Author::query()->where('name',$item['author'])->first();

                $poetry = new Poetry();
                $poetry->name = $item['rhythmic'];
                $poetry->author_id = $author->id;
                $poetry->save();

                foreach ($item['paragraphs'] as $value) {
                    $verse = new Verse();
                    $verse->poetry_id = $poetry->id;
                    $verse->stanza = $value;
                    $verse->annotate = '';
                    $verse->en = '';
                    $verse->save();
                }
            }

        }catch (\Exception $exception){
            dd($exception->getMessage() . ' Line:' . $exception->getLine() . ' name:' . $item['rhythmic']);
        }
    }

    public function createTangShiData()
    {
        try{
            $tangauthor = json_decode(Storage::disk('public')->get('PoemData/authors.tang.json'),true);

//            foreach ($tangauthor as $item) {
//                $author = new Author();
//                $author->name = $item['name'];
//                $author->introduce = $item['desc'];
//                $author->save();
//            }
            $tangshi = json_decode(Storage::disk('public')->get('PoemData/tangshi.json'),true);

            foreach ($tangshi as $item) {
                $author = new Author();
                $author->name = $item['author'];
                $author->introduce = '';
                foreach ($tangauthor as $itemauthor) {
                    if ($item['author'] == $itemauthor['name']){
                        $author->introduce = $itemauthor['desc'];
                    }
                }
                $author->save();

                $poetry = new Poetry();
                $poetry->name = $item['title'];
                $poetry->author_id = $author->id;
                $poetry->save();

                $paragraphs = explode("\n", $item['contents']);

                foreach ($paragraphs as $value) {
                    $verse = new Verse();
                    $verse->poetry_id = $poetry->id;
                    $verse->stanza = $value;
                    $verse->annotate = '';
                    $verse->en = '';
                    $verse->save();
                }
            }

        }catch (\Exception $exception){
            dd($exception->getMessage() . ' Line:' . $exception->getLine() . ' name:' . $item['title']);
        }
    }

    public function createDaoDeJingData()
    {
        try {
            $author = new Author();
            $author->name = '老子';
            $author->introduce = '老子,姓李名耳,字聃(dān),春秋末期楚国人,中国古代思想家、哲学家、文学家和史学家,道家学派创始人和主要代表人物。老子出生于楚国苦县,早年跟随常枞(zōnɡ)学习知识。周灵王二十一年,老子入周王室任守藏(zàng)室史,后来受到排挤,被免去官职。';
            $author->save();

            $collects = DB::table('article')->get();
            foreach ($collects as $collect) {

                $poetry = new Poetry();
                $poetry->name = '道德经 ' . $collect->title;
                $poetry->author_id = $author->id;
                $poetry->save();


                $verse = new Verse();
                $verse->poetry_id = $poetry->id;
                $verse->stanza = $collect->body;
                $verse->annotate = '';
                $verse->en = '';
                $verse->save();
            }

        }catch (\Exception $exception){
            dd($exception->getMessage() . ' Line:' . $exception->getLine() . ' name:' . $collect->title);
        }
    }
}