| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407 | 
							- <?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\ProductInventory;
 
- use App\Model\ProductInventorySet;
 
- use App\Model\Role;
 
- use App\Model\RoleMenu;
 
- use App\Model\RoleMenuButton;
 
- use App\Model\Storehouse;
 
- use App\Model\Supplier;
 
- 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()
 
-         ]);
 
-         EmployeeRole::where('del_time',0)->whereIn('employee_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,'姓名不存在!'];
 
-         if(empty($data['depart'])) 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()
 
-         ]);
 
-         RoleMenu::where('del_time',0)->where('role_id',$data['id'])->update([
 
-             'del_time' => time()
 
-         ]);
 
-         RoleMenuButton::where('del_time',0)->where('role_id',$data['id'])->update([
 
-             'del_time' => time()
 
-         ]);
 
-         return [true,'删除成功'];
 
-     }
 
-     /**
 
-      * 角色列表
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function roleList($data,$user){
 
-         $model = Role::TopClear($user,$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不能为空!'];
 
-             $top_depart_id = Role::where('id',$data['id'])->value('top_depart_id');
 
-             $bool = Role::where('title',$data['title'])
 
-                 ->where('top_depart_id',$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();
 
-             $time = time();
 
-             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->rate = $value['rate'] ?? 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,'管理员账号部门关联生成失败'];
 
-                     }
 
-                     //公司系统设置
 
-                     ProductInventorySet::insert(['top_depart_id' => $depart_id, 'param_one' => 1, 'crt_time' => $time]);
 
- //                    if(! empty($value['is_main'])){
 
- //                        //总供应商 所有分社可见
 
- //                        Supplier::insert([
 
- //                            'title' => $value['title'],
 
- //                            'crt_id' => $user['id'],
 
- //                            'crt_time' => $time,
 
- //                            'depart_id' => $depart_id,
 
- //                            'top_depart_id' => $depart_id,
 
- //                            'is_main' => Supplier::is_main,
 
- //                        ]);
 
- //                    }
 
-                 }
 
-             }
 
-             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,'部门下有人员档案!'];
 
-         $bool = ProductInventory::where('top_depart_id',$data['id'])->where('del_time',0)->exists();
 
-         if($bool) 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','rate')
 
-             ->orderby('code', 'asc');
 
-         if(! empty($data['get_top']) && $data['get_top'] == 1){
 
-             //指派销售
 
-             $depart_id = $this->getDepartIdListOfMySales($user);
 
-             $model->whereIn('id', $depart_id);
 
-         }else{
 
-             if($user['id'] != Employee::SPECIAL_ADMIN && ! $user['is_all_depart'] && empty($data['get_top'])){
 
-                 $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);
 
-     }
 
-     //获取指派销售时的部门
 
-     public function getDepartIdListOfMySales($user){
 
-         $top = $user['depart_top'][0] ?? [];
 
-         $top = $top['depart_id'] ?? 0;
 
-         $list = Depart::where('del_time',0)->select('id','parent_id')->get()->toArray();
 
-         // 查找所有子级id
 
-         $childIds = $this->findChildIds($top, $list);
 
-         $result = array_merge($childIds, [$top]);
 
-         return $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;
 
-     }
 
-     /**
 
-      * 班组编辑
 
-      * @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,'账号停用!'];
 
-         //总社分社信息
 
-         $return = EmployeeService::getLoginDepart($res['id']);
 
-         $is_main = $return[4] ?? 0;
 
-         $depart_top = $return[1] ?? [];
 
-         $depart_top_title = $depart_top[0]['title'] ?? "";
 
-         $depart_top_id = $depart_top[0]['depart_id'] ?? 0;
 
-         return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'is_main' => $is_main, 'top_depart_id' => $depart_top_id,'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) || $employee_id == Employee::SPECIAL_ADMIN) 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;
 
-     }
 
-     //通过角色获取菜单
 
-     public function getMenuByRoleInList($user){
 
-         $role_id = $user['role'] ?? [];
 
-         $menu = SysMenu::where('del_time',0)->select('id')->get()->toArray();
 
-         if($user['id'] == Employee::SPECIAL_ADMIN) return array_column($menu,'id');
 
-         //没绑定角色
 
-         if(empty($role_id)) return [];
 
-         $role_menu = RoleMenu::whereIn('role_id',$role_id)
 
-             ->where('del_time',0)
 
-             ->select('menu_id')
 
-             ->get()->toArray();
 
-         return array_column($role_menu,'menu_id');
 
-     }
 
-     //通过角色获取菜单以及按钮
 
-     public function getMenuByRole($user){
 
-         $role_id = $user['role'] ?? [];
 
-         $menu = SysMenu::where('del_time',0)->select('id','uri')->get()->toArray();
 
-         $button = SysMenuButton::where('del_time',0)->select('id','title','sort','func','menu_id')->get()->toArray();
 
-         $button_map = [];
 
-         foreach ($button as $value){
 
-             $button_map[$value['menu_id']][] = $value;
 
-         }
 
-         $object = [];
 
-         //超级管理员
 
-         if($user['id'] == Employee::SPECIAL_ADMIN){
 
-             foreach ($menu as $value){
 
-                 $object[] = [
 
-                     'id' => $value['id'],
 
- //                    'type' => 0,//所有权限
 
-                     'uri' => $value['uri'],
 
-                     'button' => $button_map[$value['id']] ?? [],
 
-                 ];
 
-             }
 
-         }else{
 
-             //没绑定角色
 
-             if(empty($role_id)) return [];
 
-             $search = RoleMenu::whereIn('role_id',$role_id)
 
-                 ->where('del_time',0)
 
-                 ->select('menu_id','type')
 
-                 ->get()->toArray();
 
-             $menu_map = array_column($menu,'uri','id');
 
-             //该角色下 菜单里所有按钮
 
-             $button_menu = $this->fillRoleButton($role_id);
 
-             $button_t = array_column($button,null,'id');
 
-             foreach ($search as $value){
 
-                 $bt = $button_menu[$value['menu_id']] ?? [];
 
-                 $new = [];
 
-                 foreach ($bt as $b){
 
-                     if(! empty($button_t[$b])) $new[] = $button_t[$b];
 
-                 }
 
-                 $object[] = [
 
-                     'id' => $value['menu_id'],
 
-                     'uri' => $menu_map[$value['menu_id']] ?? '',
 
- //                    'type' => $value['type'],
 
-                     'button' => $new,
 
-                 ];
 
-             }
 
-         }
 
-         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 = $head = [];
 
-         $is_all_depart = $is_behind_main = 0;
 
-         if(! empty($depart)){
 
-             //库存校验
 
-             $set_map = ProductInventorySet::where('del_time',0)->pluck('param_one','top_depart_id')->toArray();
 
-             //所有部门
 
-             $list = Depart::where('del_time',0)->get()->toArray();
 
-             $depart_map = array_column($list,null,'id');
 
-             foreach ($depart as $key => $value){
 
-                 if($value['parent_id'] == 0){//顶级
 
-                     $is_stock = $set_map[$value['depart_id']] ?? 1;
 
-                     $depart[$key]['is_stock'] = $is_stock;
 
-                     $top[$value['depart_id']] = [
 
-                         'depart_id' => $value['depart_id'],
 
-                         'is_main' => $value['is_main'],
 
-                         'basic_type_id' => $value['basic_type_id'],
 
-                         'title' => $value['title'],
 
-                         'is_stock' => $is_stock,
 
-                     ];
 
-                     $map[$value['depart_id']] = $value['depart_id'];
 
-                     if(! empty($value['is_main']) && ! $is_all_depart) $is_all_depart = 1;
 
-                     if(! empty($value['is_main']) && ! $is_behind_main) $is_behind_main = 1;
 
-                 }else{
 
-                     $t = self::getTopParentId($value['depart_id'],$list);
 
-                     if($t && isset($depart_map[$t])) {
 
-                         $is_stock = $set_map[$t] ?? 1;
 
-                         $depart[$key]['is_stock'] = $is_stock;
 
-                         $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'],
 
-                             'is_stock' => $set_map[$t] ?? 1,
 
-                         ];
 
-                         $map[$value['depart_id']] = $t;
 
-                         if(! empty($t_tmp['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
 
-                         if(! empty($t_tmp['is_main']) && ! $is_behind_main) $is_behind_main = 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']]);
 
-                     }
 
-                 }
 
-             }
 
-             foreach ($list as $value){
 
-                 if(empty($value['parent_id']) && ! empty($value['is_main'])) $head = $value;
 
-             }
 
-         }
 
-         $top = array_values($top);
 
-         $rule = array_unique($rule);
 
-         return [$depart, $top, $map, $rule, $is_all_depart, $head, $is_behind_main];
 
-     }
 
-     //判断是否总公司
 
-     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;
 
-     }
 
- }
 
 
  |