ArticleChapterService.class.php 6.38 KB
<?php
/**
 * Created by PhpStorm.
 * User: liyifei2012it
 * Date: 17/4/12
 * Time: 10:46
 */
namespace Common\Service;

use Common\Model\ArticleChapterModel;

class ArticleChapterService extends AbstractService
{
    // 构造方法
    public function __construct()
    {
        parent::__construct();
        $this->_d = new ArticleChapterModel();
    }

    /**
     * 格式化章节数据
     * @author liyifei
     * @param Int $article_id 课程ID
     * @return array
     */
    public function formatDBData($article_id)
    {
        $conds = ['article_id' => $article_id];
        $orders = [
            'parent_id' => 'asc',
            '`order`' => 'asc',
        ];
        $chapter_list = $this->list_by_conds($conds, null, $orders);
        if (empty($chapter_list)) {

            return [];
        }

        $formatData = [];
        foreach ($chapter_list as $chapter) {

            if ($chapter['parent_id'] == 0) {

                $formatData[$chapter['article_chapter_id']] = [
                    'article_chapter_id' => $chapter['article_chapter_id'],
                    'chapter_name' => $chapter['chapter_name'],
                    'order' => $chapter['order'],
                    'source' => [],
                ];
            }

            if ($chapter['parent_id'] > 0) {

                $formatData[$chapter['parent_id']]['source'][$chapter['article_chapter_id']] = [
                    'article_chapter_id' => $chapter['article_chapter_id'],
                    'source_id' => $chapter['source_id'],
                    'et_ids' => empty($chapter['et_ids']) ? [] : unserialize($chapter['et_ids']),
                    'rand_num' => $chapter['rand_num'],
                    'order' => $chapter['order'],
                ];
            }
        }

        return $formatData;
    }

    /**
     * 保存课程、章节关系
     * @author liyifei
     * @param int $article_id 课程ID
     * @param array $chapters 章节信息
     * @return bool
     */
    public function saveData($article_id, $chapters)
    {
        // 筛选出被删除的章节
        $chapters_old = $this->formatDBData($article_id);
        if (!empty($chapters_old)) {
            // 筛选出被删除的章节
            $chapter_id_old = array_column($chapters_old, 'article_chapter_id');
            $chapter_id_new = array_unique(array_column($chapters, 'article_chapter_id'));
            // 新章节不参与对比(即章节ID:article_chapter_id=0)
            $unset_key = array_search(0, $chapter_id_new);

            if ($unset_key !== false) {

                unset($chapter_id_new[$unset_key]);
            }

            $chapter_id_new = array_values($chapter_id_new);
            $delete_chapter_ids = array_diff($chapter_id_old, $chapter_id_new);

            // 对比相同章节的素材
            foreach ($chapters as $chapter) {

                foreach ($chapters_old as $chapter_old) {
                    // 跳过将被删除的章节
                    if (!empty($delete_chapter_ids) && in_array($chapter_old['article_chapter_id'], $delete_chapter_ids)) {

                        continue;
                    }

                    if ($chapter['article_chapter_id'] == $chapter_old['article_chapter_id']) {
                        // 筛选出被删除的素材
                        $source_key_old = array_column($chapter_old['source'], 'article_chapter_id');
                        $source_key_new = array_column($chapter['source'], 'article_chapter_id');

                        // 新素材不参与对比(即素材主键ID:article_chapter_id=0)
                        $unset_key = array_search(0, $source_key_new);
                        if ($unset_key !== false) {

                            unset($source_key_new[$unset_key]);
                        }

                        $source_key_new = array_values($source_key_new);
                        $diff_chapter_ids = array_diff($source_key_old, $source_key_new);

                        $delete_chapter_ids = array_merge($delete_chapter_ids, $diff_chapter_ids);
                    }
                }
            }
        }

        // 删除章节和素材
        if (!empty($delete_chapter_ids)) {
            // 删除素材
            $this->delete_by_conds(['parent_id' => $delete_chapter_ids]);
            // 删除章节、素材
            $this->delete($delete_chapter_ids);
        }

        // 新增、修改章节
        $insert_data = [];
        foreach ($chapters as $chapter) {

            $article_chapter_id = $chapter['article_chapter_id'];
            // 新增章节,获取章节ID
            if ($article_chapter_id == 0) {

                $article_chapter_id = $this->insert([
                    'article_id' => $article_id,
                    'parent_id' => 0,
                    'source_id' => 0,
                    'chapter_name' => $chapter['chapter_name'],
                    'order' => $chapter['order'],
                ]);
            } else { // 修改章节数据

                $chapter_data = [
                    'article_id' => $article_id,
                    'parent_id' => 0,
                    'source_id' => 0,
                    'chapter_name' => $chapter['chapter_name'],
                    'order' => $chapter['order'],
                ];
                $this->update_by_conds(['article_chapter_id' => $chapter['article_chapter_id']], $chapter_data);
            }

            // 组合写入素材数据的sql
            foreach ($chapter['source'] as $source) {

                $source_data = [
                    'article_id' => $article_id,
                    'parent_id' => $article_chapter_id,
                    'source_id' => $source['source_id'],
                    'chapter_name' => $chapter['chapter_name'],
                    'et_ids' => empty($source['et_ids']) ? '' : serialize($source['et_ids']),
                    'rand_num' => $source['rand_num'],
                    'order' => intval($source['order']),
                ];

                // 新增素材
                if ($source['article_chapter_id'] == 0) {

                    $insert_data[] = $source_data;
                } else { // 修改素材

                    $this->update_by_conds(['article_chapter_id' => $source['article_chapter_id']], $source_data);
                }
            }
        }

        if (!empty($insert_data)) {

            $this->insert_all($insert_data);
        }

        return true;
    }
}