| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345 | 
							- <?php
 
- namespace App\Service;
 
- use App\Model\BasicType;
 
- use App\Model\Depart;
 
- use App\Model\Employee;
 
- use App\Model\EmployeeDepartPermission;
 
- use App\Model\EmployeeManagerDepart;
 
- use App\Model\EmployeeMenuPermission;
 
- use App\Model\EmployeeRole;
 
- use App\Model\EmployeeTeamPermission;
 
- use App\Model\Role;
 
- use App\Model\RoleMenu;
 
- use App\Model\RoleMenuButton;
 
- use App\Model\Storehouse;
 
- use App\Model\SysMenu;
 
- use App\Model\SysMenuButton;
 
- use App\Model\Team;
 
- use Illuminate\Support\Facades\DB;
 
- use Illuminate\Support\Facades\Hash;
 
- use Mockery\Exception;
 
- /**
 
-  * 人员相关
 
-  * @package App\Models
 
-  */
 
- class EmployeeService extends Service
 
- {
 
-     /**
 
-      * 用户编辑
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function employeeEdit($data,$user){
 
-         list($status,$msg) = $this->employeeRule($data,false);
 
-         if(!$status) return [$status,$msg];
 
-         try {
 
-             DB::beginTransaction();
 
-             $model = new Employee();
 
-             $model = $model->where('id',$data['id'])->first();
 
-             $model->number = $data['number'];
 
-             $model->emp_name = $data['emp_name'];
 
-             $model->mobile = $data['mobile'] ?? '';
 
-             $model->leave_time = $data['leave_time'] ?? '';
 
-             $model->entry_time = $data['entry_time'] ?? '';
 
-             $model->state = empty($data['leave_time']) ? Employee::USE : Employee::NOT_USE;
 
-             $model->is_admin = $data['is_admin'];
 
-             if($model->is_admin == 1){
 
-                 $model->account = $data['number'];
 
-                 if($data['password'] !== '******'){
 
-                     $model->password   = Hash::make($data['password']);
 
-                 }
 
-             }
 
-             $model->save();
 
-             EmployeeDepartPermission::where('employee_id',$data['id'])->delete();
 
-             if(isset($data['depart'])){
 
-                 $insert = [];
 
-                 foreach ($data['depart'] as $value){
 
-                     $insert[] = [
 
-                         'employee_id' => $model->id,
 
-                         'depart_id' => $value,
 
-                     ];
 
-                 }
 
-                 EmployeeDepartPermission::insert($insert);
 
-             }
 
-             EmployeeRole::where('employee_id',$data['id'])->update([
 
-                 'del_time' => time()
 
-             ]);
 
-             if(isset($data['role'])){
 
-                 $insert = [];
 
-                 foreach ($data['role'] as $value){
 
-                     $insert[] = [
 
-                         'employee_id' => $model->id,
 
-                         'role_id' => $value,
 
-                         'crt_time' => time(),
 
-                         'upd_time' => time(),
 
-                     ];
 
-                 }
 
-                 EmployeeRole::insert($insert);
 
-             }
 
-             DB::commit();
 
-         }catch (\Exception $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getMessage()];
 
-         }
 
-         return [true,''];
 
-     }
 
-     /**
 
-      * 用户新增
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function employeeAdd($data,$user){
 
-         list($status,$msg) = $this->employeeRule($data);
 
-         if(!$status) return [$status,$msg];
 
-         try{
 
-             DB::beginTransaction();
 
-             $model = new Employee();
 
-             $model->number = $data['number'];
 
-             $model->emp_name = $data['emp_name'];
 
-             $model->mobile = $data['mobile'] ?? '';
 
-             $model->leave_time = $data['leave_time'] ?? '';
 
-             $model->entry_time = $data['entry_time'] ?? '';
 
-             $model->state = empty($data['leave_time']) ? Employee::USE : Employee::NOT_USE;
 
-             $model->crt_id = $user['id'];
 
-             $model->is_admin = $data['is_admin'];
 
-             if($model->is_admin == 1){
 
-                 $model->account = $data['number'];
 
-                 if($data['password'] !== '********'){
 
-                     $model->password = Hash::make($data['password']);
 
-                 }
 
-             }
 
-             $model->save();
 
-             if(isset($data['depart'])){
 
-                 $insert = [];
 
-                 foreach ($data['depart'] as $value){
 
-                     $insert[] = [
 
-                         'employee_id' => $model->id,
 
-                         'depart_id' => $value,
 
-                     ];
 
-                 }
 
-                 EmployeeDepartPermission::insert($insert);
 
-             }
 
-             if(isset($data['role'])){
 
-                 $insert = [];
 
-                 foreach ($data['role'] as $value){
 
-                     $insert[] = [
 
-                         'employee_id' => $model->id,
 
-                         'role_id' => $value,
 
-                         'crt_time' => time(),
 
-                         'upd_time' => time(),
 
-                     ];
 
-                 }
 
-                 EmployeeRole::insert($insert);
 
-             }
 
-             DB::commit();
 
-         }catch (Exception $e){
 
-             DB::rollBack();
 
-             return [false, $e->getMessage()];
 
-         }
 
-         return [true,''];
 
-     }
 
-     /**
 
-      * 用户删除
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function employeeDel($data){
 
-         if($this->isEmpty($data,'id')) return [false,'请选择删除的数据!'];
 
-         Employee::whereIn('id',$data['id'])->update([
 
-             'del_time'=>time()
 
-         ]);
 
-         return [true,'删除成功'];
 
-     }
 
-     /**
 
-      * 用户列表
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function employeeList($data,$user){
 
-         $model = Employee::where('del_time',0)
 
-             ->select('number','mobile','emp_name','id','entry_time','leave_time','is_admin','state')
 
-             ->orderBy('id','desc');
 
-         if(! empty($data['depart'])) {
 
-             $employee_id = DB::table('employee_depart_permission')
 
-                 ->where("depart_id", $data['depart'])
 
-                 ->select("employee_id")
 
-                 ->get()->toArray();
 
-             $employee_id = array_column($employee_id,'employee_id');
 
-             $model->whereIn("id", $employee_id);
 
-         }else{
 
-             $employee_id = $this->getEmployee($user);
 
-             $model->whereIn('id',$employee_id);
 
-         }
 
-         if(! empty($data['number'])) $model->where('number', 'LIKE', '%'.$data['number'].'%');
 
-         if(! empty($data['emp_name'])) $model->where('emp_name', 'LIKE', '%'.$data['emp_name'].'%');
 
-         if(! empty($data['state'])) $model->where('state',$data['state']);
 
-         if(! empty($data['mobile'])) $model->where('mobile', 'LIKE', '%'.$data['mobile'].'%');
 
-         if(! isset($data['all_emp'])) $model->where('id','<>',Employee::SPECIAL_ADMIN);
 
-         if(! empty($data['role'])) {
 
-             $emp = EmployeeRole::where('role_id',$data['role'])
 
-                 ->where('del_time',0)
 
-                 ->select('employee_id')->get()->toArray();
 
-             $model->whereIn('id',array_column($emp,'employee_id'));
 
-         }
 
-         if($user['id'] != Employee::SPECIAL_ADMIN) $model->where('is_manager',0);
 
-         $list = $this->limit($model,'',$data);
 
-         //组织数据
 
-         $list = $this->organizationEmployeeData($list);
 
-         return [true , $list];
 
-     }
 
-     /**
 
-      * 用户数据组装
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function organizationEmployeeData($data) {
 
-         if (empty($data['data'])) return $data;
 
-         $res = DB::table('employee_role as a')
 
-             ->leftJoin('role as b','a.role_id','=','b.id')
 
-             ->where('a.del_time',0)
 
-             ->where('b.del_time',0)
 
-             ->whereIn("a.employee_id",array_column($data['data'],'id'))
 
-             ->select('a.employee_id','b.title','b.id')
 
-             ->get()->toArray();
 
-         $role = $role2 = [];
 
-         foreach ($res as $value){
 
-             if(isset($role[$value->employee_id])){
 
-                 $role[$value->employee_id] .= ',' . $value->title;
 
-             }else{
 
-                 $role[$value->employee_id] = $value->title;
 
-             }
 
-             $role2[$value->employee_id][] = $value->id;
 
-         }
 
-         $res = DB::table('employee_depart_permission as a')
 
-             ->select('a.employee_id','b.title','b.id')
 
-             ->join('depart as b','a.depart_id','=','b.id')
 
-             ->whereIn("a.employee_id",array_column($data['data'],'id'))
 
-             ->orderBy('b.id')
 
-             ->get()->toArray();
 
-         $depart_title = $depart_id = [];
 
-         foreach ($res as $value){
 
-             if(isset($depart_title[$value->employee_id])){
 
-                 $depart_title[$value->employee_id] .= ',' . $value->title;
 
-             }else{
 
-                 $depart_title[$value->employee_id] = $value->title;
 
-             }
 
-             $depart_id[$value->employee_id][] = $value->id;
 
-         }
 
-         foreach ($data['data'] as $key => $value){
 
-             $data['data'][$key]['role'] = $role2[$value['id']] ?? [];
 
-             $data['data'][$key]['role_name'] = $role[$value['id']] ?? '';
 
-             $data['data'][$key]['depart'] = $depart_id[$value['id']] ?? [];
 
-             $data['data'][$key]['depart_title'] = $depart_title[$value['id']] ?? '';
 
-         }
 
-         return $data;
 
-     }
 
-     //获取当前顶级部门下人员id
 
-     public function getEmployee($user){
 
-         $top_depart_id = $user['depart_top'][0] ?? [];
 
-         $top_depart_id = $top_depart_id['depart_id'] ?? 0;
 
-         $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
 
-         // 查找所有子级id
 
-         $childIds = $this->findChildIds($top_depart_id, $list);
 
-         $childIds[] = $top_depart_id;
 
-         $employee_id = EmployeeDepartPermission::whereIn('depart_id',$childIds)
 
-             ->select("employee_id")
 
-             ->get()->toArray();
 
-         return array_unique(array_column($employee_id,'employee_id'));
 
-     }
 
-     /**
 
-      * 用户参数规则
 
-      * @param $data
 
-      * @param $is_add
 
-      * @return array
 
-      */
 
-     public function employeeRule($data,$is_add = true){
 
-         if($this->isEmpty($data,'number')) return [false,'工号不存在!'];
 
-         if($this->isEmpty($data,'emp_name')) return [false,'姓名不存在!'];
 
-         $mobile = $data['mobile'] ?? "";
 
-         $number = $data['number'] ?? "";
 
-         if(! $is_add){
 
-             if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
 
-             $bool = Employee::where('del_time',0)
 
-                 ->where('id','<>',$data['id'])
 
-                 ->where(function ($query) use ($mobile, $number){
 
-                     $query->where('number', $number);
 
-                     $query->when(! empty($mobile), function ($query) use ($mobile) {
 
-                         return $query->orWhere('mobile', $mobile);
 
-                     });
 
-                 })->exists();
 
-         }else{
 
-             $bool = Employee::where('del_time',0)
 
-                 ->where(function ($query) use ($mobile, $number){
 
-                     $query->where('number', $number);
 
-                     $query->when(! empty($mobile), function ($query) use ($mobile) {
 
-                         return $query->orWhere('mobile', $mobile);
 
-                     });
 
-                 })->exists();
 
-         }
 
-         if($bool) return [false,'工号或手机号码已存在!'];
 
-         return [true,''];
 
-     }
 
-     /**
 
-      * 角色编辑
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function roleEdit($data,$user){
 
-         list($status,$msg) = $this->roleRule($data,$user, false);
 
-         if(!$status) return [$status,$msg];
 
-         $model = new Role();
 
-         $model = $model->where('id',$data['id'])->first();
 
-         $model->title = $data['title'];
 
-         $model->save();
 
-         return [true,'保存成功!'];
 
-     }
 
-     /**
 
-      * 角色新增
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function roleAdd($data,$user){
 
-         list($status,$msg) = $this->roleRule($data,$user);
 
-         if(!$status) return [$status,$msg];
 
-         $model = new Role();
 
-         $model->title = $data['title'] ;
 
-         $model->depart_id = $data['depart_id'] ?? 0;
 
-         $model->top_depart_id = $data['top_depart_id'] ?? 0;
 
-         $model->save();
 
-         return [true,'保存成功!'];
 
-     }
 
-     /**
 
-      * 角色删除
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function roleDel($data){
 
-         if($this->isEmpty($data,'id')) return [false,'ID必须!'];
 
-         $bool = EmployeeRole::where('del_time',0)
 
-             ->whereIn('role_id',$data['id'])
 
-             ->exists();
 
-         if($bool) return [false,'角色已绑定人员!'];
 
-         Role::where('id',$data['id'])->update([
 
-             'del_time' => time()
 
-         ]);
 
-         return [true,'删除成功'];
 
-     }
 
-     /**
 
-      * 角色列表
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function roleList($data,$user){
 
-         $model = new Role(['userData' => $user, 'search' => $data]);
 
-         $model = $model->where('del_time',0)
 
-             ->select('title','crt_time','id','upd_time')
 
-             ->orderBy('id','desc');
 
-         if(! empty($data['title'])) $model->where('title', 'LIKE', '%' . $data['title'] . '%');
 
-         $list = $this->limit($model,'',$data);
 
-         return [200,$list];
 
-     }
 
-     /**
 
-      * 角色参数规则
 
-      * @param $data
 
-      * @param $is_check
 
-      * @return array
 
-      */
 
-     public function roleRule(&$data,$user, $is_check = true){
 
-         if($this->isEmpty($data,'title')) return [false,'名称不能为空!'];
 
-         //所属部门 以及  顶级部门
 
-         if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
 
-         $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
 
-         if($is_check){
 
-             $bool = Role::where('title',$data['title'])
 
-                 ->where('top_depart_id',$data['top_depart_id'])
 
-                 ->where('del_time',0)
 
-                 ->exists();
 
-             if($bool) return [false,'角色名称已存在!'];
 
-         }else{
 
-             if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
 
-             $bool = Role::where('title',$data['title'])
 
-                 ->where('top_depart_id',$data['top_depart_id'])
 
-                 ->where('id','<>',$data['id'])
 
-                 ->where('del_time',0)
 
-                 ->exists();
 
-             if($bool) return [false,'角色名称已存在!'];
 
-         }
 
-         return [true,''];
 
-     }
 
-     /**
 
-      * 角色菜单更新
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function roleMenu($data){
 
-         if(empty($data['role_id']))  return [false,'角色不能为空!'];
 
-         if(empty($data['menu'])) return [false,'菜单数据不能为空!'];
 
-         DB::beginTransaction();
 
-         try {
 
-             RoleMenu::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
 
-             RoleMenuButton::where('del_time',0)->where('role_id',$data['role_id'])->update(['del_time' => time()]);
 
-             $insert = $insert2 = [];
 
-             foreach ($data['menu'] as $t){
 
-                 $insert[] = [
 
-                     'role_id' => $data['role_id'],
 
-                     'menu_id' => $t['menu_id'],
 
-                     'type' => $t['type'],
 
-                     'crt_time' => time()
 
-                 ];
 
-                 if(! empty($t['button'])){
 
-                     foreach ($t['button'] as $b){
 
-                         $insert2[] = [
 
-                             'role_id' => $data['role_id'],
 
-                             'menu_id' => $t['menu_id'],
 
-                             'button_id' => $b,
 
-                             'crt_time' => time()
 
-                         ];
 
-                     }
 
-                 RoleMenuButton::insert($insert2);
 
-                 }
 
-             }
 
-             RoleMenu::insert($insert);
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false,$exception->getMessage()];
 
-         }
 
-         return [true,'保存成功!'];
 
-     }
 
-     /**
 
-      * 角色详情
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function roleDetail($data){
 
-         if(empty($data['role_id'])) return [false,'请选择角色'];
 
-         $role = Role::where('id',$data['role_id'])
 
-             ->where('del_time',0)
 
-             ->select('id','title')
 
-             ->first();
 
-         if(empty($role)) return [false,'角色不存在或已被删除'];
 
-         $role = $role->toArray();
 
-         $menu = RoleMenu::where('role_id',$data['role_id'])
 
-             ->where('del_time',0)
 
-             ->select('menu_id','type')
 
-             ->get()->toArray();
 
-         $button = $this->fillRoleButton([$data['role_id']]);
 
-         foreach ($menu as $key => $value){
 
-             $menu[$key]['button'] = $button[$value['menu_id']] ?? [];
 
-         }
 
-         $role['menu'] = $menu;
 
-         return [true, $role];
 
-     }
 
-     /**
 
-      * 部门编辑
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function departEdit($data, $user){
 
-         list($status,$msg) = $this->departRule($data,$user,false);
 
-         if(!$status) return [$status,$msg];
 
-         $update = $msg['data'][0];
 
-         $model = new Depart();
 
-         $model->where('id',$data['id'])->update($update);
 
-         return [true,'保存成功!'];
 
-     }
 
-     /**
 
-      * 部门新增
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function departAdd($data,$user){
 
-         list($status,$msg) = $this->departRule($data,$user);
 
-         if(!$status) return [$status,$msg];
 
-         try {
 
-             DB::beginTransaction();
 
-             foreach ($msg['data'] as $value){
 
-                 $model = new Depart();
 
-                 $model->parent_id = $value['parent_id'];
 
-                 $model->title = $value['title'];
 
-                 $model->code = $value['code'];
 
-                 $model->is_main = $value['is_main'];
 
-                 $model->basic_type_id = $value['basic_type_id'] ?? 0;
 
-                 $model->save();
 
-                 $depart_id = $model->id;
 
-                 if(empty($depart_id)) {
 
-                     DB::rollBack();
 
-                     return [false,'部门新建失败'];
 
-                 }
 
-                 if(empty($value['parent_id'])){
 
-                     $m = new Storehouse();
 
-                     $m->title = $value['title'];
 
-                     $m->depart_id = $depart_id;
 
-                     $m->top_depart_id = $depart_id;
 
-                     $m->crt_id = $user['id'];
 
-                     $m->save();
 
-                     if(empty($m->id)) {
 
-                         DB::rollBack();
 
-                         return [false,'仓库生成失败'];
 
-                     }
 
-                     $employee = new Employee();
 
-                     $number = "admin" . $value['code'];
 
-                     $employee->number = $number;
 
-                     $employee->emp_name = $value['title'] . "管理员账号";
 
-                     $employee->entry_time = date('Y-m-d');
 
-                     $employee->state = 1;
 
-                     $employee->crt_id = $user['id'];
 
-                     $employee->is_admin = 1;
 
-                     $employee->account = $number;
 
-                     $employee->password = Hash::make("password");
 
-                     $employee->is_manager = 1;
 
-                     $employee->save();
 
-                     if(empty($employee->id)) {
 
-                         DB::rollBack();
 
-                         return [false,'管理员账号生成失败'];
 
-                     }
 
-                     $depart = new EmployeeDepartPermission();
 
-                     $depart->employee_id = $employee->id;
 
-                     $depart->depart_id = $depart_id;
 
-                     $depart->save();
 
-                     if(empty($depart->id)) {
 
-                         DB::rollBack();
 
-                         return [false,'管理员账号部门关联生成失败'];
 
-                     }
 
-                 }
 
-             }
 
-             DB::commit();
 
-         }catch (\Exception $exception){
 
-             DB::rollBack();
 
-             return [false,$exception->getMessage()];
 
-         }
 
-         return [true,'保存成功!'];
 
-     }
 
-     /**
 
-      * 部门删除
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function departDel($data){
 
-         list($status,$msg) = $this->checkDepartDel($data);
 
-         if(! $status) return [false, $msg];
 
-         Depart::whereIn('id',$data['id'])->update([
 
-             'del_time'=>time()
 
-         ]);
 
-         return [true,'删除成功'];
 
-     }
 
-     /**
 
-      * 判断部门是否可以删除
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function checkDepartDel($data){
 
-         if($this->isEmpty($data,'id')) return [false,'ID必须!'];
 
-         $bool = Depart::whereIn('parent_id',$data['id'])->where('del_time',0)->exists();
 
-         if($bool) return [false,'部门下有子部门!'];
 
-         if($this->checkDepartHasPerson($data['id'])) return [false,'部门下有人员档案!'];
 
-         return [true, ''];
 
-     }
 
-     /**
 
-      * 部门列表
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function departList($data,$user){
 
-         $model = Depart::where('del_time',0)
 
-             ->select('title','id','code','parent_id','is_main','basic_type_id')
 
-             ->orderby('code', 'asc');
 
-         if($user['id'] != Employee::SPECIAL_ADMIN && ! $user['is_all_depart']){
 
-             $depart_id = $this->getDepartIdList($user);
 
-             $model->whereIn('id',$depart_id);
 
-         }
 
-         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
 
-         if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
 
-         $list = $model->get()->toArray();
 
-         $list = $this->fillDepartList($list, $user);
 
-         $list_tree = $list;
 
-         if(! empty($list_tree)) {
 
-             $list_tree = $this->makeTree(0,$list_tree);
 
-             $list_tree = $this->set_sort_circle($list_tree);
 
-         }
 
-         return [200,['data' => $list,'tree' => $list_tree]];
 
-     }
 
-     public function fillDepartList($list,$user){
 
-         if(empty($list)) return $list;
 
-         $basic = BasicType::where('del_time',0)
 
-             ->whereIn('id', array_unique(array_column($list,'basic_type_id')))
 
-             ->pluck('title','id')->toArray();
 
-         foreach ($list as $key => $value){
 
-             $list[$key]['basic_type_title'] = $basic[$value['basic_type_id']] ?? '';
 
-             $list[$key]['is_show_basic_type'] = $user['is_all_depart'];
 
-         }
 
-         return $list;
 
-     }
 
-     //获取可见的部门范围
 
-     public function getDepartIdList($user){
 
-         $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
 
-         $result = [];
 
-         foreach ($user['depart_range'] as $v){
 
-             // 查找所有父级id
 
-             $parentIds = $this->findParentIds($v, $list);
 
-             // 查找所有子级id
 
-             $childIds = $this->findChildIds($v, $list);
 
-             // 合并父级和子级id
 
-             $tmp = array_merge($parentIds, $childIds, [$v]);
 
-             $result = array_merge($result,$tmp);
 
-         }
 
-         return array_unique($result);
 
-     }
 
-     /**
 
-      * 部门参数规则
 
-      * @param $data
 
-      * @param $is_check
 
-      * @return array
 
-      */
 
-     public function departRule($data,$user, $is_check = true){
 
-         if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
 
-         $code = array_column($data['data'],'code');
 
-         $title = array_column($data['data'],'title');
 
-         $code = array_map(function($val) {
 
-             return $val !== null ? $val : 0;
 
-         }, $code);
 
-         $title = array_map(function($val) {
 
-             return $val !== null ? $val : 0;
 
-         }, $title);
 
-         $code_count = array_count_values($code);
 
-         $title_count = array_count_values($title);
 
-         foreach ($code as $value){
 
-             if(empty($value)) return [false,'编码不能为空!'];
 
-             if($code_count[$value] > 1) return [false,'编码不能重复'];
 
-         }
 
-         foreach ($title as $value){
 
-             if(empty($value)) return [false,'名称不能为空!'];
 
-             if($title_count[$value] > 1) return [false,'名称不能重复'];
 
-         }
 
-         $count = 0;
 
-         foreach ($data['data'] as $value){
 
-             if(empty($value['parent_id']) && ! empty($value['is_main'])) $count ++;
 
-         }
 
-         if($count > 1) return [false,'顶级总社只允许存在一个!'];
 
-         if($count == 1){
 
-             $id = $data['id'] ?? 0;
 
-             $bool = Depart::where('del_time',0)
 
-                 ->where('parent_id',0)
 
-                 ->where('is_main',1)
 
-                 ->when(! empty($id), function ($query) use ($id) {
 
-                     return $query->where('id', '<>',$id);
 
-                 })
 
-                 ->exists();
 
-             if($bool) return [false,'顶级总社只允许存在一个!'];
 
-         }
 
-         foreach ($data['data'] as $key => $value){
 
-             if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = 0;
 
-             $data['data'][$key]['upd_time'] = time();
 
-             //Depart::whereRaw("(binary code = '{$value['code']}' OR title = '{$value['title']}')")
 
-             if($is_check){
 
-                 if(empty($user['is_all_depart']) && empty($value['parent_id'])) return [false,'上级部门必须选择'];
 
-                 $data['data'][$key]['crt_time'] = time();
 
-                 $bool = Depart::whereRaw("binary code = '{$value['code']}'")
 
-                     ->where('del_time',0)
 
-                     ->exists();
 
-             }else{
 
-                 if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
 
-                 $bool = Depart::whereRaw("binary code = '{$value['code']}'")
 
-                     ->where('id','<>',$data['id'])
 
-                     ->where('del_time',0)
 
-                     ->exists();
 
-             }
 
-             if($bool) return [false,'编码不能重复'];
 
-         }
 
-         return [true, $data];
 
-     }
 
-     /**
 
-      * 检测部门下是否存在人员
 
-      * @param $depart_id
 
-      * @return false
 
-      */
 
-     public function checkDepartHasPerson($depart_id = []){
 
-         if(empty($depart_id)) return false;
 
-         $bool = EmployeeDepartPermission::from('employee_depart_permission as a')
 
-             ->leftJoin('employee as b','b.id','a.employee_id')
 
-             ->where('b.del_time',0)
 
-             ->whereIn('a.depart_id',$depart_id)
 
-             ->exists();
 
-         return $bool;
 
-     }
 
-     public function departSet($data,$user){
 
-         if(empty($data['id'])) return [false,'请选择部门'];
 
-         if(empty($data['grade']) || ! is_numeric($data['grade'])) return [false,'请输入分社等级'];
 
-         try {
 
-             DB::beginTransaction();
 
-             Depart::where('id',$data['id'])->update(['grade' => $data['grade']]);
 
-             $time = time();
 
-             DepartPriceName::where('del_time',0)->where('depart_id',$data['id'])->update([
 
-                 'del_time' => $time
 
-             ]);
 
-             if(! empty($data['title'])){
 
-                 $insert = [];
 
-                 foreach ($data['title'] as $value){
 
-                     $insert[] = [
 
-                         'depart_id' => $data['id'],
 
-                         'title' => $value,
 
-                         'crt_time' => $time,
 
-                     ];
 
-                 }
 
-                 DepartPriceName::insert($insert);
 
-             }
 
-             DB::commit();
 
-         }catch (\Exception $exception){
 
-             DB::rollBack();
 
-             return [false,$exception->getMessage()];
 
-         }
 
-         return [true,''];
 
-     }
 
-     /**
 
-      * 班组编辑
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function teamEdit($data){
 
-         list($status,$msg) = $this->teamRule($data,false);
 
-         if(!$status) return [$status,$msg];
 
-         $model = new Team();
 
-         $model = $model->where('id',$data['id'])->first();
 
-         $model->title = $data['title'];
 
-         $model->code = $data['code'];
 
-         $model->save();
 
-         return [true,'保存成功!'];
 
-     }
 
-     /**
 
-      * 班组新增
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function teamAdd($data,$user){
 
-         list($status,$msg) = $this->teamRule($data);
 
-         if(!$status) return [$status,$msg];
 
-         $model = new Team();
 
-         $model->title = $data['title'] ;
 
-         $model->code = $data['code'];
 
-         $model->save();
 
-         return [true,'保存成功!'];
 
-     }
 
-     /**
 
-      * 班组删除
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function teamDel($data){
 
-         if($this->isEmpty($data,'id')) return [false,'ID必须!'];
 
-         Team::where('id',$data['id'])->update([
 
-             'del_time'=>time()
 
-         ]);
 
-         return [true,'删除成功'];
 
-     }
 
-     /**
 
-      * 班组列表
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function teamList($data){
 
-         $list = Team::where('del_time',0)
 
-             ->select('title','id','crt_time','upd_time','code')
 
-             ->orderBy('id','desc');
 
-         $list = $this->limit($list,'',$data);
 
-         return [200,$list];
 
-     }
 
-     /**
 
-      * 班组参数规则
 
-      * @param $data
 
-      * @param $is_add
 
-      * @return array
 
-      */
 
-     public function teamRule($data,$is_add = true){
 
-         if($this->isEmpty($data,'title')) return [false,'名称不存在!'];
 
-         if($this->isEmpty($data,'code')) return [false,'编码不存在'];
 
-         $model = Team::where('title',$data['title'])
 
-             ->where('code',$data['code'])
 
-             ->where('del_time',0);
 
-         if(! $is_add){
 
-             if($this->isEmpty($data,'id')) return [false,'ID不能为空'];
 
-             $model->where('id','<>',$data['id']);
 
-         }
 
-         $bool = $model->exists();
 
-         if($bool) return [false,'名称和编码已存在!'];
 
-         return [true,''];
 
-     }
 
-     /**
 
-      * 班组详情
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function teamDetail($data){
 
-         if($this->isEmpty($data,'id')) return [false,'ID不能为空!'];
 
-         $result = EmployeeTeamPermission::from('employee_team_permission as a')
 
-             ->leftJoin('employee as b','b.id','a.employee_id')
 
-             ->where('team_id',$data['id'])
 
-             ->select('b.id','b.emp_name','b.number as code')
 
-             ->get()->toArray();
 
-         return [true,$result];
 
-     }
 
-     /**
 
-      * 人员权限
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function employeeRole($data){
 
-         $role_ids = [];
 
-         $employee_ids = [];
 
-         foreach ($data as $v){
 
-             if(isset($v['role_id'])){
 
-                 if(!in_array($v['role_id'],$role_ids)){
 
-                     $role_ids[] = $v['role_id'];
 
-                 }
 
-             }
 
-             if(isset($v['employee_id'])){
 
-                 if(!in_array($v['employee_id'],$employee_ids)){
 
-                     $employee_ids[] = $v['employee_id'];
 
-                 }
 
-             }
 
-         }
 
-         EmployeeMenuPermission::wherein('role_id',$role_ids)->delete();
 
-         EmployeeMenuPermission::wherein('employee_id',$employee_ids)->delete();
 
-         EmployeeMenuPermission::insert($data);
 
-         return [200,'保存成功!'];
 
-     }
 
-     /**
 
-      * 人员部门关系更新
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function employeeDepart($data){
 
-         if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
 
-         DB::beginTransaction();
 
-         try {
 
-             if($data['type'] == 1){
 
-                 EmployeeDepartPermission::whereIn('depart_id',$data['insert']['depart_id'])->delete();
 
-             }else{
 
-                 EmployeeDepartPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
 
-             }
 
-             $insert = [];
 
-             foreach ($data['insert']['depart_id'] as $t){
 
-                 foreach ($data['insert']['employee_id'] as $e){
 
-                     $insert[] = [
 
-                         'depart_id' => $t,
 
-                         'employee_id' => $e
 
-                     ];
 
-                 }
 
-             }
 
-             EmployeeDepartPermission::insert($insert);
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false,$exception->getMessage()];
 
-         }
 
-         return [true,'保存成功!'];
 
-     }
 
-     /**
 
-      * 人员班组关心更新
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function employeeTeam($data){
 
-         if($this->isEmpty($data,'insert')) return [false,'数据不能为空!'];
 
-         DB::beginTransaction();
 
-         try {
 
-             if($data['type'] == 1){
 
-                 EmployeeTeamPermission::whereIn('team_id',$data['insert']['team_id'])->delete();
 
-             }else{
 
-                 EmployeeTeamPermission::whereIn('employee_id',$data['insert']['employee_id'])->delete();
 
-             }
 
-             $insert = [];
 
-             foreach ($data['insert']['team_id'] as $t){
 
-                 foreach ($data['insert']['employee_id'] as $e){
 
-                     $insert[] = [
 
-                         'team_id' => $t,
 
-                         'employee_id' => $e
 
-                     ];
 
-                 }
 
-             }
 
-             EmployeeTeamPermission::insert($insert);
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false,$exception->getMessage()];
 
-         }
 
-         return [true,'保存成功!'];
 
-     }
 
-     /**
 
-      * 登陆参数规则
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function loginRule($data){
 
-         if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
 
-         if($this->isEmpty($data,'password')) return [false,'密码不存在!'];
 
-         $account = $data['account'];
 
-         $res = Employee::where('del_time',0)
 
-             ->where(function ($query)use($account) {
 
-                 $query->where('account', $account)
 
-                     ->orWhere('mobile', $account);
 
-             })
 
-             ->get()->toArray();
 
-         if(empty($res)) return [false,'账号不存在或已被删除!'];
 
-         if(count($res) > 1) return [false,'该手机号检测出多个账户,请联系后台!'];
 
-         $res = reset($res);
 
-         if(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
 
-         if($res['is_admin'] != Employee::IS_ADMIN) return [false,'该账号不能登录!'];
 
-         if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
 
-         $is_main = EmployeeService::isMain($res['id']);
 
-         $return = EmployeeService::getLoginDepart($res['id']);
 
-         $depart_top_title = $return[1] ?? [];
 
-         $depart_top_title = $depart_top_title[0]['title'] ?? "";
 
-         return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'is_main' => $is_main, 'top_depart_title' => $depart_top_title]];
 
-     }
 
-     /**
 
-      * 检查人员信息
 
-      * @param $userId
 
-      * @return array
 
-      */
 
-     public static function checkUser($userId){
 
-         $res = Employee::where('id', $userId)
 
-             ->where('del_time',0)
 
-             ->where('is_admin',Employee::IS_ADMIN)
 
-             ->where('state',Employee::USE)->get()->first();
 
-         if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
 
-         return [true, $res];
 
-     }
 
-     /**
 
-      * 获取登录账号的角色
 
-      * @param $employee_id
 
-      * @return array
 
-      */
 
-     public static function getPersonRole($employee_id){
 
-         if(empty($employee_id)) return [];
 
-         $role = EmployeeRole::where('del_time',0)
 
-             ->where('employee_id',$employee_id)
 
-             ->select('role_id')
 
-             ->get()->toArray();
 
-         //组织
 
-         $role_id = array_unique(array_column($role,'role_id'));
 
-         asort($role_id);
 
-         $role_id = array_values($role_id);
 
-         return $role_id;
 
-     }
 
-     /**
 
-      * 获取登录账号的角色的菜单
 
-      * @param $role_id
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function getMenuByRole($role_id,$user){
 
-         $menu = SysMenu::where('del_time',0)->select('id')->get()->toArray();
 
-         $button = SysMenuButton::where('del_time',0)->select('id','menu_id')->get()->toArray();
 
-         $button_map = [];
 
-         foreach ($button as $value){
 
-             $button_map[$value['menu_id']][] = $value['id'];
 
-         }
 
-         $object = [];//返回的模型
 
-         if($user['id'] == Employee::SPECIAL_ADMIN || $user['is_manager']){
 
-             //超级管理员
 
-             foreach ($menu as $value){
 
-                 $object[] = [
 
-                     'type' => 0,//所有权限
 
-                     'menu_id' => $value['id'],
 
-                     'button' => $button_map[$value['id']] ?? [],
 
-                 ];
 
-             }
 
-             return $object;
 
-         }
 
-         //没绑定角色
 
-         if(empty($role_id)) return [];
 
-         $search = RoleMenu::whereIn('role_id',$role_id)
 
-             ->where('del_time',0)
 
-             ->select('menu_id','type')
 
-             ->get()->toArray();
 
-         $button = $this->fillRoleButton($role_id);
 
-         $tmp = [];
 
-         foreach ($search as $value){
 
-             if(! in_array($value['menu_id'],$tmp)){
 
-                 $object[] = [
 
-                     'menu_id' => $value['menu_id'],
 
-                     'type' => $value['type'],
 
-                     'button' => $button[$value['menu_id']] ?? [],
 
-                 ];
 
-                 $tmp[] = $value['menu_id'];
 
-             }
 
-         }
 
-         unset($tmp);
 
-         return $object;
 
-     }
 
-     /**
 
-      * 人员直接绑定部门
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function employeeManagerDepart($data,$user){
 
-         if($user['id'] != Employee::SPECIAL_ADMIN) return [false,'非ADMIN账号不能操作'];
 
-         if($this->isEmpty($data,'employee_id')) return [false,'请选择操作人员'];
 
-         if($this->isEmpty($data,'depart_id')) return [false,'请选择部门'];
 
-         EmployeeManagerDepart::where('employee_id',$data['employee_id'])->update([
 
-             'del_time' => time()
 
-         ]);
 
-         $insert = [];
 
-         foreach ($data['depart_id'] as $value){
 
-             $insert[] = [
 
-                 'employee_id' => $data['employee_id'],
 
-                 'depart_id' => $value,
 
-                 'crt_time' => time(),
 
-                 'upd_time' => time(),
 
-             ];
 
-         }
 
-         EmployeeManagerDepart::insert($insert);
 
-         return [true,''];
 
-     }
 
-     /**
 
-      * 填充角色下的按钮
 
-      * @param $role_id
 
-      * @return array
 
-      */
 
-     public function fillRoleButton($role_id){
 
-         $button = RoleMenuButton::whereIn('role_id',$role_id)
 
-             ->where('del_time',0)
 
-             ->select('menu_id','button_id')
 
-             ->get()->toArray();
 
-         $button_map = [];
 
-         foreach ($button as $value){
 
-             if(! isset($button_map[$value['menu_id']])){
 
-                 $button_map[$value['menu_id']][] = $value['button_id'];
 
-             }else{
 
-                 if(! in_array($value['button_id'], $button_map[$value['menu_id']])) $button_map[$value['menu_id']][] = $value['button_id'];
 
-             }
 
-         }
 
-         return $button_map;
 
-     }
 
-     /**
 
-      * 获取登录账号的部门
 
-      * @param $employee_id
 
-      * @return array|string[]
 
-      */
 
-     public static function getLoginDepart($employee_id){
 
-         if(empty($employee_id)) return [];
 
-         //自己绑定的部门 启用的部门
 
-         $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
 
-             ->join('depart as b','b.id','a.depart_id')
 
-             ->where('a.employee_id',$employee_id)
 
-             ->where('b.is_use',Depart::IS_UES)
 
-             ->select('a.depart_id','b.is_main','b.parent_id','b.basic_type_id','b.title')
 
-             ->orderBy('b.parent_id','asc')
 
-             ->orderBy('b.is_main','desc')
 
-             ->orderBy('a.depart_id','asc')
 
-             ->get()->toArray();
 
-         $top = $map = $rule = [];
 
-         $is_all_depart = 0;
 
-         if(! empty($depart)){
 
-             $list = Depart::where('del_time',0)->get()->toArray();
 
-             $depart_map = array_column($list,null,'id');
 
-             foreach ($depart as $value){
 
-                 if($value['parent_id'] == 0){//顶级
 
-                     $top[$value['depart_id']] = [
 
-                         'depart_id' => $value['depart_id'],
 
-                         'is_main' => $value['is_main'],
 
-                         'basic_type_id' => $value['basic_type_id'],
 
-                         'title' => $value['title'],
 
-                     ];
 
-                     $map[$value['depart_id']] = $value['depart_id'];
 
-                     if(! empty($value['is_main']) && ! $is_all_depart) $is_all_depart = 1;
 
-                 }else{
 
-                     $t = self::getTopParentId($value['depart_id'],$list);
 
-                     if($t && isset($depart_map[$t])) {
 
-                         $t_tmp = $depart_map[$t] ?? [];
 
-                         $top[$t_tmp['id']] = [
 
-                             'depart_id' => $t_tmp['id'],
 
-                             'is_main' => $t_tmp['is_main'],
 
-                             'basic_type_id' => $t_tmp['basic_type_id'],
 
-                             'title' => $t_tmp['title'],
 
-                         ];
 
-                         $map[$value['depart_id']] = $t;
 
-                         if(! empty($tmp['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
 
-                     }
 
-                 }
 
-             }
 
-             foreach ($depart as $value){
 
-                 if(in_array($value['depart_id'],$rule)) continue;
 
-                 if(! $value['parent_id']){ //顶级
 
-                     if($value['is_main']) {//是总公司
 
-                         //所有部门都有
 
-                         $rule = array_column($list,'id');
 
-                     }else{//不是总公司
 
-                         //自己以及子部门
 
-                         $depart_id = array_merge(self::getAllIds($list,$map[$value['depart_id']]),[$map[$value['depart_id']]]);
 
-                         $rule = array_merge_recursive($rule,$depart_id);
 
-                     }
 
-                 }else{//非顶级
 
-                     if($value['is_main']) {//是总社
 
-                         $top_tmp = $map[$value['depart_id']];
 
-                         if(! empty($depart_map[$top_tmp]['is_main'])){
 
-                             //顶级公司是总公司 所有部门都有
 
-                             $rule = array_column($list,'id');
 
-                         }else{
 
-                             //顶级公司是分公司 分公司所有部门
 
-                             $depart_id = array_merge(self::getAllIds($list,$top_tmp),[$top_tmp]);
 
-                             $rule = array_merge_recursive($rule,$depart_id);
 
-                         }
 
-                     }else{//不是总社
 
-                         $rule = array_merge($rule,[$value['depart_id']]);
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         return [$depart, array_values($top), $map, array_unique($rule), $is_all_depart];
 
-     }
 
-     //判断是否总公司
 
-     public static function isMain($employee_id){
 
-         $is_main = 0;
 
-         if(empty($employee_id)) return $is_main;
 
-         //admin账号
 
-         if($employee_id == Employee::SPECIAL_ADMIN) {
 
-             $is_main = 1;
 
-             return $is_main;
 
-         }
 
-         //自己绑定的部门 启用的部门
 
-         $depart = EmployeeDepartPermission::from('employee_depart_permission as a')
 
-             ->join('depart as b','b.id','a.depart_id')
 
-             ->where('a.employee_id',$employee_id)
 
-             ->where('b.is_use',Depart::IS_UES)
 
-             ->select('a.depart_id','b.is_main','b.parent_id')
 
-             ->orderBy('b.parent_id','asc')
 
-             ->orderBy('b.is_main','desc')
 
-             ->orderBy('a.depart_id','asc')
 
-             ->get()->toArray();
 
-         if(! empty($depart)){
 
-             $list = Depart::where('del_time',0)->get()->toArray();
 
-             $depart_map = array_column($list,null,'id');
 
-             foreach ($depart as $value){
 
-                 if($value['parent_id'] == 0){//顶级
 
-                     if(! empty($value['is_main']) && ! $is_main) $is_main = 1;
 
-                 }else{
 
-                     $t = self::getTopParentId($value['depart_id'],$list);
 
-                     if($t && isset($depart_map[$t])) {
 
-                         $tmp_is_main = $depart_map[$t]['is_main'] ?? 0;
 
-                         if(! empty($tmp_is_main) && ! $is_main) $is_main = 1;
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         return $is_main;
 
-     }
 
-     /**
 
-      * 获取顶级id
 
-      * @param $id
 
-      * @param $data
 
-      * @return int
 
-      */
 
-     public static function getTopParentId($id, $data) {
 
-         foreach ($data as $item) {
 
-             if ($item['id'] == $id) {
 
-                 if ($item['parent_id'] == 0) {
 
-                     // 找到最顶级的id
 
-                     return $item['id'];
 
-                 } else {
 
-                     // 继续递归查找父级
 
-                     return self::getTopParentId($item['parent_id'], $data);
 
-                 }
 
-             }
 
-         }
 
-         // 如果没有找到匹配的id,则返回null或者其他你希望的默认值
 
-         return 0;
 
-     }
 
-     /**
 
-      * 递归获取所有id
 
-      * @param $data
 
-      * @param $id
 
-      * @return array
 
-      */
 
-     public static function getAllIds($data, $id) {
 
-         $result = array(); // 存储结果的数组
 
-         foreach ($data as $node) {
 
-             if ($node['parent_id'] == $id) { // 如果当前节点的父 ID 等于指定 ID,则将该节点添加到结果中
 
-                 $result[] = $node['id'];
 
-                 // 递归查询该节点的所有子孙节点,并将结果合并到结果数组中
 
-                 $result = array_merge($result, self::getAllIds($data, $node['id']));
 
-             }
 
-         }
 
-         return $result;
 
-     }
 
- }
 
 
  |