<?php

namespace app\controller;

use app\BaseController;
use Overtrue\Pinyin\Pinyin;
use think\facade\Config;
use think\facade\Db;
use think\facade\Log;

class Index extends BaseController
{


    //之前的充值 消费 提现统计
    public function totalPrice($openid, $area)
    {
        $pinyin = new Pinyin();

        $s = $pinyin->sentence($area);
        $s = strtr($s, array(' ' => ''));

        $account = Db::table('user_' . $s)->where('openid', $openid)->value('account');

        if ($account > 0) {

            $chongzhi = Db::table('recharge_' . $s)->where('openid', $openid)->sum('total');
            $chongzhi_usd = Db::table('recharge_' . $s)->where('openid', $openid)->sum('total_used');

            $refund = Db::table('refund_' . $s)->where('openid', $openid)->sum('refund_total');


            $order = Db::table('charge')->where('openid', $openid)->where('type', 1)->sum('TotalMoney');
            $order_1 = Db::table('charge')->where('openid', $openid)->where('type', 1)->sum('WithholdingMoney');
            $order_2 = Db::table('charge')->where('openid', $openid)->where('type', 1)->sum('FeedbackMoney');
            $order2 = Db::table('charge_order_' . $s)->where('openid', $openid)->sum('TotalMoney');
            echo '用户账户余额：' . $account;
            echo '<br>';
            echo '用户充值金额：' . number_format($chongzhi, 2);
            echo '<br>';
            echo '用户充值中提现金额：' . number_format($chongzhi_usd, 2);
            echo '<br>';
            echo '用户提现金额：' . number_format($refund, 2);
            echo '<br>';
            echo '用户老订单使用金额' . number_format($order2, 2);
            echo '<br>';
            echo '用户新订单充值金额' . number_format($order_1, 2);
            echo '<br>';
            echo '用户新订单退款金额' . number_format($order_2, 2);
            echo '<br>';
            echo '用户新订单消费金额' . number_format($order, 2);
            echo '<br>';
            echo '<br>';
            echo '<br>';
        }


    }

