InitQuestionChapterCommand.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 InitQuestionChapterCommand extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'washdata:initChapter';
  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_migrate_test',
  43. 'db_username' => 'company_migrate_test',
  44. 'db_password' => '7dSTAAxa64AkHB8R'
  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. // $dataCount = count($tables);
  65. //
  66. // $this->info("总共有数据表:{$dataCount} 个");
  67. //
  68. // $this->info('开始处理...');
  69. //
  70. // $bar = $this->output->createProgressBar($dataCount);
  71. $tableName = 'kailin_question_chapter';
  72. $insertDataArray = [
  73. [
  74. 'name' => '法规',
  75. 'parent_id' => 1,
  76. 'status' => 0,
  77. 'weight' => 0,
  78. 'level' => 1,
  79. 'created_id' => 0,
  80. 'updated_id' => 0,
  81. 'insert_time' => time(),
  82. 'update_time' => time(),
  83. ]
  84. ];
  85. $needWashDataArray = [
  86. [
  87. 'chapterName' => '中药综',
  88. 'filePath' => 'E:\\鸭题库数据初始化目录\\中药综'
  89. ]
  90. ];
  91. foreach ($needWashDataArray as $value) {
  92. $parentId = DB::connection('company')->table($tableName)->where('name', $value['chapterName'])->value('id');
  93. //获取路径下的子级文件夹名称
  94. $returnFiles = [];
  95. $this->getSubdirectories($value['filePath'], 0, $value['chapterName'], $returnFiles);
  96. //dd($returnFiles);
  97. foreach ($returnFiles as $returnFile) {
  98. if ($returnFile['level'] == 1) {
  99. if ($returnFile['parent'] == $value['chapterName']) {
  100. $insertData = [
  101. 'name' => $returnFile['name'],
  102. 'parent_id' => $parentId,
  103. 'status' => 0,
  104. 'weight' => 0,
  105. 'level' => $returnFile['level'] + 1,
  106. 'created_id' => 0,
  107. 'updated_id' => 0,
  108. 'insert_time' => time(),
  109. 'update_time' => time(),
  110. ];
  111. if (!DB::connection('company')->table($tableName)->where('name', $returnFile['name'])->exists()) {
  112. DB::connection('company')->table($tableName)->insert($insertData);
  113. }
  114. }
  115. }
  116. $secondParentId = DB::connection('company')->table($tableName)->where('name', $returnFile['parent'])->value('id');
  117. if ($returnFile['level'] == 2) {
  118. $insertData = [
  119. 'name' => $returnFile['name'],
  120. 'parent_id' => $secondParentId,
  121. 'status' => 0,
  122. 'weight' => 0,
  123. 'level' => $returnFile['level'] + 1,
  124. 'created_id' => 0,
  125. 'updated_id' => 0,
  126. 'insert_time' => time(),
  127. 'update_time' => time(),
  128. ];
  129. DB::connection('company')->table($tableName)->insert($insertData);
  130. }
  131. $thirdParentId = DB::connection('company')->table($tableName)->where('name', $returnFile['parent'])->value('id');
  132. if ($returnFile['level'] == 3) {
  133. $insertData = [
  134. 'name' => $returnFile['name'],
  135. 'parent_id' => $thirdParentId,
  136. 'status' => 0,
  137. 'weight' => 0,
  138. 'level' => $returnFile['level'] + 1,
  139. 'created_id' => 0,
  140. 'updated_id' => 0,
  141. 'insert_time' => time(),
  142. 'update_time' => time(),
  143. ];
  144. DB::connection('company')->table($tableName)->insert($insertData);
  145. }
  146. }
  147. //dd($returnFiles);
  148. }
  149. // $parentId = DB::connection('company')->table($tableName)->where('level', 1)->pluck('id')->toArray();
  150. //
  151. // dd($parentId);
  152. //DB::connection('company')->table($tableName)->insert($insertDataArray);
  153. $this->info('success~');
  154. }
  155. // function getSubdirectories($directory, $level = 0, &$result = []) {
  156. // // 打开目录
  157. // if (!is_dir($directory)) {
  158. // return; // 如果不是目录,直接返回
  159. // }
  160. //
  161. // // 获取目录中的所有条目
  162. // $entries = scandir($directory);
  163. //
  164. // // 遍历目录中的每个条目
  165. // foreach ($entries as $entry) {
  166. // // 跳过当前目录(.)和上级目录(..)
  167. // if ($entry === '.' || $entry === '..') {
  168. // continue;
  169. // }
  170. //
  171. // // 构造完整的子目录路径
  172. // $subdir = $directory . DIRECTORY_SEPARATOR . $entry;
  173. //
  174. // // 检查是否是目录
  175. // if (is_dir($subdir)) {
  176. // // 将目录名称和级数存储到结果数组中
  177. // $result[] = ['name' => $entry, 'level' => $level + 1];
  178. //
  179. // // 递归调用,进入下一级目录
  180. // $this->getSubdirectories($subdir, $level + 1, $result);
  181. // }
  182. // }
  183. // }
  184. // function getSubdirectories($directory, $level = 0, $parentName = '', &$result = []) {
  185. // // 打开目录
  186. // if (!is_dir($directory)) {
  187. // return; // 如果不是目录,直接返回
  188. // }
  189. //
  190. // // 获取目录中的所有条目
  191. // $entries = scandir($directory);
  192. //
  193. // // 遍历目录中的每个条目
  194. // foreach ($entries as $entry) {
  195. // // 跳过当前目录(.)和上级目录(..)
  196. // if ($entry === '.' || $entry === '..') {
  197. // continue;
  198. // }
  199. //
  200. // // 构造完整的子目录路径
  201. // $subdir = $directory . DIRECTORY_SEPARATOR . $entry;
  202. //
  203. // // 检查是否是目录
  204. // if (is_dir($subdir)) {
  205. // // 将目录名称、级数和父级目录名称存储到结果数组中
  206. // $result[] = [
  207. // 'name' => $entry,
  208. // 'level' => $level + 1,
  209. // 'parent' => $parentName
  210. // ];
  211. //
  212. // // 递归调用,进入下一级目录
  213. // $this->getSubdirectories($subdir, $level + 1, $entry, $result);
  214. // }
  215. // }
  216. // }
  217. function getSubdirectories($directory, $level = 0, $parentName = '', &$result = []) {
  218. // 打开目录
  219. if (!is_dir($directory)) {
  220. return; // 如果不是目录,直接返回
  221. }
  222. // 获取目录中的所有条目
  223. $entries = scandir($directory);
  224. // 遍历目录中的每个条目
  225. foreach ($entries as $entry) {
  226. // 跳过当前目录(.)和上级目录(..)
  227. if ($entry === '.' || $entry === '..') {
  228. continue;
  229. }
  230. // 构造完整的子目录路径
  231. $subdir = $directory . DIRECTORY_SEPARATOR . $entry;
  232. // 检查是否是目录
  233. if (is_dir($subdir)) {
  234. // 过滤掉目录名称中的数字
  235. $filteredName = preg_replace('/\d+/', '', $entry);
  236. // 将目录名称、级数和父级目录名称存储到结果数组中
  237. $result[] = [
  238. 'name' => $filteredName,
  239. 'level' => $level + 1,
  240. 'parent' => $parentName
  241. ];
  242. // 递归调用,进入下一级目录
  243. $this->getSubdirectories($subdir, $level + 1, $filteredName, $result);
  244. }
  245. }
  246. }
  247. }