79 lines
2.7 KiB
PHP
79 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Virtual\Commands\Schedule;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Domains\Card\Repositories\CardRepository;
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
use App\Domains\Virtual\Repositories\OrderCardPartitionRepository;
|
|
use App\Models\Card\Card;
|
|
|
|
class AutoActivate extends Command
|
|
{
|
|
protected $name = 'virtual:schedule-auto-activate';
|
|
|
|
protected $description = '自动激活脚本';
|
|
|
|
const INTERVAL_MONTH = 6;
|
|
|
|
public function handle()
|
|
{
|
|
$datetime = $this->getDateTime();
|
|
|
|
$time = $datetime->copy()->subMonths(self::INTERVAL_MONTH);
|
|
|
|
$this->info('自动激活脚本');
|
|
|
|
// $simArray = app(OrderCardPartitionRepository::class)->select('sim')
|
|
// ->where('created_at', '>=', $time->copy()->startOfMonth())
|
|
// ->where('created_at', '<=', $time->copy()->endOfMonth())
|
|
// ->whereNull('service_start_at')
|
|
// ->get()->pluck('sim')->toArray();
|
|
|
|
// $this->info(microtime(true));
|
|
|
|
// foreach (array_chunk($simArray, 1000) as $values) {
|
|
// Card::query()->whereIn('sim', $values)->whereNull('virtual_activated_at')->update(['virtual_activated_at' => $datetime->startOfMonth()]);
|
|
// $values = implode(',', array_keys($values));
|
|
// DB::statement("select fix_timelines('{{$values}}'::INT8[]);");
|
|
// }
|
|
|
|
$sql = "WITH sim_array AS (
|
|
SELECT sim FROM virtual_order_cards WHERE created_at >= '%s' AND created_at <= '%s' AND service_start_at IS NULL ORDER BY created_at DESC
|
|
), activate_updates AS (
|
|
UPDATE cards SET virtual_activated_at='%s' WHERE sim IN (SELECT * FROM sim_array) AND virtual_activated_at IS NULL
|
|
)
|
|
SELECT fix_timelines(ARRAY(SELECT * FROM sim_array));
|
|
";
|
|
|
|
DB::statement(sprintf($sql, $time->copy()->startOfMonth(), $time->copy()->endOfMonth(), $datetime->startOfMonth()));
|
|
|
|
app(CardRepository::class)->forgetCached();
|
|
app(OrderCardPartitionRepository::class)->forgetCached();
|
|
|
|
$this->info('自动激活脚本执行完毕');
|
|
}
|
|
|
|
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'],
|
|
];
|
|
}
|
|
}
|