RangeModel.class.php 2.83 KB
<?php
/**
 * 直播对象范围表
 * @author: houyingcai
 * @email:    594609175@qq.com
 * @date :  2018-01-08 16:01:17
 * @version $Id$
 */

namespace Common\Model;

use Common\Common\Constant;

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

    /**
     * 根据条件读取数据数组
     *
     * @param array $conds 条件数组
     * @param string $fields 读取字段
     *
     * @return array|bool
     */
    public function list_by_rang_conds($conds, $fields = '*')
    {

        $params = array();
        // 条件
        $wheres = array();
        if (!$this->_parse_rang_where($wheres, $params, $conds)) {
            return false;
        }

        // 企业标记
        $wheres_prod[] = "(`domain`=?";
        $params[] = QY_DOMAIN;

        // 状态条件
        $wheres_prod[] = "`status`<?)";
        $params[] = $this->get_st_delete();

        // 读取记录
        $sql = "SELECT {$fields} FROM __TABLE__ ";

        $sql .= 'WHERE ' . implode(' OR ', $wheres) . ' AND ' . implode(' AND ', $wheres_prod);

        return $this->_m->fetch_array($sql, $params);
    }

    /**
     *
     * @param array $wheres
     * @param array $params
     * @param array $conds
     * @return bool
     */
    protected function _parse_rang_where(&$wheres = [], &$params = [], $conds = [])
    {

        // 全公司
        $wheres[] = '( type=? AND obj_id=? )';
        $params[] = Constant::RIGHT_TYPE_ALL;
        $params[] = Constant::RIGHT_TYPE_ALL;

        // 人员
        if (!empty($conds[Constant::RIGHT_TYPE_USER])) {
            $wheres[] = '( type=? AND obj_id IN(?) )';
            $params[] = Constant::RIGHT_TYPE_USER;
            $params[] = $conds[Constant::RIGHT_TYPE_USER];
        }
        // 职位
        if (!empty($conds[Constant::RIGHT_TYPE_JOB])) {
            $wheres[] = '( type=? AND obj_id IN(?) )';
            $params[] = Constant::RIGHT_TYPE_JOB;
            $params[] = $conds[Constant::RIGHT_TYPE_JOB];
        }

        // 角色
        if (!empty($conds[Constant::RIGHT_TYPE_ROLE])) {
            $wheres[] = '( type=? AND obj_id IN(?) )';
            $params[] = Constant::RIGHT_TYPE_ROLE;
            $params[] = $conds[Constant::RIGHT_TYPE_ROLE];
        }

        // 部门
        if (!empty($conds[Constant::RIGHT_TYPE_DEPARTMENT])) {
            $wheres[] = '( type=? AND obj_id IN(?) )';
            $params[] = Constant::RIGHT_TYPE_DEPARTMENT;
            $params[] = $conds[Constant::RIGHT_TYPE_DEPARTMENT];
        }

        // 如果存在tag
        if (!empty($conds[Constant::RIGHT_TYPE_TAG])) {
            $wheres[] = '( type=? AND obj_id IN(?) )';
            $params[] = Constant::RIGHT_TYPE_TAG;
            $params[] = $conds[Constant::RIGHT_TYPE_TAG];
        }

        return true;
    }

}