InitQuestionTopicCommand.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. namespace App\Console\Commands\WashData;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Config;
  5. use Illuminate\Support\Facades\DB;
  6. class InitQuestionTopicCommand extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'washdata:initTopic';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '题库解析';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. //连接数据库
  37. $dataBaseInfos = [
  38. //saas-company
  39. 'company' => [
  40. 'host' => '39.108.116.125',
  41. 'port' => 3306,
  42. 'db_name' => 'company_61',
  43. 'db_username' => 'company_61',
  44. 'db_password' => 'N35fYCPHziAAGd4L'
  45. ]
  46. ];
  47. $companyConnectionConfig = [
  48. 'driver' => 'mysql',
  49. 'host' => $dataBaseInfos['company']['host'],
  50. 'port' => $dataBaseInfos['company']['port'],
  51. 'database' => $dataBaseInfos['company']['db_name'],
  52. 'username' => $dataBaseInfos['company']['db_username'],
  53. 'password' => $dataBaseInfos['company']['db_password'],
  54. 'charset' => 'utf8mb4',
  55. 'collation' => 'utf8mb4_unicode_ci',
  56. 'unix_socket' => env('DB_SOCKET', ''),
  57. 'prefix' => env('DB_PREFIX', ''),
  58. 'strict' => false,
  59. 'sticky' => true,
  60. 'engine' => null,
  61. 'options' => [],
  62. ];
  63. Config::set("database.connections.company", $companyConnectionConfig);
  64. $tableName = 'kailin_question_topic';
  65. $tableNameChapter = 'kailin_question_chapter';
  66. $directory = "E:\\鸭题库数据初始化题目\\中药综\\13第十三章 中药用药安全";
  67. $needExchangeTopics = $this->getFilesRecursively($directory);
  68. //dd($needExchangeTopics);
  69. $dataCount = count($needExchangeTopics);
  70. $this->info('开始转换');
  71. $this->info("总共有:{$dataCount} 节");
  72. $bar = $this->output->createProgressBar($dataCount);
  73. foreach ($needExchangeTopics as $needExchangeTopic) {
  74. $chapterName = $needExchangeTopic['fileName'];
  75. // if (strpos($chapterName, '节')) {
  76. // $chapterName = explode('节', $chapterName)[1];
  77. // $chapterName = trim($chapterName);
  78. // //通过chapterName获取chapterId
  79. // $chapterId = DB::connection('company')->table($tableNameChapter)->where('name', 'like', '%' . $chapterName . '%')->value('id');
  80. // } else {
  81. // //通过chapterName获取chapterId
  82. // $chapterId = DB::connection('company')->table($tableNameChapter)->where('name', $chapterName)->value('id');
  83. // }
  84. $chapterId = DB::connection('company')->table($tableNameChapter)->where('name', $chapterName)->value('id');
  85. $this->info('获取章节id =' . $chapterId);
  86. if (empty($chapterId)) {
  87. $this->info('获取章节id失败!');
  88. $this->info('章节名称='.$chapterName);
  89. return ;
  90. }
  91. $data = file_get_contents($needExchangeTopic['data']);
  92. $data = json_decode($data, true);
  93. foreach ($data['data'] as $value) {
  94. //题目类型
  95. $style = $value['style'];
  96. //$styleName = $this->getStyleName($style);
  97. //dd($styleName);
  98. //题目
  99. // $title = '';
  100. // if (!empty($value['exerciseQuestionsTopicPo'])) {
  101. // $title = $value['exerciseQuestionsTopicPo']['content'] . "\r\n";
  102. // }
  103. // $title = $title . $value['title'];
  104. $title = $value['title'];
  105. $questions_ex = null;
  106. if (!empty($value['exerciseQuestionsTopicPo'])) {
  107. $questions_ex = $value['exerciseQuestionsTopicPo']['content'];
  108. }
  109. //解析
  110. $explain = $value['explain'];
  111. //准确率
  112. $accuracy = $value['accuracy'];
  113. //
  114. $correctAnswerStr = ''; //正确选项Str
  115. $correctAnswers = []; //正确答案数据
  116. $selectAContent = '';
  117. $selectBContent = '';
  118. $selectCContent = '';
  119. $selectDContent = '';
  120. $selectEContent = '';
  121. //考点
  122. $examPointStr = '';
  123. $examPointNames = [];
  124. $examPoints = $value['points'];
  125. foreach ($examPoints as $examPoint) {
  126. $examPointNames [] = $examPoint['name'];
  127. }
  128. $examPointStr = implode(',', $examPointNames);//考点
  129. //dd($examPointStr);
  130. //$answerFlag = false;
  131. // $answer = $data['answer'];
  132. //
  133. // if (in_array($answer, $answerArray)) {
  134. // $answerFlag = true; //获取answer成功
  135. // }
  136. $questionCount = $value['questionCount'] ; //选项数量
  137. //获取选项:
  138. $questionsOptionsPos = $value['questionsOptionsPos'];
  139. foreach ($questionsOptionsPos as $key => $item) {
  140. //配伍题的选项在$questions_ex 答案不能混淆
  141. //解析中包含A、B、C、D、E的时候也不能混淆
  142. if (strpos($explain, 'A') || strpos($explain, 'B') || strpos($explain, 'C') || strpos($explain, 'D') || strpos($explain, 'E')) {
  143. if ($key == 0 && $key <= $questionCount - 1) {
  144. $selectAContent = $item['option'];
  145. if ($item['isTrue'] == true) {
  146. $correctAnswers [] = 'A';
  147. }
  148. } elseif ($key == 1 && $key <= $questionCount - 1) {
  149. $selectBContent = $item['option'];
  150. if ($item['isTrue'] == true) {
  151. $correctAnswers [] = 'B';
  152. }
  153. } elseif ($key == 2 && $key <= $questionCount - 1) {
  154. $selectCContent = $item['option'];
  155. if ($item['isTrue'] == true) {
  156. $correctAnswers [] = 'C';
  157. }
  158. } elseif ($key == 3 && $key <= $questionCount - 1) {
  159. $selectDContent = $item['option'];
  160. if ($item['isTrue'] == true) {
  161. $correctAnswers [] = 'D';
  162. }
  163. } elseif ($key == 4 && $key <= $questionCount - 1) {
  164. $selectEContent = $item['option'];
  165. if ($item['isTrue'] == true) {
  166. $correctAnswers [] = 'E';
  167. }
  168. }
  169. } else {
  170. if ($style == 6) {
  171. if ($key == 0 && $key <= $questionCount - 1) {
  172. $selectAContent = $item['option'];
  173. if ($item['isTrue'] == true) {
  174. $correctAnswers [] = 'A';
  175. }
  176. } elseif ($key == 1 && $key <= $questionCount - 1) {
  177. $selectBContent = $item['option'];
  178. if ($item['isTrue'] == true) {
  179. $correctAnswers [] = 'B';
  180. }
  181. } elseif ($key == 2 && $key <= $questionCount - 1) {
  182. $selectCContent = $item['option'];
  183. if ($item['isTrue'] == true) {
  184. $correctAnswers [] = 'C';
  185. }
  186. } elseif ($key == 3 && $key <= $questionCount - 1) {
  187. $selectDContent = $item['option'];
  188. if ($item['isTrue'] == true) {
  189. $correctAnswers [] = 'D';
  190. }
  191. } elseif ($key == 4 && $key <= $questionCount - 1) {
  192. $selectEContent = $item['option'];
  193. if ($item['isTrue'] == true) {
  194. $correctAnswers [] = 'E';
  195. }
  196. }
  197. } else {
  198. if ($key == 0 && $key <= $questionCount - 1) {
  199. $selectCContent = $item['option'];
  200. if ($item['isTrue'] == true) {
  201. $correctAnswers [] = 'C';
  202. }
  203. } elseif ($key == 1 && $key <= $questionCount - 1) {
  204. $selectDContent = $item['option'];
  205. if ($item['isTrue'] == true) {
  206. $correctAnswers [] = 'D';
  207. }
  208. } elseif ($key == 2 && $key <= $questionCount - 1) {
  209. $selectAContent = $item['option'];
  210. if ($item['isTrue'] == true) {
  211. $correctAnswers [] = 'A';
  212. }
  213. } elseif ($key == 3 && $key <= $questionCount - 1) {
  214. $selectBContent = $item['option'];
  215. if ($item['isTrue'] == true) {
  216. $correctAnswers [] = 'B';
  217. }
  218. } elseif ($key == 4 && $key <= $questionCount - 1) {
  219. $selectEContent = $item['option'];
  220. if ($item['isTrue'] == true) {
  221. $correctAnswers [] = 'E';
  222. }
  223. }
  224. }
  225. }
  226. }
  227. sort($correctAnswers);
  228. $correctAnswerStr = implode(',', $correctAnswers); //正确答案
  229. //dd($correctAnswerStr);
  230. if ($style == 6) {
  231. $selectAContent = '';
  232. $selectBContent = '';
  233. $selectCContent = '';
  234. $selectDContent = '';
  235. $selectEContent = '';
  236. }
  237. $insertData = [
  238. 'title' => $title,
  239. 'style' => $style,
  240. 'select_a' => $selectAContent,
  241. 'select_b' => $selectBContent,
  242. 'select_c' => $selectCContent,
  243. 'select_d' => $selectDContent,
  244. 'select_e' => $selectEContent,
  245. 'question_count' => $questionCount,
  246. 'questions_ex' => $questions_ex,
  247. 'explain' => $explain,
  248. 'correct_answer' => $correctAnswerStr,
  249. 'accuracy' => $accuracy,
  250. 'chapter_id' => $chapterId,
  251. 'point_id' => 0,
  252. 'insert_time' => time(),
  253. 'update_time' => time()
  254. ];
  255. DB::connection('company')->table($tableName)->insert($insertData);
  256. // $exportData = [];
  257. //
  258. // $exportData['style'] = $styleName;
  259. // $exportData['title'] = $title;
  260. //
  261. // $exportData['selectA'] = $selectAContent;
  262. // $exportData['selectB'] = $selectBContent;
  263. // $exportData['selectC'] = $selectCContent;
  264. // $exportData['selectD'] = $selectDContent;
  265. // $exportData['selectE'] = $selectEContent;
  266. // $exportData['answer'] = $correctAnswerStr;
  267. // $exportData['explain'] = $explain;
  268. // $exportData['accuracy'] = $accuracy;
  269. // $exportData['point'] = $examPointStr;
  270. // $this->putRow($exportData);
  271. }
  272. $bar->advance();
  273. }
  274. $bar->finish();
  275. $this->info('');
  276. $this->info('导出完成');
  277. }
  278. public function getFilesRecursively($directory) {
  279. // 检查目录是否存在
  280. if (!is_dir($directory)) {
  281. return "目录不存在";
  282. }
  283. // 获取目录下的所有文件和子目录
  284. $files = scandir($directory);
  285. // 过滤掉 "." 和 ".."(当前目录和上一级目录)
  286. $files = array_diff($files, array('.', '..'));
  287. $result = [];
  288. foreach ($files as $file) {
  289. // 拼接完整的文件路径
  290. $filePath = $directory . DIRECTORY_SEPARATOR . $file;
  291. // 判断是否为文件
  292. if (is_file($filePath)) {
  293. // 过滤掉文件名中包含“数据源”的文件
  294. if (strpos($file, '数据源') === false) {
  295. // 获取文件名的主名称(去除.txt后缀)
  296. $fileNameWithoutExtension = pathinfo($file, PATHINFO_FILENAME);
  297. // 获取文件所在的目录路径
  298. $fileDir = dirname($filePath);
  299. // 去除文件名中的数字(如1, 2, 3...10, 11, 12等)
  300. // 去除文件名中的数字和连字符(-)
  301. $fileNameWithoutNumbers = preg_replace('/\d+/', '', $fileNameWithoutExtension);
  302. // 获取文件的大小
  303. $fileSize = filesize($filePath);
  304. $result[] = [
  305. 'fileName' => $fileNameWithoutNumbers, // 文件名(去除.txt后缀)
  306. 'data' => $filePath, // 完整路径
  307. 'directory' => $fileDir, // 文件所在目录
  308. 'size' => $fileSize // 文件大小
  309. ];
  310. }
  311. } elseif (is_dir($filePath)) {
  312. // 如果是目录,则递归调用
  313. $result = array_merge($result, $this->getFilesRecursively($filePath));
  314. }
  315. }
  316. return $result;
  317. }
  318. }