ExportQuestionBankDataCommand.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace App\Console\Commands\Export;
  3. use App\Servers\QuestionBank\QuestionChapterService;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Carbon;
  6. use Illuminate\Support\Facades\Config;
  7. use Illuminate\Support\Facades\DB;
  8. class ExportQuestionBankDataCommand extends Command
  9. {
  10. use ExportTrait;
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'export:questionBankData';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '题库数据导出';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return mixed
  36. */
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return mixed
  41. */
  42. public function handle()
  43. {
  44. $head = [
  45. '用户id',
  46. '手机号',
  47. '注册时间',
  48. '第一次学习时间',
  49. '第一天做题数量',
  50. '学习进度',
  51. ];
  52. //正式
  53. $dataBaseInfos = [
  54. //saas-company
  55. 'company' => [
  56. 'host' => '120.24.49.2',
  57. 'port' => 3306,
  58. 'db_name' => 'company_61',
  59. 'db_username' => 'company_61',
  60. 'db_password' => 'NKD4saLKN7zWKrzz'
  61. ]
  62. ];
  63. $companyConnectionConfig = [
  64. 'driver' => 'mysql',
  65. 'host' => $dataBaseInfos['company']['host'],
  66. 'port' => $dataBaseInfos['company']['port'],
  67. 'database' => $dataBaseInfos['company']['db_name'],
  68. 'username' => $dataBaseInfos['company']['db_username'],
  69. 'password' => $dataBaseInfos['company']['db_password'],
  70. 'charset' => 'utf8mb4',
  71. 'collation' => 'utf8mb4_unicode_ci',
  72. 'unix_socket' => env('DB_SOCKET', ''),
  73. 'prefix' => env('DB_PREFIX', ''),
  74. 'strict' => false,
  75. 'sticky' => true,
  76. 'engine' => null,
  77. 'options' => [],
  78. ];
  79. Config::set("database.connections.company", $companyConnectionConfig);
  80. $tableNameCustomer = 'kailin_custom';
  81. $tableStudyProc = 'kailin_question_point_study_progress';
  82. $tableAnswerTopic = 'kailin_question_user_topic_answer_statistics';
  83. //$tableAnswerTopic = 'kailin_question_user_topic_answer_record';
  84. $tableAnswerRealTopic = 'kailin_question_user_real_topic_answer_record';
  85. $tableNameChapter = 'kailin_question_chapter';
  86. $directory = storage_path('exports/');
  87. $fileName = '统计-'. Carbon::today()->toDateString();
  88. $this->createdFile($directory, $fileName);
  89. $this->putRow($head);
  90. $allUserInfos = DB::connection('company')->table($tableNameCustomer)->get(['uid', 'phone', 'insert_time'])->toArray();
  91. $dataCount = count($allUserInfos);
  92. $this->info('开始处理');
  93. $this->info("总共有:{$dataCount} 用户需要处理");
  94. $bar = $this->output->createProgressBar($dataCount);
  95. foreach ($allUserInfos as $allUserInfo) {
  96. $uid = $allUserInfo['uid'];
  97. //通过uid获取第一次的学习时间
  98. $firstStudyTime = '';
  99. $studyInfo = DB::connection('company')->table($tableStudyProc)->where('user_id', $uid)->first();
  100. if ($studyInfo) {
  101. $firstStudyTime = $studyInfo['insert_time'];
  102. }
  103. $regDate = date("Y-m-d", $allUserInfo['insert_time']);
  104. $startTime = strtotime($regDate . " 00:00:00"); // 获取指定日期的开始时间戳
  105. $endTime = strtotime($regDate . " 23:59:59"); // 获取指定日期的结束时间戳
  106. //通过uid获取学员注册当天的答题数量
  107. $answerNum = DB::connection('company')->table($tableAnswerTopic)
  108. ->where('user_id', $uid)
  109. ->where('insert_time', '>=', $startTime)
  110. ->where('insert_time', '<=', $endTime)
  111. ->count();
  112. //获取学员学习了考点的章节id
  113. $chapters = DB::connection('company')->table($tableNameChapter)->get(['id', 'parent_id'])->toArray();
  114. $questionChapterService = new QuestionChapterService();
  115. // $allSonChapterIds = $questionChapterService->getSonChapters($chapters, $chapterId);
  116. $faguiChapterIds = $questionChapterService->getSonChapters($chapters, 3);
  117. array_unshift($faguiChapterIds, 3);//放到数组前面
  118. $y1ChapterIds = $questionChapterService->getSonChapters($chapters, 4);
  119. array_unshift($y1ChapterIds, 4);//放到数组前面
  120. $y2ChapterIds = $questionChapterService->getSonChapters($chapters, 5);
  121. array_unshift($y2ChapterIds, 5);//放到数组前面
  122. $yzChapterIds = $questionChapterService->getSonChapters($chapters, 6);
  123. array_unshift($yzChapterIds, 6);//放到数组前面
  124. $zy1ChapterIds = $questionChapterService->getSonChapters($chapters, 7);
  125. array_unshift($zy1ChapterIds, 7);//放到数组前面
  126. $zy2ChapterIds = $questionChapterService->getSonChapters($chapters, 8);
  127. array_unshift($zy2ChapterIds, 8);//放到数组前面
  128. $zyzChapterIds = $questionChapterService->getSonChapters($chapters, 9);
  129. array_unshift($zyzChapterIds, 9);//放到数组前面
  130. $resultChapterIds = DB::connection('company')->table($tableStudyProc)->where('user_id', $uid)->orderBy('chapter_id', 'asc')->pluck('chapter_id')->toArray();
  131. $resultChapterIds = array_values(array_unique($resultChapterIds));
  132. $needExecuteFgChapterIds = [];
  133. $needExecuteY1ChapterIds = [];
  134. $needExecuteY2ChapterIds = [];
  135. $needExecuteYzChapterIds = [];
  136. $needExecuteZy1ChapterIds = [];
  137. $needExecuteZy2ChapterIds = [];
  138. $needExecuteZyzChapterIds = [];
  139. if (!empty($resultChapterIds)) {
  140. foreach ($resultChapterIds as $resultChapterId) {
  141. if (in_array($resultChapterId, $faguiChapterIds)) {
  142. $needExecuteFgChapterIds [] = $resultChapterId;
  143. } elseif (in_array($resultChapterId, $y1ChapterIds)) {
  144. $needExecuteY1ChapterIds [] = $resultChapterId;
  145. } elseif (in_array($resultChapterId, $y2ChapterIds)) {
  146. $needExecuteY2ChapterIds [] = $resultChapterId;
  147. } elseif (in_array($resultChapterId, $yzChapterIds)) {
  148. $needExecuteYzChapterIds [] = $resultChapterId;
  149. } elseif (in_array($resultChapterId, $zy1ChapterIds)) {
  150. $needExecuteZy1ChapterIds [] = $resultChapterId;
  151. } elseif (in_array($resultChapterId, $zy2ChapterIds)) {
  152. $needExecuteZy2ChapterIds [] = $resultChapterId;
  153. } elseif (in_array($resultChapterId, $zyzChapterIds)) {
  154. $needExecuteZyzChapterIds [] = $resultChapterId;
  155. }
  156. }
  157. $study_proc = '';
  158. if (!empty($needExecuteFgChapterIds)) {
  159. $needChapterFgId = end($needExecuteFgChapterIds);
  160. $needChapterFgId = $this->getChapterName($needChapterFgId, $tableNameChapter);
  161. $study_proc = $needChapterFgId;
  162. }
  163. if (!empty($needExecuteY1ChapterIds)) {
  164. $needChapterY1Id = end($needExecuteY1ChapterIds);
  165. $needChapterY1Id = $this->getChapterName($needChapterY1Id, $tableNameChapter);
  166. if (!empty($study_proc)) {
  167. $study_proc = $study_proc . ','. $needChapterY1Id;
  168. } else {
  169. $study_proc = $needChapterY1Id;
  170. }
  171. }
  172. if (!empty($needExecuteY2ChapterIds)) {
  173. $needChapterY2Id = end($needExecuteY2ChapterIds);
  174. $needChapterY2Id = $this->getChapterName($needChapterY2Id, $tableNameChapter);
  175. if (!empty($study_proc)) {
  176. $study_proc = $study_proc . ','. $needChapterY2Id;
  177. } else {
  178. $study_proc = $needChapterY2Id;
  179. }
  180. }
  181. if (!empty($needExecuteYzChapterIds)) {
  182. $needChapterYzId = end($needExecuteYzChapterIds);
  183. $needChapterYzId = $this->getChapterName($needChapterYzId, $tableNameChapter);
  184. if (!empty($study_proc)) {
  185. $study_proc = $study_proc . ','. $needChapterYzId;
  186. } else {
  187. $study_proc = $needChapterYzId;
  188. }
  189. }
  190. if (!empty($needExecuteZy1ChapterIds)) {
  191. $needChapterZy1Id = end($needExecuteZy1ChapterIds);
  192. $needChapterZy1Id = $this->getChapterName($needChapterZy1Id, $tableNameChapter);
  193. if (!empty($study_proc)) {
  194. $study_proc = $study_proc . ','. $needChapterZy1Id;
  195. } else {
  196. $study_proc = $needChapterZy1Id;
  197. }
  198. }
  199. if (!empty($needExecuteZy2ChapterIds)) {
  200. $needChapterZy2Id = end($needExecuteZy2ChapterIds);
  201. $needChapterZy2Id = $this->getChapterName($needChapterZy2Id, $tableNameChapter);
  202. if (!empty($study_proc)) {
  203. $study_proc = $study_proc . ','. $needChapterZy2Id;
  204. } else {
  205. $study_proc = $needChapterZy2Id;
  206. }
  207. }
  208. if (!empty($needExecuteZy2ChapterIds)) {
  209. $needChapterZyzId = end($needExecuteZy2ChapterIds);
  210. $needChapterZyzId = $this->getChapterName($needChapterZyzId, $tableNameChapter);
  211. if (!empty($study_proc)) {
  212. $study_proc = $study_proc . ','. $needChapterZyzId;
  213. } else {
  214. $study_proc = $needChapterZyzId;
  215. }
  216. }
  217. //$study_proc = implode(',', $resultChapterIds);
  218. } else {
  219. $study_proc = '';
  220. }
  221. $exportData = [];
  222. $exportData['uid'] = $uid;
  223. $exportData['phone'] = $allUserInfo['phone'];
  224. $exportData['reg_date'] = date("Y-m-d H:i:s", $allUserInfo['insert_time']) ;
  225. $exportData['first_study_date'] = !empty($firstStudyTime) ? date("Y-m-d H:i:s", $firstStudyTime) : '';
  226. $exportData['first_answer_num'] = $answerNum;
  227. $exportData['study_proc'] = $study_proc;
  228. $this->putRow($exportData);
  229. $bar->advance();
  230. }
  231. $bar->finish();
  232. $this->info('');
  233. $this->info('导出完成');
  234. }
  235. public function getChapterName($chapterId, $tableNameChapter)
  236. {
  237. $infos = DB::connection('company')->table($tableNameChapter)->where('id', $chapterId)->first();
  238. $baseName = $infos['name'];
  239. $level = $infos['level'];
  240. if ($level == 3) {
  241. //父级id也就是第二级的id
  242. $thirdChapterParentId = DB::connection('company')->table($tableNameChapter)->where('id', $chapterId)->value('parent_id');
  243. $secondLevelName = DB::connection('company')->table($tableNameChapter)->where('id', $thirdChapterParentId)->value('name');
  244. //dd($secondLevelName . '-' . $baseName);
  245. return $secondLevelName . '->' . $baseName;
  246. } elseif ($level == 4) {
  247. //父级id也就是第三级的id
  248. $fourthLevelChapterParentId = DB::connection('company')->table($tableNameChapter)->where('id', $chapterId)->value('parent_id');
  249. $thirdLevelInfo = DB::connection('company')->table($tableNameChapter)->where('id', $fourthLevelChapterParentId)->first();
  250. $thirdLevelName = $thirdLevelInfo['name'];
  251. $thirdLevelId = $thirdLevelInfo['id'];
  252. //父级id也就是第二级的id
  253. $thirdLevelChapterParentId = DB::connection('company')->table($tableNameChapter)->where('id', $thirdLevelId)->value('parent_id');
  254. $secondLevelName = DB::connection('company')->table($tableNameChapter)->where('id', $thirdLevelChapterParentId)->value('name');
  255. //dd($secondLevelName . '--' . $thirdLevelName .'--'. $baseName);
  256. return $secondLevelName . '->' . $thirdLevelName .'->'. $baseName;
  257. } elseif ($level == 5) {
  258. $fifthChapterParentId = DB::connection('company')->table($tableNameChapter)->where('id', $chapterId)->value('parent_id');
  259. $fourthLevelInfo = DB::connection('company')->table($tableNameChapter)->where('id', $fifthChapterParentId)->first();
  260. $fourthLevelName = $fourthLevelInfo['name'];
  261. $fourthLevelId = $fourthLevelInfo['id'];
  262. $fourthChapterParentId = DB::connection('company')->table($tableNameChapter)->where('id', $fourthLevelId)->value('parent_id');
  263. $thirdLevelInfo = DB::connection('company')->table($tableNameChapter)->where('id', $fourthChapterParentId)->first();
  264. $thirdLevelName = $thirdLevelInfo['name'];
  265. $thirdLevelId = $thirdLevelInfo['id'];
  266. $thirdChapterParentId = DB::connection('company')->table($tableNameChapter)->where('id', $thirdLevelId)->value('parent_id');
  267. $secondLevelName = DB::connection('company')->table($tableNameChapter)->where('id', $thirdChapterParentId)->value('name');
  268. //dd($secondLevelName . '-' . $thirdLevelName . '-'. $fourthLevelName . '-' . $baseName);
  269. return $secondLevelName . '->' . $thirdLevelName . '->'. $fourthLevelName . '->' . $baseName;
  270. } elseif ($level == 6) {
  271. $sixChapterParentId = DB::connection('company')->table($tableNameChapter)->where('id', $chapterId)->value('parent_id');
  272. $fifthLevelInfo = DB::connection('company')->table($tableNameChapter)->where('id', $sixChapterParentId)->first();
  273. $fifthLevelName = $fifthLevelInfo['name'];
  274. $fifthLevelId = $fifthLevelInfo['id'];
  275. $fifthChapterParentId = DB::connection('company')->table($tableNameChapter)->where('id', $fifthLevelId)->value('parent_id');
  276. $fourthLevelInfo = DB::connection('company')->table($tableNameChapter)->where('id', $fifthChapterParentId)->first();
  277. $fourthLevelName = $fourthLevelInfo['name'];
  278. $fourthLevelId = $fourthLevelInfo['id'];
  279. $fourthChapterParentId = DB::connection('company')->table($tableNameChapter)->where('id', $fourthLevelId)->value('parent_id');
  280. $thirdLevelInfo = DB::connection('company')->table($tableNameChapter)->where('parent_id', $fourthChapterParentId)->first();
  281. $thirdLevelName = $thirdLevelInfo['name'];
  282. $thirdLevelId = $thirdLevelInfo['id'];
  283. $thirdChapterParentId = DB::connection('company')->table($tableNameChapter)->where('id', $thirdLevelId)->value('parent_id');
  284. $secondLevelName = DB::connection('company')->table($tableNameChapter)->where('id', $thirdChapterParentId)->value('name');
  285. //dd($secondLevelName . '-' . $thirdLevelName . '-'. $fourthLevelName . '-' . $fifthLevelName . '-' . $baseName);
  286. return $secondLevelName . '->' . $thirdLevelName . '->'. $fourthLevelName . '->' . $fifthLevelName . '->' . $baseName;
  287. }
  288. }
  289. }