InitQuestionRealTopicCommand.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 InitQuestionRealTopicCommand extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'washdata:initRealTopic';
  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_real_topic';
  65. $tableNameCatalogue = 'kailin_question_past_real_catalogue';
  66. $directory = "E:\\鸭题库数据初始化历年真题\\中药综\\历年真题";
  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. $year = $needExchangeTopic['fileName'];
  75. //法规
  76. $catalogueId = DB::connection('company')->table($tableNameCatalogue)->where('chapter_id', 9)->where('year', $year)->value('id');
  77. $this->info('获取目录id =' . $catalogueId);
  78. if (empty($catalogueId)) {
  79. $this->info('获取目录id失败!');
  80. $this->info('目录年份='.$year);
  81. return ;
  82. }
  83. $data = file_get_contents($needExchangeTopic['data']);
  84. $data = json_decode($data, true);
  85. foreach ($data['data'] as $item) {
  86. //真题组名称:
  87. $groupName = $item['groupName'];
  88. $paperQuestions = $item['paperQuestionVos'];
  89. foreach ($paperQuestions as $value) {
  90. //题目类型
  91. $style = $value['style'];
  92. //$styleName = $this->getStyleName($style);
  93. //题目
  94. $title = $value['title'];
  95. $questions_ex = null;
  96. if (!empty($value['exerciseQuestionsTopicPo'])) {
  97. $questions_ex = $value['exerciseQuestionsTopicPo']['content'];
  98. }
  99. //解析
  100. $explain = $value['explain'];
  101. //准确率
  102. $accuracy = $value['accuracy'];
  103. //
  104. $correctAnswerStr = ''; //正确选项Str
  105. $correctAnswers = []; //正确答案数据
  106. $selectAContent = '';
  107. $selectBContent = '';
  108. $selectCContent = '';
  109. $selectDContent = '';
  110. $selectEContent = '';
  111. //考点
  112. $examPointStr = '';
  113. $examPointNames = [];
  114. $examPoints = $value['points'];
  115. foreach ($examPoints as $examPoint) {
  116. $examPointNames [] = $examPoint['name'];
  117. }
  118. $examPointStr = implode(',', $examPointNames);//考点
  119. //dd($examPointStr);
  120. $questionCount = $value['questionCount']; //选项数量
  121. //获取选项:
  122. $questionsOptionsPos = $value['questionsOptionsPos'];
  123. foreach ($questionsOptionsPos as $key => $item) {
  124. if ($key == 0 && $key <= $questionCount - 1) {
  125. $selectAContent = $item['option'];
  126. if ($item['isTrue'] == true) {
  127. $correctAnswers [] = 'A';
  128. }
  129. } elseif ($key == 1 && $key <= $questionCount - 1) {
  130. $selectBContent = $item['option'];
  131. if ($item['isTrue'] == true) {
  132. $correctAnswers [] = 'B';
  133. }
  134. } elseif ($key == 2 && $key <= $questionCount - 1) {
  135. $selectCContent = $item['option'];
  136. if ($item['isTrue'] == true) {
  137. $correctAnswers [] = 'C';
  138. }
  139. } elseif ($key == 3 && $key <= $questionCount - 1) {
  140. $selectDContent = $item['option'];
  141. if ($item['isTrue'] == true) {
  142. $correctAnswers [] = 'D';
  143. }
  144. } elseif ($key == 4 && $key <= $questionCount - 1) {
  145. $selectEContent = $item['option'];
  146. if ($item['isTrue'] == true) {
  147. $correctAnswers [] = 'E';
  148. }
  149. }
  150. }
  151. $correctAnswerStr = implode(',', $correctAnswers); //正确答案
  152. if ($style == 6) {
  153. $selectAContent = '';
  154. $selectBContent = '';
  155. $selectCContent = '';
  156. $selectDContent = '';
  157. $selectEContent = '';
  158. }
  159. $insertData = [
  160. 'title' => $title,
  161. 'style' => $style,
  162. 'group_name' => $groupName,
  163. 'select_a' => $selectAContent,
  164. 'select_b' => $selectBContent,
  165. 'select_c' => $selectCContent,
  166. 'select_d' => $selectDContent,
  167. 'select_e' => $selectEContent,
  168. 'question_count' => $questionCount,
  169. 'questions_ex' => $questions_ex,
  170. 'explain' => $explain,
  171. 'correct_answer' => $correctAnswerStr,
  172. 'accuracy' => $accuracy,
  173. 'catalogue_id' => $catalogueId,
  174. 'insert_time' => time(),
  175. 'update_time' => time()
  176. ];
  177. DB::connection('company')->table($tableName)->insert($insertData);
  178. }
  179. }
  180. $bar->advance();
  181. }
  182. }
  183. public function getFilesRecursively($directory) {
  184. // 检查目录是否存在
  185. if (!is_dir($directory)) {
  186. return "目录不存在";
  187. }
  188. // 获取目录下的所有文件和子目录
  189. $files = scandir($directory);
  190. // 过滤掉 "." 和 ".."(当前目录和上一级目录)
  191. $files = array_diff($files, array('.', '..'));
  192. $result = [];
  193. foreach ($files as $file) {
  194. // 拼接完整的文件路径
  195. $filePath = $directory . DIRECTORY_SEPARATOR . $file;
  196. // 判断是否为文件
  197. if (is_file($filePath)) {
  198. // 过滤掉文件名中包含“数据源”的文件
  199. if (strpos($file, '数据源') === false) {
  200. // 获取文件名的主名称(去除.txt后缀)
  201. $fileNameWithoutExtension = pathinfo($file, PATHINFO_FILENAME);
  202. // 获取文件所在的目录路径
  203. $fileDir = dirname($filePath);
  204. // 去除文件名中的数字(如1, 2, 3...10, 11, 12等)
  205. // 去除文件名中的数字和连字符(-)
  206. $fileNameWithoutNumbers = preg_replace('/\d+/', '', $fileNameWithoutExtension);
  207. // 获取文件的大小
  208. $fileSize = filesize($filePath);
  209. $result[] = [
  210. 'fileName' => $fileNameWithoutExtension, // 文件名(去除.txt后缀)
  211. 'data' => $filePath, // 完整路径
  212. 'directory' => $fileDir, // 文件所在目录
  213. 'size' => $fileSize // 文件大小
  214. ];
  215. }
  216. } elseif (is_dir($filePath)) {
  217. // 如果是目录,则递归调用
  218. $result = array_merge($result, $this->getFilesRecursively($filePath));
  219. }
  220. }
  221. return $result;
  222. }
  223. }