123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- namespace app\logics;
- use app\model\Customer;
- use app\model\CustomerClue;
- use app\model\Employee;
- use app\model\Footprints;
- use app\model\Org;
- use app\model\Pool;
- use think\facade\Db;
- class OrgLogic
- {
- /*
- * addnode 添加组织结构节点
- * @params
- * $name string 节点名称
- * $level integer 层级数
- * $pid integer 上一级id,即父id
- */
- public static function addnode($name = '', $level = 1, $pid = 0)
- {
- $org_obj = new Org();
- $org_obj->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;
- }
- }
- }
- }
|