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;//校区+部门的组合 // 获取员工详细信息 $personnelInfo = (new PersonnelInfo())->where(['person_id' => $where['id']])->findOrEmpty()->toArray(); $data['info'] = $personnelInfo; $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) { if (!$where) { return [ 'code' => 0, 'msg' => '查询条件不能为空' ]; } try { // 开启事务 \think\facade\Db::startTrans(); // 分离基础信息和详细信息 $personnelData = []; $infoData = []; // 基础信息字段 $baseFields = ['head_img', 'name', 'gender', 'birthday', 'phone', 'email', 'wx', 'address', 'native_place', 'education', 'profile', 'emergency_contact_phone', 'id_card_front', 'id_card_back']; foreach ($baseFields as $field) { if (isset($data[$field])) { $personnelData[$field] = $data[$field]; } } // 添加更新时间 $personnelData['update_time'] = date('Y-m-d H:i:s'); // 更新基础信息 $model = $this->model; if (!empty($where['id'])) { $model = $model->where('id', $where['id']); } $updateResult = $model->update($personnelData); // 处理详细信息 if (isset($data['info']) && is_array($data['info'])) { $infoData = $data['info']; $infoData['updated_at'] = date('Y-m-d H:i:s'); // 检查是否已存在详细信息记录 $existingInfo = (new PersonnelInfo())->where(['person_id' => $where['id']])->findOrEmpty(); if ($existingInfo->isEmpty()) { // 创建新记录 $infoData['person_id'] = $where['id']; $infoData['created_at'] = date('Y-m-d H:i:s'); (new PersonnelInfo())->insert($infoData); } else { // 更新现有记录 (new PersonnelInfo())->where(['person_id' => $where['id']])->update($infoData); } } // 提交事务 \think\facade\Db::commit(); return [ 'code' => 1, 'msg' => '操作成功' ]; } catch (\Exception $e) { // 回滚事务 \think\facade\Db::rollback(); return [ 'code' => 0, 'msg' => '操作失败:' . $e->getMessage() ]; } } //员工信息-获取全部用户 public function getAll(array $where, string $field = '*') { if (!$where) { return [ 'code' => 0, 'msg' => '查询条件不能为空' ]; } $model = $this->model; $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'], 'status' => 'pending' ]); } return true; } /** * 添加报销申请(带审批流程) * @param array $data * @return array */ public function reimbursementAddWithApproval(array $data) { $res = [ 'code' => 0, 'msg' => '添加失败', 'data' => [] ]; try { // 开启事务 \think\facade\Db::startTrans(); // 创建报销记录 $reimbursementData = [ 'applicant_id' => $this->member_id, 'amount' => $data['amount'], 'description' => $data['description'], 'receipt_url' => $data['receipt_url'] ?? '', 'status' => 'pending', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]; $reimbursement = new Reimbursement(); $reimbursementId = $reimbursement->insertGetId($reimbursementData); if (!$reimbursementId) { \think\facade\Db::rollback(); $res['msg'] = '创建报销记录失败'; return $res; } // 检查是否使用审批流程 if (isset($data['use_approval']) && $data['use_approval'] && isset($data['approval_config_id']) && $data['approval_config_id'] > 0) { // 启动审批流程 $approvalService = new \app\service\school_approval\SchoolApprovalProcessService(); $processData = [ 'process_name' => '报销申请 - ' . $data['description'], 'applicant_id' => $this->member_id, 'remarks' => $data['remarks'] ?? '报销申请', 'business_type' => 'expense_approval', 'business_id' => $reimbursementId, 'business_data' => $data ]; $processId = $approvalService->create($processData, $data['approval_config_id']); // 更新报销记录关联审批流程 $reimbursement->where(['id' => $reimbursementId])->update([ 'process_id' => $processId ]); \think\facade\Db::commit(); $res = [ 'code' => 1, 'msg' => '报销申请已提交,等待审批', 'data' => [ 'type' => 'approval', 'reimbursement_id' => $reimbursementId, 'process_id' => $processId ] ]; } else { // 直接创建(无审批) \think\facade\Db::commit(); $res = [ 'code' => 1, 'msg' => '报销记录创建成功', 'data' => [ 'type' => 'direct', 'reimbursement_id' => $reimbursementId ] ]; } } catch (\Exception $e) { \think\facade\Db::rollback(); $res['msg'] = '创建报销申请失败:' . $e->getMessage(); } return $res; } /** * 添加新员工信息 * @param array $data * @return array */ public function addPersonnel(array $data) { $res = [ 'code' => 0, 'msg' => '添加失败', 'data' => [] ]; // 检查手机号是否已存在 $existingPersonnel = $this->model->where('phone', $data['phone'])->find(); if ($existingPersonnel) { $res['msg'] = '该手机号已存在,请更换'; return $res; } try { // 开启事务 $this->model->startTrans(); // 准备基本员工信息数据 $personnelData = [ 'name' => $data['name'], 'head_img' => $data['head_img'] ?? '', 'gender' => intval($data['gender']), 'birthday' => !empty($data['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' => $data['status'] ?? 1, // 使用传入的状态,默认为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'), 'deleted_at' => 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'] ?? '', 'age' => $data['age'] ?? null, 'politics' => $data['politics'] ?? '', 'university' => $data['university'] ?? '', 'education' => $data['education'] ?? '', 'major' => $data['major'] ?? '', 'graduation_date' => $data['graduation_date'] ?? '', '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::table('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 权限里有校区 id 则使用权限内的班级数据 if (empty($data['campus_id']) && $this->campus_id) { $campusPersonWhere[] = ['campus_id', '=', $this->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' => [] ]; } } /** * 获取职位类型列表 * @return array */ public function getRoleTypes(): array { try { $roleTypes = SysRole::where('status', 1) ->field('role_id, role_name, role_key') ->order('role_id ASC') ->select() ->toArray(); return [ 'code' => 1, 'msg' => '获取成功', 'data' => $roleTypes ]; } catch (\Exception $e) { return [ 'code' => 0, 'msg' => '获取职位类型失败:' . $e->getMessage(), 'data' => [] ]; } } /** * 获取销售部门员工列表(根据校区筛选) * @param string $campus * @return array */ public function getSalesPersonnelByCampus($campus = '') { try { $where = []; // 排除的角色key列表 $excludeRoleKeys = ['teacher', 'market', 'market_manager', 'teacher_manager', 'superadmin', 'finance_role']; // 查询除了排除角色外的所有角色ID $roleIds = SysRole::where('status', 1) ->where('role_key', 'not in', $excludeRoleKeys) ->column('role_id'); if (empty($roleIds)) { return [ 'code' => 1, 'msg' => '暂无符合条件的角色', 'data' => [] ]; } // 构建校区人员角色关系查询条件 $campusPersonWhere = [ ['role_id', 'in', $roleIds] ]; // 根据传入的校区进行筛选 if (!empty($campus)) { $campusPersonWhere[] = ['campus_id', '=', $campus]; } // 查询符合条件的人员ID $personIds = CampusPersonRole::where($campusPersonWhere) ->column('person_id'); if (empty($personIds)) { return [ 'code' => 1, 'msg' => '暂无符合条件的人员数据', 'data' => [] ]; } // 从personnel表中获取人员信息,包含姓名和电话 $personnel = $this->model ->whereIn('id', $personIds) ->where('deleted_at', 0) // 只获取未删除的人员 ->field('id, name, phone') ->order('create_time DESC') ->select() ->toArray(); return [ 'code' => 1, 'msg' => '获取成功', 'data' => $personnel ]; } catch (\Exception $e) { return [ 'code' => 0, 'msg' => '获取人员列表失败:' . $e->getMessage(), 'data' => [] ]; } } }