<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use JPush\Client as JPush;
class PushController extends Controller
{
    public function push($sys_num,$text,$type){
        if($type==1){
            //$admin=M("admin");
            //$order_list=M("order_list");
            // 服务商id
            $admin_id = DB::table('order_list')->where('sys_num',$sys_num)->value("ser_id");
            // 登录名
            $admin_acc = DB::table('admin')->where('id',$admin_id)->value("admin_acc");
            // 订单数
            $num=DB::table('order_list')->where(['stauas'=>10,'ser_id'=>$admin_id])->count();
            $num=(int)($num);
            // 推送序号
            $serial_num=ltrim($sys_num,"L");
            $serial_num=(int)($serial_num);
            // 离线消息保留时长(秒)
            $time=(int)(86400);
        }else{
            // 别名
            $admin_acc=$sys_num;
            // 订单数
            $num=(int)(0);
            // 推送序号
            $serial_num=(int)(1);
            // 离线消息保留时长(秒)
            $time=(int)(60);
        }

        if($text=='新订单'){
            $letter='xindingdan';
            $letters='xindingdan.caf';
        }else if($text=='夜间订单'){
            $letter='yejiandingdan';
            $letters='cuidingla.caf';
        }else if($text=='加急订单'){
            $letter='jiajidingdan';
            $letters='cuidingla.caf';
        }else if($text=='催单了'){
            $letter='cuidingla';
            $letters='cuidingla.caf';
        }else if($text=='异常登录'){
            $letter='yichangdenglu';
            $letters='yichangdenglu.caf';
        }
        if(empty($text)){
            $text='新订单';
            $letter='xindingdan';
            $letters='xindingdan.caf';
        }
        //require_once("./JPush/JPush.php");
        $app_key = '99f536f5c2eb4c68819d26f9';
        $master_secret = '8e2af7f62f82add3701ff7ea';
        $client = new JPush($app_key, $master_secret);
        $result = $client->push()
            // IOS	安卓
            ->setPlatform(array('ios', 'android'))
            // 别名
            ->addAlias($admin_acc)
            ->addTag(array($admin_acc))
            /**
             * 安卓
             * 1)通知内容   2)通知标题   3)通知栏样式ID   4)扩展字段
             */
            ->addAndroidNotification($text, '速电快保', 1, array("key1"=>$letter,"key2"=>$num,'key3'=>'',"key4"=>date('Y-m-d H:i:s')))


            /**
             * IOS
             * 1)通知内容  2)通知提示声音   3)应用角标(数)   4)推送唤醒   5)   6)扩展字段
             */
            ->addIosNotification($text, $letters, $num, true, 'iOS category', array("key1"=>$letter,"key2"=>$num,'key3'=>'',"key4"=>date('Y-m-d H:i:s')))
            ->setMessage("msg content", 'msg title', 'type', array("key1"=>"value1", "key2"=>"value2", "key3"=>"value3", "key4"=>"value4"))
            // 1)推送序号   2)离线消息保留时长(秒)   3)要覆盖的消息ID   4)是否生产环境
            ->setOptions(100000, $time, null, true)
            // 请求
            ->send();

        /*->setPlatform('ios', 'android')
        ->addAlias('测试服务y')
        //->addTag(array('tag1', 'tag2'))
        ->setNotificationAlert('Hi, JPush')
        ->addAndroidNotification('Hi, android notification', 'notification title', 1, array("key1"=>"value1", "key2"=>"value2"))
        ->addIosNotification("Hi, iOS notification", 'iOS sound', '+1', true, 'iOS category', array("key1"=>"value1", "key2"=>"value2"))
        ->setMessage("msg content", 'msg title', 'type', array("key1"=>"value1", "key2"=>"value2"))
        ->setOptions(100000, 3600, null, true)
        ->send();*/

        //echo 'Result=' . json_encode($result) . $br;
    }
    public function res_push($sys_num,$admin_id,$text,$type,$feck_time){
        //$sys_num='123431231';
        //$admin_id='12881002';
        //$text='下线通知';
        //$type='3';
        if(!$feck_time){
            $feck_time=date("Y-m-d H:i:s");
        }
        $equipid="";
        if($type==1){
            //$admin=M("admin");
            //$order_list=M("order_list");
            // 登录名
            $admin_acc = DB::table('admin')->where('id',$admin_id)->value('admin_acc');
            // 订单数
            $num=DB::table('order_list')->where(['stauas'=>10,'ser_id'=>$admin_id])->count();
            $num=(int)($num);
            // 推送序号
            $serial_num=ltrim($sys_num,"L");
            $serial_num=(int)($serial_num);
            // 离线消息保留时长(秒)
            $time=(int)(86400);
        }else if($type==2){
            $admin_acc=$admin_id;
            $num=(int)(1);
            // 离线消息保留时长(秒)
            $time=(int)(86400);
        }else if($type==3){
            $equipid=$sys_num;
            $admin_acc=$admin_id;
            $num=(int)(1);
            // 离线消息保留时长(秒)
            $time=(int)(86400);
        }else{
            // 别名
            $admin_acc=$sys_num;
            // 订单数
            $num=(int)(0);
            // 推送序号
            $serial_num=(int)(1);
            // 离线消息保留时长(秒)
            $time=(int)(60);
        }

        if($text=='新订单'){
            $letter='xindingdan';
            $letters='xindingdan.caf';
        }else if($text=='夜间订单'){
            $letter='yejiandingdan';
            $letters='cuidingla.caf';
        }else if($text=='加急订单'){
            $letter='jiajidingdan';
            $letters='cuidingla.caf';
        }else if($text=='催单了'){
            $letter='cuidingla';
            $letters='cuidingla.caf';
        }else if($text=='异常登录'){
            $letter='yichangdenglu';
            $letters='yichangdenglu.caf';
        }else if($text=='下线通知'){
            $letter='UUID';
            $letters='UUID.caf';
        }
        if(empty($text)){
            $text='新订单';
            $letter='xindingdan';
            $letters='xindingdan.caf';
        }

        if($text=='下线通知'){
            //require_once("./JPush/JPush.php");
            $app_key = '99f536f5c2eb4c68819d26f9';
            $master_secret = '8e2af7f62f82add3701ff7ea';
            $client = new JPush($app_key, $master_secret);
            $result = $client->push()
                // IOS	安卓
                ->setPlatform(array('ios', 'android'))
                // 别名
                ->addAlias($admin_acc)
                /**
                 * 安卓
                 * 1)通知内容   2)通知标题   3)通知栏样式ID   4)扩展字段
                 */
                //->addAndroidNotification($text, '速电快保', 1, array("key1"=>$letter,"key2"=>$num,"key3"=>$equipid,"key4"=>date('Y-m-d H:i:s')))


                /**
                 * IOS
                 * 1)通知内容  2)通知提示声音   3)应用角标(数)   4)推送唤醒   5)   6)扩展字段
                 */
                ->addIosNotification($text, $letters, $num, true, 'iOS category', array("key1"=>$letter,"key2"=>$num,"key3"=>$equipid,"key4"=>$feck_time))
                ->setMessage("下线通知", '速电快保', '1', array("key1"=>$letter, "key2"=>$num, "key3"=>$equipid, "key4"=>$feck_time))
                // 1)推送序号   2)离线消息保留时长(秒)   3)要覆盖的消息ID   4)是否生产环境
                ->setOptions(100000, $time, null, true)
                // 请求
                ->send();
        }else{
            //require_once("./JPush/JPush.php");
            $app_key = '99f536f5c2eb4c68819d26f9';
            $master_secret = '8e2af7f62f82add3701ff7ea';

            $client = new JPush($app_key, $master_secret);

            $result = $client->push()
                // IOS	安卓
                ->setPlatform(array('ios', 'android'))
                // 别名
                ->addAlias($admin_acc)
                ->addTag(array($admin_acc))
                /**
                 * 安卓
                 * 1)通知内容   2)通知标题   3)通知栏样式ID   4)扩展字段
                 */
                ->addAndroidNotification($text, '速电快保', 1, array("key1"=>$letter,"key2"=>$num,"key3"=>$equipid,"key4"=>$feck_time))


                /**
                 * IOS
                 * 1)通知内容  2)通知提示声音   3)应用角标(数)   4)推送唤醒   5)   6)扩展字段
                 */
                ->addIosNotification($text, $letters, $num, true, 'iOS category', array("key1"=>$letter,"key2"=>$num,"key3"=>$equipid,"key4"=>$feck_time))
                //->setMessage("msg content", 'msg title', 'type', array("key1"=>"value1", "key2"=>"value2", "key3"=>"value3", "key4"=>"value4"))
                // 1)推送序号   2)离线消息保留时长(秒)   3)要覆盖的消息ID   4)是否生产环境
                ->setOptions(100000, $time, null, true)
                // 请求
                ->send();
        }

        /*->setPlatform('ios', 'android')
        ->addAlias('测试服务y')
        //->addTag(array('tag1', 'tag2'))
        ->setNotificationAlert('Hi, JPush')
        ->addAndroidNotification('Hi, android notification', 'notification title', 1, array("key1"=>"value1", "key2"=>"value2"))
        ->addIosNotification("Hi, iOS notification", 'iOS sound', '+1', true, 'iOS category', array("key1"=>"value1", "key2"=>"value2"))
        ->setMessage("msg content", 'msg title', 'type', array("key1"=>"value1", "key2"=>"value2"))
        ->setOptions(100000, 3600, null, true)
        ->send();*/

        //echo 'Result=' . json_encode($result) . $br;
        return $result;
    }
}
