UpdateProjectTdk.php
7.1 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
<?php
/**
* @remark :
* @name :UpdateProjectTdk.php
* @author :lyh
* @method :post
* @time :2025/7/2 11:11
*/
namespace App\Console\Commands\Tdk;
use App\Models\Blog\Blog;
use App\Models\Blog\BlogCategory;
use App\Models\Com\NoticeLog;
use App\Models\CustomModule\CustomModuleCategory;
use App\Models\CustomModule\CustomModuleContent;
use App\Models\News\News;
use App\Models\News\NewsCategory;
use App\Models\Product\Category;
use App\Models\Product\Keyword;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use PhpOffice\PhpSpreadsheet\IOFactory;
class UpdateProjectTdk extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'update_project_tdk';
/**
* The console command description.
*
* @var string
*/
protected $description = '根据给定的文件更新项目的tdk';
public function handle(){
$noticeLogModel = new NoticeLog();
while (true){
$task_id = $this->getTaskId();
if(empty($task_id)){
sleep(30);
continue;
}
$info = $noticeLogModel->read(['id'=>$task_id]);
if($info !== false){
echo 'start--'.PHP_EOL;
$project_id = $info['data']['project_id'] ?? 0;
if(empty($project_id)){
echo date('Y-m-d H:i:s').'未获取到项目id.'.PHP_EOL;
$noticeLogModel->edit(['status'=>$noticeLogModel::STATUS_FAIL],['id'=>$task_id]);
continue;
}
ProjectServer::useProject($project_id);
$url = $info['data']['url'] ?? '';
if(empty($url)){
echo date('Y-m-d H:i:s').'未获取到项目更新文件.项目id为:'.$project_id.PHP_EOL;
//未获取到文件,跳过
$noticeLogModel->edit(['status'=>$noticeLogModel::STATUS_FAIL],['id'=>$task_id]);
DB::disconnect('custom_mysql');
continue;
}
// 下载到 Laravel storage 的临时路径
$tempPath = storage_path('app/temp_url.xlsx');
file_put_contents($tempPath, file_get_contents($url));
// 载入 Excel
try {
$spreadsheet = IOFactory::load($tempPath);
$sheet = $spreadsheet->getActiveSheet();
$rows = $sheet->toArray();
unlink($tempPath);
}catch (\Exception $e){
echo '文件打不开'.PHP_EOL;
$noticeLogModel->edit(['status'=>$noticeLogModel::STATUS_FAIL],['id'=>$task_id]);
DB::disconnect('custom_mysql');
continue;
}
$dataRows = array_slice($rows, 1);
foreach ($dataRows as $item){
//获取路由
$route = trim($item[0],'/');
$route = basename($route);
$this->updateTdk($route,$item);
}
echo date('Y-m-d H:i:s').',end'.PHP_EOL;
$noticeLogModel->edit(['status'=>$noticeLogModel::STATUS_SUCCESS],['id'=>$task_id]);
DB::disconnect('custom_mysql');
}
}
return true;
}
/**
* @remark :获取任务id
* @name :getTaskId
* @author :lyh
* @method :post
* @time :2025/7/2 11:19
*/
public function getTaskId()
{
$task_id = Redis::rpop('update_project_tdk_task');
if (empty($task_id)) {
$noticeLogModel = new NoticeLog();
$ids = $noticeLogModel->selectField(['status'=>$noticeLogModel::STATUS_PENDING,'type'=>$noticeLogModel::TYPE_UPDATE_PROJECT_TDK],'id');
if(!empty($ids)){
foreach ($ids as $id) {
Redis::lpush('update_project_tdk_task', $id);
}
}
$task_id = Redis::rpop('update_project_tdk_task');
}
return $task_id;
}
/**
* @remark :更新tdk
* @name :updateTdk
* @author :lyh
* @method :post
* @time :2025/7/2 14:59
*/
public function updateTdk($route,$item){
$routeMapModel = new RouteMap();
$routeInfo = $routeMapModel->read(['route'=>$route]);
if($routeInfo === false){
return false;
}
switch ($routeInfo['source']){
case $routeMapModel::SOURCE_PRODUCT:
$productModel = new Product();
$productModel->edit(['seo_mate'=>json_encode(['title'=>$item[1],'description'=>$item[2],'keyword'=>$item[3]])],['id'=>$routeInfo['source_id']]);
break;
case $routeMapModel::SOURCE_PRODUCT_CATE:
$productCategoryModel = new Category();
$productCategoryModel->edit(['seo_title'=>$item[1],'seo_des'=>$item[2],'seo_keywords'=>$item[3]],['id'=>$routeInfo['source_id']]);
break;
case $routeMapModel::SOURCE_PRODUCT_KEYWORD:
$productKeywordModel = new Keyword();
$productKeywordModel->edit(['seo_title'=>$item[1],'seo_description'=>$item[2],'seo_keywords'=>$item[3]],['id'=>$routeInfo['source_id']]);
break;
case $routeMapModel::SOURCE_BLOG:
$blogModel = new Blog();
$blogModel->edit(['seo_title'=>$item[1],'seo_description'=>$item[2],'seo_keywords'=>$item[3]],['id'=>$routeInfo['source_id']]);
break;
case $routeMapModel::SOURCE_BLOG_CATE:
$blogCateModel = new BlogCategory();
$blogCateModel->edit(['seo_title'=>$item[1],'seo_des'=>$item[2],'seo_keywords'=>$item[3]],['id'=>$routeInfo['source_id']]);
break;
case $routeMapModel::SOURCE_NEWS:
$newsModel = new News();
$newsModel->edit(['seo_title'=>$item[1],'seo_description'=>$item[2],'seo_keywords'=>$item[3]],['id'=>$routeInfo['source_id']]);
break;
case $routeMapModel::SOURCE_NEWS_CATE:
$newsCateModel = new NewsCategory();
$newsCateModel->edit(['seo_title'=>$item[1],'seo_des'=>$item[2],'seo_keywords'=>$item[3]],['id'=>$routeInfo['source_id']]);
break;
case $routeMapModel::SOURCE_MODULE:
$contentModel = new CustomModuleContent();
$contentModel->edit(['seo_title'=>$item[1],'seo_description'=>$item[2],'seo_keywords'=>$item[3]],['id'=>$routeInfo['source_id']]);
break;
case $routeMapModel::SOURCE_MODULE_CATE:
$categoryModel = new CustomModuleCategory();
$categoryModel->edit(['seo_title'=>$item[1],'seo_description'=>$item[2],'seo_keywords'=>$item[3]],['id'=>$routeInfo['source_id']]);
break;
default:
break;
}
return true;
}
}