model = new LessonCourseTeaching(); // 加载教研管理配置 $this->config = config('teaching_management.teaching_management', []); } /** * 获取模块配置 * @param string|null $moduleKey * @return array|null */ private function getModuleConfig(?string $moduleKey = null) { if ($moduleKey) { return $this->config['module_configs'][$moduleKey] ?? null; } return $this->config['module_configs'] ?? []; } /** * 根据table_type获取模块配置 * @param int $tableType * @return array|null */ private function getModuleConfigByTableType(int $tableType): ?array { $configs = $this->getModuleConfig(); foreach ($configs as $key => $config) { if ($config['table_type'] === $tableType) { return $config; } } return null; } /** * 获取所有模块配置(公共方法,供外部调用) * @return array */ public function getAllModuleConfigs(): array { return $this->getModuleConfig(); } /** * 统一获取列表数据 * @param array $where * @return array */ public function getPage(array $where = []) { $field = 'id,title,image,type,url,content,status,create_time,update_time,delete_time,table_type,user_permission,user_permission_name,exam_papers_id'; $order = 'id desc'; $search_model = $this->model->withSearch(["title","status","create_time","update_time","table_type"], $where) ->field($field) ->order($order); $list = $this->pageQuery($search_model); // 处理用户权限回显数据 if (!empty($list['data'])) { foreach ($list['data'] as &$item) { $item['user_permission_list'] = $this->parseUserPermissionToList($item['user_permission']); } } return $list; } /** * 解析用户权限字段为列表格式 * @param string|null $userPermission * @return array */ private function parseUserPermissionToList(?string $userPermission): array { if (empty($userPermission)) { return []; } try { $personIds = array_filter(explode(',', $userPermission)); if (empty($personIds)) { return []; } // 获取人员详细信息 $personnel = Db::table('school_personnel') ->whereIn('id', $personIds) ->where('deleted_at', 0) ->field('id,name,phone') ->select() ->toArray(); return $personnel; } catch (\Exception $e) { Log::error("解析用户权限失败: " . $e->getMessage()); return []; } } /** * 统一新增数据 * @param array $data * @return mixed */ public function add(array $data) { try { // 处理用户权限字段 if (isset($data['user_permission']) && is_array($data['user_permission'])) { $data['user_permission'] = implode(',', $data['user_permission']); } // 检查是否需要自动分发 $tableType = $data['table_type'] ?? 0; $moduleConfig = $this->getModuleConfigByTableType($tableType); if ($moduleConfig && ($moduleConfig['auto_distribute'] ?? false)) { $autoPermissions = $this->getCoachPersonnel(); if (!empty($autoPermissions)) { // 合并用户指定的权限和自动分发的权限 $existingPermissions = !empty($data['user_permission']) ? explode(',', $data['user_permission']) : []; $allPermissions = array_unique(array_merge($existingPermissions, $autoPermissions['person_ids'])); $data['user_permission'] = implode(',', $allPermissions); $data['user_permission_name'] = implode(',', $autoPermissions['person_names']); Log::info("自动分发人员权限", [ 'table_type' => $tableType, 'module' => $moduleConfig['name'], 'permissions' => $data['user_permission'], 'names' => $data['user_permission_name'] ]); } } $res = $this->model->create($data); return $res->id; } catch (\Exception $e) { Log::error("添加教研管理数据失败: " . $e->getMessage()); throw new \Exception("添加失败: " . $e->getMessage()); } } /** * 统一编辑数据 * @param int $id * @param array $data * @return bool */ public function edit(int $id, array $data) { try { // 获取现有记录 $existingRecord = $this->model->where('id', $id)->find(); if (!$existingRecord) { throw new \Exception("记录不存在"); } // 处理用户权限字段 if (isset($data['user_permission']) && is_array($data['user_permission'])) { $data['user_permission'] = implode(',', $data['user_permission']); } // 检查是否需要重新同步自动分发权限 $tableType = $existingRecord['table_type']; $moduleConfig = $this->getModuleConfigByTableType($tableType); if ($moduleConfig && ($moduleConfig['auto_distribute'] ?? false)) { $autoPermissions = $this->getCoachPersonnel(); if (!empty($autoPermissions)) { // 获取当前用户指定的权限 $userPermissions = !empty($data['user_permission']) ? explode(',', $data['user_permission']) : []; // 获取之前自动分发的权限(从现有记录中获取) $existingPermissions = !empty($existingRecord['user_permission']) ? explode(',', $existingRecord['user_permission']) : []; // 重新合并:用户权限 + 最新的教练部人员 $allPermissions = array_unique(array_merge($userPermissions, $autoPermissions['person_ids'])); $data['user_permission'] = implode(',', $allPermissions); $data['user_permission_name'] = implode(',', $autoPermissions['person_names']); Log::info("编辑时重新同步人员权限", [ 'id' => $id, 'table_type' => $tableType, 'module' => $moduleConfig['name'], 'old_permissions' => $existingRecord['user_permission'], 'new_permissions' => $data['user_permission'] ]); } } $this->model->where('id', $id)->update($data); return true; } catch (\Exception $e) { Log::error("编辑教研管理数据失败: " . $e->getMessage()); throw new \Exception("编辑失败: " . $e->getMessage()); } } /** * 获取教练部人员列表(用于自动分发) * @return array|null */ private function getCoachPersonnel(): ?array { try { $coachDeptId = $this->config['coach_department_id'] ?? 24; // 1. 查询教练部对应的角色 $roles = Db::table('school_sys_role') ->where('dept_id', $coachDeptId) ->column('role_id'); Log::info("查询教练部角色", ['dept_id' => $coachDeptId, 'roles' => $roles]); if (empty($roles)) { Log::warning("教练部未找到对应角色", ['dept_id' => $coachDeptId]); return null; } // 2. 查询这些角色对应的人员 $personnel = Db::table('school_campus_person_role') ->alias('cpr') ->join('school_personnel pd', 'cpr.person_id = pd.id') ->whereIn('cpr.role_id', $roles) ->where('pd.deleted_at', 0) ->field('pd.id,pd.name,pd.phone') ->group('pd.id') ->select() ->toArray(); if (empty($personnel)) { Log::warning("教练部角色未找到对应人员", ['role_ids' => $roles]); return null; } $personIds = array_column($personnel, 'id'); $personNames = array_column($personnel, 'name'); return [ 'person_ids' => $personIds, 'person_names' => $personNames, 'personnel_data' => $personnel ]; } catch (\Exception $e) { Log::error("获取教练部人员失败: " . $e->getMessage()); return null; } } /** * 批量更新指定模块的人员权限(定时任务使用) * @param array|null $tableTypes 要更新的table_type数组,null表示更新所有需要自动分发的模块 * @return array */ public function batchUpdatePersonnelPermissions(?array $tableTypes = null): array { $result = [ 'success' => 0, 'failed' => 0, 'total' => 0, 'errors' => [] ]; try { // 获取需要自动分发的模块 $autoDistributeModules = []; foreach ($this->getModuleConfig() as $key => $config) { if ($config['auto_distribute'] ?? false) { if ($tableTypes === null || in_array($config['table_type'], $tableTypes)) { $autoDistributeModules[] = $config['table_type']; } } } if (empty($autoDistributeModules)) { return $result; } // 获取最新的教练部人员 $autoPermissions = $this->getCoachPersonnel(); if (!$autoPermissions) { $result['errors'][] = "无法获取教练部人员信息"; return $result; } // 批量更新记录 $records = $this->model->whereIn('table_type', $autoDistributeModules)->select(); $result['total'] = count($records); foreach ($records as $record) { try { // 保留原有用户指定的权限,更新教练部权限 $existingPermissions = !empty($record['user_permission']) ? explode(',', $record['user_permission']) : []; $allPermissions = array_unique(array_merge($existingPermissions, $autoPermissions['person_ids'])); $this->model->where('id', $record['id'])->update([ 'user_permission' => implode(',', $allPermissions), 'user_permission_name' => implode(',', $autoPermissions['person_names']), 'update_time' => date('Y-m-d H:i:s') ]); $result['success']++; } catch (\Exception $e) { $result['failed']++; $result['errors'][] = "ID {$record['id']}: " . $e->getMessage(); } } Log::info("批量更新人员权限完成", $result); } catch (\Exception $e) { Log::error("批量更新人员权限失败: " . $e->getMessage()); $result['errors'][] = $e->getMessage(); } return $result; } // =================== 兼容性方法:保持原有方法名的简单封装 =================== /** * 跳绳教案库列表(兼容性方法) */ public function jumpPetPage(array $where = []) { $where['table_type'] = 2; return $this->getPage($where); } /** * 跳绳教案库新增(兼容性方法) */ public function jumpAdd(array $data) { $data['table_type'] = 2; return $this->add($data); } /** * 跳绳教案库编辑(兼容性方法) */ public function jumpEdit(int $id, array $data) { return $this->edit($id, $data); } /** * 增高教案库列表(兼容性方法) */ public function enPetPage(array $where = []) { $where['table_type'] = 3; return $this->getPage($where); } /** * 增高教案库新增(兼容性方法) */ public function enAdd(array $data) { $data['table_type'] = 3; return $this->add($data); } /** * 增高教案库编辑(兼容性方法) */ public function enEdit(int $id, array $data) { return $this->edit($id, $data); } /** * 篮球教案库列表(兼容性方法) */ public function basketballPetPage(array $where = []) { $where['table_type'] = 4; return $this->getPage($where); } /** * 篮球教案库新增(兼容性方法) */ public function basketballAdd(array $data) { $data['table_type'] = 4; return $this->add($data); } /** * 篮球教案库编辑(兼容性方法) */ public function basketballEdit(int $id, array $data) { return $this->edit($id, $data); } /** * 强化教案库列表(兼容性方法) */ public function strengPetPage(array $where = []) { $where['table_type'] = 5; return $this->getPage($where); } /** * 强化教案库新增(兼容性方法) */ public function strengAdd(array $data) { $data['table_type'] = 5; return $this->add($data); } /** * 强化教案库编辑(兼容性方法) */ public function strengEdit(int $id, array $data) { return $this->edit($id, $data); } /** * 空中忍者教案库列表(兼容性方法) */ public function ninjaPetPage(array $where = []) { $where['table_type'] = 6; return $this->getPage($where); } /** * 空中忍者教案库新增(兼容性方法) */ public function ninjaAdd(array $data) { $data['table_type'] = 6; return $this->add($data); } /** * 空中忍者教案库编辑(兼容性方法) */ public function ninjaEdit(int $id, array $data) { return $this->edit($id, $data); } /** * 少儿安防教案库列表(兼容性方法) */ public function securityPetPage(array $where = []) { $where['table_type'] = 7; return $this->getPage($where); } /** * 少儿安防教案库新增(兼容性方法) */ public function securityAdd(array $data) { $data['table_type'] = 7; return $this->add($data); } /** * 少儿安防教案库编辑(兼容性方法) */ public function securityEdit(int $id, array $data) { return $this->edit($id, $data); } /** * 体能教案库列表(兼容性方法) */ public function physicalPetPage(array $where = []) { $where['table_type'] = 8; return $this->getPage($where); } /** * 体能教案库新增(兼容性方法) */ public function physicalAdd(array $data) { $data['table_type'] = 8; return $this->add($data); } /** * 体能教案库编辑(兼容性方法) */ public function physicalEdit(int $id, array $data) { return $this->edit($id, $data); } // =================== 其他原有方法(保持不变) =================== /** * 获取热身动做列表 */ public function actionPetPage(array $where = []) { $where['table_type'] = 9; // 假设热身动作库的table_type为9 return $this->getPage($where); } /** * 添加热身动做 */ public function actionAdd(array $data) { $data['table_type'] = 9; return $this->add($data); } /** * 热身动做编辑 */ public function actionEdit(int $id, array $data) { return $this->edit($id, $data); } /** * 获取趣味游戏列表 */ public function gamesPetPage(array $where = []) { $where['table_type'] = 10; return $this->getPage($where); } /** * 添加趣味游戏 */ public function gamesAdd(array $data) { $data['table_type'] = 10; return $this->add($data); } /** * 趣味游戏编辑 */ public function gamesEdit(int $id, array $data) { return $this->edit($id, $data); } /** * 获取体能动作列表 */ public function fitnessPetPage(array $where = []) { $where['table_type'] = 11; return $this->getPage($where); } /** * 添加体能动作 */ public function fitnessAdd(array $data) { $data['table_type'] = 11; return $this->add($data); } /** * 体能动作编辑 */ public function fitnessEdit(int $id, array $data) { return $this->edit($id, $data); } /** * 获取放松游戏列表 */ public function relaxationPetPage(array $where = []) { $where['table_type'] = 12; return $this->getPage($where); } /** * 添加放松游戏 */ public function relaxationAdd(array $data) { $data['table_type'] = 12; return $this->add($data); } /** * 放松游戏编辑 */ public function relaxationEdit(int $id, array $data) { return $this->edit($id, $data); } /** * 公共库相关方法 */ public function publicPetPage(array $where = []) { return $this->getPage($where); } public function publicAdd(array $data) { return $this->add($data); } public function publicEdit(int $id, array $data) { return $this->edit($id, $data); } /** * 绑定模块 */ public function bindingModuleAdd(int $id, array $data) { $this->model->where([['id', '=', $id]])->update($data); return true; } /** * 获取教研管理信息 */ public function getInfo(int $id) { $field = 'id,title,image,type,url,content,status,create_time,update_time,delete_time,table_type,user_permission,user_permission_name,url,exam_papers_id'; $info = $this->model->field($field)->where([['id', "=", $id]])->findOrEmpty()->toArray(); $info['status'] = strval($info['status']); $info['type'] = strval($info['type']); // 添加用户权限列表 $info['user_permission_list'] = $this->parseUserPermissionToList($info['user_permission']); return $info; } /** * 删除教研管理 */ public function del(int $id) { $model = $this->model->where([['id', '=', $id]])->find(); $res = $model->delete(); return $res; } /** * 获取人员数据 */ public function getPersonnelDataAll(array $where = []) { $personnelDataModel = new PersonnelData(); $order = 'a.id desc'; $whereArr = []; if (!empty($where['name'])) { $whereArr[] = ['a.name','like',"%".$where['name']."%"]; } if (!empty($where['phone'])) { $whereArr[] = ['a.phone','like',"%".$where['phone']."%"]; } if (!empty($where['role_id'])) { $whereArr[] = ['b.role_id','=',$where['role_id']]; } if (!empty($where['dept_id'])) { $whereArr[] = ['b.dept_id','=',$where['dept_id']]; } $search_model = $personnelDataModel ->alias("a") ->join(['school_campus_person_role' => 'b'],'a.id = b.person_id','left') ->where('a.is_sys_user', 1) ->field("a.*,b.role_id,b.dept_id") ->where($whereArr) ->group("a.id") ->order($order); $list = $this->pageQuery($search_model); return $list; } /** * 获取试卷列表 */ public function getTestPaperList(array $where = []) { $ExamPapersModel = new ExamPapers(); $field = 'id,selection_mode,total_score,passing_score,created_at'; $order = 'id desc'; $whereArr = []; if (!empty($where['total_score'])) { $whereArr[] = ['total_score','=',$where['total_score']]; } if (!empty($where['selection_mode'])) { $whereArr[] = ['selection_mode','=',$where['selection_mode']]; } $search_model = $ExamPapersModel->where($whereArr)->field($field)->order($order); $list = $this->pageQuery($search_model); return $list; } /** * 绑定试卷模块 */ public function bindingTestModule(int $id, array $data) { $this->model->where([['id', '=', $id]])->update($data); return true; } }