FetchTicketProjects.php
7.0 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
185
186
187
188
189
190
191
192
<?php
namespace App\Console\Commands\WorkOrder;
use App\Models\Manage\Manage;
use App\Models\Project\Project;
use App\Models\WorkOrder\TicketProject;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
class FetchTicketProjects extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'workorder:fetch-ticket-projects {version}';
/**
* The console command description.
*
* @var string
*/
protected $description = '同步V5,V6的项目到 gl_ticket_projects 表';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$version = $this->argument('version');
if ($version == 'v5') {
$this->fetch_v5();
} elseif ($version == 'v6') {
$this->fetch_v6();
} else {
$this->error('Invalid action. Use "v5" or "v6".');
return 1;
}
return 0;
}
/**
* @return void
* 请求:https://www.quanqiusou.cn/extend_api/webs/globalso_all.php
*/
public function fetch_v5()
{
# pm 项目经理 assm 售后服务经理
$response = Http::get('https://www.quanqiusou.cn/extend_api/webs/globalso_all.php');
if ($response->status() == 200) {
$items = $response->json();
foreach ($items as $item) {
# V5: 版本号+postid
$uuid = md5("V5{$item['postid']}");
$project = TicketProject::where('uuid', $uuid)->first();
$assm_id = Manage::where('name', $item['assm'])->value('id') ?? Manage::where('name', '张鸿飞')->value('id') ?? 0; //售后服务经理
$seom_id = Manage::where('name', $item['yhs'])->value('id') ?? Manage::where('name', '陶婵')->value('id') ?? 0; //优化师
// 如果 $item['cate'] 包含”推广“字符,则 $engineer_name = $item['assm']
/**
* 第一负责人逻即说明:
* 优化推广项目:找售后服务经理??鸿飞
* 建站类项目: 找杨长远
*/
$engineer_id = (strpos($item['cate'], '推广') !== false) ? $assm_id : Manage::where('name', '杨长远')->value('id') ?? 0;
$fields = [
'post_id' => $item['postid'],
'company_name' => $item['company'],
'title' => $item['title'],
'engineer_id' => $engineer_id, // 第一负责人
'assm_id' => $assm_id,
'seom_id' => $seom_id,
'website' => $item['main_url'] ?? '',
];
if (!$project) {
$new = new TicketProject();
$new->uuid = $uuid;
$new->version = 5;
$new->table_id = 0;
foreach ($fields as $k => $v) {
$new->$k = $v;
}
$new->save();
} else {
$changed = false;
foreach ($fields as $k => $v) {
if ($project->$k != $v) {
$project->$k = $v;
$changed = true;
}
}
if ($changed) {
$project->save();
}
}
echo "V5: {$item['postid']} - {$item['title']} - {$item['company']} - {$item['main_url']}\n";
}
}
}
/**
* @return void
* 1. 按照ID升序查询 gl_project 表 limit 10
* 2。同步到 TicketProject 后,redis 缓存 ID
*/
public function fetch_v6()
{
$lastid = 0;
while (true) {
try {
$items = Project::where('id', '>', intval($lastid))
->orderBy('id', 'asc')
->limit(10)
->get();
if ($items->isEmpty()) {
echo "not found items \n";
break;
}
foreach ($items as $item) {
$uuid = md5("V5{$item->id}");
$project = TicketProject::where('uuid', $uuid)->first();
// 售后服务经理
$assm_id = $item->deploy_optimize->manager_mid ?? $item->deploy_optimize->tech_leader ?? Manage::where('name', '张鸿飞')->value('id') ?? 0;
$seom_id = $item->deploy_optimize->optimist_mid ?? $assm_id;
/**
* 第一负责人逻辑
* 建站类项目:找杨长远
* 推广类:找售后
*/
if (in_array($item->type, [Project::TYPE_TWO, Project::TYPE_FOUR, Project::TYPE_SIX])) {
// 优化推广项目
$engineer_id = $assm_id;
}elseif ($item->type == Project::TYPE_THREE) {
$engineer_id = Manage::where('name', '杨长远')->value('id') ?? 0; // 建站类项目找杨长远
}else{
$engineer_id = $item->deploy_build->manager_mid ?? $item->deploy_build->leader_mid ?? 0;
}
$fields = [
'company_name' => $item->company,
'title' => $item->title,
'assm_id' => $assm_id,
'seom_id' => $seom_id,
'engineer_id' => $engineer_id
];
if (!$project) {
$project = new TicketProject();
$project->uuid = $uuid;
$project->post_id = $item->post_id;
$project->version = 6;
$project->table_id = $item->id;
foreach ($fields as $k => $v) {
$project->$k = $v;
}
$project->save();
}else{
$changed = false;
foreach ($fields as $k => $v) {
if ($project->$k != $v) {
$project->$k = $v;
$changed = true;
}
}
if ($changed) {
$project->save();
}
}
$lastid = $item->id;
echo date('Y-m-d H:i:s') . " V6: $item->id {$item->company} fetch ok \n";
}
}catch (\Exception $exception) {
echo $exception;
break;
}
}
}
}