ExportRealTopicExchangeCommand.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace App\Console\Commands\Export;
  3. use Illuminate\Console\Command;
  4. class ExportRealTopicExchangeCommand extends Command
  5. {
  6. use ExportTrait;
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'export:exchangeRealTopic';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '题库解析';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return mixed
  32. */
  33. public function handle()
  34. {
  35. $head = [
  36. '题组名称',
  37. '题目类型',
  38. '题目',
  39. '选项A',
  40. '选项B',
  41. '选项c',
  42. '选项D',
  43. '选项E',
  44. '正确答案',
  45. '解析',
  46. '正确率',
  47. '考点'
  48. ];
  49. $directory = "E:\\鸭题库数据\\药二\\历年真题";
  50. $needExchangeTopics = $this->getFilesRecursively($directory);
  51. //dd($needExchangeTopics);
  52. $dataCount = count($needExchangeTopics);
  53. $this->info('开始转换');
  54. $this->info("总共有:{$dataCount} 套题需要转换");
  55. $bar = $this->output->createProgressBar($dataCount);
  56. foreach ($needExchangeTopics as $needExchangeTopic) {
  57. $this->createdFile($needExchangeTopic['directory'], $needExchangeTopic['fileName']);
  58. $this->putRow($head);
  59. $data = file_get_contents($needExchangeTopic['data']);
  60. $data = json_decode($data, true);
  61. foreach ($data['data'] as $item) {
  62. //真题组名称:
  63. $groupName = $item['groupName'];
  64. $paperQuestions = $item['paperQuestionVos'];
  65. foreach ($paperQuestions as $value) {
  66. //题目类型
  67. $style = $value['style'];
  68. $styleName = $this->getStyleName($style);
  69. //dd($styleName);
  70. //题目
  71. $title = '';
  72. if (!empty($value['exerciseQuestionsTopicPo'])) {
  73. $title = $value['exerciseQuestionsTopicPo']['content'] . "\r\n";
  74. }
  75. $title = $title . $value['title'];
  76. //解析
  77. $explain = $value['explain'];
  78. //准确率
  79. $accuracy = $value['accuracy'];
  80. //
  81. $correctAnswerStr = ''; //正确选项Str
  82. $correctAnswers = []; //正确答案数据
  83. $selectAContent = '';
  84. $selectBContent = '';
  85. $selectCContent = '';
  86. $selectDContent = '';
  87. $selectEContent = '';
  88. //考点
  89. $examPointStr = '';
  90. $examPointNames = [];
  91. $examPoints = $value['points'];
  92. foreach ($examPoints as $examPoint) {
  93. $examPointNames [] = $examPoint['name'];
  94. }
  95. $examPointStr = implode(',', $examPointNames);//考点
  96. //dd($examPointStr);
  97. //$answerFlag = false;
  98. // $answer = $data['answer'];
  99. //
  100. // if (in_array($answer, $answerArray)) {
  101. // $answerFlag = true; //获取answer成功
  102. // }
  103. $questionCount = $value['questionCount']; //选项数量
  104. //获取选项:
  105. $questionsOptionsPos = $value['questionsOptionsPos'];
  106. foreach ($questionsOptionsPos as $key => $item) {
  107. if ($key == 0 && $key <= $questionCount - 1) {
  108. $selectAContent = $item['option'];
  109. if ($item['isTrue'] == true) {
  110. $correctAnswers [] = 'A';
  111. }
  112. } elseif ($key == 1 && $key <= $questionCount - 1) {
  113. $selectBContent = $item['option'];
  114. if ($item['isTrue'] == true) {
  115. $correctAnswers [] = 'B';
  116. }
  117. } elseif ($key == 2 && $key <= $questionCount - 1) {
  118. $selectCContent = $item['option'];
  119. if ($item['isTrue'] == true) {
  120. $correctAnswers [] = 'C';
  121. }
  122. } elseif ($key == 3 && $key <= $questionCount - 1) {
  123. $selectDContent = $item['option'];
  124. if ($item['isTrue'] == true) {
  125. $correctAnswers [] = 'D';
  126. }
  127. } elseif ($key == 4 && $key <= $questionCount - 1) {
  128. $selectEContent = $item['option'];
  129. if ($item['isTrue'] == true) {
  130. $correctAnswers [] = 'E';
  131. }
  132. }
  133. }
  134. $correctAnswerStr = implode(',', $correctAnswers); //正确答案
  135. //dd($correctAnswerStr);
  136. $exportData = [];
  137. $exportData['groupName'] = $groupName;
  138. $exportData['style'] = $styleName;
  139. $exportData['title'] = $title;
  140. $exportData['selectA'] = $selectAContent;
  141. $exportData['selectB'] = $selectBContent;
  142. $exportData['selectC'] = $selectCContent;
  143. $exportData['selectD'] = $selectDContent;
  144. $exportData['selectE'] = $selectEContent;
  145. $exportData['answer'] = $correctAnswerStr;
  146. $exportData['explain'] = $explain;
  147. $exportData['accuracy'] = $accuracy;
  148. $exportData['point'] = $examPointStr;
  149. $this->putRow($exportData);
  150. }
  151. }
  152. $this->closeFile();
  153. $bar->advance();
  154. }
  155. $bar->finish();
  156. $this->info('');
  157. $this->info('导出完成');
  158. }
  159. public function getStyleName($style)
  160. {
  161. if ($style == 6) {
  162. $styleName = '配伍题';
  163. } elseif ($style == 2) {
  164. $styleName = '单选题';
  165. } elseif ($style == 3) {
  166. $styleName = '多选题';
  167. } else {
  168. $styleName = $style;
  169. }
  170. return $styleName;
  171. }
  172. public function getFilesRecursively($directory) {
  173. // 检查目录是否存在
  174. if (!is_dir($directory)) {
  175. return "目录不存在";
  176. }
  177. // 获取目录下的所有文件和子目录
  178. $files = scandir($directory);
  179. // 过滤掉 "." 和 ".."(当前目录和上一级目录)
  180. $files = array_diff($files, array('.', '..'));
  181. $result = [];
  182. foreach ($files as $file) {
  183. // 拼接完整的文件路径
  184. $filePath = $directory . DIRECTORY_SEPARATOR . $file;
  185. // 判断是否为文件
  186. if (is_file($filePath)) {
  187. // 过滤掉文件名中包含“数据源”的文件
  188. if (strpos($file, '数据源') === false) {
  189. // 获取文件名的主名称(去除.txt后缀)
  190. $fileNameWithoutExtension = pathinfo($file, PATHINFO_FILENAME);
  191. // 获取文件所在的目录路径
  192. $fileDir = dirname($filePath);
  193. // 获取文件的大小
  194. $fileSize = filesize($filePath);
  195. $result[] = [
  196. 'fileName' => $fileNameWithoutExtension, // 文件名(去除.txt后缀)
  197. 'data' => $filePath, // 完整路径
  198. 'directory' => $fileDir, // 文件所在目录
  199. 'size' => $fileSize // 文件大小
  200. ];
  201. }
  202. } elseif (is_dir($filePath)) {
  203. // 如果是目录,则递归调用
  204. $result = array_merge($result, $this->getFilesRecursively($filePath));
  205. }
  206. }
  207. return $result;
  208. }
  209. }