CustomtaskRightModel.class.php 3.19 KB
<?php
/**
 * Created by PhpStorm.
 * User: liyifei2012it
 * Date: 17/5/18
 * Time: 11:34
 */
namespace Common\Model;

use Common\Common\Constant;

class CustomtaskRightModel extends AbstractModel
{
    /**
     * 权限类型(1=全公司;2=部门;3=标签;4=人员;5=职位;6=角色)
     */
    const OBJ_TYPE_DEP = 2;
    const OBJ_TYPE_MEMBER = 4;
    const OBJ_TYPE_JOB = 5;
    const OBJ_TYPE_ROLE = 6;

    // 构造方法
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 根据权限数据构建查询条件
     * @author zhonglei
     * @param array $rights 权限数据
     * @return array
     */
    public function buildConds($rights)
    {
        $conds = [];

        if (!is_array($rights) || empty($rights)) {
            return $conds;
        }

        foreach ($rights as $k => $v) {
            switch ($k) {
                // 全公司
                case Constant::RIGHT_TYPE_ALL:
                    $conds[] = "`obj_type` = {$k}";
                    break;

                // 部门、标签、人员、职位、角色
                default:
                    $obj_id = implode("','", $v);
                    $conds[] = "(`obj_type` = {$k} and `obj_id` in ('{$obj_id}'))";
            }
        }

        return $conds;
    }

    /**
     * 根据权限数据捞取任务ID
     * @author tangxinguo
     * @param array $rights 权限数据
     * @param array $customtask_ids 权限数据
     * @return array
     */
    public function idByRight($rights, $customtask_ids = [])
    {
        $res = [];

        if (!is_array($rights) || empty($rights)) {
            return $res;
        }

        $conds = $this->buildConds($rights);
        $where = " `domain` = ? and `status` < ? and (" . implode(' OR ', $conds) . ")";
        if (!empty($customtask_ids)) {
            $where .= " and `customtask_id` in (" . implode(',', $customtask_ids) . ")";
        }
        $sql = "SELECT `customtask_id` FROM __TABLE__ WHERE {$where}";

        $params = [
            QY_DOMAIN,
            $this->get_st_delete(),
        ];

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

    /**
     * 没有企业标识的查询
     * @param        $conds
     * @param null   $page_option
     * @param array  $order_option
     * @param string $fields
     * @return array|bool
     */
    public function listWithOutDomian($conds, $page_option = null, $order_option = array(), $fields = '*')
    {
        $params = array();
        // 条件
        $wheres = array();
        if (!$this->_parse_where($wheres, $params, $conds)) {
            return false;
        }
        // 状态条件
        $wheres[] = "`{$this->prefield}status`<?";
        $params[] = $this->get_st_delete();

        // 排序
        $orderby = '';
        if (!$this->_order_by($orderby, $order_option)) {
            return false;
        }
        // 分页参数
        $limit = '';
        if (!$this->_limit($limit, $page_option)) {
            return false;
        }
        // 读取记录
        return $this->_m->fetch_array("SELECT {$fields} FROM __TABLE__ WHERE " . implode(' AND ',
                $wheres) . "{$orderby}{$limit}", $params);
    }
}