InitQuestionTopicPointCommand.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 InitQuestionTopicPointCommand extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'washdata:initTopicPoint';
  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. $tableNamePoint = 'kailin_question_point';
  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. $data = file_get_contents($needExchangeTopic['data']);
  75. $data = json_decode($data, true);
  76. foreach ($data['data'] as $value) {
  77. $title = $value['title'];
  78. $topicId = DB::connection('company')->table($tableName)->where('title', $title)->value('id');
  79. if ($topicId) {
  80. $examPointNames = [];
  81. $examPoints = $value['points'];
  82. foreach ($examPoints as $examPoint) {
  83. $examPointNames [] = $examPoint['name'];
  84. }
  85. $pointIds = [];
  86. foreach ($examPointNames as $examPointName) {
  87. //通过考点名称,获取考点的id
  88. $pointId = DB::connection('company')->table($tableNamePoint)->where('name', $examPointName)->value('id');
  89. if (!empty($pointId)) {
  90. $pointIds [] = $pointId;
  91. }
  92. }
  93. $pointStr = implode(',', $pointIds);
  94. DB::connection('company')->table($tableName)->where('id', $topicId)->update(['point_id' => $pointStr]);
  95. }
  96. //通过title获取到kailin_question_topic的id
  97. }
  98. $bar->advance();
  99. }
  100. $bar->finish();
  101. $this->info('');
  102. $this->info('初始化完成');
  103. }
  104. public function getFilesRecursively($directory) {
  105. // 检查目录是否存在
  106. if (!is_dir($directory)) {
  107. return "目录不存在";
  108. }
  109. // 获取目录下的所有文件和子目录
  110. $files = scandir($directory);
  111. // 过滤掉 "." 和 ".."(当前目录和上一级目录)
  112. $files = array_diff($files, array('.', '..'));
  113. $result = [];
  114. foreach ($files as $file) {
  115. // 拼接完整的文件路径
  116. $filePath = $directory . DIRECTORY_SEPARATOR . $file;
  117. // 判断是否为文件
  118. if (is_file($filePath)) {
  119. // 过滤掉文件名中包含“数据源”的文件
  120. if (strpos($file, '数据源') === false) {
  121. // 获取文件名的主名称(去除.txt后缀)
  122. $fileNameWithoutExtension = pathinfo($file, PATHINFO_FILENAME);
  123. // 获取文件所在的目录路径
  124. $fileDir = dirname($filePath);
  125. // 去除文件名中的数字(如1, 2, 3...10, 11, 12等)
  126. // 去除文件名中的数字和连字符(-)
  127. $fileNameWithoutNumbers = preg_replace('/\d+/', '', $fileNameWithoutExtension);
  128. // 获取文件的大小
  129. $fileSize = filesize($filePath);
  130. $result[] = [
  131. 'fileName' => $fileNameWithoutNumbers, // 文件名(去除.txt后缀)
  132. 'data' => $filePath, // 完整路径
  133. 'directory' => $fileDir, // 文件所在目录
  134. 'size' => $fileSize // 文件大小
  135. ];
  136. }
  137. } elseif (is_dir($filePath)) {
  138. // 如果是目录,则递归调用
  139. $result = array_merge($result, $this->getFilesRecursively($filePath));
  140. }
  141. }
  142. return $result;
  143. }
  144. }