<?php

namespace app\api\controller;

use app\admin\logic\operation\OpurchaseclassLogic;
use app\admin\validate\tools\GenerateTableValidate;
use app\admin\logic\tools\GeneratorLogic;
use app\common\model\goods\Goods;
use app\common\model\goods\Goodsclass;
use app\common\service\pay\PayService;
use app\common\service\PushService;
use app\common\service\wechat\WeChatMnpService;
use app\common\service\wechat\WeChatOaService;
use DateTime;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use stdClass;
use think\facade\Db;
use Webman\Config;
use Webman\RedisQueue\Redis;
use Yansongda\Pay\Pay;
use Webman\Push\Api;
use JPush\Client as JPush;
use support\Log;

class IndexController extends BaseApiController
{
    public $notNeedLogin = ['index','app_update'];

    public function index()
    {
        Redis::send('push-platform-print', ['order_id' => 39],5);

        // $auth_code=$this->request->get('auth_code');
        // $config = Config::get('payment');
        // Pay::config($config);

        // $result = Pay::alipay()->pos([
        //     'out_trade_no' => time(),
        //     'auth_code' => $auth_code,
        //     'total_amount' => '0.01',
        //     'subject' => 'yansongda 测试 - 01',
        //     'extend_params'=>['attach'=>'cashierclass']
        // ]);
       d(1);
        // $arr = [];
        // foreach ($a as $k => $v) {
        //     $pid = Goodsclass::where('id', $v)->value('pid');
        //     $arr[$pid][] = $v;
        // }
        // foreach ($arr as $k => $v) {
        //     Goodsclass::where('id', $k)->update(['children' => json_encode($v, true)]);
        // }
        d(1);
        // $a=array_values($a);
        // $client = new JPush($app_key, $master_secret);

        // $client->push()
        // ->setPlatform('all')
        // ->addAllAudience()
        // ->setNotificationAlert('Hello, JPush')
        // ->send();


        // var_dump(2323);
        return json(['msg' =>create_password(123456, '11d3')]);
//         PushService::push('store_merchant_502', 502, '支付超时,订单已被取消,请重新提交订单');
// d(1);
    //    $extra=$this->request->post();
    //    Redis::send('push-supplier-products', ['order_id'=>11]);
    // createSupplierGoods
    //    d(OpurchaseclassLogic::createSupplierGoods(['goods'=>99]));
        $queue = 'send-mail';
        // 数据,可以直接传数组,无需序列化
        $data = ['to' => 'tom@gmail.com', 'content' => 'hello'];
        // 投递消息
        Redis::send($queue, $data);
        // 投递延迟消息,消息会在60秒后处理
        // Redis::send($queue, $data, 5);

        return json(['msg' =>create_password(123456, '11d3')]);
    }

    /**
     * @notes 下载文件
     */
    public function download()
    {
        $params = (new GenerateTableValidate())->goCheck('download');
        $result = GeneratorLogic::download($params['file']);
        if (false === $result) {
            return $this->fail(GeneratorLogic::getError() ?: '下载失败');
        }
        return response()->download($result, 'webman-curd.zip');
    }

    /**
     * @notes 获取app更新信息
     */
    public function app_update(){
       $find= Db::name('app_update')->where('type',2)->order('id','desc')->findOrEmpty();
       return $this->success('ok',$find);
    }
}