    public function index()
    {


        //充值记录
//        // $new_user = Db::table('user')->select();
//        $data = [];
//        // foreach ($new_user as $key => $value) {
//        //     $openid = $value['openid'];
//        //     $pinyin = new Pinyin();
//
//        //     $s = $pinyin->sentence($value['area']);
//        //     $s = strtr($s, array(' ' => ''));
//        $list = Db::table('charge')->select();
//        foreach ($list as $key => $value) {
//            unset($value['order_id']);
//            $data[] = $value;
//        }
//        // }
//        // $id = Db::table('zxc_charge_order')->insertAll($data);
//        // halt($id);
//        halt($data);
//        exit();
        // // 查询用户余额
        // $new_user = Db::table('user')->select();

        // $data = [];
        // $i = 0;
        // foreach ($new_user as $key => $value) {

        //     // $this->totalPrice($value['openid'], $value['area']);
        //     $openid = $value['openid'];
        //     $pinyin = new Pinyin();

        // $s = $pinyin->sentence($value['area']);
        // $s = strtr($s, array(' ' => ''));

        // $account = Db::table('user_' . $s)->where('openid', $openid)->value('account');

        // if ($account > 0) {
        //   $i++;
        //     $chongzhi = Db::table('recharge_' . $s)->where('openid', $openid)->sum('total');
        //     $chongzhi_usd = Db::table('recharge_' . $s)->where('openid', $openid)->sum('total_used');

        //     $refund = Db::table('refund_' . $s)->where('openid', $openid)->sum('refund_total');


        //     $order = Db::table('charge')->where('openid', $openid)->where('type', 1)->sum('TotalMoney');
        //     $order_1 = Db::table('charge')->where('openid', $openid)->where('type', 1)->sum('WithholdingMoney');
        //     $order_2 = Db::table('charge')->where('openid', $openid)->where('type', 1)->sum('FeedbackMoney');
        //     $order2 = Db::table('charge_order_' . $s)->where('openid', $openid)->sum('TotalMoney');
        //     $order2_1 = Db::table('charge_order_' . $s)->where('openid', $openid)->sum('WithholdingMoney');
        //     $order2_2 = Db::table('charge_order_' . $s)->where('openid', $openid)->sum('FeedbackMoney');
        //     echo '用户信息：'.$openid.' ' . $account;
        //     echo '<br>';
        //     echo '用户充值金额：' . number_format($chongzhi/100, 2);
        //     echo '<br>';
        //     echo '用户充值中提现金额：' . number_format($chongzhi_usd/100, 2);
        //     echo '<br>';
        //     echo '用户提现金额：' . number_format($refund/100, 2);
        //     echo '<br>';
        //     echo '用户老订单使用金额' . number_format($order2, 2);
        //     echo '<br>';
        //     echo '用户老订单充值金额' . number_format($order2_1, 2);
        //     echo '<br>';
        //     echo '用户老订单退款金额' . number_format($order2_2, 2);
        //     echo '<br>';
        //     echo '用户新订单充值金额' . number_format($order_1, 2);
        //     echo '<br>';
        //     echo '用户新订单退款金额' . number_format($order_2, 2);
        //     echo '<br>';
        //     echo '用户新订单消费金额' . number_format($order, 2);
        //     echo '<br>';
        //     echo '<br>';
        //     echo '<br>';
        // }
        // }
        // echo '共'.$i.'个用户';
        // halt($data);


//        退款
        // $charge_info = Db::table('zxc_charge_order')->where('StartChargeSeq', 'MACFHBM3X176243163424257415')->find();

        // if ($charge_info['type'] == 1 && $charge_info['directly_refund_status'] == 0 && $charge_info['is_wind'] == 0) {
        //     $td = new WechatReimburse();
        //     $res = $td->Refund2($charge_info['order_id'], $charge_info['openid'], $charge_info['FeedbackMoney'], $charge_info['WithholdingMoney'], $charge_info['directly_pay_no']);
        //     var_dump($res);
        //     Db::table('charge_logo')->save(['name' => '充电结束即充即退执行退款', 'mark' => json_encode($charge_info)]);
        // }else{
            // echo '已退款';
        // }

        // exit();


        //生成二维码
        // $pile = Db::table('charge_pile')->where('charge_station_id','>',20)->select();
        // foreach ($pile as $key=>$value){
        //      $this->getwxacode($value['ConnectorID']);
        //      echo $value['ConnectorID'].'<br>';
        // }
        // halt([]);
        // $res = $this->getwxacode('9920000040835A');
        // halt($res);


        // 检查费用
//        $charge_order_info = Db::table('zxc_charge_order')->where('StartChargeSeq', 'MACFHBM3X176069545021698018')->find();
//        halt($charge_order_info);
//        $per_price = (new HardMessage())->Rank($charge_order_info['charge_station_id'], '2025-10-17 18:05:48', '2025-10-17 18:06:26', $charge_order_info['openid'])['Elect'];
//        halt($per_price);


        // // 测试加金额
        // $StartChargeSeq = 'MACFHBM3X176075347725868066';
        // $charge_info = Db::table('charge')->where('StartChargeSeq', $StartChargeSeq)
        //         ->field('WithholdingMoney,charge_station_id,openid,FeedbackMoney,type,directly_refund_status')->find();
        // $check = $charge_info['FeedbackMoney'];
        // $ff = 0.01;
        // $area = \app\model\ChargeOrder::where('StartChargeSeq', $StartChargeSeq)->value('area');
        //         $pinyin = new Pinyin();
        //         $s = $pinyin->sentence($area);
        //         $s = strtr($s, array(' ' => ''));
        //         $table = 'charge_order_' . $s;
        //          $table = 'charge';
        //         $table_user = 'user_' . $s;
        // if (empty($check)) {
        //     Db::table($table_user)->where('openid', $charge_info['openid'])->update(['account' => Db::raw('account+' . $ff)]);
        //      Db::table('charge_logo')->save(['name'=>'充电结束执行用余额增加退款','mark'=>$ff]);
        //     // Db::table('charge')->where('StartChargeSeq', $StartChargeSeq)->save($ss);
        // }
        //       exit();


        // 计算费用
        // $price = (new HardMessage)->GetPrice(19, 'obUtOvjCdTSrZTqbjTByESPo6kLE', 'user_qianlongzhiyaozhan');
        //             $total = $array['TotalPower'] * $price['EPrice'] + ($array['TotalPower'] * $price['SPrice']);
        //             $ElectMoney = $data['TotalPower'] * $price['EPrice'];
        //             $SeviceMoney = $data['TotalPower'] * $price['SPrice'];

        //             halt($price);


        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">16载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    public function hello($name = 'ThinkPHP6')
    {

        return 'hello,' . $name;
    }

    public function config()
    {
        echo Config::has('database.connections.mysql.hostname');
    }

    function postRequest($url, $data, $headers = [])
    {
        $ch = curl_init();

        // 如果是数组，自动转换为 JSON
        if (is_array($data)) {
            $data = json_encode($data, JSON_UNESCAPED_UNICODE);
            $headers[] = 'Content-Type: application/json';
        }

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        // 跳过 HTTPS 证书验证（仅测试环境使用，生产环境需配置证书）
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            throw new Exception('cURL 请求失败: ' . curl_error($ch));
        }
        curl_close($ch);

        return $response;
    }

    function base64_save($base64_img, $path)
    {
        $img_base = str_replace('data:image/jpeg;base64,', '', $base64_img);
        $img_name = time() . rand(1000, 9999) . '.jpg'; //图片新名称
        $img_path = $path . $img_name;
        file_put_contents($img_path, base64_decode($img_base));

        return $img_name; //返回新名称
    }

    public function getwxacode($ConnectorID)
    {

        $url_get = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . config("wx.AppID") . '&secret=' . config("wx.AppSecret");
        $tmptoken = file_get_contents($url_get);
        $tmptoken = json_decode($tmptoken, true);
        $token = $tmptoken['access_token'];
        $url = 'https://api.weixin.qq.com/wxa/getwxacode?access_token=' . $token;
        $data = [
            'path' => '/pages/charging/charging?ConnectorID=' . $ConnectorID,
            'env_version' => 'release',// 试用版（develop/trial/release）
        ];
        $res = $this->postRequest($url, $data);
        $base64_image = "data:image/jpeg;base64," . base64_encode($res);

        $res = (new Event())->base64_image_content($base64_image, '../public/qrcode/', $ConnectorID);
        return $res;

    }


}
