model = new Personnel(); } //获取员工信息 public function info(array $where, string $field = '*') { $model = $this->model; $res = [ 'code' => 0, 'msg' => '请添加检索条件', 'data' => [] ]; if (!$where) { return $res; } if (!empty($where['id'])) { $model = $model->where('id', $where['id']); } $data = $model->field($field)->find();//员工信息 //查询用户角色校区部门信息 $campus_person_role = CampusPersonRole::where('person_id', $where['id']) ->with([ 'campus' => function ($query) { }, 'sysRole' => function ($query) { }, 'departments' => function ($query) { } ]) ->select()->toArray(); $role_id = array_unique(array_column($campus_person_role, 'role_id')); $dept_id = array_unique(array_column($campus_person_role, 'dept_id')); //查询用户角色 $role = SysRole::whereIn('role_id', $role_id)->where('status', 1)->field('role_id,role_name,role_key,addon_keys,status')->select()->toArray(); $role_name_arr = []; $role_key_arr = []; foreach ($role as $v) { $role_name_arr[] = $v['role_name']; $role_key_arr[] = $v['role_key']; } $role_name_str = implode(',', $role_name_arr);// //校区部门 // 初始化新的校区-部门合并数组 和 部门名称字符串 $cameus_dept_arr = []; $department_name_arr = []; foreach ($campus_person_role as $v) { $campusId = $v['campus_id']; $deptId = $v['dept_id']; $deptName = $v['dept_id_name']; // 收集部门名称 $department_name_arr[] = $deptName; // 构建校区+部门结构 if (!isset($cameus_dept_arr[$campusId])) { $cameus_dept_arr[$campusId] = [ 'campus_id' => $campusId, 'campus_id_name' => $v['campus_id_name'], 'dept_arr' => [] ]; } // 检查去重 $exists = false; foreach ($cameus_dept_arr[$campusId]['dept_arr'] as $dept) { if ($dept['dept_id'] === $deptId) { $exists = true; break; } } if (!$exists) { $cameus_dept_arr[$campusId]['dept_arr'][] = [ 'dept_id' => $deptId, 'dept_name' => $deptName ]; } } // 最终转换 $cameus_dept_arr = array_values($cameus_dept_arr); $department_name_str = implode(',', $department_name_arr); if ($data) { $data = $data->toArray(); $data['role'] = $role; $data['role_name_str'] = $role_name_str; $data['role_key_arr'] = $role_key_arr; $data['department_name_str'] = $department_name_str; $data['cameus_dept_arr'] = $cameus_dept_arr;//校区+部门的组合 $res['code'] = 1; $res['msg'] = '操作成功'; $res['data'] = $data; } else { $data = []; $res['code'] = 0; $res['msg'] = '未找到数据'; $res['data'] = $data; } return $res; } //员工信息-修改 public function edit(array $where, array $data) { $data['update_time'] = date('Y-m-d H:i:s'); if (!$where) { return [ 'code' => 0, 'msg' => '查询条件不能为空' ]; } $model = $this->model; if (!empty($where['id'])) { $model = $model->where('id', $where['id']); } $res = $model->update($data);//员工信息 if ($res) { $res = [ 'code' => 1, 'msg' => '操作成功' ]; } else { $res = [ 'code' => 0, 'msg' => '操作失败' ]; } return $res; } //员工信息-获取全部用户 public function getAll(array $where, string $field = '*') { if (!$where) { return [ 'code' => 0, 'msg' => '查询条件不能为空' ]; } $model = $this->model; //存在员工id的时候 // if ((!empty($where['personnel_id']) || isset($where['personnel_id'])) && $where['personnel_id'] !== '') { // //查询这个员工的校区id // $campus_id = CampusPersonRole::where('person_id', $where['personnel_id']) // ->distinct(true) // ->column('campus_id'); // if ($campus_id[0]) { // // $person_id_arr = CampusPersonRole::whereIn('campus_id', $campus_id) // ->where(['dept_id' => 3]) // ->distinct(true) // ->column('person_id'); // if ($person_id_arr) { // //根据校区id获取校区下的全部员工 // $model = $model->whereIn('id', $person_id_arr); // } // }else{ // $person_id_arr = CampusPersonRole::whereIn('campus_id', $campus_id) // ->where(['dept_id' => 3]) // ->distinct(true) // ->column('person_id'); // if ($person_id_arr) { // //根据校区id获取校区下的全部员工 // $model = $model->whereIn('id', $person_id_arr); // } // } // } // // if (empty($where['account_type'])) { // $model = $model->where('account_type', $where['account_type']); // } // // if (!empty($where['campus'])) { //// $model = $model->where('campus', $where['campus']); // // $person_id_arr = CampusPersonRole::whereIn('campus_id', $where['campus']) // ->distinct(true) // ->where(['dept_id' => 3]) // ->column('person_id'); // // if ($person_id_arr) { // //根据校区id获取校区下的全部员工 // $model = $model->whereIn('id', $person_id_arr); // } // // } $res = $model->field($field) ->select() ->toArray();//员工信息 return $res; } //验证旧密码 public function checkOldPwd(string $old_passowrd) { $res = [ 'code' => 0, 'msg' => '操作失败', 'data' => [] ]; $personnel_id = $this->member_id; $phone = $this->model->where('id', $personnel_id)->value('phone'); $password = (new SysUser())->where('username', $phone)->value('password'); if (!check_password($old_passowrd, $password)) { $res['msg'] = '旧密码错误'; return $res; } $res['code'] = 1; $res['msg'] = '密码正确'; $res['data'] = [ 'key_value' => $this->setEditPasswordKey($phone) ]; return $res; } //设置新密码 public function edidPassword($phone, $new_password, $key_value) { $checkEditPasswordKey = $this->checkEditPasswordKey($phone, $key_value);//验证修改密码允许的缓存key if (!$checkEditPasswordKey['code']) { return $checkEditPasswordKey; } //查询用户修改密码 $update = (new SysUser())->where('username', $phone)->update([ 'password' => create_password($new_password),//创建密码 'update_time' => time(), ]); if (!$update) { $res = [ 'code' => 0, 'msg' => '操作失败', 'data' => [] ]; } else { $res = [ 'code' => 1, 'msg' => '操作成功', 'data' => [] ]; } return $res; } //生成修改密码允许的缓存key public function setEditPasswordKey(string $phone) { $key_name = 'edit_password_' . $phone; //生成字符串,存入cache中 //check_password()//验证 //create_password()//创建 $key_value = create_password($key_name); // 缓存在3600秒之后过期 Cache::set($key_name, $key_value, 3600); return $key_value; } //验证修改密码允许的缓存key public function checkEditPasswordKey(string $phone, string $key_value) { $res = [ 'code' => 0, 'msg' => '' ]; $key_name = 'edit_password_' . $phone; $key_value_cache = Cache::get($key_name); if (empty($key_value_cache)) { $res['msg'] = '参数已过期,请重新输入旧密码进行验证'; return $res; } //验证 if ($key_value_cache != $key_value) { $res['msg'] = '参数不正确,请重新输入旧密码进行验证'; return $res; } $res['code'] = 1; $res['msg'] = '操作成功'; return $res; } /** * 获取会员的模型对象(todo 慎用!!! 现主要用于登录) * @param array $data * @return Member|array|mixed|Model !!! 仔细看,返回值是模型对象 如果想要判断是否为空 请用 $member->isEmpty() */ public function findMemberInfo(array $data) { //会员账号 if (!empty($data['username'])) $where[] = ['username', '=', $data['username']]; //会员手机号 if (!empty($data['mobile'])) $where[] = ['mobile', '=', $data['mobile']]; //会员id if (!empty($data['id'])) $where[] = ['id', '=', $data['id']]; //微信公众号openid if (!empty($data['wx_openid'])) $where[] = ['wx_openid', '=', $data['wx_openid']]; //微信小程序openid if (!empty($data['weapp_openid'])) $where[] = ['weapp_openid', '=', $data['weapp_openid']]; // 微信unionid if (!empty($data['wx_unionid'])) $where[] = ['wx_unionid', '=', $data['wx_unionid']]; if (!empty($data['username|mobile'])) $where[] = ['username|mobile', '=', $data['username|mobile']]; if (empty($where)) { $where[] = ['member_id', '=', -1]; } return $this->model->where($where)->findOrEmpty(); } public function reimbursement_list(){ $reimbursement = new Reimbursement(); $data = $reimbursement->where(['applicant_id' => $this->member_id])->select()->toArray(); return $data; } public function reimbursement_info(array $data){ $id = $data['id']; $reimbursement = new Reimbursement(); $info = $reimbursement->where(['id' =>$id ])->find()->toArray(); return $info; } public function reimbursement_add(array $data){ $reimbursement = new Reimbursement(); if($data['id']){ $reimbursement->where(['id' => $data['id']])->update([ 'applicant_id' => $this->member_id, 'amount' => $data['amount'], 'description' => $data['description'], 'receipt_url' => $data['receipt_url'] ]); }else{ $reimbursement->insert([ 'applicant_id' => $this->member_id, 'amount' => $data['amount'], 'description' => $data['description'], 'receipt_url' => $data['receipt_url'] ]); } return true; } /** * 添加新员工信息 * @param array $data * @return array */ public function addPersonnel(array $data) { $res = [ 'code' => 0, 'msg' => '添加失败', 'data' => [] ]; try { // 开启事务 $this->model->startTrans(); // 检查手机号是否已存在 $existingPersonnel = $this->model->where('phone', $data['phone'])->find(); if ($existingPersonnel) { $res['msg'] = '该手机号已存在,请更换'; return $res; } // 准备基本员工信息数据 $personnelData = [ 'name' => $data['name'], 'head_img' => $data['head_img'] ?? '', 'gender' => intval($data['gender']), 'birthday' => $data['birthday'] ?? null, 'phone' => $data['phone'], 'email' => $data['email'] ?? '', 'wx' => $data['wx'] ?? '', 'address' => $data['current_address'] ?? '', 'native_place' => $data['native_place'] ?? '', 'education' => $data['education'] ?? '', 'profile' => $data['remark'] ?? '', 'emergency_contact_phone' => $data['emergency_phone'] ?? '', 'employee_number' => $this->generateEmployeeNumber(), 'status' => 1, 'is_sys_user' => 0, 'account_type' => $data['account_type'], 'create_time' => date('Y-m-d H:i:s'), 'update_time' => date('Y-m-d H:i:s'), 'join_time' => $data['join_time'] ?? date('Y-m-d H:i:s'), 'delete_time' => 0 ]; // 插入员工基本信息 $personnelId = $this->model->insertGetId($personnelData); if (!$personnelId) { $this->model->rollback(); $res['msg'] = '添加员工基本信息失败'; return $res; } // 如果有详细信息,插入到personnel_info表 if ($this->hasDetailInfo($data)) { $detailData = [ 'person_id' => $personnelId, 'name' => $data['name'], 'ethnicity' => $data['ethnicity'] ?? '', 'birthday' => $data['birthday'] ?? '', 'age' => $data['age'] ?? null, 'politics' => $data['politics'] ?? '', 'university' => $data['university'] ?? '', 'education' => $data['education'] ?? '', 'major' => $data['major'] ?? '', 'graduation_date' => $data['graduation_date'] ?? '', 'native_place' => $data['native_place'] ?? '', 'household_place' => $data['household_place'] ?? '', 'household_type' => $data['household_type'] ?? '', 'household_address' => $data['household_address'] ?? '', 'current_address' => $data['current_address'] ?? '', 'emergency_contact' => $data['emergency_contact'] ?? '', 'emergency_phone' => $data['emergency_phone'] ?? '', 'marital_status' => $data['marital_status'] ?? '', 'bank_card' => $data['bank_card'] ?? '', 'bank_name' => $data['bank_name'] ?? '', 'remark' => $data['remark'] ?? '', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]; $insertResult = \think\facade\Db::name('school_personnel_info')->insert($detailData); if (!$insertResult) { $this->model->rollback(); $res['msg'] = '添加员工详细信息失败'; return $res; } } // 提交事务 $this->model->commit(); $res = [ 'code' => 1, 'msg' => '添加员工信息成功', 'data' => [ 'personnel_id' => $personnelId, 'employee_number' => $personnelData['employee_number'] ] ]; } catch (\Exception $e) { $this->model->rollback(); $res['msg'] = '添加员工信息异常:' . $e->getMessage(); } return $res; } /** * 检查是否有详细信息需要保存 * @param array $data * @return bool */ private function hasDetailInfo(array $data): bool { $detailFields = [ 'ethnicity', 'age', 'politics', 'university', 'education', 'major', 'graduation_date', 'household_place', 'household_type', 'household_address', 'emergency_contact', 'emergency_phone', 'marital_status', 'bank_card', 'bank_name' ]; foreach ($detailFields as $field) { if (!empty($data[$field])) { return true; } } return false; } /** * 生成员工编号 * @return string */ private function generateEmployeeNumber(): string { $prefix = 'EMP'; $date = date('Ymd'); // 查询今天已生成的最大编号 $maxNumber = $this->model ->where('employee_number', 'like', $prefix . $date . '%') ->max('employee_number'); if ($maxNumber) { // 提取序号并加1 $sequence = intval(substr($maxNumber, -4)) + 1; } else { // 今天第一个员工 $sequence = 1; } return $prefix . $date . str_pad($sequence, 4, '0', STR_PAD_LEFT); } /** * 获取教练数据列表 * @return array */ public function getCoachList() { $res = [ 'code' => 0, 'msg' => '获取教练列表失败', 'data' => [] ]; try { // 查询dept_id=2的所有人员关系 $campusPersonRoles = CampusPersonRole::where('dept_id', 2) ->field('person_id') ->select() ->toArray(); if (empty($campusPersonRoles)) { $res = [ 'code' => 1, 'msg' => '暂无教练数据', 'data' => [ 'coach_list' => [], 'education_list' => [], 'assistant_list' => [] ] ]; return $res; } // 提取所有person_id $personIds = array_unique(array_column($campusPersonRoles, 'person_id')); // 根据person_id查询Personnel表获取姓名、头像、手机号 $personnelList = $this->model ->whereIn('id', $personIds) ->field('id as person_id, name, head_img, phone') ->select() ->toArray(); if (empty($personnelList)) { $res = [ 'code' => 1, 'msg' => '暂无教练数据', 'data' => [ 'coach_list' => [], 'education_list' => [], 'assistant_list' => [] ] ]; return $res; } // 查询每个人员的具体角色信息 $coachList = []; $educationList = []; $assistantList = []; foreach ($personnelList as $personnel) { // 查询该人员的角色信息 $roles = CampusPersonRole::where('person_id', $personnel['person_id']) ->where('dept_id', 2) ->with(['sysRole' => function ($query) { $query->field('role_id, role_name, role_key'); }]) ->select() ->toArray(); $roleNames = []; $roleKeys = []; foreach ($roles as $role) { if (!empty($role['sys_role'])) { $roleNames[] = $role['sys_role']['role_name']; $roleKeys[] = $role['sys_role']['role_key']; } } $roleNameStr = implode(',', $roleNames); $roleKeyStr = implode(',', $roleKeys); // 根据角色名称分类 $personnelData = [ 'id' => $personnel['person_id'], 'person_id' => $personnel['person_id'], // 确保person_id字段存在 'name' => $personnel['name'], 'head_img' => $personnel['head_img'], 'phone' => $personnel['phone'], 'role_names' => $roleNameStr, 'role_keys' => $roleKeyStr ]; // 根据角色进行分类 - 使用更灵活的匹配规则 $isCoach = false; $isEducation = false; $isAssistant = false; // 检查是否为教练类角色 if (stripos($roleNameStr, '教练') !== false || stripos($roleNameStr, '教师') !== false || stripos($roleNameStr, '主教') !== false || stripos($roleKeyStr, 'coach') !== false || stripos($roleKeyStr, 'teacher') !== false) { $isCoach = true; } // 检查是否为教务类角色 if (stripos($roleNameStr, '教务') !== false || stripos($roleKeyStr, 'education') !== false || stripos($roleKeyStr, 'academic') !== false) { $isEducation = true; } // 检查是否为助教类角色 if (stripos($roleNameStr, '助教') !== false || stripos($roleNameStr, '助理') !== false || stripos($roleKeyStr, 'assistant') !== false) { $isAssistant = true; } // 如果没有明确的角色分类,则作为通用人员添加到所有列表 if (!$isCoach && !$isEducation && !$isAssistant) { $isCoach = $isEducation = $isAssistant = true; } // 根据分类结果添加到对应列表 if ($isCoach) { $coachList[] = $personnelData; } if ($isEducation) { $educationList[] = $personnelData; } if ($isAssistant) { $assistantList[] = $personnelData; } } $res = [ 'code' => 1, 'msg' => '获取成功', 'data' => [ 'coach_list' => $coachList, 'education_list' => $educationList, 'assistant_list' => $assistantList, 'all_personnel' => $personnelList // 返回所有人员数据,前端可以根据需要自行分类 ] ]; } catch (\Exception $e) { $res['msg'] = '获取教练列表异常:' . $e->getMessage(); } return $res; } /** * 获取教练列表(用于添加课程安排) * @param array $data * @return array */ public function getCoachListForSchedule(array $data) { try { $where = []; // 查询条件:dept_id=2(教练部门) $campusPersonWhere = ['dept_id' => 2]; // 校区筛选 if (!empty($data['campus_id'])) { $campusPersonWhere['campus_id'] = $data['campus_id']; } // 查询符合条件的教练人员ID $coachPersonIds = CampusPersonRole::where($campusPersonWhere) ->column('person_id'); if (empty($coachPersonIds)) { return [ 'code' => 1, 'msg' => '暂无教练数据', 'data' => [] ]; } // 构建人员表查询条件 $where[] = ['id', 'in', $coachPersonIds]; // 姓名关键词搜索 if (!empty($data['keyword'])) { $where[] = ['name', 'like', '%' . $data['keyword'] . '%']; } // 状态筛选,默认获取有效教练 if (isset($data['status'])) { $where[] = ['status', '=', $data['status']]; } // 只获取未逻辑删除的教练 $where[] = ['deleted_at', '=', 0]; $coachList = $this->model ->where($where) ->field('id, name, head_img, phone, employee_number') ->order('create_time DESC') ->select() ->toArray(); return [ 'code' => 1, 'msg' => '获取成功', 'data' => $coachList ]; } catch (\Exception $e) { return [ 'code' => 0, 'msg' => '获取教练列表失败:' . $e->getMessage(), 'data' => [] ]; } } }