<?php

namespace App\Console\Commands\Transfer;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

use App\Transfer\Notice;
use App\Transfer\NoticeCategory;
use App\Models\SubsiteNotice;
use App\Models\NoticeCategory as newNoticeCategory;
use App\Models\Notice as newNotice;
use App\Transfer\ExplainCategory;
use App\Models\SubsiteExplain;
use App\Models\Explain as newExplain;
use App\Models\ExplainCategory as newExplainCategory;
use App\Transfer\Feedback;
use App\Models\Feedback as newFeedback;
use App\Transfer\Appeal;
use App\Models\Appeal as newAppeal;
use App\Transfer\Report;
use App\Transfer\ReportResume;
use App\Models\Report as newReport;
use App\Transfer\Help;
use App\Transfer\HelpCategory;
use App\Models\Help as newHelp;
use App\Models\HelpCategory as newHelpCategory;
use App\Transfer\Hrtool;
use App\Transfer\HrtoolCategory;
use App\Models\Hrtools as newHrtools;
use App\Models\HrtoolsCategory as newHrtoolsCategory;

use App\Transfer\Link;
use App\Transfer\LinkCategory;
use App\Models\SubsiteLink;
use App\Models\Link as newLink;
use App\Models\LinkCategory as newLinkCategory;

class TransferContent extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'aix:transfer-content';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '转移旧系统内容数据';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //转移新闻资讯模块(不导入)
        //转移广告模块(手动导入)

        //转移友情链接模块
        $this->info("清除友情链接分类数据表:link_categorys");
        DB::table('link_categorys')->delete();
        $this->info("清除友情链接数据表:links");
        DB::table('links')->delete();
        $this->info("清除友情链接与分站关系数据表:subsite_link");
        DB::table('subsite_link')->delete();
        $this->info("开始转移数据...");

        $total = LinkCategory::count();
        $bar = $this->output->createProgressBar($total);
        LinkCategory::orderBy('id', 'asc')->chunk(500, function ($categories) use ($bar) {
            $cate_array = [];
            $link_array = [];
            $link_subsite_array = [];

            foreach ($categories as $cate) {
                $this->handleLinkCategory($cate, $cate_array);
                $this->handleLink($cate->id, $cate->links, $link_array, $link_subsite_array);
            }
            //批量插入数据;
            if ($cate_array) {
                newLinkCategory::insert($cate_array);
            }
            if ($link_array) {
                newLink::insert($link_array);
            }
            if ($link_subsite_array) {
                SubsiteLink::insert($link_subsite_array);
            }
            $bar->advance(500);
        });
        $bar->finish();
        $this->line("  完成!");
        $this->info("转移友情链接数据成功.");

        //转移HR工具箱模块
        $this->info("清除HR工具箱分类数据表:hrtools_categorys");
        DB::table('hrtools_categorys')->delete();
        $this->info("清除HR工具箱数据表:hrtools");
        DB::table('hrtools')->delete();
        $this->info("开始转移数据...");

        $total = HrtoolCategory::count();
        $bar = $this->output->createProgressBar($total);
        HrtoolCategory::orderBy('c_id', 'asc')->chunk(500, function ($hrtool_categories) use ($bar) {
            $hrtool_cate_array = [];
            $hrtool_array = [];
            foreach ($hrtool_categories as $cate) {
                $this->handleHrtoolCategory($cate, $hrtool_cate_array);
                $this->handleHrtool($cate->hrtools, $hrtool_array);
            }
            if ($hrtool_cate_array) {
                newHrtoolsCategory::insert($hrtool_cate_array);
            }
            if ($hrtool_array) {
                newHrtools::insert($hrtool_array);
            }
            $bar->advance(500);
        });
        $bar->finish();
        $this->line("  完成!");
        $this->info("转移HR工具箱数据成功.");

        //转移帮助模块
        $this->info("清除帮助中心分类数据表:help_categorys");
        DB::table('help_categorys')->delete();
        $this->info("清除帮助中心数据表:helps");
        DB::table('helps')->delete();
        $this->info("开始转移数据...");

        $total = HelpCategory::count();
        $bar = $this->output->createProgressBar($total);
        HelpCategory::orderBy('id', 'asc')->chunk(500, function ($help_categories) use ($bar) {
            $help_cate_array = [];
            $help_array = [];
            foreach ($help_categories as $cate) {
                $this->handleHelpCategory($cate, $help_cate_array);
                $this->handleHelp($cate->helps, $help_array);
            }
            if ($help_cate_array) {
                newHelpCategory::insert($help_cate_array);
            }
            if ($help_array) {
                newHelp::insert($help_array);
            }
            $bar->advance(500);
        });
        $bar->finish();
        $this->line("  完成!");
        $this->info("转移帮助中心数据成功.");

        //转移举报职位模块
        $this->info("清除举报数据表:reports");
        DB::table('reports')->delete();
        $this->info("开始转移数据...");
        $total = Report::count();
        $bar = $this->output->createProgressBar($total);
        Report::orderBy('id', 'asc')->chunk(500, function ($reports) use ($bar) {
            $report_array = [];
            foreach ($reports as $report) {
                $this->handleJobReport($report, $report_array);
            }
            if ($report_array) {
                newReport::insert($report_array);
            }
            $bar->advance(500);
        });
        $bar->finish();
        $this->line("  完成!");
        $this->info("转移举报职位数据成功.");

        //转移举报简历模块
        $total = ReportResume::count();
        $bar = $this->output->createProgressBar($total);
        ReportResume::orderBy('id', 'asc')->chunk(500, function ($resume_reports) use ($bar) {
            $resume_report_array = [];
            foreach ($resume_reports as $report) {
                $this->handleResumeReport($report, $resume_report_array);
            }
            if ($resume_report_array) {
                newReport::insert($resume_report_array);
            }
            $bar->advance(500);
        });
        $bar->finish();
        $this->line("  完成!");
        $this->info("转移举报简历数据成功.");

        //转移账号申诉模块
        $this->info("清除账号申诉数据表:appeals");
        DB::table('appeals')->delete();
        $this->info("开始转移数据...");
        $total = Appeal::count();
        $bar = $this->output->createProgressBar($total);
        Appeal::orderBy('id', 'asc')->chunk(500, function ($appeals) use ($bar) {
            $appeal_array = [];
            foreach ($appeals as $appeal) {
                $this->handleAppeal($appeal, $appeal_array);
            }
            if ($appeal_array) {
                newAppeal::insert($appeal_array);
            }
            $bar->advance(500);
        });
        $bar->finish();
        $this->line("  完成!");
        $this->info("转移账号申诉数据成功.");

        //转移意见建议模块
        $this->info("清除意见建议数据表:feedbacks");
        DB::table('feedbacks')->delete();
        $this->info("开始转移数据...");
        $total = NoticeCategory::count();
        $bar = $this->output->createProgressBar($total);
        Feedback::orderBy('id', 'asc')->chunk(500, function ($feedbacks) use ($bar) {
            $feed_array = [];
            foreach ($feedbacks as $feedback) {
                $this->handleFeedback($feedback, $feed_array);
            }
            if ($feed_array) {
                newFeedback::insert($feed_array);
            }
            $bar->advance(500);
        });
        $bar->finish();
        $this->line("  完成!");
        $this->info("转移意见建议数据成功.");

        //转移公告模块
        $this->info("清除公告分类数据表:notice_categorys");
        DB::table('notice_categorys')->delete();
        $this->info("清除公告数据表:notices");
        DB::table('notices')->delete();
        $this->info("清除公告分站关系数据表:subsite_notice");
        DB::table('subsite_notice')->delete();
        $this->info("开始转移数据...");

        $total = NoticeCategory::count();
        $bar = $this->output->createProgressBar($total);
        NoticeCategory::orderBy('id', 'asc')->chunk(500, function ($categories) use ($bar) {
            $cate_array = [];
            $notice_array = [];
            $notice_subsite_array = [];

            foreach ($categories as $cate) {
                //处理公告分类表
                $this->handleNoticeCategory($cate, $cate_array);
                //处理公告表、公告与分站关系表
                $this->handleNotice($cate->notices, $notice_array, $notice_subsite_array);
            }
            //批量插入数据;
            if ($cate_array) {
                newNoticeCategory::insert($cate_array);
            }
            if ($notice_array) {
                newNotice::insert($notice_array);
            }
            if ($notice_subsite_array) {
                SubsiteNotice::insert($notice_subsite_array);
            }
            $bar->advance(500);
        });
        $bar->finish();
        $this->line("  完成!");
        $this->info("转移公告数据成功.");

        //转移说明页模块
        $this->info("清除说明页分类数据表:explain_categorys");
        DB::table('explain_categorys')->delete();
        $this->info("清除说明页数据表:explains");
        DB::table('explains')->delete();
        $this->info("清除说明页分站关系数据表:subsite_explain");
        DB::table('subsite_explain')->delete();

        $this->info("开始转移数据...");
        $total = ExplainCategory::count();
        $bar = $this->output->createProgressBar($total);
        ExplainCategory::orderBy('id', 'asc')->chunk(500, function ($categories) use ($bar) {
            $cate_array = [];
            $explain_array = [];
            $explain_subsite_array = [];

            foreach ($categories as $cate) {
                //处理说明页分类表
                $this->handleExplainCategory($cate, $cate_array);
                //处理说明页表、说明页与分站关系表
                $this->handleExplain($cate->explains, $explain_array, $explain_subsite_array);
            }
            //批量插入数据;
            if ($cate_array) {
                newExplainCategory::insert($cate_array);
            }
            if ($explain_array) {
                newExplain::insert($explain_array);
            }
            if ($explain_subsite_array) {
                SubsiteExplain::insert($explain_subsite_array);
            }
            $bar->advance(500);
        });
        $bar->finish();
        $this->line("  完成!");
        $this->info("转移说明页数据成功.");
    }

    public function handleLinkCategory($link_cate, &$result_array)
    {
        $time = date('Y-m-d H:i:s', time());
        if (strpos($link_cate->c_alias, 'QS_') === 0) {
            $alias = substr_replace($link_cate->c_alias, 'AIX_', 0, 3);
        } else {
            $alias = $link_cate->c_alias;
        }
        $cate_data = array(
            'id' => $link_cate->id,
            'category_name' => $link_cate->categoryname,
            'alias' => $alias,
            'admin_set' => $link_cate->c_sys,
            'created_at' => $time,
            'updated_at' => $time
        );
        $result_array[] = $cate_data;
    }

    public function handleLink($type_id, $links, &$result_array, &$sub_array)
    {
        foreach ($links as $key => $link) {
            $ltime = date('Y-m-d H:i:s', time());
            $link_data = array(
                'id' => $link->link_id,
                'is_display' => $link->display,
                'type_id' => $type_id,
                'link_title' => $link->link_name,
                'link_url' => $link->link_url,
                'link_logo' => !empty($link->link_logo) ? 'old/link_logo/' . $link->link_logo : '',
                'note' => htmlspecialchars_decode($link->notes),
                'list_order' => $link->show_order,
                'subsite_id' => $link->subsite_id,
                'created_at' => $ltime,
                'updated_at' => $ltime,
            );
            $result_array[] = $link_data;

            $sub_data = array(
                'link_id' => $link->link_id,
                'subsite_id' => $link->subsite_id,
                'created_at' => $ltime,
                'updated_at' => $ltime,
            );
            $sub_array[] = $sub_data;
        }
    }

    public function handleHrtoolCategory($hrtool_cate, &$result_array)
    {
        $time = date('Y-m-d H:i:s', time());
        $data = array(
            'id' => $hrtool_cate->c_id,
            'category_name' => $hrtool_cate->c_name,
            'category_img' => !empty($hrtool_cate->c_img) ? 'old/resource/hrtools_img/' . $hrtool_cate->c_img : '',
            'admin_set' => $hrtool_cate->c_adminset,
            'list_order' => $hrtool_cate->c_order,
            'content' => $hrtool_cate->c_desc,
            'created_at' => $time,
            'updated_at' => $time
        );
        $result_array[] = $data;
    }

    public function handleHrtool($hrtools, &$result_array)
    {
        foreach ($hrtools as $key => $val) {
            $htime = date('Y-m-d H:i:s', time());
            $hr_data = array(
                'id' => $val->h_id,
                'type_id' => $val->h_typeid,
                'title' => $val->h_filename,
                'tit_color' => $val->h_color,
                'tit_b' => $val->h_strong,
                'file_url' => !empty($val->h_fileurl) ? 'old/hrtools/' . $val->h_fileurl : '',
                'list_order' => $val->h_order,
                'created_at' => $htime,
                'updated_at' => $htime,
            );
            $result_array[] = $hr_data;
        }
    }

    public function handleHelpCategory($help_cate, &$result_array)
    {
        $time = date('Y-m-d H:i:s', time());
        $cate_data = array(
            'id' => $help_cate->id,
            'parent_id' => $help_cate->parentid,
            'category_name' => $help_cate->categoryname,
            'list_order' => $help_cate->category_order,
            'created_at' => $time,
            'updated_at' => $time
        );
        $result_array[] = $cate_data;
    }

    public function handleHelp($helps, &$result_array)
    {
        foreach ($helps as $help) {
            $htime = date('Y-m-d H:i:s', $help->addtime);
            $help_data = array(
                'id' => $help->id,
                'type_id' => $help->type_id,
                'parent_id' => $help->parentid,
                'title' => $help->title,
                'content' => $help->content,
                'list_order' => $help->ordid,
                'click' => $help->click,
                'created_at' => $htime,
                'updated_at' => $htime,
            );
            $result_array[] = $help_data;
        }
    }

    public function handleJobReport($report, &$result_array)
    {
        $data = array(
            'uid' => $report->uid,
            'username' => $report->username,
            'utype' => 1,
            'utype_id' => $report->jobs_id,
            'utype_realname' => $report->jobs_name,
            'type_id' => $report->report_type,
            'phone' => $report->telephone,
            'content' => $report->content,
            'audit' => $report->audit,
            'subsite_id' => $report->subsite_id,
            'created_at' => date('Y-m-d H:i:s', $report->addtime),
            'updated_at' => date('Y-m-d H:i:s', $report->addtime),
        );
        $result_array[] = $data;
    }

    public function handleResumeReport($resume_report, &$result_array)
    {
        $report_type = 7 + (int)$resume_report->report_type;
        $data = array(
            'uid' => $resume_report->uid,
            'username' => $resume_report->username,
            'utype' => 2,
            'utype_id' => $resume_report->resume_id,
            'utype_realname' => $resume_report->resume_realname,
            'type_id' => $report_type,
            'phone' => '',
            'content' => '',
            'audit' => $resume_report->audit,
            'subsite_id' => $resume_report->subsite_id,
            'created_at' => date('Y-m-d H:i:s', $resume_report->addtime),
            'updated_at' => date('Y-m-d H:i:s', $resume_report->addtime),
        );
        $result_array[] = $data;
    }

    public function handleAppeal($appeal, &$result_array)
    {
        $appeal_data = array(
            'id' => $appeal->id,
            'real_name' => $appeal->realname,
            'mobile' => $appeal->mobile,
            'email' => $appeal->email,
            'description' => $appeal->description,
            'subsite_id' => $appeal->subsite_id,
            'status' => $appeal->status,
            'created_at' => date('Y-m-d H:i:s', $appeal->addtime),
            'updated_at' => date('Y-m-d H:i:s', $appeal->addtime)
        );
        $result_array[] = $appeal_data;
    }

    public function handleFeedback($feedback, &$result_array)
    {
        $audit = 0;
        if ($feedback->audit == 1) {
            $audit = 0;
        } elseif ($feedback->audit == 2) {
            $audit = 1;
        }
        $data = array(
            'id' => $feedback->id,
            'type' => $feedback->infotype,
            'content' => $feedback->feedback,
            'contact' => $feedback->tel,
            'audit' => $audit,
            'subsite_id' => $feedback->subsite_id?$feedback->subsite_id:0,
            'created_at' => date('Y-m-d H:i:s', $feedback->addtime),
            'updated_at' => date('Y-m-d H:i:s', $feedback->addtime)
        );
        $result_array[] = $data;
    }

    public function handleExplainCategory($cate, &$result_array)
    {
        $time = date('Y-m-d H:i:s', time());
        $data['id'] = $cate->id;
        $data['category_name'] = $cate->categoryname;
        $data['list_order'] = $cate->category_order;
        $data['admin_set'] = $cate->admin_set;
        $data['status'] = 1;
        $data['created_at'] = $time;
        $data['updated_at'] = $time;

        $result_array[] = $data;
    }

    public function handleExplain($explains, &$result_array, &$sub_array)
    {
        foreach ($explains as $key => $explain) {
            $etime = date('Y-m-d H:i:s', $explain->addtime);
            $is_url = $explain->is_url;
            if ($explain->is_url == 'http://') {
                $is_url = null;
            }
            $data['id'] = $explain->id;
            $data['title'] = $explain->title;
            $data['tit_color'] = $explain->tit_color ? $explain->tit_color : '#000000';
            $data['tit_b'] = $explain->tit_b;
            $data['content'] = htmlspecialchars_decode($explain->content);
            $data['is_display'] = $explain->is_display;
            $data['is_url'] = $is_url;
            $data['seo_keywords'] = $explain->seo_keywords;
            $data['seo_description'] = $explain->seo_description;
            $data['click'] = $explain->click;
            $data['list_order'] = $explain->show_order;
            $data['subsite_id'] = $explain->subsite_id;
            $data['created_at'] = $etime;
            $data['updated_at'] = $etime;

            $result_array[] = $data;

            //分站对应关系
            $sub_data = array(
                'explain_id' => $explain->id,
                'subsite_id' => $explain->subsite_id,
                'created_at' => $etime,
                'updated_at' => $etime,
            );

            $sub_array[] = $sub_data;
        }
    }

    public function handleNoticeCategory($cate, &$result_array)
    {
        $time = date('Y-m-d H:i:s', time());
        $data['id'] = $cate->id;
        $data['category_name'] = $cate->categoryname;
        $data['listorder'] = $cate->sort;
        $data['admin_set'] = $cate->admin_set;
        $data['status'] = 1;
        $data['created_at'] = $time;
        $data['updated_at'] = $time;

        $result_array[] = $data;
    }

    public function handleNotice($notices, &$result_array, &$sub_array)
    {
        foreach ($notices as $key => $notice) {
            $ntime = date('Y-m-d H:i:s', $notice->addtime);
            $is_url = $notice->is_url;
            if ($notice->is_url == 'http://') {
                $is_url = null;
            }
            $data['id'] = $notice->id;
            $data['type_id'] = $notice->type_id;
            $data['title'] = $notice->title;
            $data['content'] = htmlspecialchars_decode($notice->content);
            $data['tit_color'] = $notice->tit_color ? $notice->tit_color : '#000000';
            $data['tit_b'] = $notice->tit_b;
            $data['is_display'] = $notice->is_display;
            $data['is_url'] = $is_url;
            $data['seo_keywords'] = $notice->seo_keywords;
            $data['seo_description'] = $notice->seo_description;
            $data['click'] = $notice->click;
            $data['sort'] = $notice->sort;
            $data['subsite_id'] = $notice->subsite_id;
            $data['created_at'] = $ntime;
            $data['updated_at'] = $ntime;

            $result_array[] = $data;

            //分站对应关系
            $sub_data = array(
                'notice_id' => $notice->id,
                'subsite_id' => $notice->subsite_id,
                'created_at' => $ntime,
                'updated_at' => $ntime,
            );
            $sub_array[] = $sub_data;
        }
    }
}