HotelRecords_2Command.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Command;
  4. use App\Model\Mongo\HotelLogModel;
  5. use Carbon\Carbon;
  6. use Hyperf\Command\Command as HyperfCommand;
  7. use Hyperf\Command\Annotation\Command;
  8. use Psr\Container\ContainerInterface;
  9. /**
  10. * @Command
  11. */
  12. #[Command]
  13. class HotelRecords_2Command extends HyperfCommand
  14. {
  15. /**
  16. * @var ContainerInterface
  17. */
  18. protected $container;
  19. public function __construct(ContainerInterface $container)
  20. {
  21. $this->container = $container;
  22. parent::__construct('command:hotelRecords2');
  23. }
  24. public function configure()
  25. {
  26. parent::configure();
  27. $this->setDescription('写入开房记录!');
  28. }
  29. public function handle()
  30. {
  31. // 数据地址
  32. $path = BASE_PATH . '/storage/hotelRecords_2';
  33. // 地址是正确
  34. if (is_dir($path)) {
  35. $info = opendir($path);
  36. $hotelLog = make(HotelLogModel::class);
  37. $errorArray = [];
  38. while (($file = readdir($info)) !== false) {
  39. if ($file === '.' || $file === '..') {
  40. // 只获取当前目录的
  41. continue;
  42. }
  43. $this->line($file, 'info');
  44. // 开始读取文件
  45. $file_path = $path . '/' . $file;
  46. if (file_exists($file_path)) {
  47. $fp = fopen($file_path, "r");
  48. // 每次读取一行解析
  49. $i = 0;
  50. while (!feof($fp)) {
  51. $str = fgets($fp);
  52. if ($i > 0) {
  53. // 表头不要
  54. if ($str) {
  55. $this->line('文件名为⬇⬇⬇----' . $file, 'info');
  56. $this->line('字符串为--->>>' . $str, 'info');
  57. // 字段存在才执行
  58. if (strpos($str, ',')) {
  59. // 包含数据的才要
  60. // 开始解析数据
  61. $toIngoArray = explode(',', $str);
  62. // 开房时间处理
  63. $check_time = $toIngoArray[31] ?? '';
  64. if (strtotime($check_time)) {
  65. $check_time = Carbon::parse($toIngoArray[31])->toDateTimeString();
  66. }
  67. // 出生日期处理
  68. $birthday = $toIngoArray[6] ?? '';
  69. if (strlen($birthday) === 8) {
  70. $birthday = substr($birthday, 0, 4) . '-' . substr($birthday, 4,
  71. 2) . '-' . substr($birthday, 6, 2);
  72. }
  73. $createInfo = [
  74. // 姓名
  75. 'username' => $toIngoArray[0] ?? '',
  76. // 身份类型
  77. 'ct_type' => $toIngoArray[3] ?? '',
  78. // 证件号码
  79. 'ct_fid' => $toIngoArray[4] ?? '',
  80. // 性别(1男2女3未知)
  81. 'gender' => ($toIngoArray[5] ?? 3) === "M" ? 1 : ($toIngoArray[5] === "F" ? 2 : 3),
  82. // 出生日期
  83. 'birthday' => $birthday,
  84. // 电话号码
  85. 'mobile' => $toIngoArray[19] ?? '',
  86. // 开房地址
  87. 'check_address' => $toIngoArray[7] ?? '',
  88. // 开房时间
  89. 'check_time' => $check_time,
  90. ];
  91. // 存到mongo db.createCollection("hotel_log");
  92. $res = $hotelLog->db()->insertOne($createInfo);
  93. if (!$res) {
  94. array_push($errorArray, $createInfo);
  95. }
  96. $this->line('执行完成--ok--'
  97. . $file . '------>>>姓名:'
  98. . $createInfo['username']
  99. . '-身份类型:'
  100. . $createInfo['ct_type']
  101. . '-证件号码:'
  102. . $createInfo['ct_fid']
  103. . '-性别(1男2女3未知):'
  104. . $createInfo['gender']
  105. . '-出生日期:'
  106. . $createInfo['birthday']
  107. . '-电话号码:'
  108. . $createInfo['mobile']
  109. . '-开房地址:'
  110. . $createInfo['check_address']
  111. . '-开房时间:'
  112. . $createInfo['check_time'],
  113. 'info');
  114. }
  115. }
  116. }
  117. $i++;
  118. }
  119. fclose($fp);
  120. }
  121. }
  122. closedir($info);
  123. // 失败的
  124. $this->line('--失败⬇--', 'info');
  125. var_dump($errorArray);
  126. }
  127. }
  128. }