ChatRecordSyncCommand.php 4.5 KB
<?php
/**
 * +-----------------------------------------------------------------------------------------------------------------------
 * Command :通聊天记录到本地服务器 Command
 * +-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Console\Commands
 * @package   App\Console\Commands
 * @author    Richer <yangzi1028@163.com>
 * @date      2023年4月26日14:08:55
 * @copyright 2022-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Console\Commands;

use App\Models\Chat\ChatRecord;
use App\Models\Chat\ChatRecordItem;
use App\Models\Chat\ChatRecordRemote;
use App\Models\Traits\ChatTrait;
use App\Models\User\TimesRecord;
use Illuminate\Console\Command;

class ChatRecordSyncCommand extends Command
{
    use ChatTrait;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:chat-sync';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '同步远程的聊天记录到本地服务器';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->log_channel = 'sync';
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $remote = app(ChatRecordRemote::class);

        record_log($this->log_channel, $this->description, 'begin');

        record_log($this->log_channel, "开始同步聊天记录" );
        // 获取本地的全部聊天记录和记录的明细
        ChatRecord::with(['items' => function ($query) {
            $query->where('synchronized', 0);
        }])->where('synchronized', 0)->chunkById(100, function ($records) use ($remote) {
            $records->each(function ($record) {
                record_log($this->log_channel, "id :" .$record->id);
                $this->syncRecord($record);
//                $record->items->each(function ($item) {
//                    $this->syncRecordItem($item);
//                });
            });
        });
        record_log($this->log_channel, "完成同步聊天记录" );

        record_log($this->log_channel, "开始同步聊天记录明细" );

        ChatRecordItem::where('synchronized', 0)->chunkById(100, function ($items)  {
            $items->each(function ($item) {
                record_log($this->log_channel, "id :" .$item->id);
                $this->syncRecordItem($item);
            });
        });
        record_log($this->log_channel, "完成同步聊天记录明细" );

        record_log($this->log_channel, "开始同步次数记录明细" );

        TimesRecord::where('synchronized', 0)->chunkById(100, function ($items)  {
            $items->each(function ($item) {
                record_log($this->log_channel, "id :" .$item->id);
                $this->syncUserTimesRecord($item);
            });
        });
        record_log($this->log_channel, "完成同步次数记录明细" );

        record_log($this->log_channel, $this->description, 'end');
    }

    /**
     * Gets connection name from command argument or remote config default
     *
     * @return string
     */
    protected function getConnection()
    {
        $argumentConnection = $this->argument('connection');
        if(!$argumentConnection){
            return config('remote.default');
        }
    }

    /**
     * Gets directory of Laravel install on remote machine
     *
     * @param string $connection
     * @return string
     */
    protected function getRemoteDirectory($connection)
    {
        return config('remote.connections.'.$connection.'.path');
    }


    /**
     * Get import command
     *
     * @return string
     */
    public function getMysqlImportCommand($config, $sqlDump)
    {
        $command = sprintf('mysql --user=%s --password=%s %s < %s',
            escapeshellarg($config['username']),
            escapeshellarg($config['password']),
            escapeshellarg($config['database']),
            $sqlDump
        );
        // $command = sprintf('mysql --user=%s --password=%s %s < %s',
        // 	escapeshellarg($config['username']),
        // 	escapeshellarg($config['password']),
        // 	escapeshellarg($config['database']),
        //     $sqlDump
        // );
        return $command;
    }
}