SyncInquiryProjectRoute.php
5.7 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2025/2/18
* Time: 17:10
*/
namespace App\Console\Commands\Inquiry;
use App\Models\Channel\Channel;
use App\Models\Domain\DomainInfo;
use App\Models\Inquiry\InquiryProject;
use App\Models\Inquiry\InquiryProjectRoute;
use App\Models\Product\Category;
use App\Models\Product\Product;
use App\Models\Project\OnlineCheck;
use App\Models\Project\Project;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
/**
* Class SyncInquiryProjectRoute
* @package App\Console\Commands\Inquiry
*/
class SyncInquiryProjectRoute extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync_inquiry_project_route';
/**
* The console command description.
*
* @var string
*/
protected $description = '同步询盘信息:项目对应路由,';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* 同步优化项目及路由
* TODO 同步v4 v5 v6项目以及路由, 删除过期项目及路由
* @return bool
*/
public function handle()
{
while (true) {
$sync_id = Redis::rpop('sync_inquiry_project_route_task');
if (empty($sync_id)) {
sleep(60);
continue;
}
$task = InquiryProject::where(['id' => $sync_id])->first();
if (empty($task))
continue;
// 同步对应项目路由, 以及删除过期路由
if ($task->version == InquiryProject::VERSION_SIX){
$this->syncGloV6Route($task);
} else {
$this->syncGloV5Route($task);
}
}
return true;
}
/**
* 同步v4 v5项目路由
* @param $task
* @return bool
*/
public function syncGloV5Route($task)
{
$date = intval(date('Ymd'));
$result = file_get_contents(storage_path('logs/sync_inquiry_project_route/' . $task->id . '.json'));
$result = json_decode($result, true);
if (empty($result)) {
// 未获取到数据 删除当前项目过期路由
$this->deleteExpire($task->id, $date);
$this->log('syncGloV5Route 未获取到路由信息:' . $task->id . ', 路由获取地址:' . ($task->is_split && $task->test_url ? $task->test_url : $task->main_url) . 'k_u_api.php');
return false;
}
foreach ($result as $key=>$val) {
try {
$tmp = explode('|', $val);
$url_tmp = parse_url($tmp[0]);
$route = trim($url_tmp['path'], '/');
$title = str_replace('+', ' ', $tmp[1]);
if (strlen($title) > 200 || strlen($route) > 200) {
$this->log('syncGloV5Route 路由或标题过长,无效记录');
continue;
}
InquiryProjectRoute::saveProjectRoute($task->id, $title, $route, $date);
} catch (\Exception $e) {
$this->log('syncGloV5Route 解析路径:' . $val . ', 错误信息:' . $e->getMessage());
echo 'syncGloV5Route 解析路径:' . $val . ', 错误信息:' . $e->getMessage() . PHP_EOL;
}
}
// 删除当前项目过期路由
$this->deleteExpire($task->id, $date);
return true;
}
/**
* 同步v6项目路由
* @param $task
* @return bool
*/
public function syncGloV6Route($task)
{
$date = intval(date('Ymd'));
ProjectServer::useProject($task->primary_id);
// TODO 产品分类标题、路由, 产品标题、路由, 同步到路由表
$category = Category::where('status', Category::STATUS_ACTIVE)->get(['title', 'route']);
foreach ($category as $key=>$val) {
InquiryProjectRoute::saveProjectRoute($task->id, $val->title, $val->route, $date);
}
// 产品数量会比较多, 所以使用分页 同步数据
$id = 0;
while (true) {
echo '同步项目路由:' . $id . PHP_EOL;
$product = Product::where('status', Product::STATUS_ON)->where('id', '>', $id)->orderBy('id', 'asc')->limit(1000)->get(['id', 'title', 'route']);
if ($product->isEmpty())
break;
foreach ($product as $key=>$val) {
$id = $val->id;
InquiryProjectRoute::saveProjectRoute($task->id, $val->title, $val->route, $date);
}
}
DB::disconnect('custom_mysql');
// 删除当前项目过期路由
$this->deleteExpire($task->id, $date);
return true;
}
/**
* 删除过期数据, 非当前更新数据, 都删除, 误删第二天再重新同步更新
* @param $project_id
* @param $date
* @return bool
*/
public function deleteExpire($project_id, $date)
{
$project_route_num = InquiryProjectRoute::where(['project_id' => $project_id])->where('date', '<', $date)->delete();
$this->log('项目ID:' . $project_id . ', 删除过期路由数量:' . $project_route_num);
return true;
}
/**
* 输出日志到特定的文件内, 这个文件需要定时排除内容
* @param $message
* @return bool
*/
public function log($message)
{
$message = date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
file_put_contents(storage_path('logs/zhl/output') . date('Y-m-d') . '.log', $message, FILE_APPEND);
return true;
}
}