| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723 | 
							- <?php
 
- namespace App\Service;
 
- use App\Model\BasicType;
 
- use App\Model\Depart;
 
- use App\Model\DepartIndex;
 
- use App\Model\Employee;
 
- use App\Model\EmployeeDepartPermission;
 
- use App\Model\EmployeeFile;
 
- 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\SysMenu;
 
- use App\Model\SysMenuButton;
 
- use App\Model\Team;
 
- use App\Model\WxEmployeeOfficial;
 
- use App\Service\Weixin\WeixinService;
 
- use Illuminate\Support\Facades\DB;
 
- use Illuminate\Support\Facades\Hash;
 
- use Mockery\Exception;
 
- /**
 
-  * 人员相关
 
-  * @package App\Models
 
-  */
 
- class EmployeeService extends Service
 
- {
 
-     public function employeeEditImg($data, $user){
 
-         $time = time();
 
-         try {
 
-             DB::beginTransaction();
 
-             $old = EmployeeFile::where('del_time',0)
 
-                 ->where('employee_id',$user['id'])
 
-                     ->select('file')
 
-                 ->get()->toArray();
 
-             $old = array_column($old,'file');
 
-             EmployeeFile::where('del_time',0)
 
-                 ->where('employee_id',$user['id'])
 
-                 ->update(['del_time' => $time]);
 
-             $new = [];
 
-             $insert = [];
 
-             if(! empty($data['img_url'])){
 
-                 $insert[] = [
 
-                     'employee_id' => $user['id'],
 
-                     'file' => $data['img_url'],
 
-                     'crt_time' => $time,
 
-                 ];
 
-                 EmployeeFile::insert($insert);
 
-                 $new[] = $data['img_url'];
 
-             }
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getMessage()];
 
-         }
 
-         return [true, ['file' => ['new' => $new, 'old' => $old]]];
 
-     }
 
-     public function getEmployeeImg($user){
 
-         $file = EmployeeFile::where('del_time',0)
 
-             ->where('employee_id',$user['id'])
 
-             ->select('file')
 
-             ->get()->toArray();
 
-         $file = array_column($file,'file');
 
-         $file = $file[0] ?? "";
 
-         $img_str = "";
 
-         $timpstamp = 86400;
 
-         if(! empty($file)){
 
-             $fileUploadService = new FileUploadService();
 
-             $img_str = $fileUploadService->getFileShow($file, $timpstamp);
 
-         }
 
-         return [true, ['img_url' => $img_str]];
 
-     }
 
-     public function employeeEditOther($data,$user){
 
-         list($status,$msg) = $this->employeeOtherRule($data,$user);
 
-         if(!$status) return [$status,$msg];
 
-         try {
 
-             DB::beginTransaction();
 
-             $model = new Employee();
 
-             $model = $model->where('id',$user['id'])->first();
 
-             $model->password = Hash::make($data['new_password']);
 
-             $model->save();
 
-             DB::commit();
 
-         }catch (\Exception $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getMessage()];
 
-         }
 
-         return [true,''];
 
-     }
 
-     public function employeeOtherRule($data,$user){
 
-         if(! isset($data['old_password'])) return [false,'请输入原密码'];
 
-         if($data['old_password'] == "") return [false,'原密码不能为空'];
 
-         if(! isset($data['new_password'])) return [false,'请输入新密码'];
 
-         if($data['new_password'] == "") return [false,'新密码不能为空'];
 
-         if(! isset($data['re_password'])) return [false,'请输入确认密码'];
 
-         if($data['re_password'] == "") return [false,'确认密码不能为空'];
 
-         if(! Hash::check($data['old_password'], $user['password'])) return [false,'原密码错误'];
 
-         if($data['new_password'] == $data['old_password']) return [false,'原密码与新密码一致'];
 
-         if($data['new_password'] !== $data['re_password']) return [false,'新密码与确认密码不一致'];
 
-         return [true,''];
 
-     }
 
