vd/app/Domains/Virtual/Commands/Schedule/AutoActivate.php
2020-01-14 20:09:52 +08:00

63 lines
2.0 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 = sprintf("SELECT sim FROM virtual_order_cards WHERE created_at >= '%s' AND created_at <= '%s' AND service_start_at IS NULL AND deleted_at IS NULL AND refunded_at IS NULL ORDER BY created_at DESC", $time->copy()->startOfMonth(), $time->copy()->endOfMonth());
$updateSql = "UPDATE cards SET virtual_activated_at='%s' WHERE sim IN (%s) AND virtual_activated_at IS NULL";
DB::statement(sprintf($updateSql, $datetime->startOfMonth(), $simArray));
$fixSql = "SELECT fix_timelines(ARRAY(%s))";
DB::statement(sprintf($fixSql, $simArray));
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'],
];
}
}