Dashboard.php 8.4 KB
<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;
use think\Db;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++) {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        /*Config 配置*/
        //当日起止时间戳
        $TodatStartTime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $TodatEndTime = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        /*新增逻辑*/
        //当天登录人数总和 $todayuserlogin
        $Dashboard = Db::name('dashboard')->find();
        if (!empty($Dashboard)) {
            if ($Dashboard['start'] != 0 && $Dashboard['end'] != 0) {
                $map['updatetime'] = ['between', [$Dashboard['start'], $Dashboard['end']]];
                $map2['createtime'] = ['between', [$Dashboard['start'], $Dashboard['end']]];
            } else {
                $map2 = [];
                $map['updatetime'] = ['between', [$TodatStartTime, $TodatEndTime]];
            }
            if ($Dashboard['qudao_id'] != 1) {
                $map3['qudao_id'] = ['EQ', $Dashboard['qudao_id']];
            } else {
                $map3 = [];
            }
        } else {
            $map2 = [];
            $map['updatetime'] = ['between', [$TodatStartTime, $TodatEndTime]];
            $map3 = [];
        }
        Db::name('dashboard')->where('id', $Dashboard['id'])->delete();
        $todayuserlogin = Db::name('user')->where($map)->select();
        //当天成功支付人数总和
        $todayorderUser = Db::name('pay_order')->where($map)->where('status', 1)->select();
        $todayorder = $this->second_array_unique_bykey($todayorderUser, 'user_id');
        //支付转化率 当天支付人数总和/当天登录人数总和
        if (count($todayuserlogin) == 0 || count($todayorder) == 0) {
            $sevendau = 0;
        } else {
            $sevendau = count($todayorder) / count($todayuserlogin) * 100;
        }
        //今日支付金额
        if (empty($todayorder)) {
            $unsettleorder = 0;
        } else {
            foreach ($todayorder as $k => $v) {
                $TodayMoney[] = $v['money'];
            }
            $unsettleorder = array_sum($TodayMoney);
        }
        //当天成功支付订单数
        $todayusersignup = Db::name('pay_order')->where($map)->where('status', 1)->select();
        //人均订单数
        if (count($todayusersignup) == 0 || count($todayorder) == 0) {
            $sevendnu = 0;
        } else {
            $sevendnu = count($todayusersignup) / count($todayorder);
        }
        //新增新客数
        $NewUserOrder = Db::name('pay_order')->where($map)->where('status', 1)->where('is_new', 1)->select();
        //客单价
        if ($unsettleorder == 0 || count($todayorder) == 0) {
            $UserOrderPrice = 0;
        } else {
            $UserOrderPrice = $unsettleorder / count($todayorder);
        }
        //用户级别统计
        //初始化参数
        $First = 0;
        $Second = 0;
        $Third = 0;
        $Fourth = 0;
        $Fifth = 0;
        $Sixth = 0;
        $Seventh = 0;
        $Eighth = 0;
        $UserCountArray = Db::name('user')->where($map2)->where($map3)->select();
        if (!empty($UserCountArray)) {
            foreach ($UserCountArray as $k => $v) {
                if ($v['level'] == 1) {
                    $First++;
                } elseif ($v['level'] == 2) {
                    $Second++;
                } elseif ($v['level'] == 3) {
                    $Third++;
                } elseif ($v['level'] == 4) {
                    $Fourth++;
                } elseif ($v['level'] == 5) {
                    $Fifth++;
                } elseif ($v['level'] == 6) {
                    $Sixth++;
                } elseif ($v['level'] == 7) {
                    $Seventh++;
                } elseif ($v['level'] == 8) {
                    $Eighth++;
                }
            }
        }
        //授权登录人数
        $CountMiniAppLoginHistory = Db::name('user')->where($map2)->select();
        //当前做过测试人数
        $SleepHistoryArray = Db::name('sleep')->where($map2)->select();
        $SleepHistory = $this->second_array_unique_bykey($SleepHistoryArray, 'user_id');
        //当前获取报告人数
        $SleepTypeFirstArray = Db::name('sleep')->where($map2)->where('type', 1)->select();
        $SleepTypeFirst = $this->second_array_unique_bykey($SleepHistoryArray, 'user_id');
        //当前下过单的人数
        $UpOrderNumArray = Db::name('pay_order')->where($map2)->where('status', 1)->select();
        $UpOrderNum = $this->second_array_unique_bykey($UpOrderNumArray, 'user_id');
        //渠道下拉菜单
        $Qudao = Db::name('qudao_title')->select();
        $this->view->assign([
            /*用户级别统计*/
            'UserFirst' => $First,
            'UserSecond' => $Second,
            'UserThird' => $Third,
            'UserFourth' => $Fourth,
            'UserFifth' => $Fifth,
            'UserSixth' => $Sixth,
            'UserSeventh' => $Seventh,
            'UserEighth' => $Eighth,
            //当前授权登录人数
            'CountMiniAppLoginHistory' => count($CountMiniAppLoginHistory),
            //当前做过测试人数
            'SleepHistory' => count($SleepHistory),
            //当前获取报告人数
            'SleepTypeFirst' => count($SleepTypeFirst),
            //当前下过单的人数
            'UpOrderNum' => count($UpOrderNum),
            //当天登录人数总和
            'todayuserlogin' => count($todayuserlogin),
            //当天成功支付订单数
            'todayusersignup' => count($todayusersignup),
            //当天成功支付人数总和
            'todayorder' => count($todayorder),
            //今日支付金额
            'unsettleorder' => $unsettleorder,
            //人均订单数
            'sevendnu' => $sevendnu,
            //支付转化率
            'sevendau' => $sevendau . '%',
            //新增新客数
            'NewUserOrder' => count($NewUserOrder),
            //客单价
            'UserOrderPrice' => $UserOrderPrice,
            'paylist' => $paylist,
            'createlist' => $createlist,
            'addonversion' => $addonVersion,
            'uploadmode' => $uploadmode,
            //渠道下拉菜单
            'qudao' => $Qudao
        ]);

        return $this->view->fetch();
    }

    // 数组去重
    function second_array_unique_bykey($arr, $key)
    {
        $tmp_arr = array();
        foreach ($arr as $k => $v) {
            if (in_array($v[$key], $tmp_arr))   //搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
            {
                unset($arr[$k]); //销毁一个变量  如果$tmp_arr中已存在相同的值就删除该值
            } else {
                $tmp_arr[$k] = $v[$key];  //将不同的值放在该数组中保存
            }
        }
        //ksort($arr); //ksort函数对数组进行排序(保留原键值key)  sort为不保留key值
        return $arr;
    }


    public function ajaxBtn()
    {
        if ($this->request->isPost()) {
            $params = $this->request->param();
            $Start = strtotime($params['Start']);
            $End = strtotime($params['End']);
            $data = [
                'start' => $Start,
                'end' => $End,
                'qudao_id' => $params['Qudao']
            ];
            $res = Db::name('dashboard')->insert($data);
            if ($res) {
                $this->success();
            } else {
                $this->error('查询失败');
            }
        }
    }
}