| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php
- namespace App\Console\Commands\WashData;
- use App\Console\Commands\Export\ExportTrait;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Config;
- use Illuminate\Support\Facades\DB;
- class UpdateQuestionTopicCommand extends Command
- {
- use ExportTrait;
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'washdata:updateTopic';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '更新题库的题目';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $head = [
- '题目id',
- '题目类型',
- '题目',
- '选项A',
- '选项B',
- '选项c',
- '选项D',
- '选项E',
- '正确答案',
- '解析',
- '正确率',
- '题目扩展',
- '章节名称'
- ];
- //连接数据库
- //测试
- // $dataBaseInfos = [
- // //saas-company
- // 'company' => [
- // 'host' => '39.108.116.125',
- // 'port' => 3306,
- // 'db_name' => 'company_61',
- // 'db_username' => 'company_61',
- // 'db_password' => 'N35fYCPHziAAGd4L'
- // ]
- // ];
- //正式
- $dataBaseInfos = [
- //saas-company
- 'company' => [
- 'host' => '120.24.49.2',
- 'port' => 3306,
- 'db_name' => 'company_61',
- 'db_username' => 'company_61',
- 'db_password' => 'NKD4saLKN7zWKrzz'
- ]
- ];
- $companyConnectionConfig = [
- 'driver' => 'mysql',
- 'host' => $dataBaseInfos['company']['host'],
- 'port' => $dataBaseInfos['company']['port'],
- 'database' => $dataBaseInfos['company']['db_name'],
- 'username' => $dataBaseInfos['company']['db_username'],
- 'password' => $dataBaseInfos['company']['db_password'],
- 'charset' => 'utf8mb4',
- 'collation' => 'utf8mb4_unicode_ci',
- 'unix_socket' => env('DB_SOCKET', ''),
- 'prefix' => env('DB_PREFIX', ''),
- 'strict' => false,
- 'sticky' => true,
- 'engine' => null,
- 'options' => [],
- ];
- Config::set("database.connections.company", $companyConnectionConfig);
- $tableNameTopic = 'kailin_question_topic';
- $tableNameChapter = 'kailin_question_chapter';
- $directory = "E:\\鸭题库数据初始化考点_第二次\\中药二\\28第二十八章 骨伤科常用中成药";
- $needExchangeTopics = $this->getFilesRecursively($directory);
- //dd($needExchangeTopics);
- $dataCount = count($needExchangeTopics);
- $this->info('开始转换');
- $this->info("总共有:{$dataCount} 节");
- $bar = $this->output->createProgressBar($dataCount);
- foreach ($needExchangeTopics as $needExchangeTopic) {
- $chapterName = $needExchangeTopic['fileName'];
- $chapterId = DB::connection('company')->table($tableNameChapter)->where('name', $chapterName)->value('id');
- $this->info('获取章节id =' . $chapterId);
- if (empty($chapterId)) {
- $this->info('获取章节id失败!');
- $this->info('章节名称='.$chapterName);
- return ;
- }
- //通过章节获取所有的题目id
- //$topicIds = DB::connection('company')->table($tableNameTopic)->where('chapter_id', $chapterId)
- $data = file_get_contents($needExchangeTopic['data']);
- $data = json_decode($data, true);
- $changeTopicIds = [];
- foreach ($data['data'] as $value) {
- $title = $value['title'];
- $topicId = DB::connection('company')->table($tableNameTopic)->where('chapter_id', $chapterId)->where('title', $title)->value('id');
- if (!$topicId) {
- continue;
- }
- $changeTopicIds [] = $topicId;
- $questions_ex = null;
- if (!empty($value['exerciseQuestionsTopicPo'])) {
- $questions_ex = $value['exerciseQuestionsTopicPo']['content'];
- }
- //题目类型
- $style = $value['style'];
- //解析
- $explain = $value['explain'];
- //准确率
- $accuracy = $value['accuracy'];
- $correctAnswers = []; //正确答案数据
- $selectAContent = '';
- $selectBContent = '';
- $selectCContent = '';
- $selectDContent = '';
- $selectEContent = '';
- $questionCount = $value['questionCount'] ; //选项数量
- //获取选项:
- $questionsOptionsPos = $value['questionsOptionsPos'];
- foreach ($questionsOptionsPos as $key => $item) {
- if ($key == 0 && $key <= $questionCount - 1) {
- $selectAContent = $item['option'];
- if ($item['isTrue'] == true) {
- $correctAnswers [] = 'A';
- }
- } elseif ($key == 1 && $key <= $questionCount - 1) {
- $selectBContent = $item['option'];
- if ($item['isTrue'] == true) {
- $correctAnswers [] = 'B';
- }
- } elseif ($key == 2 && $key <= $questionCount - 1) {
- $selectCContent = $item['option'];
- if ($item['isTrue'] == true) {
- $correctAnswers [] = 'C';
- }
- } elseif ($key == 3 && $key <= $questionCount - 1) {
- $selectDContent = $item['option'];
- if ($item['isTrue'] == true) {
- $correctAnswers [] = 'D';
- }
- } elseif ($key == 4 && $key <= $questionCount - 1) {
- $selectEContent = $item['option'];
- if ($item['isTrue'] == true) {
- $correctAnswers [] = 'E';
- }
- }
- }
- sort($correctAnswers);
- $correctAnswerStr = implode(',', $correctAnswers); //正确答案
- if ($style == 6) {
- $selectAContent = '';
- $selectBContent = '';
- $selectCContent = '';
- $selectDContent = '';
- $selectEContent = '';
- }
- $updateData = [
- 'style' => $style,
- 'select_a' => $selectAContent,
- 'select_b' => $selectBContent,
- 'select_c' => $selectCContent,
- 'select_d' => $selectDContent,
- 'select_e' => $selectEContent,
- 'question_count' => $questionCount,
- 'questions_ex' => $questions_ex,
- 'explain' => $explain,
- 'correct_answer' => $correctAnswerStr,
- 'accuracy' => $accuracy,
- 'update_time' => time()
- ];
- DB::connection('company')->table($tableNameTopic)->where('id', $topicId)->update($updateData);
- }
- //导出未修改的题目
- $this->createdFile($needExchangeTopic['directory'], $needExchangeTopic['fileName']);
- $this->putRow($head);
- $topicInfos = DB::connection('company')->table($tableNameTopic)->where('chapter_id', $chapterId)->get()->toArray();
- foreach ($topicInfos as $topicInfo) {
- if (!in_array($topicInfo['id'], $changeTopicIds)) {
- $exportData = [];
- $exportData['id'] = $topicInfo['id'];
- $exportData['style'] = $this->getStyleName($topicInfo['style']);
- $exportData['title'] = $topicInfo['title'];
- $exportData['selectA'] = $topicInfo['select_a'];
- $exportData['selectB'] = $topicInfo['select_b'];
- $exportData['selectC'] = $topicInfo['select_c'];
- $exportData['selectD'] = $topicInfo['select_d'];
- $exportData['selectE'] = $topicInfo['select_e'];
- $exportData['answer'] = $topicInfo['correct_answer'];
- $exportData['explain'] = $topicInfo['explain'];
- $exportData['accuracy'] = $topicInfo['accuracy'];
- $exportData['questions_ex'] = $topicInfo['questions_ex'];
- $exportData['chapter_id'] = $chapterName;
- $this->putRow($exportData);
- }
- }
- $bar->advance();
- }
- $bar->finish();
- $this->info('');
- $this->info('导出完成');
- }
- public function getStyleName($style)
- {
- if ($style == 6) {
- $styleName = '配伍题';
- } elseif ($style == 2) {
- $styleName = '单选题';
- } elseif ($style == 3) {
- $styleName = '多选题';
- } else {
- $styleName = $style;
- }
- return $styleName;
- }
- public function getFilesRecursively($directory) {
- // 检查目录是否存在
- if (!is_dir($directory)) {
- return "目录不存在";
- }
- // 获取目录下的所有文件和子目录
- $files = scandir($directory);
- // 过滤掉 "." 和 ".."(当前目录和上一级目录)
- $files = array_diff($files, array('.', '..'));
- $result = [];
- foreach ($files as $file) {
- // 拼接完整的文件路径
- $filePath = $directory . DIRECTORY_SEPARATOR . $file;
- // 判断是否为文件
- if (is_file($filePath)) {
- // 过滤掉文件名中包含“数据源”的文件
- if (strpos($file, '数据源') === false) {
- // 获取文件名的主名称(去除.txt后缀)
- $fileNameWithoutExtension = pathinfo($file, PATHINFO_FILENAME);
- // 获取文件所在的目录路径
- $fileDir = dirname($filePath);
- // 去除文件名中的数字(如1, 2, 3...10, 11, 12等)
- // 去除文件名中的数字和连字符(-)
- $fileNameWithoutNumbers = preg_replace('/\d+/', '', $fileNameWithoutExtension);
- // 获取文件的大小
- $fileSize = filesize($filePath);
- $result[] = [
- 'fileName' => $fileNameWithoutNumbers, // 文件名(去除.txt后缀)
- 'data' => $filePath, // 完整路径
- 'directory' => $fileDir, // 文件所在目录
- 'size' => $fileSize // 文件大小
- ];
- }
- } elseif (is_dir($filePath)) {
- // 如果是目录,则递归调用
- $result = array_merge($result, $this->getFilesRecursively($filePath));
- }
- }
- return $result;
- }
- }
|