PushNotify.php 3.2 KB
<?php

namespace App\Console\Commands\WorkOrder;

use App\Models\ProjectAssociation\ProjectAssociation;
use App\Models\Workchat\MessagePush;
use App\Models\WorkOrder\Tickets;
use Illuminate\Console\Command;

class PushNotify extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'workorder:push-notify';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'tickets push notify';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        while (true) {
            try {
                $tick = Tickets::where('ding', 0)
                    ->where('submit_side', 2)
//                    ->where('project_id', 1)
                    ->first();
                if (!$tick) {
                    echo now() . " WARNING | 没有待推送的工单\n";
                    sleep(3);
                    continue;
                }
                $project = $tick->project;
                if ($project->version != 6 || $project->is_del == 1) {
                    echo now() . " WARNING | 项目版本或状态异常 \n";
                    $tick->ding = 1;
                    $tick->save();
                    continue;
                }
                $message_push = new MessagePush();
                $message_push->project_id = $project->table_id;
                $message_push->friend_id = ProjectAssociation::where('project_id', $project->table_id)
                    ->where('status', ProjectAssociation::STATUS_NORMAL)
                    ->where('binding_app', ProjectAssociation::ENTERPRISE_WECHAT)
                    ->value('friend_id');
                if (empty($message_push->friend_id))
                {
                    echo now() . " WARNING | 项目ID:{$project->table_id} 没有绑定企微群\n";
                    $tick->ding = 1;
                    $tick->save();
                    continue;
                }
                $message_push->content_type = 'Link';
                $message_push->content = json_encode([
                    'title' => '工单查看 - ' . $project->company_name,
                    'desc' => $tick->title,
                    'size' => 0,
                    'thumbSize' => 0,
                    'thumbUrl' => 'https://oa.quanqiusou.cn/logo.ico',
                    'url' => 'https://oa.quanqiusou.cn/tickets?project_id='.$project->uuid
                ], JSON_UNESCAPED_UNICODE);
                $message_push->send_time = now();
                $message_push->type = MessagePush::TYPE_TICKET;
                $message_push->save();
                $tick->ding = 1;
                $tick->save();
                echo now() . " INFO | 项目ID:{$project->table_id} 工单ID:{$tick->id} 推送成功\n";
            }catch (\Exception $exception){
                echo date('Y-m-d H:i:s')." ERROR | ".$exception->getMessage()."\n";
                break;
            }
        }
    }
}