input('id/d',0), 'name' => input('name','','trim'), 'level' => input('level','','trim'), 'content' => input('content','','trim'), 'account' => input('account','','trim'), 'parent_id' => input('parentId/d',0), ]; if($data['id'] > 0){ $result = validate('Org')->check($data,[],'edit'); }else{ $result = validate('Org')->check($data,[],'add'); } if(true !== $result){ HelpHander::error(validate('Org')->getError()); } $id = $data['id']; $account = $data['account']; unset($data['id']); unset($data['account']); $this->startTrans(); try{ if($id > 0){ $ret = $this->allowField(true)->save($data,['id'=>$id]); if(!$ret){ HelpHander::error('操作失败'); } }else{ // 获取机构代码 $pcode = Db::name('org')->where('id',$data['parent_id'])->value('code'); $maxcode = Db::name('org')->where('parent_id',$data['parent_id'])->order('id desc')->value('code'); if($maxcode){ $data['code'] = $maxcode + 1; }else{ $data['code'] = $pcode.'10'; } $ret = $this->allowField(true)->save($data); if(!$ret){ \exception('操作失败'); } // 创建默认角色 $roles = model('Roles')->addInitRoles($this->id); if($roles <= 0){ \exception('操作失败'); } // 创建用户 $res = model('User')->addUserOrg($account,$roles); if(!$res){ \exception('操作失败'); } } $this->commit(); }catch (Exception $e){ $this->rollback(); HelpHander::error('操作失败'); } return true; } public function info($id){ $info = $this->where('id',$id)->where('del',0)->find(); if(!$info){ HelpHander::error('数据不存在'); } // 获取上级组织 $pername = ''; if($info['parent_id'] > 0){ $pername = Db::name('org')->where('id',$info['parent_id'])->value('name'); } $info['pername'] = $pername; return $info->toArray(); } // 获取组织列表,只取未删除和未禁用的组织 public function lists($page,$size){ $lists = $this ->where('del',0) ->where('enable',1) ->page($page,$size) ->order('id asc') ->select(); return $lists?$lists->toArray():[]; } // 启用组织 public function toEnableOrg($orgId){ $info = $this->where('id',$orgId)->where('del',0)->find(); if(!$info){ HelpHander::error('组织不存在'); } if($info['enable'] == 1){ HelpHander::error('启用中,无需重复启用'); } $ret = $this->where('id',$orgId)->setField('enable',1); if(!$ret){ HelpHander::error('操作失败'); } return true; } // 作废组织(包括所有下级组织,部门和岗位) public function disableOrg($orgId){ $info = $this->where('id',$orgId)->where('del',0)->find(); if(!$info){ HelpHander::error('组织不存在'); } if($info['enable'] == 0){ HelpHander::error('组织已禁用'); } $orgs = $this->getOrgChildren($orgId); $orgs[] = $orgId; $this->startTrans(); try{ Db::name('org')->where('id','in',$orgs)->setField('enable',0); Db::name('dep')->where('org_id','in',$orgs)->setField('enable',0); Db::name('job')->where('org_id','in',$orgs)->setField('enable',0); $this->commit(); }catch (Exception $e){ $this->rollback(); HelpHander::error('操作失败'); } return true; } // 显示直属下级机构列表 public function selByOrgId($id){ $lists = Db::name('org') ->alias('o') ->join('org op','o.parent_id = op.id') ->where('o.del',0) ->where('o.parent_id',$id) ->order('o.id asc') ->field('op.id,op.name,o.name as nname,o.id as nid,o.parent_id as nparent_id,o.level as nlevel,o.content as ncontent,o.enable as nenable') ->select(); return $lists?$lists:[]; } // 获取组织的所有下级组织id(启用状态的) public function getOrgChildren($ids,&$result = []){ $children = Db::name('org') ->where('del',0) ->where('enable',1) ->where('parent_id','in',$ids) ->column('id'); if($children){ $result = array_merge($result,$children); $this->getOrgChildren($children,$result); } return $result; } // 根据用户ID获取组织列表 public function queryOrgListByUserId($userId){ $list = Db::name('user_roles') ->alias('ur') ->join('roles r','r.id = ur.roles_id') ->join('org o','o.id = r.org_id') ->where('r.type',3) ->where('ur.user_id',$userId) ->field('o.id,o.name') ->select(); return $list?$list:[]; } }