WorkOrderDing.php
7.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
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
193
194
195
196
197
198
199
<?php
namespace App\Console\Commands\WorkOrder;
use App\Models\Manage\Manage;
use App\Models\Manage\ManageHr;
use App\Models\WorkOrder\TicketChat;
use App\Models\WorkOrder\TicketDing;
use App\Models\WorkOrder\TicketLog;
use App\Services\DingTalkService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
class WorkOrderDing extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'workorder:ding {action}';
/**
* The console command description.
*
* @var string
*/
protected $description = '售后工单钉钉通知';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$action = $this->argument('action');
$this->$action();
}
public function dingLog()
{
while (true) {
try {
$log = TicketLog::where('ding', 0)->first();
if (!$log) {
echo now() . " | INFO | 没有通知任务\n";
sleep(3);
continue;
}
$mobile = ManageHr::where('manage_id', $log->engineer_id)->value('mobile');
$response = Http::withBasicAuth(
env('DINGDING_BASIC_USER'),
env('DINGDING_BASIC_PASS')
)->get('https://oa.cmer.com/api/dingding/user/' . $mobile);
if ($response->status() == 200) {
$userid = $response->json()['data']['userid'];
$ding = new DingTalkService();
$created_at = date('m-d H', strtotime($log->ticket->created_at));
$plan_end_at = date('m-d H', strtotime($log->ticket->plan_end_at));
$resp = $ding->danliao(json_encode([
'text' => "您有新工单{$log->ticket_id}!{$created_at} 至 {$plan_end_at}!",
'title' => 'AI协同工单 - ' . $log->ticket->project->title,
'picUrl' => 'https://hub.globalso.com/logocm.png',
'messageUrl' => 'https://oa.quanqiusou.cn/afterorder?project_id=' . $log->ticket->project->uuid,
]), [$userid], 'sampleLink');
$log->ding = 1;
echo now() . " | INFO | 工单ID: {$log->ticket_id} 通知成功\n";
}else
{
$log->ding = 2;
echo now() . " | ERROR | 工单ID: {$log->ticket_id} 通知失败\n";
}
$log->save();
}catch (\Exception $exception){
echo now() . " | ERROR | log ID {$log->id} {$exception->getMessage()} {$exception->getTraceAsString()} \n";
$log->ding = 2;
$log->save();
}
}
}
public function dingChat()
{
while (true) {
$chat = TicketChat::where([
'ding' => 0,
'submit_side' => 2
])->first();
if (!$chat) {
echo now() . " | INFO | 没有通知任务\n";
sleep(3);
continue;
}
try {
$project = $chat->ticket->project;
// 通知谁?暂时通知A端最近一次提交的chat记录的人,如果没有,则通第一负责人
$lastChat = TicketChat::where('ticket_id', $chat->ticket_id)
->where('submit_side', 1)
->orderBy('id', 'desc')
->first();
if ($lastChat) {
$mobile = Manage::where('id', $lastChat->manage_id)->first()->mobile;
}else
{
$mobile = Manage::where('id', $project->first_engineer)->first()->mobile;
}
$response = Http::withBasicAuth(
env('DINGDING_BASIC_USER'),
env('DINGDING_BASIC_PASS')
)->get('https://oa.cmer.com/api/dingding/user/' . $mobile);
if ($response->status() == 200) {
$userid = $response->json()['data']['userid'];
$ding = new DingTalkService();
$resp = $ding->danliao(json_encode([
'text' => "客户对工单(ID: {$chat->ticket_id})进行了补充,请及时查看处理!",
'title' => 'AI协同工单 - ' . $project->title,
'picUrl' => 'https://hub.globalso.com/logocm.png',
'messageUrl' => 'https://oa.quanqiusou.cn/afterorder?project_id=' . $project->uuid,
]), [$userid], 'sampleLink');
$chat->ding = 1;
echo now() . " | INFO | 工单ID: {$chat->ticket_id} 通知成功\n";
}else
{
$chat->ding = 2;
echo now() . " | ERROR | 工单ID: {$chat->ticket_id} 通知失败\n";
}
$chat->save();
}catch (\Exception $exception) {
echo now() . " | ERROR | chat ID {$chat->id} {$exception->getMessage()} {$exception->getTraceAsString()} \n";
$chat->ding = 2;
$chat->save();
}
}
}
/**
* 消费 gl_ticket_dings 表的钉钉通知
*/
public function dingSql()
{
while (true) {
$tickDing = TicketDing::where('status', 0)->first();
if (!$tickDing) {
echo now() . " | INFO | 没有通知任务\n";
sleep(3);
continue;
}
try {
$mobiles = ManageHr::whereIn('manage_id', json_decode($tickDing->userIds))
->pluck('mobile')
->toArray();
$response = Http::withBasicAuth(
env('DINGDING_BASIC_USER'),
env('DINGDING_BASIC_PASS')
)->get('https://oa.cmer.com/api/dingding/usersByMobiles', [
'mobiles' => $mobiles,
]);
if ($response->status() == 200) {
$users = $response->json()['data'];
$userIds = [];
foreach ($users as $user) {
$userIds[] = $user['userid'];
}
$ding = new DingTalkService();
$resp = $ding->danliao($tickDing->msgParam, $userIds, $tickDing->msgKey);
$tickDing->status = 1;
$tickDing->save();
echo now() . " | INFO | gl_ticket_dings ID: {$tickDing->id} 通知成功\n";
}else
{
$tickDing->status = 2; // 标记为失败
$tickDing->errorMsg = '钉钉用户查询失败';
$tickDing->save();
echo now() . " | ERROR | gl_ticket_dings ID: {$tickDing->id} 通知失败\n";
}
}catch (\Exception $exception){
echo now() . " | ERROR | gl_ticket_dings ID {$tickDing->id} {$exception->getMessage()} {$exception->getTraceAsString()} \n";
$ding->status = 2; // 标记为失败
$ding->errorMsg = $exception->getMessage();
$ding->save();
}
}
}
}