|
|
@@ -0,0 +1,111 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Longyi\Gift\Console\Commands;
|
|
|
+
|
|
|
+use Illuminate\Console\Command;
|
|
|
+use Longyi\Gift\Models\GiftCards;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+use Monolog\Logger;
|
|
|
+use Monolog\Handler\StreamHandler;
|
|
|
+
|
|
|
+class ImportGiftCards extends Command
|
|
|
+{
|
|
|
+ protected $signature = 'gift-cards:import {--force}';
|
|
|
+ protected $customLogger = null;
|
|
|
+ protected $logFile = 'gift_import.log';
|
|
|
+
|
|
|
+ public function handle()
|
|
|
+ {
|
|
|
+ set_time_limit(0);
|
|
|
+ $limit = 500;
|
|
|
+ $offset = 0;
|
|
|
+ // 初始化自定义日志
|
|
|
+ $this->initCustomLogger($this->logFile);
|
|
|
+ //用户等级
|
|
|
+ $data = [];
|
|
|
+ do {
|
|
|
+ try {
|
|
|
+ $data = $this->fetchData($offset, $limit);
|
|
|
+ $this->saveDatas($data);
|
|
|
+ $offset = $offset + $limit;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ $this->logError(sprintf('[GiftCard Import] Error at offset %d: %s', $offset, $e->getMessage()));
|
|
|
+ exit(1);
|
|
|
+ }
|
|
|
+ echo $offset;
|
|
|
+ echo PHP_EOL;
|
|
|
+
|
|
|
+ } while (is_array($data) && !empty($data));
|
|
|
+
|
|
|
+ echo 'gift card import over~~';
|
|
|
+ echo PHP_EOL;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function fetchData($offset, $limit)
|
|
|
+ {
|
|
|
+ $sql = "select * from giftcard_customer where status=1 and endtime > '" . date('Y-m-d H:i:s') . "' order by gitfcard_id ASC limit {$offset}, {$limit}";
|
|
|
+ return DB::select($sql);
|
|
|
+ }
|
|
|
+ protected function saveDatas($data)
|
|
|
+ {
|
|
|
+ if (!$data) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ foreach ($data as $item) {
|
|
|
+ $this->saveGiftCard($item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function saveGiftCard($item)
|
|
|
+ {
|
|
|
+ GiftCards::addGiftCard(
|
|
|
+ $item->customer_id,
|
|
|
+ $item->giftcard_amount,
|
|
|
+ 0,
|
|
|
+ $item->endtime,
|
|
|
+ 'activity_26_06_03',
|
|
|
+ '手动迁移'
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Initialize custom logger with specified file
|
|
|
+ *
|
|
|
+ * @param string $logFile
|
|
|
+ */
|
|
|
+ protected function initCustomLogger(string $logFile)
|
|
|
+ {
|
|
|
+ $this->customLogger = new Logger('gift-card-import');
|
|
|
+ $this->customLogger->pushHandler(new StreamHandler($logFile, Logger::INFO));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Log info message
|
|
|
+ *
|
|
|
+ * @param string $message
|
|
|
+ */
|
|
|
+ protected function logInfo(string $message)
|
|
|
+ {
|
|
|
+ if ($this->customLogger) {
|
|
|
+ $this->customLogger->info($message);
|
|
|
+ } else {
|
|
|
+ Log::info($message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Log error message
|
|
|
+ *
|
|
|
+ * @param string $message
|
|
|
+ */
|
|
|
+ protected function logError(string $message)
|
|
|
+ {
|
|
|
+ if ($this->customLogger) {
|
|
|
+ $this->customLogger->error($message);
|
|
|
+ } else {
|
|
|
+ Log::error($message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|