-     /**
 
-      * 用户编辑
 
-      * @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'];
 
-             $model->account = $data['number'];
 
-             if($model->is_admin == 1){
 
-                 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'];
 
-             $model->account = $data['number'];
 
-             if($model->is_admin == 1){
 
-                 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()
 
-         ]);
 
-         EmployeeDepartPermission::whereIn('employee_id',$data['id'])->delete();
 
-         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'])) {
 
-             $depart = Depart::where('del_time',0)
 
-                 ->select('id','parent_id')
 
-                 ->get()->toArray();
 
-             $result = array_merge($this->getAllDescendants($depart,$data['depart']),[$data['depart']]);
 
-             $employee_id = DB::table('employee_depart_permission')
 
-                 ->whereIn("depart_id", $result)
 
-                 ->select("employee_id")
 
-                 ->get()->toArray();
 
-             $employee_id = array_column($employee_id,'employee_id');
 
-             $model->whereIn("id", $employee_id);
 
-         }else{
 
-             if(! $user['is_all_depart']){
 
-                 $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;
 
-         }
 
-         $map = $this->getTopDepartSon();
 
-         $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 = $man_top_depart = [];
 
-         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;
 
-             $tmp = $map[$value->id] ?? [];
 
-             if(empty($tmp)) continue;
 
-             $key = $tmp['id'] . $tmp['title'];
 
-             if(! isset($man_top_depart[$value->employee_id][$key])) {
 
-                 $man_top_depart[$value->employee_id][$key] = $map[$value->id];
 
-             }
 
-         }
 
-         foreach ($man_top_depart as $key => $value){
 
-             $man_top_depart[$key] = array_values($value);
 
-         }
 
-         $wx = WxEmployeeOfficial::where('appid',WeixinService::APPID)
 
-             ->where('employee_id','>',0)
 
-             ->select('employee_id')
 
-             ->get()->toArray();
 
-         $wx = array_column($wx,'employee_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']] ?? '';
 
-             $is_wx = "未绑定微信公众号";
 
-             if(in_array($value['id'], $wx)) $is_wx = "已绑定微信公众号";
 
-             $data['data'][$key]['is_wx'] = $is_wx;
 
-             $data['data'][$key]['my_top'] = $man_top_depart[$value['id']] ?? [];
 
-         }
 
-         return $data;
 
-     }
 
-     //门店下所有子集
 
-     public function getTopDepartSon(){
 
-         $departList = Depart::where('del_time',0)
 
-             ->select('id','parent_id','title')
 
-             ->get()->toArray();
 
-         $map = [];
 
-         foreach ($departList as $value){
 
-             if($value['parent_id'] == 0){
 
-                 $result = array_merge($this->getAllDescendants($departList,$value['id']),[$value['id']]);
 
-                 foreach ($result as $val){
 
-                     $map[$val] = [
 
-                         'id' => $value['id'],
 
-                         'title' => $value['title'],
 
-                     ];
 
-                 }
 
-             }
 
-         }
 
-         return $map;
 
-     }
 
-     //获取当前顶级部门下人员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 [true, $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];
 
-     }
 
-     public function departSetIndex($data,$user){
 
-         if(empty($data['data'])) return [false, '指标数据不能为空'];
 
-         $time = time();
 
-         $insert = [];
 
-         foreach ($data['data'] as $value){
 
-             if(empty($value['top_depart_id'])) return [false, '请选择门店'];
 
-             if(! isset($value['param_one'])) return [false, '请填写销售额'];
 
-             if(floatval($value['param_one']) == 0.0) return [false,'销售额不能为空'];
 
-             $insert[] = [
 
-                 'top_depart_id' => $value['top_depart_id'],
 
-                 'param_one' => $value['param_one'],
 
-                 'crt_time' => $time,
 
-             ];
 
-         }
 
-         if(empty($insert)) return [true, ''];
 
-         //删除指标
 
-         DepartIndex::where('del_time',0)
 
-             ->update(['del_time' => $time]);
 
-         //写入指标
 
-         DepartIndex::insert($insert);
 
-         return [true, ''];
 
-     }
 
-     /**
 
-      * 部门编辑
 
-      * @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->notify_id = $value['notify_id'] ?? 0;
 
-                 $model->area = $value['area'] ?? 0;
 
-                 $model->province = $value['province'] ?? "";
 
-                 $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::whereIn('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','notify_id','channel_id','area','province')
 
-             ->orderby('id', 'asc');
 
-         if(! empty($data['get_top']) && $data['get_top'] == 1){
 
-             //指派销售
 
-             $depart_id = $this->getDepartIdListOfMySales($user);
 
-             $model->whereIn('id', $depart_id);
 
-         }else{
 
-             if(! $user['is_all_depart']){
 
-                 $depart_id = $this->getDepartIdList($user);
 
-                 $model->whereIn('id',$depart_id);
 
-             }
 
-         }
 
-         if(isset($data['parent_id'])) $model->where('parent_id', $data['parent_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();
 
-         $emp_id = array_unique(array_merge_recursive(array_column($list,'notify_id'),array_column($list,'channel_id')));
 
-         $emp_map = Employee::whereIn('id',$emp_id)
 
-             ->pluck('emp_name','id')
 
-             ->toArray();
 
-         //省
 
-         $address_map = config('address');
 
-         $address_map = array_column($address_map,'label','value');
 
-         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'];
 
-             $list[$key]['notify_name'] = $emp_map[$value['notify_id']] ?? '';
 
-             $list[$key]['channel_name'] = $emp_map[$value['channel_id']] ?? '';
 
-             $list[$key]['area_name'] = Depart::$area[$value['area']] ?? '';
 
-             $list[$key]['province_name'] = $address_map[$value['province']] ?? '';
 
-         }
 
-         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,'总社已存在!'];
 
-         }
 
-         //省
 
-         $address_map = config('address');
 
-         $address_map = array_column($address_map,'label','value');
 
-         foreach ($data['data'] as $key => $value){
 
-             if(! empty($value['area'])){
 
-                 $area = array_keys(Depart::$area);
 
-                 if(! in_array($value['area'], $area)) return [false, '门店所属大区不存在'];
 
-             }
 
-             if(! empty($value['province']) && ! isset($address_map[$value['province']])) return [false, '门店所属省份不存在'];
 
-             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不能为空!'];
 
-                 if($data['id'] == $user['head']['id'] && empty($data['is_main'])) return [false,'总社不支持变更!'];
 
-                 $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,'账号停用!'];
 
-         //门店的信息
 
-         $login_message = EmployeeService::getLoginMessage($res['id']);
 
-         return [true, ['id'=>$res['id'],'name'=>$res['emp_name'],'all_top'=> $login_message]];
 
-     }
 
-     /**
 
-      * 检查人员信息
 
-      * @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 static function getPersonRoleQx($role_id = []){
 
-         if(empty($role_id)) return [];
 
-         $role = RoleMenu::where('del_time',0)
 
-             ->whereIn('role_id',$role_id)
 
-             ->select('menu_id','type')
 
-             ->get()->toArray();
 
-         $sysmenu = SysMenu::where('del_time',0)
 
-             ->where('is_authority','>',0)
 
-             ->select('id')
 
-             ->get()->toArray();
 
-         $sysmenu = array_column($sysmenu,'id');
 
-         $return = [];
 
-         foreach ($role as $value){
 
-             if(! in_array($value['menu_id'],$sysmenu)) continue;
 
-             if(isset($return[$value['menu_id']])){
 
-                 if($return[$value['menu_id']] < $value['type']) $return[$value['menu_id']] = $value['type'];
 
-             }else{
 
-                 $return[$value['menu_id']] = $value['type'];
 
-             }
 
-         }
 
-         return $return;
 
-     }
 
-     public static function getSpecialButton($role_id,$user){
 
-         $return = [];
 
-         $special_button = config('specialButton');
 
-         if($user == Employee::SPECIAL_ADMIN) {
 
-             foreach ($special_button as $value){
 
-                 $return[] = $value['id'];
 
-             }
 
-             return $return;
 
-         }
 
-         $role_button = RoleMenuButton::where('del_time',0)
 
-             ->where('button_id','<',0)
 
-             ->whereIn('role_id',$role_id)
 
-             ->select('menu_id','button_id')
 
-             ->get()->toArray();
 
-         foreach ($role_button as $value){
 
-             $return[] = $value['button_id'];
 
-         }
 
-         return $return;
 
-     }
 
-     //通过角色获取菜单
 
-     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, $top_depart_id = 0){
 
-         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 = [];
 
-         $res_data = [];//获取当前所在部门(也可能是门店 为了 depart_id)
 
-         $my_depart = [];//当前门店下 我勾选的门店以及部门信息
 
-         $is_all_depart = 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($value['depart_id'] == $top_depart_id) {
 
-                         $my_depart[] = $depart[$key];
 
-                         if(empty($res_data)){
 
-                             $res_data = $depart[$key];
 
-                         }else{
 
-                             if($value['depart_id'] >= $res_data['depart_id']) $res_data = $depart[$key];
 
-                         }
 
-                     }
 
-                 }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] ?? [];
 
-                         if( ! isset($top[$t_tmp['id']])){
 
-                             $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($t == $top_depart_id){
 
-                         $my_depart[] = $depart[$key];
 
-                         if(empty($res_data)){
 
-                             $res_data = $depart[$key];
 
-                         }else{
 
-                             if($value['depart_id'] >= $res_data['depart_id']) $res_data = $depart[$key];
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-             //-------------重组当前所在门店
 
-             $top = array_values($top);
 
-             usort($top, function($a, $b) {
 
-                 return $b['is_main'] - $a['is_main'];
 
-             });
 
-             if(! empty($top_depart_id)){
 
-                 $targetArray = null;
 
-                 foreach ($top as $key => $value) {
 
-                     if ($value['depart_id'] == $top_depart_id) {
 
-                         $targetArray = $value;
 
-                         unset($top[$key]); // 从原数组中移除目标数组
 
-                         break; // 找到后跳出循环
 
-                     }
 
-                 }
 
-                 if ($targetArray) array_unshift($top, $targetArray);
 
-             }
 
-             //-------------重组当前所在门店
 
-             //-------------拥有的数据权限(门店、部门)
 
-             $my_top = $top[0] ?? [];
 
-             foreach ($my_depart as $value){
 
-                 if(in_array($value['depart_id'],$rule)) continue;
 
-                 if(! $value['parent_id']){ //顶级
 
-                     if(! empty($value['is_main']) && ! $is_all_depart) $is_all_depart = 1;
 
-                     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(! empty($my_top['is_main']) && $value['is_main'] && ! $is_all_depart) $is_all_depart = 1;
 
-                     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;
 
-             }
 
-         }
 
-         $rule = array_unique($rule);
 
-         return [$depart,$top,$map,$rule,$is_all_depart,$head,$res_data];
 
-     }
 
-     //获取用户的所属门店信息
 
-     public static function getLoginMessage($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 = [];
 
-         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(! isset($top[$value['depart_id']])){
 
-                         $top[$value['depart_id']] = [
 
-                             'depart_id' => $value['depart_id'],
 
-                             'is_main' => $value['is_main'],
 
-                             'title' => $value['title'],
 
-                         ];
 
-                     }
 
-                 }else{
 
-                     //门店
 
-                     $t = self::getTopParentId($value['depart_id'],$list);
 
-                     if($t && isset($depart_map[$t])) {
 
-                         $t_tmp = $depart_map[$t] ?? [];
 
-                         $is_all_depart = 0;
 
-                         if(! empty($t_tmp['is_main']) && $value['is_main']) $is_all_depart = 1;
 
-                         if(! isset($top[$t_tmp['id']])){
 
-                             $top[$t_tmp['id']] = [
 
-                                 'depart_id' => $t_tmp['id'],
 
-                                 'is_main' => $is_all_depart,
 
-                                 'title' => $t_tmp['title'],
 
-                             ];
 
-                         }else{
 
-                             if(! empty($is_all_depart)) $top[$t_tmp['id']]['is_main'] = $is_all_depart;
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-         }
 
-         $top = array_values($top);
 
-         usort($top, function($a, $b) {
 
-             return $b['is_main'] - $a['is_main'];
 
-         });
 
-         return $top;
 
-     }
 
-     /**
 
-      * 获取顶级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;
 
-     }
 
-     public static function checkWxUser($userId){
 
-         $res = Employee::where('id', $userId)
 
-             ->where('del_time',0)
 
-             ->where('state',Employee::USE)->get()->first();
 
-         if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
 
-         return [true, $res];
 
-     }
 
-     public function getTopMessage($data,$user){
 
-         $top = $user['depart_top'] ?? [];
 
-         return [true, ['top' => $top]];
 
-     }
 
- }
 
 
  |