<?php

namespace App\Task;

use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Logger\LoggerFactory;
use App\Model\DevicesModel;
use App\Service\QueueService;
use App\Service\ALiSmsService;
use Hyperf\Utils\ApplicationContext;
class FooTask
{
    use \Hyperf\Di\Aop\ProxyTrait;
    use \Hyperf\Di\Aop\PropertyHandlerTrait;
    /**
     * @Inject
     * @var QueueService
     */
    protected $service;
    private $logger;
    private $DevicesModel;
    private $redisClient;
    /**
     * @Inject
     * @var ALiSmsService
     */
    protected $smsphone;
    public function __construct(LoggerFactory $loggerFactory, DevicesModel $devicesModel)
    {
        $this->__handlePropertyHandler(__CLASS__);
        $this->logger = $loggerFactory->get('log', 'default');
        $container = ApplicationContext::getContainer();
        $this->redisClient = $container->get(\Redis::class);
        $this->DevicesModel = $devicesModel;
    }
    public function execute()
    {
        $data = $this->DevicesModel->devicephone();
        if ($data != '') {
            $this->jsondata($data);
        }
    }
    //解出 json 字符串
    public function jsondata($info)
    {
        $data = [];
        $infos = json_decode($info, true);
        foreach ($infos as $key => $value) {
            if (substr($value['phone'], 0, 1) == '[') {
                $newphone = json_decode($value['phone'], true);
                foreach ($newphone as $k => $v) {
                    $infodata = ['phone' => $v['cphone'], 'devicenum' => substr($value['devicenum'], -3), 'typedevicenum' => $value['devicenum']];
                    array_push($data, $infodata);
                }
            } else {
                $infodatas = ['phone' => $value['phone'], 'devicenum' => substr($value['devicenum'], -3), 'typedevicenum' => $value['devicenum']];
                array_push($data, $infodatas);
            }
        }
        return $this->toarray($data);
    }
    //
    /*二维数组去重*/
    public function toarray($data)
    {
        $public_info = [];
        for ($i = 0; $i < count($data); $i++) {
            $a = $data[$i];
            unset($data[$i]);
            if (!in_array($a, $data)) {
                $public_info[] = $a;
            }
        }
        return $this->example($public_info);
    }
    //
    /*数组总量进行判断*/
    public function example($data)
    {
        if (count($data) > 1) {
            foreach ($data as $key => $value) {
                $newdata = ['phone' => $value['phone'], 'devicenum' => $value['devicenum'], 'typedevicenum' => $value['typedevicenum']];
                $this->service->push($newdata);
            }
        } else {
            if (!$this->redisClient->get($data['typedevicenum'])) {
                $smsdata = $data[0]['phone'];
                $template_param = ['cphone' => $data[0]['phone'], 'position' => '您的设备尾号为' . $data[0]['devicenum'], 'alarm' => '离线'];
                $typeinfo = $this->smsphone($template_param, $smsdata);
                if ($typeinfo['Message'] == 'OK' || $typeinfo['Code'] == 'OK') {
                    $redisdata = ['data' => $data, 'sendSms' => json_decode($typeinfo, true), 'time' => time()];
                    $this->redisClient->set($data[0]['typedevicenum'], json_encode($redisdata));
                }
            }
        }
    }
    public function smsphone($template_param, $phone)
    {
        $params = array('SignName' => '泽宏云', 'AccessKeyId' => 'LTAI2xiZNF3iV2aV', 'TemplateCode' => 'SMS_169897307', 'PhoneNumbers' => $phone, 'TemplateParam' => json_encode($template_param, JSON_UNESCAPED_UNICODE), 'RegionId' => 'cn-beijing');
        return $this->smsphone->sendSms('bprEWwn1M0xgglRQCQEMYSPiYctDk4', $params);
    }
}