ChatRecordSyncCommand.php
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?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;
}
}