WorkOrderDing.php
5.2 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
<?php
namespace App\Console\Commands\WorkOrder;
use App\Models\Manage\Manage;
use App\Models\WorkOrder\TicketChat;
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 = $log->engineer->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: {$log->ticket_id}),请及时处理!",
'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();
}
}
}
}