PathContentModel.class.php 2.68 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhonglei
 * Date: 18/4/24
 * Time: 11:57
 */

namespace Common\Model;

class PathContentModel extends AbstractModel
{
    /**
     * 构造方法
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 根据应用获取应用数据ID数组
     * @author zhonglei
     * @param string $app 应用
     * @param int $path_id 路径ID
     * @return array
     */
    public function getAppDataIds($app, $path_id = 0)
    {
        if (empty($app)) {
            return [];
        }

        $wheres = [];
        $params = [];
        $this->_parse_where($wheres, $params, ['app' => strtolower($app)]);

        // 路径ID
        if ($path_id) {
            $wheres[] = "`{$this->prefield}path_id`!=?";
            $params[] = $path_id;
        }
        // 企业标记
        $wheres[] = "`{$this->prefield}domain`=?";
        $params[] = QY_DOMAIN;
        // 状态条件
//        $wheres[] = "`{$this->prefield}status`<?";
//        $params[] = $this->get_st_delete();

        $wheres_sql = implode(' AND ', $wheres);
        $sql = "SELECT DISTINCT `app_data_id` FROM __TABLE__ WHERE {$wheres_sql}";
        $list = $this->_m->fetch_array($sql, $params);

        return empty($list) ? [] : array_column($list, 'app_data_id');
    }

    /**
     * 验证学习内容是否已被其他路径使用
     * @param array $data 使用的学习内容
     * @param int $pathId 路径ID
     * @return array
     */
    public function getUsePathList($data, $pathId = 0)
    {
        if (empty($data)) {
            return [];
        }

        $orWhere = [];
        foreach ($data as $key => $value) {
            // 当为课程是不做筛选
            if ($value['app'] == 'course') {
                continue;
            }
            if ($value['app'] && $value['app_data_id']) {
                $orWhere[] = "(`{$this->prefield}app`='{$value['app']}' AND `{$this->prefield}app_data_id`={$value['app_data_id']})";
            }
        }
        $orWhere = implode(' OR ', $orWhere);
        if (empty($orWhere)) {
            return [];
        }

        // 路径ID
        if ($pathId) {
            $wheres[] = "`{$this->prefield}path_id`!=?";
            $params[] = $pathId;
        }

        // 企业标记
        $wheres[] = "`{$this->prefield}domain`=?";
        $params[] = QY_DOMAIN;
        // 状态条件
//        $wheres[] = "`{$this->prefield}status`<?";
//        $params[] = $this->get_st_delete();

        $wheres_sql = implode(' AND ', $wheres);
        $sql = "SELECT * FROM __TABLE__ WHERE {$wheres_sql} AND ({$orWhere})";

        $list = $this->_m->fetch_array($sql, $params);

        return $list;
    }
}