save([ 'name' => $name, 'level' => $level, 'pid' => $pid ]); $nowid = $org_obj->id; if ($pid > 0) { $parentnode = Org::find($pid); $nowpath = $parentnode->path . $nowid . '-'; $nowinfo = $parentnode->info . '/' . $name; } else { $nowpath = '1-'; $nowinfo = $name; } if ($org_obj->save(['path' => $nowpath, 'info' => $nowinfo])) { return $org_obj; } else { return false; } } /** * disnode 关闭节点。如果有子节点则全部关闭 * * @params * $id integer 要关闭的组织结构节点 */ public static function disnode($id, &$msg) { $tragetnode = Org::find($id); $thepath = $tragetnode->path; // 查找结点下是否有员工 $orgids = Org::where([['path', 'like', $thepath . '%']])->column('id'); // 如果有子部门,则先删除子部门再进行该部门删除 if (count($orgids) > 1) { $msg = '删除失败,尚有子部门存在'; return 1; } $had = Employee::where([['org_id', 'in', $orgids], ['state', 'in', ['在职', '待审核']]])->count(); if ($had) { $msg = '删除失败,尚有人员在职或待审核'; return 1; } // 检测是否有资源 $customerResource = Customer::where(['org_id' => $id])->count(); $clueResource = CustomerClue::where(['org_id' => $id])->count(); $footprintResource = Footprints::where(['org_id' => $id])->count(); if ($customerResource !== 0 || $clueResource !== 0 || $footprintResource !== 0) { $msg = '该部门尚有客户资源,请选择接收部门'; return 2; } $allsubnodeschangenum = Org::where([ ['path', 'like', $thepath . '%'] ])->delete(); if ($allsubnodeschangenum > 0) { return true; } else { $msg = '删除失败'; return 1; } } public static function level() { $where[] = ['status', '=', 1]; $levelArr = Org::where($where)->column('level'); return array_unique($levelArr); } public static function levelnodes($level) { $where[] = ['status', '=', 1]; $where[] = ['level', '=', $level]; $levelArr = Org::where($where)->select()->toArray(); foreach ($levelArr as &$item) { $item['haspool'] = false; $item['poolname'] = ''; $poolobj = Pool::where('org_id', $item['id'])->find(); if ($poolobj) { $item['haspool'] = true; $item['poolname'] = $poolobj->name; } } return $levelArr; } public static function subnodes($id) { $theorg = Org::find($id); $suborgs = []; if ($theorg) { $suborgs = Org::where([['path', 'like', $theorg->path . '%'], ['status', '=', 1]])->select()->toArray(); } return $suborgs; } public static function subfirstlevelnodes($id) { $suborgs = Org::where([['pid', '=', $id], ['status', '=', 1]])->select()->toArray(); return $suborgs; } public static function subfirstlevelnodeswithempnum($id) { $suborgs = Org::where([['pid', '=', $id], ['status', '=', 1]])->select()->toArray(); foreach ($suborgs as &$item) { $orgids = orgSubIds($item['id']); $allempnum = Employee::where([['org_id', 'in', $orgids],['state','=','在职']])->count(); $item['allempnum'] = $allempnum; } //$theorg = Org::find($id); //$suborgs = []; //if ($theorg) { // $suborgs = Org::where([['path', 'like', $theorg->path . '%'],['status','=',1]])->with('employee')->select()->toArray(); // foreach($suborgs as &$item){ // $item['empnum'] = count($item['employee']); // } //} return $suborgs; } public static function mystruc($orgids) { $where = [ ['status', '=', 1], ['id', 'in', $orgids] ]; $allnodes = Org::where($where)->order('level desc, id asc')->select(); self::thestruclooporigin($allnodes, $arr); return [$arr]; //return json_encode([$arr]); } public static function struc($id) { $where = [ ['path', 'like', $id . '-%'], ['status', '=', 1] ]; $allnodes = Org::where($where)->order('level desc, id asc')->select(); //查询个部门人数 $count = Employee::where([['root_id','=',$id],['state','like','%在职%'], ['uid', '<>', '']])->group('org_id')->column('count(*) count','org_id'); foreach ($allnodes as $k => $v) { $allnodes[$k]['count'] = isset($count[$v['id']]) ? $count[$v['id']] : 0;//本部门人数 $allnodes[$k]['all_count'] = 0;//包含子部门总数 foreach ($allnodes as $k2 => $v2) { if (strpos($v2['path'],$v['path']) !== false) { $allnodes[$k]['all_count'] = isset($count[$v2['id']]) ? $count[$v2['id']]+$allnodes[$k]['all_count'] : $allnodes[$k]['all_count']; } } // $allnodes[$k]['name'] = $allnodes[$k]['name'].' ('.$allnodes[$k]['all_count'].')'; } self::thestruclooporigin($allnodes, $arr); return json_encode([$arr]); } private static function thestruclooporigin($allnodes, &$arr) { $length = count($allnodes); for ($i = 0; $i < $length; $i++) { $item = [ 'title' => $allnodes[$i]->name, 'spread' => true, 'id' => $allnodes[$i]->id, 'level' => $allnodes[$i]->level, 'pid' => $allnodes[$i]->pid, 'info' => $allnodes[$i]->info, 'all_count' => $allnodes[$i]->all_count, 'org_type' => $allnodes[$i]->org_type ]; if (isset($arr[$item['id']])) { $item['children'] = $arr[$item['id']]; unset($arr[$item['id']]); } else { $item['children'] = []; } if ($i !== $length - 1) { $arr[$item['pid']][] = $item; } else { $arr = $item; } } } }