ItemService.class.php 2.52 KB
<?php
/**
 * Created by PhpStorm.
 * User: liyifei2012it
 * Date: 18/3/28
 * Time: 15:51
 */
namespace Common\Service;

use Common\Model\ItemModel;

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

    /**
     * 评分项数量、评分类型是否有变化
     * @author liyifei
     * @param int $article_id 课程ID
     * @param array $data 评分项数据
     * @return bool
     */
    public function diffItem($article_id, $data)
    {
        if (empty($data) || !is_array($data)) {
            return false;
        }

        // 数据库数据
        $db_data = $this->list_by_conds(['article_id' => $article_id]);
        if (empty($db_data)) {
            return true;
        }

        // 评分项数量不可修改
        $item_ids = array_column($data, 'item_id');
        $db_item_ids = array_column($db_data, 'item_id');
        if (array_diff($item_ids, $db_item_ids) || count($item_ids) != count($db_item_ids)) {
            return false;
        }

        // 评分类型不可修改
        $db_data = array_combine_by_key($db_data, 'item_id');
        foreach ($data as $v) {
            $item_id = $v['item_id'];
            if ($v['score_type'] != $db_data[$item_id]['score_type']) {
                return false;
            }
        }

        return true;
    }

    /**
     * 保存评分设置项
     * @author liyifei
     * @param int $article_id 课程ID
     * @param array $score_item 评分项
     * @return bool
     */
    public function saveData($article_id, $score_item)
    {
        $insert_data = [];

        foreach ($score_item as $item) {
            // 新增数据
            if ($item['item_id'] == 0) {
                $insert_data[] = [
                    'article_id' => $article_id,
                    'content' => $item['content'],
                    'score_type' => $item['score_type'],
                    'score_order' => $item['score_order'],
                ];

            // 修改数据(发布原草稿模式的培训时,可以修改评分类型)
            } else {
                $data = [
                    'content' => $item['content'],
                    'score_type' => $item['score_type'],
                    'score_order' => $item['score_order'],
                ];
                $this->update($item['item_id'], $data);
            }
        }

        if (!empty($insert_data)) {
            $this->insert_all($insert_data);
        }
    }
}