ExportTopicExchangeCommand.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. namespace App\Console\Commands\Export;
  3. use Illuminate\Console\Command;
  4. class ExportTopicExchangeCommand 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:exchangeTopic';
  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. '选项A',
  39. '选项B',
  40. '选项c',
  41. '选项D',
  42. '选项E',
  43. '正确答案',
  44. '解析',
  45. '正确率',
  46. '考点'
  47. ];
  48. //$dataTest = file_get_contents();
  49. //$this->createdFile('');
  50. //$this->putRow($head);
  51. // $needExchangeTopics = [
  52. // [
  53. // 'fileName' => '1第一节 感冒',
  54. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\1第一节 感冒\\1第一节 感冒.txt"
  55. // ],
  56. // [
  57. // 'fileName' => '2第二节 咳嗽',
  58. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\2第二节 咳嗽\\2第二节 咳嗽.txt"
  59. // ],
  60. // [
  61. // 'fileName' => '3第三节 喘证',
  62. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\3第三节 喘证\\3第三节 喘证.txt"
  63. // ],
  64. // [
  65. // 'fileName' => '4第四节 肺胀',
  66. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\4第四节 肺胀\\4第四节 肺胀.txt"
  67. // ],
  68. // [
  69. // 'fileName' => '5第五节 心悸',
  70. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\5第五节 心悸\\5第五节 心悸.txt"
  71. // ],
  72. // [
  73. // 'fileName' => '6第六节 胸痹',
  74. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\6第六节 胸痹\\6第六节 胸痹.txt"
  75. // ],
  76. // [
  77. // 'fileName' => '7第七节 不寐',
  78. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\7第七节 不寐\\7第七节 不寐.txt"
  79. // ],
  80. // [
  81. // 'fileName' => '8第八节 胃痛',
  82. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\8第八节 胃痛\\8第八节 胃痛.txt"
  83. // ],
  84. // [
  85. // 'fileName' => '9第九节 泄泻',
  86. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\9第九节 泄泻\\9第九节 泄泻.txt"
  87. // ],
  88. // [
  89. // 'fileName' => '10第十节 便秘',
  90. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\10第十节 便秘\\10第十节 便秘.txt"
  91. // ],
  92. // [
  93. // 'fileName' => '11第十一节 头痛',
  94. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\11第十一节 头痛\\11第十一节 头痛.txt"
  95. // ],
  96. // [
  97. // 'fileName' => '12第十二节 眩晕',
  98. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\12第十二节 眩晕\\12第十二节 眩晕.txt"
  99. // ],
  100. // [
  101. // 'fileName' => '13第十三节 胁痛',
  102. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\13第十三节 胁痛\\13第十三节 胁痛.txt"
  103. // ],
  104. // [
  105. // 'fileName' => '14第十四节 中风',
  106. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\14第十四节 中风\\14第十四节 中风.txt"
  107. // ],
  108. // [
  109. // 'fileName' => '15第十五节 汗证',
  110. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\15第十五节 汗证\\15第十五节 汗证.txt"
  111. // ],
  112. // [
  113. // 'fileName' => '4第四节 肝功能检查',
  114. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\4第四节 肝功能检查\\4第四节 肝功能检查.txt"
  115. // ],
  116. // [
  117. // 'fileName' => '1第一节 血常规检查',
  118. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\1第一节 血常规检查\\1第一节 血常规检查.txt"
  119. // ],
  120. // [
  121. // 'fileName' => '2第二节 尿常规检查',
  122. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\2第二节 尿常规检查\\2第二节 尿常规检查.txt"
  123. // ],
  124. // [
  125. // 'fileName' => '3第三节 粪常规检查',
  126. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\3第三节 粪常规检查\\3第三节 粪常规检查.txt"
  127. // ],
  128. // [
  129. // 'fileName' => '4第四节 肝功能检查',
  130. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\4第四节 肝功能检查\\4第四节 肝功能检查.txt"
  131. // ],
  132. // [
  133. // 'fileName' => '1第一节 血常规检查',
  134. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\1第一节 血常规检查\\1第一节 血常规检查.txt"
  135. // ],
  136. // [
  137. // 'fileName' => '2第二节 尿常规检查',
  138. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\2第二节 尿常规检查\\2第二节 尿常规检查.txt"
  139. // ],
  140. // [
  141. // 'fileName' => '3第三节 粪常规检查',
  142. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\3第三节 粪常规检查\\3第三节 粪常规检查.txt"
  143. // ],
  144. // [
  145. // 'fileName' => '4第四节 肝功能检查',
  146. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\4第四节 肝功能检查\\4第四节 肝功能检查.txt"
  147. // ],
  148. // [
  149. // 'fileName' => '3第三节 粪常规检查',
  150. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\3第三节 粪常规检查\\3第三节 粪常规检查.txt"
  151. // ],
  152. // [
  153. // 'fileName' => '4第四节 肝功能检查',
  154. // 'data' => "E:\\鸭题库数据\中药综\\5第五章 中医内科常见病的辨证论治\\4第四节 肝功能检查\\4第四节 肝功能检查.txt"
  155. // ],
  156. //
  157. // ];
  158. $directory = "E:\\鸭题库数据\\药综\\6第六章 常见病症的健康管理";
  159. $needExchangeTopics = $this->getFilesRecursively($directory);
  160. //dd($needExchangeTopics);
  161. $dataCount = count($needExchangeTopics);
  162. $this->info('开始转换');
  163. $this->info("总共有:{$dataCount} 节");
  164. $bar = $this->output->createProgressBar($dataCount);
  165. //$answerArray = ['A', 'B', 'C', 'D', 'E'];
  166. foreach ($needExchangeTopics as $needExchangeTopic) {
  167. $this->createdFile($needExchangeTopic['directory'], $needExchangeTopic['fileName']);
  168. $this->putRow($head);
  169. $data = file_get_contents($needExchangeTopic['data']);
  170. $data = json_decode($data, true);
  171. foreach ($data['data'] as $value) {
  172. //题目类型
  173. $style = $value['style'];
  174. $styleName = $this->getStyleName($style);
  175. //dd($styleName);
  176. //题目
  177. $title = '';
  178. if (!empty($value['exerciseQuestionsTopicPo'])) {
  179. $title = $value['exerciseQuestionsTopicPo']['content'] . "\r\n";
  180. }
  181. $title = $title . $value['title'];
  182. //解析
  183. $explain = $value['explain'];
  184. //准确率
  185. $accuracy = $value['accuracy'];
  186. //
  187. $correctAnswerStr = ''; //正确选项Str
  188. $correctAnswers = []; //正确答案数据
  189. $selectAContent = '';
  190. $selectBContent = '';
  191. $selectCContent = '';
  192. $selectDContent = '';
  193. $selectEContent = '';
  194. //考点
  195. $examPointStr = '';
  196. $examPointNames = [];
  197. $examPoints = $value['points'];
  198. foreach ($examPoints as $examPoint) {
  199. $examPointNames [] = $examPoint['name'];
  200. }
  201. $examPointStr = implode(',', $examPointNames);//考点
  202. //dd($examPointStr);
  203. //$answerFlag = false;
  204. // $answer = $data['answer'];
  205. //
  206. // if (in_array($answer, $answerArray)) {
  207. // $answerFlag = true; //获取answer成功
  208. // }
  209. $questionCount = $value['questionCount'] ; //选项数量
  210. //获取选项:
  211. $questionsOptionsPos = $value['questionsOptionsPos'];
  212. foreach ($questionsOptionsPos as $key => $item) {
  213. if ($key == 0 && $key <= $questionCount-1) {
  214. $selectAContent = $item['option'];
  215. if ($item['isTrue'] == true) {
  216. $correctAnswers [] = 'A';
  217. }
  218. } elseif ($key == 1 && $key <= $questionCount-1) {
  219. $selectBContent = $item['option'];
  220. if ($item['isTrue'] == true) {
  221. $correctAnswers [] = 'B';
  222. }
  223. } elseif ($key == 2 && $key <= $questionCount-1) {
  224. $selectCContent = $item['option'];
  225. if ($item['isTrue'] == true) {
  226. $correctAnswers [] = 'C';
  227. }
  228. } elseif ($key == 3 && $key <= $questionCount-1) {
  229. $selectDContent = $item['option'];
  230. if ($item['isTrue'] == true) {
  231. $correctAnswers [] = 'D';
  232. }
  233. } elseif ($key == 4 && $key <= $questionCount-1) {
  234. $selectEContent = $item['option'];
  235. if ($item['isTrue'] == true) {
  236. $correctAnswers [] = 'E';
  237. }
  238. }
  239. }
  240. $correctAnswerStr = implode(',', $correctAnswers); //正确答案
  241. //dd($correctAnswerStr);
  242. $exportData = [];
  243. $exportData['style'] = $styleName;
  244. $exportData['title'] = $title;
  245. $exportData['selectA'] = $selectAContent;
  246. $exportData['selectB'] = $selectBContent;
  247. $exportData['selectC'] = $selectCContent;
  248. $exportData['selectD'] = $selectDContent;
  249. $exportData['selectE'] = $selectEContent;
  250. $exportData['answer'] = $correctAnswerStr;
  251. $exportData['explain'] = $explain;
  252. $exportData['accuracy'] = $accuracy;
  253. $exportData['point'] = $examPointStr;
  254. $this->putRow($exportData);
  255. }
  256. $this->closeFile();
  257. $bar->advance();
  258. }
  259. $bar->finish();
  260. $this->info('');
  261. $this->info('导出完成');
  262. }
  263. public function getStyleName($style)
  264. {
  265. if ($style == 6) {
  266. $styleName = '配伍题';
  267. } elseif ($style == 2) {
  268. $styleName = '单选题';
  269. } elseif ($style == 3) {
  270. $styleName = '多选题';
  271. } else {
  272. $styleName = $style;
  273. }
  274. return $styleName;
  275. }
  276. public function getFilesRecursively($directory) {
  277. // 检查目录是否存在
  278. if (!is_dir($directory)) {
  279. return "目录不存在";
  280. }
  281. // 获取目录下的所有文件和子目录
  282. $files = scandir($directory);
  283. // 过滤掉 "." 和 ".."(当前目录和上一级目录)
  284. $files = array_diff($files, array('.', '..'));
  285. $result = [];
  286. foreach ($files as $file) {
  287. // 拼接完整的文件路径
  288. $filePath = $directory . DIRECTORY_SEPARATOR . $file;
  289. // 判断是否为文件
  290. if (is_file($filePath)) {
  291. // 过滤掉文件名中包含“数据源”的文件
  292. if (strpos($file, '数据源') === false) {
  293. // 获取文件名的主名称(去除.txt后缀)
  294. $fileNameWithoutExtension = pathinfo($file, PATHINFO_FILENAME);
  295. // 获取文件所在的目录路径
  296. $fileDir = dirname($filePath);
  297. // 获取文件的大小
  298. $fileSize = filesize($filePath);
  299. $result[] = [
  300. 'fileName' => $fileNameWithoutExtension, // 文件名(去除.txt后缀)
  301. 'data' => $filePath, // 完整路径
  302. 'directory' => $fileDir, // 文件所在目录
  303. 'size' => $fileSize // 文件大小
  304. ];
  305. }
  306. } elseif (is_dir($filePath)) {
  307. // 如果是目录,则递归调用
  308. $result = array_merge($result, $this->getFilesRecursively($filePath));
  309. }
  310. }
  311. return $result;
  312. }
  313. }