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,'']; } public function employeeEdit($data,$user){ list($status,$msg) = $this->employeeRule($data,$user,false); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new Employee(); $model = $model->where('id',$data['id'])->first(); $model->number = $data['number'] ?? ""; $model->title = $data['title'] ?? ""; $model->mobile = $data['mobile'] ?? ""; $model->sex = $data['sex'] ?? 0; $model->education = $data['education'] ?? ""; $model->id_card = $data['id_card'] ?? ""; $model->major = $data['major'] ?? ""; $model->p_title = $data['p_title'] ?? ""; $model->state = $data['state'] ?? 0; $model->is_admin = $data['is_admin'] ?? 0; if($model->is_admin && $data['password'] !== '******') { $model->password = Hash::make($data['password']); $model->p_version = $model->p_version + 1; } $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,'']; } public function employeeAdd($data,$user){ list($status,$msg) = $this->employeeRule($data, $user); if(!$status) return [$status,$msg]; try{ DB::beginTransaction(); $model = new Employee(); $model->number = $data['number'] ?? ""; $model->title = $data['title'] ?? ""; $model->mobile = $data['mobile'] ?? ""; $model->sex = $data['sex'] ?? 0; $model->education = $data['education'] ?? ""; $model->id_card = $data['id_card'] ?? ""; $model->major = $data['major'] ?? ""; $model->p_title = $data['p_title'] ?? ""; $model->state = $data['state'] ?? 0; $model->crt_id = $user['id']; $model->is_admin = $data['is_admin'] ?? 0; $model->account = $data['account'] ?? ""; if($model->is_admin) $model->password = Hash::make($data['password']); $model->top_depart_id = $data['top_depart_id']; $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,'']; } 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,'删除成功']; } public function employeeDetail($data, $user){ if(empty($data['id'])) return [false,'人员id不能为空']; list($status, $return) = $this->employeeList(['id' => [$data['id']]], $user); $user = $return['data'][0] ?? []; return [true, $user]; } public function employeeCommon($data,$user, $field = []){ if(empty($field)) $field = Employee::$field; $model = Employee::TopClear($user,$data); $model = $model->where('del_time',0) ->where('is_admin', '<>', Employee::IS_ADMIN_TWO) ->select($field) ->orderBy('id','desc'); if(! empty($data['id'])) $model->whereIn('id', $data['id']); if(! empty($data['number'])) $model->where('number', 'LIKE', '%'.$data['number'].'%'); if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); if(! empty($data['mobile'])) $model->where('mobile', 'LIKE', '%'.$data['mobile'].'%'); if(isset($data['is_admin'])) $model->where('is_admin', $data['is_admin']); if(! empty($data['state'])) $model->where('state', $data['state']); if(isset($data['education'])) $model->where('education', $data['education']); 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(! empty($data['depart'])) { // 1. 获取所有子部门 ID(包含自身) $allDeparts = Depart::where('del_time', 0)->select('id', 'parent_id')->get(); $departIds = array_merge($this->getAllDescendants($allDeparts->toArray(), $data['depart']), [$data['depart']]); // 2. 优化:直接使用子查询,避免拉取巨大的 $employee_id 数组到内存 $model->whereIn('id', function ($query) use ($departIds) { $query->select('employee_id') ->from('employee_depart_permission') ->whereIn('depart_id', $departIds); }); } return $model; } /** * 产品列表 * @param $data * @param $user * @return array */ public function employeeList($data,$user){ $model = $this->employeeCommon($data, $user); $list = $this->limit($model,'',$data); $list = $this->organizationEmployeeData($list); return [true, $list]; } public function organizationEmployeeData($data) { if (empty($data['data'])) return $data; // 获取员工ID并查询扩展数据 $employee_ids = array_column($data['data'], 'id'); list($status, $extraMap) = $this->getEmployee($employee_ids); foreach ($data['data'] as &$item) { $id = $item['id']; $extra = $extraMap[$id] ?? null; $item['role'] = $extra['role_ids'] ?? []; $item['role_name'] = isset($extra['role_names']) ? implode(',', $extra['role_names']) : ''; $item['depart'] = $extra['depart_ids'] ?? []; $item['depart_title'] = isset($extra['depart_names']) ? implode(',', $extra['depart_names']) : ''; // 业务状态字段 $item['is_admin_title'] = Employee::IS_ADMIN_TITLE[$item['is_admin']] ?? ""; $item['state_title'] = Employee::State_Type[$item['state']] ?? ""; $item['sex_title'] = Employee::SEX_TYPE[$item['sex']] ?? ""; $item['education_title'] = Employee::Education[$item['education']] ?? ""; $item['crt_time'] = !empty($item['crt_time']) ? date("Y-m-d", $item['crt_time']) : ""; } return $data; } public function getEmployee(array $employee_ids) { if (empty($employee_ids)) return [false, []]; // 1. 一次性获取所有角色 $roles = DB::table('employee_role as a') ->join('role as b', 'a.role_id', '=', 'b.id') ->where('a.del_time', 0) ->where('b.del_time', 0) ->whereIn("a.employee_id", $employee_ids) ->select('a.employee_id', 'b.title', 'b.id') ->get(); // 2. 一次性获取所有部门 $departs = DB::table('employee_depart_permission as a') ->join('depart as b', 'a.depart_id', '=', 'b.id') ->whereIn("a.employee_id", $employee_ids) ->select('a.employee_id', 'b.title', 'b.id') ->orderBy('b.id') ->get(); // 3. 结果按员工ID分组归纳 $resultMap = []; foreach ($employee_ids as $id) { $resultMap[$id] = [ 'role_ids' => [], 'role_names' => [], 'depart_ids' => [], 'depart_names' => [] ]; } foreach ($roles as $r) { $resultMap[$r->employee_id]['role_ids'][] = $r->id; $resultMap[$r->employee_id]['role_names'][] = $r->title; } foreach ($departs as $d) { $resultMap[$d->employee_id]['depart_ids'][] = $d->id; $resultMap[$d->employee_id]['depart_names'][] = $d->title; } return [true, $resultMap]; } public function employeeRule(&$data, $user,$is_add = true){ if(empty($data['number'])) return [false,'工号不存在']; if(empty($data['title'])) return [false,'姓名不存在']; if(! empty($data['sex']) && ! isset(Employee::SEX_TYPE[$data['sex']])) return [false, '性别不存在']; if(empty($data['mobile'])) return [false,'联系电话不能为空']; // if(! $this->isValidPhone($data['mobile'])) return [false, '手机号码格式错误']; if(! empty($data['education']) && ! isset(Employee::Education[$data['education']])) return [false, '学历不存在']; if(empty($data['id_card'])) return [false, '身份证号不能为空']; if(empty($data['depart'])) return [false,'部门不能为空']; if(empty($data['state'])) return [false,'状态不能为空']; if(! isset(Employee::State_Type[$data['state']])) return [false,'状态不存在']; if(! empty($data['is_admin'])){ if(! isset(Employee::IS_ADMIN_TITLE_SIMPLE[$data['is_admin']])) return [false, 'is_admin类型不存在']; if(empty($data['password'])) return [false, '密码不能为空']; if(mb_strlen($data['password']) < 6) return [false, '密码长度不得小于6位长度']; if(empty($data['role'])) return [false, '角色不能为空']; } $data['top_depart_id'] = $user['top_depart_id']; if(! $is_add){ if($this->isEmpty($data,'id')) return [false,'ID不能为空']; $bool = Employee::where('del_time',0) ->where('id','<>',$data['id']) ->where('top_depart_id', $data['top_depart_id']) ->where('number', $data['number']) ->exists(); }else{ $code = Depart::where('id', $user['top_depart_id'])->value('code'); $data['account'] = $code . "_" . $data['number']; $bool = Employee::where('del_time',0) ->where('top_depart_id', $data['top_depart_id']) ->where('number', $data['number']) ->exists(); } if($bool) return [false,'人员工号已存在']; return [true,'']; } 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,'']; } 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->top_depart_id = $data['top_depart_id']; $model->save(); return [true,'']; } 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, '']; } 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]; } public function roleRule(&$data,$user, $is_check = true){ if($this->isEmpty($data,'title')) return [false,'名称不能为空']; $data['top_depart_id'] = $user['top_depart_id']; 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, '']; } 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, '']; } 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 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, '']; } public function departAdd($data,$user){ list($status,$msg) = $this->departRule($data,$user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); foreach ($msg['data'] as $value){ $model = new Depart(); $model->parent_id = $value['parent_id']; $model->title = $value['title']; $model->code = $value['code']; $model->top_depart_id = $value['top_depart_id']; $model->save(); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } 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,'']; } public function checkDepartDel($data){ if($this->isEmpty($data,'id')) return [false,'ID不能为空']; $bool = Depart::whereIn('parent_id',$data['id'])->where('del_time',0)->exists(); if($bool) return [false,'部门下有子部门']; if($this->checkDepartHasPerson($data['id'])) return [false,'部门下有人员档案']; return [true, '']; } public function departList($data, $user){ $model = Depart::TopClear($user,$data); $model = $model->where('del_time',0) ->select('title','id','code','parent_id','is_use') ->orderby('id', 'asc'); 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)) { $minParentId = min(array_column($list_tree, 'parent_id')); $list_tree = $this->makeTree($minParentId,$list_tree); $list_tree = $this->set_sort_circle($list_tree); } return [true,['data' => $list,'tree' => $list_tree]]; } public function fillDepartList($list,$user){ if(empty($list)) return $list; // foreach ($list as $key => $value){ // // } return $list; } public function departRule($data,$user, $is_check = true){ if(empty($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,'名称不能重复']; } foreach ($data['data'] as $key => $value){ $top_depart_id = $user['top_depart_id']; if(empty($value['parent_id'])) $data['data'][$key]['parent_id'] = $user['top_depart_id']; $data['data'][$key]['top_depart_id'] = $top_depart_id; $data['data'][$key]['upd_time'] = time(); if($is_check){ $data['data'][$key]['crt_time'] = time(); $bool = Depart::whereRaw("binary code = '{$value['code']}'") ->where('parent_id', $top_depart_id) ->where('del_time',0) ->exists(); }else{ if($this->isEmpty($data,'id')) return [false,'id不能为空!']; $bool = Depart::whereRaw("binary code = '{$value['code']}'") ->where('parent_id', $top_depart_id) ->where('id','<>',$data['id']) ->where('del_time',0) ->exists(); } if($bool) return [false,'部门编码不能重复']; } return [true, $data]; } public function checkDepartHasPerson($depart_id = []){ if(empty($depart_id)) return false; $bool = EmployeeDepartPermission::from('employee_depart_permission as a') ->leftJoin('employee as b','b.id','a.employee_id') ->where('b.del_time',0) ->whereIn('a.depart_id',$depart_id) ->exists(); return $bool; } public function 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; } public function getEmployeeMap($employee_ids){ if(empty($employee_ids)) return []; if(! is_array($employee_ids)) $employee_ids = [$employee_ids]; return Employee::whereIn('id', $employee_ids) ->pluck('title', 'id') ->toArray(); } public static function fillMenu2($menu_id, &$user){ // 直接查询匹配的菜单 $menuItem = SysMenu::where('del_time',0) ->where('id', $menu_id) ->first(); $func = $menuItem ? $menuItem->export_file_func : ""; $funcName = $menuItem ? $menuItem->title : ""; $header_default = config("excel." . $func) ?? []; // $header_f = "extra_" . $menu_id; // $service = new TableHeadService(); // if(method_exists($service,$header_f)) $service->$header_f($header_default); $user['e_header_default'] = $header_default; return [$func, $funcName]; } }