<?php
namespace app\h5\controller;

use app\hander\HelpHander;
use EasyWeChat\Factory;
use think\Controller;
use think\Db;

class Index extends Controller
{
    public function sysarticle($name){
        $info = Db::name('sys_article')->where('name',$name)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function newDetail($id){
        $info = Db::name('news')->where('id',$id)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function errorPage(){
        return  $this->fetch('index/error');
    }

    public function article(){
        $token = input('token');
        $id = input('id');
        $info = Db::name('article')
            ->where('id', $id)
            ->field('id,path,view,video,audio,content,type')
            ->find();
        if(!$info){
            return  $this->fetch('error');
        }else{
            if($info['type'] == 1){
                $this->assign('path',$info);
                $this->assign('url',$info['path']);
                $this->assign('token',$token);
                return $this->fetch('article');
            }else{
                $this->assign('path',$info);
                $this->assign('token',$token);
                $this->assign('info',$info);
                return $this->fetch('article2');
            }
        }
    }

   // 下载页
    public function download(){
        $ios = "https://apps.apple.com/cn/app/id6504796311";
//        $ios = '#';
        $android = '';
        $app = Db::name('app_mgr')
            ->where('del',0)
            ->where('enable',1)
            ->order('ver desc, id desc')
            ->find();
        if($app){
            $android = $app['path'];
        }
        if(!$android){
            $android = '#';
        }
        if(!$ios){
            $ios = '#';
        }

        $iswx = 0;
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
            $iswx = 1;
        }

        $this->assign('iswx',$iswx);
        $this->assign('ios',$ios);
        $this->assign('android',$android);

        return $this->fetch();
    }


    //温控统计
    public function TemperatureTj(){
        $type = input('TjType/d',0);
        $id = input('id','','trim');
        if(empty($id)){
            exit('参数错误');
        }
        switch ($type){
            case 0:
                $html = $this->tj1($id);
                break;
            case 2:
                $html = $this->tj2($id);
                break;
            case 3:
                $html = $this->tj3($id);
                break;
        }
        return $html;

    }

    public function check($id){
        $res = Db::name('temperature_device')
            ->where('id',$id)
            ->where('del',0)
            ->where('enable',1)
            ->find();
        if(!$res) exit('设备不存在');
        return $res;
    }
    /**
     * 24小时温度图
     *
     * @author wst
     * @date   2021/6/9 10:59
     */
    public function tj1() {
        $id = input('id','','trim');
        if(empty($id)){
            exit('参数错误');
        }

        $res = $this->check($id);
        $time = time();
        $timeData = [];
        for ($i = 0; $i <= 23; $i++) {
            if (strlen($i) <= 1) {
                $i = '0' . $i;
            }
            $timeData[] = date('Ymd', $time) . $i;
        }
        $data = [];
        foreach ($timeData as $k => $v) {
            $info = Db::name('temperature_device_data')
                ->where('snaddr', $res['snaddr'])
                ->where('create_ymdh', $v)
                ->where('abnormal', 0)
                ->avg('temp');
            $a = [
                'name' => substr($v, -2) . ':00',
                'value' => round($info,2)
            ];
            $data[] = $a;
        }

        $this->assign('key', array_column($data, 'name'));
        $this->assign('value', array_column($data, 'value'));
        $i = array_chunk($data,6);
        $this->assign('data', $i);
        return $this->fetch();
    }
    /**
     * 最近七日温度显示
     *
     * @author wst
     * @date   2021/6/9 14:16
     */

    public function tj2() {
        $id = input('id','','trim');
        if(empty($id)){
            exit('参数错误');
        }

        $res = $this->check($id);
        $time = time();
        $timeData = [];
        for ($i = 6; $i >= 0; $i--) {
            $timeData[] =date('Ymd',$time-($i*86400));
        }
        $data = [];
        foreach ($timeData as $k => $v) {
            $info = Db::name('temperature_device_data')
                ->where('snaddr', $res['snaddr'])
                ->where('create_ymd', $v)
                ->where('abnormal', 0)
                ->avg('temp');
            $a = [
                'name' => substr($v, 0,4).'-'.substr($v, 4,2).'-'.substr($v, -2),
                'value' => round($info,2)

            ];
            $data[] = $a;
        }

        $this->assign('key', array_column($data, 'name'));
        $this->assign('value', array_column($data, 'value'));
        $i = array_chunk($data,4);

        $this->assign('data', $i);
        return $this->fetch();
    }
    /**
     * 最近30日温度显示
     *
     * @author wst
     * @date   2021/6/9 14:16
     */

    public function tj3() {
        $id = input('id','','trim');
        if(empty($id)){
            exit('参数错误');
        }

        $res = $this->check($id);
        $time = time();
        $timeData = [];
        for ($i = 29; $i >= 0; $i--) {
            $timeData[] =date('Ymd',$time-($i*86400));
        }
        $data = [];
        foreach ($timeData as $k => $v) {
            $info = Db::name('temperature_device_data')
                ->where('snaddr', $res['snaddr'])
                ->where('create_ymd', $v)
                ->where('abnormal', 0)
                ->avg('temp');
            $a = [
                'name' => substr($v, 0,4).'-'.substr($v, 4,2).'-'.substr($v, -2),
                'value' => round($info,2)

            ];
            $data[] = $a;
        }
        $this->assign('key', array_column($data, 'name'));
        $this->assign('value', array_column($data, 'value'));
        $i = array_chunk($data,5);
        $this->assign('data', $i);
        return $this->fetch();
    }

    // 运送收费页
    public function pay(){
        $id = input('id/d',0);
//        $id = 1;
        $info = Db::name('order_convey_pay')->where('id',$id)->where('status',0)->find();
        $error = '';
        if($info){
            $config = config('app.wx_config');
            $notify = config("app.app_host").'/h5/notify/pay';
            $info['url'] = '';
            try{
                $app = Factory::payment($config);
                $result = $app->order->unify([
                    'body' => '运送收费',
                    'out_trade_no' => $info['sn'],
                    'total_fee' => $info['money']*100,
                    'notify_url' => $notify, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
                    'trade_type' => 'NATIVE', // 请对应换成你的支付方式对应的值类型
                    'product_id' => $info['order_id'],
                ]);
                if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
                    $info['url'] = think_encrypt($result['code_url']);
                }
            }catch (\Exception $e){
                trace($e->getMessage());
            }
        }else{
            $error = "记录不存在或已支付";
        }
        if(!$info['url']){
            $error = "支付二维码生成失败,请重试";
        }
        $this->assign('info',$info);
        $this->assign('error',$error);
        return $this->fetch();
    }

    public function checkPay(){
        $id = input('id/d',0);
        $info = Db::name('order_convey_pay')->where('id',$id)->where('status',1)->find();
        if($info){
            $this->success('已支付');
        }else{
            $this->error('未支付');
        }
    }
    public function errortxt(){

        return  $this->fetch('repair/errortxt');
    }
}