<?php

namespace app\api\controller;

use app\admin\logic\store_product\StoreProductLogic;
use app\admin\validate\tools\GenerateTableValidate;
use app\admin\logic\tools\GeneratorLogic;
use app\common\logic\store_order\StoreOrderLogic;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\service\pay\PayService;
use app\common\service\wechat\WechatTemplate;
use Exception;
use support\Cache;
use think\facade\Db;
use Webman\Config;
use support\Log;
use Yansongda\Pay\Exception\InvalidSignException;
use  Monolog\Handler\RotatingFileHandler;
use Webman\RedisQueue\Redis;



class IndexController extends BaseApiController
{
    public $notNeedLogin = ['index', 'app_update', 'express_list', 'province', 'city', 'area', 'street','village','brigade','config'];

    public function index()
    {
        $arr = StoreBranchProduct::select();
        foreach ($arr as $item) {
            StoreProductLogic::updateGoodsclass($item['cate_id'], $item['store_id']);
        }
        d(1);
        try {
            $wechat = new PayService(1);
            $time = time();
            $order = [
                'out_trade_no' => 'PF1717558027664507',
                'out_refund_no' => 'BO' . $time,
                'amount' => [
                    'refund' => 1,
                    'total' => 1,
                    'currency' => 'CNY',
                ],
                // '_action' => 'jsapi', // jsapi 退款,默认
                // '_action' => 'app', // app 退款
                // '_action' => 'combine', // 合单退款
                // '_action' => 'h5', // h5 退款
                // '_action' => 'miniapp', // 小程序退款
                // '_action' => 'native', // native 退款

            ];

            $res =  $wechat->wechat->refund($order);
            Cache::set('kk', json_decode($res, true));
        } catch (Exception $e) {
            \support\Log::info($e->extra['message'] ?? $e->getMessage());
            throw new \Exception($e->extra['message'] ?? $e->getMessage());
        }
        d(1);








        try {
            $a = new WechatTemplate();
            $a->NewQuotationNotification(['openid' => 'ocqhF6UfFQXE-SbzbP5YVQJlQAh0', 'data1' => '阿萨', 'data2' => date('Y-m-d H:i:s'), 'data3' => '占山', 'data3' => 18982406440]);
        } catch (Exception $e) {
            d($e);
        }


        d(22);
        $auth_code = $this->request->get('code');
        $pay = (new PayService());
        $order = [
            'description' => '条码商品',
            'out_trade_no' => (string)time(),
            'payer' => [
                'auth_code' => (string)$auth_code
            ],
            'amount' => [
                'total' => 1,
            ],
            'scene_info' => [
                "store_info" => [
                    'id' => '1'
                ]
            ],
        ];

        try {
            $a = $pay->wechat->pos($order);
        } catch (\Exception $th) {
            d($th);
        }
        d($a);
        $params = ['store_id' => 2, 'pay_type' => 17];
        $a = StoreOrderLogic::createOrder([1], 0, null, $params);
        d($a);
        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);
    }


    /**
     * @notes 获取省列表
     */
    public function province()
    {
        $list = Db::name('geo_province')->select()->toArray();
        return $this->success('ok', $list);
    }
    /**
     * @notes 获取市列表
     */
    public function city()
    {
        $province_code = $this->request->get('province_code');
        $list = Db::name('geo_city')->where('province_code', $province_code)->select()?->toArray();
        return $this->success('ok', $list);
    }
    /**
     * @notes 获取区列表
     */
    public function area()
    {
        $city_code = $this->request->get('city_code');
        $list = Db::name('geo_area')->where('city_code', $city_code)->select()?->toArray();
        return $this->success('ok', $list);
    }
    /**
     * @notes 获取街道列表
     */
    public function street()
    {
        $area_code = $this->request->get('area_code');
        $list = Db::name('geo_street')->where('area_code', $area_code)->select()?->toArray();
        return $this->success('ok', $list);
    }

    /**
     * @notes 获取村列表
     */
    public function village()
    {
        $area_code = $this->request->get('street_code');
        $list = Db::name('geo_village')->where('street_code', $area_code)->select()?->toArray();
        return $this->success('ok', $list);
    }
        /**
     * @notes 获取队列表
     */
    public function brigade()
    {
        $list = Db::name('geo_brigade')->select()?->toArray();
        return $this->success('ok', $list);
    }

            /**
     * @notes 获取队列表
     */
    public function config()
    {
        $list=[
            'id'=>5,
            'store_name'=>'测试',
        ];
        return $this->success('ok', $list);
    }
}