2018-12-25 15:20:41 +08:00

93 lines
2.4 KiB
PHP

<?php
namespace App\Domains\Virtual\Commands\Sync;
use Carbon\Carbon;
use Illuminate\Console\Command as BaseCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Command extends BaseCommand
{
/**
* Execute the console command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return mixed
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->line('开始'.$this->description);
set_time_limit(0);
ini_set('memory_limit', '4096m');
ini_set('default_socket_timeout', -1);
parent::execute($input, $output);
}
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()->subMonth();
}
protected function getArguments()
{
return [
['month', InputArgument::OPTIONAL, '要同步的数据月份,默认上个月 #示例: 2018-10'],
];
}
/**
* 获取文件内容
*
* @return void
*/
protected function getFile()
{
$file = storage_path('app/command/' . env('APP_ENV') . '_' . self::FILENAME);
if (!file_exists($file)) {
$dir = dirname($file);
if (null !== $dir && !is_dir($dir)) {
mkdir($dir, 0777, true);
}
$this->saveFile(0, self::INIT_CURSOR);
}
$contents = file_get_contents($file);
return json_decode($contents, 256);
}
/**
* 写入文件
*
* @param integer $status 状态 1运行中 0运行结束
* @param integer $cursor 游标
* @return void
*/
protected function saveFile(int $status, int $cursor)
{
$file = storage_path('app/command/' . env('APP_ENV') . '-' . self::FILENAME);
$contents = json_encode([
'status' => $status,
'cursor' => $cursor,
]);
file_put_contents($file, $contents);
}
}