model = new DocumentDataSourceConfig(); } /** * 获取数据源配置分页列表 * @param array $where * @return array */ public function getPage(array $where = []): array { $field = 'id, contract_id, table_name, table_alias, field_name, field_alias, field_type, is_active, sort_order, created_at'; $order = 'sort_order asc, created_at desc'; $search_model = $this->model ->withSearch(['contract_id', 'table_name', 'field_name'], $where) ->field($field) ->order($order); $list = $this->pageQuery($search_model, $where); // 关联合同信息 if (!empty($list['data'])) { $contract_ids = array_unique(array_column($list['data'], 'contract_id')); $contracts = (new Contract())->whereIn('id', $contract_ids)->column('name', 'id'); foreach ($list['data'] as &$item) { $item['contract_name'] = $contracts[$item['contract_id']] ?? ''; // 兼容placeholder字段 $item['placeholder'] = '{{' . $item['field_alias'] . '}}'; } } return $list; } /** * 获取数据源配置信息 * @param int $id * @return array */ public function getInfo(int $id): array { $field = 'id, contract_id, table_name, table_alias, field_name, field_alias, field_type, is_active, sort_order, created_at'; $info = $this->model->field($field)->where([['id', '=', $id]])->findOrEmpty()->toArray(); if (empty($info)) { throw new \Exception('DATA_NOT_EXIST'); } // 获取合同信息 if (!empty($info['contract_id'])) { $contract = (new Contract())->where('id', $info['contract_id'])->field('id, name')->findOrEmpty(); $info['contract_name'] = $contract['name'] ?? ''; } // 兼容placeholder字段 $info['placeholder'] = '{{' . $info['field_alias'] . '}}'; return $info; } /** * 添加数据源配置 * @param array $data * @return mixed */ public function add(array $data) { // 检查占位符是否已存在 $exists = $this->model->where([ ['contract_id', '=', $data['contract_id']], ['placeholder', '=', $data['placeholder']] ])->findOrEmpty(); if (!$exists->isEmpty()) { throw new \Exception('PLACEHOLDER_EXISTS'); } $data['created_at'] = time(); $res = $this->model->save($data); if (!$res) { throw new \Exception('ADD_FAIL'); } return $this->model->id; } /** * 编辑数据源配置 * @param int $id * @param array $data * @return bool */ public function edit(int $id, array $data): bool { $info = $this->model->findOrEmpty($id); if ($info->isEmpty()) { throw new \Exception('DATA_NOT_EXIST'); } // 检查占位符是否已存在(排除当前记录) $exists = $this->model->where([ ['contract_id', '=', $data['contract_id']], ['placeholder', '=', $data['placeholder']], ['id', '<>', $id] ])->findOrEmpty(); if (!$exists->isEmpty()) { throw new \Exception('PLACEHOLDER_EXISTS'); } $res = $info->save($data); if (!$res) { throw new \Exception('EDIT_FAIL'); } return true; } /** * 删除数据源配置 * @param int $id * @return bool */ public function del(int $id): bool { $info = $this->model->findOrEmpty($id); if ($info->isEmpty()) { throw new \Exception('DATA_NOT_EXIST'); } $res = $info->delete(); if (!$res) { throw new \Exception('DELETE_FAIL'); } return true; } /** * 批量配置数据源 * @param int $contractId * @param array $configs * @return bool */ public function batchConfig(int $contractId, array $configs): bool { if (empty($contractId) || empty($configs)) { throw new \Exception('INVALID_PARAMS'); } // 开启事务 Db::startTrans(); try { // 删除原有配置 $this->model->where('contract_id', $contractId)->delete(); // 批量插入新配置 $insertData = []; foreach ($configs as $config) { $insertData[] = [ 'contract_id' => $contractId, 'placeholder' => $config['placeholder'] ?? '', 'table_name' => $config['table_name'] ?? '', 'field_name' => $config['field_name'] ?? '', 'field_type' => $config['field_type'] ?? 'string', 'is_required' => $config['is_required'] ?? 0, 'default_value' => $config['default_value'] ?? '', 'created_at' => time() ]; } if (!empty($insertData)) { $this->model->insertAll($insertData); } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); throw $e; } } /** * 获取可用数据表列表 * @return array */ public function getAvailableTables(): array { // 定义可用的数据表及其描述 $tables = [ 'school_personnel' => '员工基础信息表', 'school_personnel_info' => '员工详细信息表', 'school_member' => '会员信息表', 'school_contract_sign' => '合同签署表', 'school_course' => '课程信息表', 'school_order' => '订单信息表' ]; $result = []; foreach ($tables as $table => $description) { $result[] = [ 'table_name' => $table, 'description' => $description ]; } return $result; } /** * 获取数据表字段列表 * @param string $tableName * @return array */ public function getTableFields(string $tableName): array { try { $fields = Db::query("SHOW COLUMNS FROM {$tableName}"); $result = []; foreach ($fields as $field) { $result[] = [ 'field_name' => $field['Field'], 'field_type' => $this->parseFieldType($field['Type']), 'is_nullable' => $field['Null'] === 'YES', 'default_value' => $field['Default'], 'comment' => $field['Comment'] ?? '' ]; } return $result; } catch (\Exception $e) { throw new \Exception('TABLE_NOT_EXISTS'); } } /** * 解析字段类型 * @param string $type * @return string */ private function parseFieldType(string $type): string { if (strpos($type, 'int') !== false) { return 'integer'; } elseif (strpos($type, 'decimal') !== false || strpos($type, 'float') !== false || strpos($type, 'double') !== false) { return 'decimal'; } elseif (strpos($type, 'date') !== false || strpos($type, 'time') !== false) { return 'datetime'; } elseif (strpos($type, 'text') !== false) { return 'text'; } else { return 'string'; } } /** * 预览数据源配置效果 * @param int $contractId * @param array $sampleData * @return array */ public function preview(int $contractId, array $sampleData = []): array { // 获取合同的数据源配置 $configs = $this->model->where('contract_id', $contractId)->select()->toArray(); $result = []; foreach ($configs as $config) { $placeholder = $config['field_alias'] ?? $config['placeholder'] ?? ''; $value = $sampleData[$placeholder] ?? $config['default_value'] ?? ''; $result[] = [ 'placeholder' => '{{' . $placeholder . '}}', 'table_name' => $config['table_name'], 'field_name' => $config['field_name'], 'field_type' => $config['field_type'], 'is_required' => $config['is_active'] ?? 1, 'preview_value' => $value ]; } return $result; } }