<?php

declare (strict_types=1);

namespace library\utils;


use think\facade\Db;

class Task
{

    /**
     * 发送任务
     * @param $uid
     * @param $type
     * @param $param
     */
    public static function send($uid, $type, $param)
    {
        $updatId = Db::name("task")->insertGetId([
            'uid'  => $uid,
            'type' => $type,
            'data' => json_encode($param),
            'time' => time(),
        ]);
        //执行任务
        self::Task($type, ['taskId' => $updatId]);
    }


    /**
     * 执行任务数据
     * @param $type 任务类型
     * @param array $data 任务code
     * @return bool 任务数据
     */
    public static function Task($type, array $data = []) {
        $data['type'] = $type;
        $client       = new \swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC);
        $ret          = @$client->connect('127.0.0.1', 9506);
        if (!$ret)
            return false;
        $client->send(serialize($data) . "\r\n");
        return true;
        try {

        }catch (\think\Exception $e) {
            return  false;
        }
    }


}