From 2d7763cc9412cfc3dc99838bd96ddb608b42777e Mon Sep 17 00:00:00 2001 From: denghy Date: Mon, 22 Apr 2019 16:11:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=BF=80=E6=B4=BBVD=E5=B7=B2=E6=9C=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Kernel.php | 1 + .../Virtual/Commands/Schedule/VdActivated.php | 102 ++++++++++++++++++ .../Providers/VirtualServiceProvider.php | 1 + tests/MongoTest.php | 2 +- 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 app/Domains/Virtual/Commands/Schedule/VdActivated.php diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index bc83f569..04ad124c 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -28,6 +28,7 @@ class Kernel extends ConsoleKernel $logPath = storage_path('logs/artisan.log'); $schedule->command('real:sync-activated')->cron('* */4 * * *')->withoutOverlapping()->appendOutputTo($logPath); $schedule->command('virtual:schedule-auto-activate')->cron('* * * 1 *')->withoutOverlapping()->appendOutputTo($logPath); + $schedule->command('virtual:schedule-vd-activate')->cron('0 2 * * *')->withoutOverlapping()->appendOutputTo($logPath); } /** diff --git a/app/Domains/Virtual/Commands/Schedule/VdActivated.php b/app/Domains/Virtual/Commands/Schedule/VdActivated.php new file mode 100644 index 00000000..49ddeba9 --- /dev/null +++ b/app/Domains/Virtual/Commands/Schedule/VdActivated.php @@ -0,0 +1,102 @@ +getDateTime(); + + $time = $datetime->copy()->subMonths(self::INTERVAL_MONTH); + + $startMicrotime = new UTCDateTime($datetime->copy()->subMonth()); + + $cards = OrderCard::select('sim')->where('created_at', '>=', $time) + ->whereNull('service_start_at') + ->orderBy('created_at', 'desc')->get()->pluck('sim')->toArray(); + + foreach (array_chunk($cards, 1000) as $simArray) { + $simArray = array_map('strval', $simArray); + + $res = TblCard::select(['cNo', 'saDate']) + ->whereIn('cNo', $simArray) + ->where('saDate', 'exists', true) + ->where('saDate', '>=', $startMicrotime) + ->get()->toArray(); + + if (!empty($res)) { + $array = []; + foreach ($res as $item) { + $array[] = [ + 'sim' => $item['cNo'], + 'activated_at' => (string)Carbon::createFromTimestampMs(strval($item['saDate'])), + ]; + } + + $table = app(Card::class)->getTable(); + $as = 'as_table'; + $reference = 'sim'; + + $updates = "activated_at={$as}.activated_at::timestamp, + virtual_activated_at=COALESCE({$table}.activated_at, {$as}.activated_at::timestamp)::timestamp"; + + $parameters = collect($array)->map(function ($item) { + return "({$item['sim']}, '{$item['activated_at']}')"; + })->implode(', '); + + $from = "FROM (VALUES $parameters) AS {$as}(sim, activated_at)"; + + $where = "WHERE {$table}.{$reference} = {$as}.{$reference}::int8"; + + $sql = trim("UPDATE {$table} SET {$updates} {$from} {$where}"); + $fixSimArray = implode(',', array_pluck($array, 'sim')); + + DB::statement($sql); + DB::statement("select fix_timelines('{{$fixSimArray}}'::INT8[]);"); + } + } + + app(CardRepository::class)->forgetCached(); + app(OrderCardPartitionRepository::class)->forgetCached(); + } + + protected function getDateTime() + { + if ($month = $this->argument('month')) { + if (!preg_match('/\d{4}-\d{1,2}/', $month)) { + throw new \App\Exceptions\InvalidArgumentException('请输入正确的年月 #示例: 2018-10'); + } + + return Carbon::parse($month)->startOfMonth(); + } + + return Carbon::now()->startOfMonth(); + } + + protected function getArguments() + { + return [ + ['month', InputArgument::OPTIONAL, '要同步的数据月份,默认上个月 #示例: 2018-10'], + ]; + } +} diff --git a/app/Domains/Virtual/Providers/VirtualServiceProvider.php b/app/Domains/Virtual/Providers/VirtualServiceProvider.php index 5a0880ec..2161ee14 100644 --- a/app/Domains/Virtual/Providers/VirtualServiceProvider.php +++ b/app/Domains/Virtual/Providers/VirtualServiceProvider.php @@ -23,6 +23,7 @@ class VirtualServiceProvider extends ServiceProvider $this->commands([ \App\Domains\Virtual\Commands\Schedule\AutoActivate::class, + \App\Domains\Virtual\Commands\Schedule\VdActivated::class, \App\Domains\Virtual\Commands\Sync\CompanySync::class, \App\Domains\Virtual\Commands\Sync\PackageSync::class, \App\Domains\Virtual\Commands\Sync\ProductSync::class, diff --git a/tests/MongoTest.php b/tests/MongoTest.php index bebf36a5..88bfd86a 100644 --- a/tests/MongoTest.php +++ b/tests/MongoTest.php @@ -5,7 +5,7 @@ use App\Domains\Card\Services\CardService; require_once realpath(dirname(__FILE__) . '/TestCase.php'); $simArray = [ - '1064917585595' + '1440418967511' ]; $res = CardService::getMongoCardsInfo($simArray);