model = new CampusPersonRole(); } /** * 获取角色关系列表 * @param array $where * @return array */ public function getPage(array $data = []) { $field = 'a.*,b.phone'; $order = 'a.id desc'; $where = []; if($data['campus_id']){ $where[] = ['a.campus_id','=',$data['campus_id']]; } if($data['role_id']){ $where[] = ['a.role_id','=',$data['role_id']]; } if($data['dept_id']){ $where[] = ['a.dept_id','=',$data['dept_id']]; } if($data['person_name']){ $where[] = ['b.name','like','%'.$data['person_name'].'%']; } $search_model = $this->model ->alias("a") ->join(['school_personnel' => 'b'],'a.person_id = b.id','left') ->where($where) ->where('b.deleted_at', 0) // 只显示未删除的人员 ->with(['campus','personnel','sysRole','departments']) ->field($field)->order($order); $search_model->where(get_campus_where($this->uid,'campus_id')); $list = $this->pageQuery($search_model); return $list; } /** * 获取角色关系信息 * @param int $id * @return array */ public function getInfo(int $id) { $personnel_summary = new PersonnelSummary(); $field = 'id,campus_id,person_id,role_id,dept_id,created_at,updated_at,deleted_at'; $info = $this->model->field($field)->where([['id', "=", $id]])->with(['campus','personnel','sysRole','departments'])->findOrEmpty()->toArray(); $info['tasks'] = $personnel_summary->where(['campus_person_role_id' => $info['id']])->order("id desc")->findOrEmpty()->toArray(); return $info; } /** * 添加角色关系 * @param array $data * @return mixed */ public function add(array $data) { if($this->model->where(['person_id' => $data['person_id']])->find()){ return fail("重复操作"); } $db = app()->db; $data['dept_id'] = $db->table('school_sys_role')->where('role_id', $data['role_id'])->value('dept_id'); $res = $this->model->create($data); return success("操作成功"); } /** * 角色关系编辑 * @param int $id * @param array $data */ public function edit(int $id, array $data) { $tasks = $data['tasks']; unset($data['tasks']); // if($this->model->where([['id', '<>', $id]])->where(['person_id' => $data['person_id']])->find()){ // return fail("重复操作"); // } if($tasks){ $personnel_summary = new PersonnelSummary(); $personnel_summary->where(['id' => $tasks['id']])->update(['task_num' => $tasks['task_num']]); } // 如果角色ID为空,删除该用户的所有角色关系 if (empty($data['role_id']) || $data['role_id'] == 0) { // 获取当前记录的person_id $currentRecord = $this->model->where([['id', '=', $id]])->find(); if ($currentRecord) { // 删除该人员的所有角色关系 $this->model->where([['person_id', '=', $currentRecord->person_id]])->delete(); return success("已移除该人员的所有角色关系"); } } else { // 有角色ID时,正常更新 $data['dept_id'] = Db::table('school_sys_role')->where('role_id', $data['role_id'])->value('dept_id'); $this->model->where([['id', '=', $id]])->update($data); } return success("操作成功"); } /** * 删除角色关系 * @param int $id * @return bool */ public function del(int $id) { $model = $this->model->where([['id', '=', $id]])->find(); $res = $model->delete(); return $res; } public function getCampusAll(){ $campusModel = new Campus(); return $campusModel->select()->toArray(); } public function getPersonnelAll(){ $person_ids = $this->model->column("person_id"); $personnelModel = new Personnel(); return $personnelModel->where([['id','not in',$person_ids]])->select()->toArray(); } public function getSysRoleAll($data){ // 直接查询school_sys_role表,过滤掉超级管理员(role_key为空或role_name为超级管理员) $db = app()->db; $query = $db->table('school_sys_role') ->where('status', 1) ->where(function ($query) { $query->whereNotNull('role_key') ->where('role_key', '<>', '') ->where('role_name', '<>', '超级管理员'); }); // 如果dept_id不为0,则按部门过滤;如果为0则显示所有可用角色 if (isset($data['dept_id']) && $data['dept_id'] > 0) { $query->where('dept_id', $data['dept_id']); } return $query->select()->toArray(); } public function getDepartmentsAll(){ $departmentsModel = new Departments(); return $departmentsModel->select()->toArray(); } }