IndexController.class.php 5.58 KB
<?php
/**
 * Created by PhpStorm.
 * User: liyifei2012it
 * Date: 17/7/16
 * Time: 22:42
 */
namespace Frontend\Controller\UpdateDB;

use Common\Service\ArticleSourceService;
use Common\Service\ArticleChapterService;
use Common\Service\StudyRecordService;

class IndexController extends AbstractController
{
    /**
     * v2.0.1版本迭代,数据库升级脚本
     * @author liyifei
     */
    public function Index()
    {
        echo "升级 oa_course_article_chapter 表,请执行 Chapter 方法;<br />";
        echo "升级 oa_course_study_record 表,请执行 StudyRecord 方法。<br />";
        exit('SUCCESS');
    }

    /**
     * 将oa_course_article_source表数据,转存至oa_course_article_chapter表,并新增章节数据
     * @author liyifei
     */
    public function Chapter()
    {
        $articleSourceServ = new ArticleSourceService();
        $chapterServ = new ArticleChapterService();

        $orders = [
            'article_id' => 'asc',
            '`order`' => 'asc',
            'created' => 'asc',
        ];
        $asList = $articleSourceServ->list_all(null, $orders);

        if (!empty($asList)) {
            // 获取并去重课程ID
            $articleIds = array_column($asList, 'article_id');
            $articleIds = array_values(array_unique($articleIds));

            // 确认所有课程在章节表中无数据
            $acList = $chapterServ->list_by_conds(['article_id' => $articleIds, 'parent_id' => 0, 'source_id' => 0]);
            if (!empty($acList)) {

                $chapterArticleIds = array_column($acList, 'article_id');
                $chapterArticleIds = array_values(array_unique($chapterArticleIds));
                exit('以下课程ID已存在章节数据:' . implode(',', $chapterArticleIds));
            }

            // 为每个课程创建一个章节
            $insertChapterData = [];
            foreach ($articleIds as $articleId) {

                $insertChapterData[] = [
                    'article_id' => $articleId,
                    'parent_id' => 0,
                    'source_id' => 0,
                    'chapter_name' => '章节1',
                    'et_ids' => '',
                    'rand_num' => 0,
                    'order' => 1,
                ];
            }

            $insertChapterRes = $chapterServ->insert_all($insertChapterData);
            if (empty($insertChapterRes)) {

                exit('为课程创建章节失败');
            }

            // 获取课程新创建的章节信息
            $chapterList = $chapterServ->list_by_conds(['article_id' => $articleIds, 'parent_id' => 0, 'source_id' => 0]);
            if (!empty($chapterList)) {

                $chapterList = array_combine_by_key($chapterList, 'article_id');

                // 将课程素材写入章节表
                $insertSourceData = [];
                foreach ($asList as $source) {

                    $articleId = $source['article_id'];
                    $insertSourceData[] = [
                        'article_id' => $source['article_id'],
                        'parent_id' => $chapterList[$articleId]['article_chapter_id'],
                        'source_id' => $source['source_id'],
                        'chapter_name' => $chapterList[$articleId]['chapter_name'],
                        'et_ids' => '',
                        'rand_num' => 0,
                        'order' => $source['order'] + 1,
                        'status' => $source['status'],
                        'created' => $source['created'],
                        'updated' => $source['updated'],
                    ];
                }

                $insertSourceRes = $chapterServ->insert_all($insertSourceData);
                if (empty($insertSourceRes)) {

                    exit('为章节创建素材失败');
                }
            }
        }

        exit('SUCCESS');
    }

    /**
     * 为学习记录表,录入章节信息
     * @author liyifei
     */
    public function StudyRecord()
    {
        $recordServ = new StudyRecordService();
        $chapterServ = new ArticleChapterService();

        // 获取未写入章节信息的学习记录
        $recordList = $recordServ->list_by_conds(['article_chapter_id' => 0]);
        if (!empty($recordList)) {
            // 获取课程ID
            $articleIds = array_column($recordList, 'article_id');
            $articleIds = array_values(array_unique($articleIds));

            // 获取素材ID
            $sourceIds = array_column($recordList, 'source_id');
            $sourceIds = array_values(array_unique($sourceIds));

            // 获取课程对应的章节数据
            $chapterList = $chapterServ->list_by_conds(['article_id' => $articleIds, 'parent_id > ?' => 0, 'source_id' => $sourceIds]);
            if (empty($chapterList)) {

                exit('学习的素材无对应的章节数据,请先创建章节!');
            }

            // 为学习的课程素材,补上对应的章节ID
            foreach ($recordList as $record) {

                foreach ($chapterList as $chapter) {

                    if ($record['article_id'] == $chapter['article_id'] && $record['source_id'] == $chapter['source_id']) {
                        $data = [
                            'status' => $record['status'],
                            'updated' => $record['updated'],
                            'article_chapter_id' => $chapter['parent_id'],
                        ];
                        $recordServ->update($record['study_record_id'], $data);
                    }
                }
            }
        }

        exit('SUCCESS');
    }
}