container = $container; parent::__construct('command:hotelRecords2'); } public function configure() { parent::configure(); $this->setDescription('写入开房记录!'); } public function handle() { // 数据地址 $path = BASE_PATH . '/storage/hotelRecords_2'; // 地址是正确 if (is_dir($path)) { $info = opendir($path); $hotelLog = make(HotelLogModel::class); $errorArray = []; while (($file = readdir($info)) !== false) { if ($file === '.' || $file === '..') { // 只获取当前目录的 continue; } $this->line($file, 'info'); // 开始读取文件 $file_path = $path . '/' . $file; if (file_exists($file_path)) { $fp = fopen($file_path, "r"); // 每次读取一行解析 $i = 0; while (!feof($fp)) { $str = fgets($fp); if ($i > 0) { // 表头不要 if ($str) { $this->line('文件名为⬇⬇⬇----' . $file, 'info'); $this->line('字符串为--->>>' . $str, 'info'); // 字段存在才执行 if (strpos($str, ',')) { // 包含数据的才要 // 开始解析数据 $toIngoArray = explode(',', $str); // 开房时间处理 $check_time = $toIngoArray[31] ?? ''; if (strtotime($check_time)) { $check_time = Carbon::parse($toIngoArray[31])->toDateTimeString(); } // 出生日期处理 $birthday = $toIngoArray[6] ?? ''; if (strlen($birthday) === 8) { $birthday = substr($birthday, 0, 4) . '-' . substr($birthday, 4, 2) . '-' . substr($birthday, 6, 2); } $createInfo = [ // 姓名 'username' => $toIngoArray[0] ?? '', // 身份类型 'ct_type' => $toIngoArray[3] ?? '', // 证件号码 'ct_fid' => $toIngoArray[4] ?? '', // 性别(1男2女3未知) 'gender' => ($toIngoArray[5] ?? 3) === "M" ? 1 : ($toIngoArray[5] === "F" ? 2 : 3), // 出生日期 'birthday' => $birthday, // 电话号码 'mobile' => $toIngoArray[19] ?? '', // 开房地址 'check_address' => $toIngoArray[7] ?? '', // 开房时间 'check_time' => $check_time, ]; // 存到mongo db.createCollection("hotel_log"); $res = $hotelLog->db()->insertOne($createInfo); if (!$res) { array_push($errorArray, $createInfo); } $this->line('执行完成--ok--' . $file . '------>>>姓名:' . $createInfo['username'] . '-身份类型:' . $createInfo['ct_type'] . '-证件号码:' . $createInfo['ct_fid'] . '-性别(1男2女3未知):' . $createInfo['gender'] . '-出生日期:' . $createInfo['birthday'] . '-电话号码:' . $createInfo['mobile'] . '-开房地址:' . $createInfo['check_address'] . '-开房时间:' . $createInfo['check_time'], 'info'); } } } $i++; } fclose($fp); } } closedir($info); // 失败的 $this->line('--失败⬇--', 'info'); var_dump($errorArray); } } }