WorkOrderDing.php 5.2 KB
<?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();
            }
        }
    }
}