UpdateQuestionTopicCommand.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. namespace App\Console\Commands\WashData;
  3. use App\Console\Commands\Export\ExportTrait;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Config;
  6. use Illuminate\Support\Facades\DB;
  7. class UpdateQuestionTopicCommand extends Command
  8. {
  9. use ExportTrait;
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'washdata:updateTopic';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '更新题库的题目';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $head = [
  39. '题目id',
  40. '题目类型',
  41. '题目',
  42. '选项A',
  43. '选项B',
  44. '选项c',
  45. '选项D',
  46. '选项E',
  47. '正确答案',
  48. '解析',
  49. '正确率',
  50. '题目扩展',
  51. '章节名称'
  52. ];
  53. //连接数据库
  54. //测试
  55. // $dataBaseInfos = [
  56. // //saas-company
  57. // 'company' => [
  58. // 'host' => '39.108.116.125',
  59. // 'port' => 3306,
  60. // 'db_name' => 'company_61',
  61. // 'db_username' => 'company_61',
  62. // 'db_password' => 'N35fYCPHziAAGd4L'
  63. // ]
  64. // ];
  65. //正式
  66. $dataBaseInfos = [
  67. //saas-company
  68. 'company' => [
  69. 'host' => '120.24.49.2',
  70. 'port' => 3306,
  71. 'db_name' => 'company_61',
  72. 'db_username' => 'company_61',
  73. 'db_password' => 'NKD4saLKN7zWKrzz'
  74. ]
  75. ];
  76. $companyConnectionConfig = [
  77. 'driver' => 'mysql',
  78. 'host' => $dataBaseInfos['company']['host'],
  79. 'port' => $dataBaseInfos['company']['port'],
  80. 'database' => $dataBaseInfos['company']['db_name'],
  81. 'username' => $dataBaseInfos['company']['db_username'],
  82. 'password' => $dataBaseInfos['company']['db_password'],
  83. 'charset' => 'utf8mb4',
  84. 'collation' => 'utf8mb4_unicode_ci',
  85. 'unix_socket' => env('DB_SOCKET', ''),
  86. 'prefix' => env('DB_PREFIX', ''),
  87. 'strict' => false,
  88. 'sticky' => true,
  89. 'engine' => null,
  90. 'options' => [],
  91. ];
  92. Config::set("database.connections.company", $companyConnectionConfig);
  93. $tableNameTopic = 'kailin_question_topic';
  94. $tableNameChapter = 'kailin_question_chapter';
  95. $directory = "E:\\鸭题库数据初始化考点_第二次\\中药二\\28第二十八章 骨伤科常用中成药";
  96. $needExchangeTopics = $this->getFilesRecursively($directory);
  97. //dd($needExchangeTopics);
  98. $dataCount = count($needExchangeTopics);
  99. $this->info('开始转换');
  100. $this->info("总共有:{$dataCount} 节");
  101. $bar = $this->output->createProgressBar($dataCount);
  102. foreach ($needExchangeTopics as $needExchangeTopic) {
  103. $chapterName = $needExchangeTopic['fileName'];
  104. $chapterId = DB::connection('company')->table($tableNameChapter)->where('name', $chapterName)->value('id');
  105. $this->info('获取章节id =' . $chapterId);
  106. if (empty($chapterId)) {
  107. $this->info('获取章节id失败!');
  108. $this->info('章节名称='.$chapterName);
  109. return ;
  110. }
  111. //通过章节获取所有的题目id
  112. //$topicIds = DB::connection('company')->table($tableNameTopic)->where('chapter_id', $chapterId)
  113. $data = file_get_contents($needExchangeTopic['data']);
  114. $data = json_decode($data, true);
  115. $changeTopicIds = [];
  116. foreach ($data['data'] as $value) {
  117. $title = $value['title'];
  118. $topicId = DB::connection('company')->table($tableNameTopic)->where('chapter_id', $chapterId)->where('title', $title)->value('id');
  119. if (!$topicId) {
  120. continue;
  121. }
  122. $changeTopicIds [] = $topicId;
  123. $questions_ex = null;
  124. if (!empty($value['exerciseQuestionsTopicPo'])) {
  125. $questions_ex = $value['exerciseQuestionsTopicPo']['content'];
  126. }
  127. //题目类型
  128. $style = $value['style'];
  129. //解析
  130. $explain = $value['explain'];
  131. //准确率
  132. $accuracy = $value['accuracy'];
  133. $correctAnswers = []; //正确答案数据
  134. $selectAContent = '';
  135. $selectBContent = '';
  136. $selectCContent = '';
  137. $selectDContent = '';
  138. $selectEContent = '';
  139. $questionCount = $value['questionCount'] ; //选项数量
  140. //获取选项:
  141. $questionsOptionsPos = $value['questionsOptionsPos'];
  142. foreach ($questionsOptionsPos as $key => $item) {
  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. }
  170. sort($correctAnswers);
  171. $correctAnswerStr = implode(',', $correctAnswers); //正确答案
  172. if ($style == 6) {
  173. $selectAContent = '';
  174. $selectBContent = '';
  175. $selectCContent = '';
  176. $selectDContent = '';
  177. $selectEContent = '';
  178. }
  179. $updateData = [
  180. 'style' => $style,
  181. 'select_a' => $selectAContent,
  182. 'select_b' => $selectBContent,
  183. 'select_c' => $selectCContent,
  184. 'select_d' => $selectDContent,
  185. 'select_e' => $selectEContent,
  186. 'question_count' => $questionCount,
  187. 'questions_ex' => $questions_ex,
  188. 'explain' => $explain,
  189. 'correct_answer' => $correctAnswerStr,
  190. 'accuracy' => $accuracy,
  191. 'update_time' => time()
  192. ];
  193. DB::connection('company')->table($tableNameTopic)->where('id', $topicId)->update($updateData);
  194. }
  195. //导出未修改的题目
  196. $this->createdFile($needExchangeTopic['directory'], $needExchangeTopic['fileName']);
  197. $this->putRow($head);
  198. $topicInfos = DB::connection('company')->table($tableNameTopic)->where('chapter_id', $chapterId)->get()->toArray();
  199. foreach ($topicInfos as $topicInfo) {
  200. if (!in_array($topicInfo['id'], $changeTopicIds)) {
  201. $exportData = [];
  202. $exportData['id'] = $topicInfo['id'];
  203. $exportData['style'] = $this->getStyleName($topicInfo['style']);
  204. $exportData['title'] = $topicInfo['title'];
  205. $exportData['selectA'] = $topicInfo['select_a'];
  206. $exportData['selectB'] = $topicInfo['select_b'];
  207. $exportData['selectC'] = $topicInfo['select_c'];
  208. $exportData['selectD'] = $topicInfo['select_d'];
  209. $exportData['selectE'] = $topicInfo['select_e'];
  210. $exportData['answer'] = $topicInfo['correct_answer'];
  211. $exportData['explain'] = $topicInfo['explain'];
  212. $exportData['accuracy'] = $topicInfo['accuracy'];
  213. $exportData['questions_ex'] = $topicInfo['questions_ex'];
  214. $exportData['chapter_id'] = $chapterName;
  215. $this->putRow($exportData);
  216. }
  217. }
  218. $bar->advance();
  219. }
  220. $bar->finish();
  221. $this->info('');
  222. $this->info('导出完成');
  223. }
  224. public function getStyleName($style)
  225. {
  226. if ($style == 6) {
  227. $styleName = '配伍题';
  228. } elseif ($style == 2) {
  229. $styleName = '单选题';
  230. } elseif ($style == 3) {
  231. $styleName = '多选题';
  232. } else {
  233. $styleName = $style;
  234. }
  235. return $styleName;
  236. }
  237. public function getFilesRecursively($directory) {
  238. // 检查目录是否存在
  239. if (!is_dir($directory)) {
  240. return "目录不存在";
  241. }
  242. // 获取目录下的所有文件和子目录
  243. $files = scandir($directory);
  244. // 过滤掉 "." 和 ".."(当前目录和上一级目录)
  245. $files = array_diff($files, array('.', '..'));
  246. $result = [];
  247. foreach ($files as $file) {
  248. // 拼接完整的文件路径
  249. $filePath = $directory . DIRECTORY_SEPARATOR . $file;
  250. // 判断是否为文件
  251. if (is_file($filePath)) {
  252. // 过滤掉文件名中包含“数据源”的文件
  253. if (strpos($file, '数据源') === false) {
  254. // 获取文件名的主名称(去除.txt后缀)
  255. $fileNameWithoutExtension = pathinfo($file, PATHINFO_FILENAME);
  256. // 获取文件所在的目录路径
  257. $fileDir = dirname($filePath);
  258. // 去除文件名中的数字(如1, 2, 3...10, 11, 12等)
  259. // 去除文件名中的数字和连字符(-)
  260. $fileNameWithoutNumbers = preg_replace('/\d+/', '', $fileNameWithoutExtension);
  261. // 获取文件的大小
  262. $fileSize = filesize($filePath);
  263. $result[] = [
  264. 'fileName' => $fileNameWithoutNumbers, // 文件名(去除.txt后缀)
  265. 'data' => $filePath, // 完整路径
  266. 'directory' => $fileDir, // 文件所在目录
  267. 'size' => $fileSize // 文件大小
  268. ];
  269. }
  270. } elseif (is_dir($filePath)) {
  271. // 如果是目录,则递归调用
  272. $result = array_merge($result, $this->getFilesRecursively($filePath));
  273. }
  274. }
  275. return $result;
  276. }
  277. }