<?php
/**
 * Created by PhpStorm.
 * User: zhoujun
 * Date: 2018/9/1
 * Time: 12:11
 */

namespace addons\faqueue\library;

use addons\faqueue\model\FaqueueLog;
use think\Db;
use think\Queue;

class QueueApi
{

    public static function sendEmail($subject, $to, $message)
    {

        $data = [
            'subject' => $subject,
            'to'      => $to,
            'message' => $message,
        ];

        return self::push('addons\faqueue\library\jobs\EmailJob', $data);

    }


    public static function smsSend($mobile, $code = null, $event = 'default')
    {
        $data = [
            'method' => 'send',
            'mobile' => $mobile,
            'code'   => $code,
            'event'  => $event,
        ];
        return self::push('addons\faqueue\library\jobs\SmsJob', $data);
    }

    public static function smsNotice($mobile, $msg = '', $template = null)
    {
        $data = [
            'method'   => 'notice',
            'mobile'   => $mobile,
            'msg'      => $msg,
            'template' => $template,
        ];
        return self::push('addons\faqueue\library\jobs\SmsJob', $data);
    }

    //发生im消息推送-自定义消息飘屏
    public static function sendGroupMessage($type, $sender, $receiver, $partyInfo, $giftUserParty)
    {
        if (!$type) {
            return false;
        }

        $messageData = [
            'type'    => $type,
            'content' => [
                'party_info'  => [
                    'party_id'   => $partyInfo['id'],
                    'room_type'  => $partyInfo['room_type'],
                    'party_name' => $partyInfo['party_name']
                ],
                'notice_info' => [
                    'sender'         => $sender,
                    'receiver'       => $receiver,
                    'gift_num'       => $giftUserParty['number'],
                    'gift_name'      => $giftUserParty['gift_name'],
                    'gift_image'     => $giftUserParty['gift_gif_image'],
                    'gift_rgb_color' => "#fde4ac"
                ]
            ]
        ];

        return self::later(1, 'addons\faqueue\library\jobs\SendImMsessageJob', $messageData);
    }

    //定时检测贵族保级
    public static function timeRelegation($id, $endTime=0,$noble=0)
    {
        if (empty($id)) {
            return false;
        }

        $messageData = [
            'user_id'=>$id,
            'end_time'=>$endTime,
            'noble'=>$noble
        ];

        return self::later($endTime-time(),'addons\faqueue\library\jobs\CheckRelegation', $messageData);
//        return self::later(30,'addons\faqueue\library\jobs\CheckRelegation', $messageData);
    }

    public static function push($job, $data = '', $queue = null)
    {
        return Queue::push($job, $data, $queue);
    }

    public static function later($delay, $job, $data = '', $queue = null)
    {
        return Queue::later($delay, $job, $data, $queue);
    }
}