Dashboard.php 8.2 KB
<?php

namespace app\admin\controller;

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

/**
 * 控制台
 *
 * @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');
        //月 起止时间戳
        $start_time = mktime(0, 0, 0, date('m'), 1, date('Y'));
        $end_time = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        $map['createtime'] = ['between', [$start_time, $end_time]];
        //报修总量
        $BaoxiuArr = Db::name('baoxiudan')->where($map)->select();
        $BaoxiuWancheng = Db::name('baoxiudan')->where('type', 2)->where($map)->select();
        $BaoxiuWeiWancheng = Db::name('baoxiudan')->where('type', 1)->where($map)->select();
        $Round = round(count($BaoxiuWancheng) / count($BaoxiuArr) * 100, 2);
        $WRound = round(count($BaoxiuWeiWancheng) / count($BaoxiuArr) * 100, 2);
        //监控系统
        $jiankong = Db::name('baoxiudan')->where('system', 1)->where($map)->select();
        //通信系统
        $tongxin = Db::name('baoxiudan')->where('system', 2)->where($map)->select();
        //供配电系统
        $peidian = Db::name('baoxiudan')->where('system', 3)->where($map)->select();
        //计重设备
        $jizhong = Db::name('baoxiudan')->where('system', 4)->where($map)->select();
        //收费系统
        $shoufei = Db::name('baoxiudan')->where('system', 5)->where($map)->select();
        $this->view->assign([
            'baoxiunum' => count($BaoxiuArr),
            'baoxiu' => $Round . '%',
            'wbaoxiu' => $WRound . '%',
            'jiankong' => count($jiankong),
            'tongxin' => count($tongxin),
            'peidian' => count($peidian),
            'jizhong' => count($jizhong),
            'shoufei' => count($shoufei),
            'totaluser' => 35200,
            'totalviews' => 219390,
            'totalorder' => 32143,
            'totalorderamount' => 174800,
            'todayuserlogin' => 321,
            'todayusersignup' => 430,
            'todayorder' => 2324,
            'unsettleorder' => 132,
            'sevendnu' => '80%',
            'sevendau' => '32%',
            'paylist' => $paylist,
            'createlist' => $createlist,
            'addonversion' => $addonVersion,
            'uploadmode' => $uploadmode,
        ]);
        $ZhanDianArr = Db::name('zhandian')->select();
        foreach ($ZhanDianArr as $k => $v) {
            $data[$k]['id'] = $v['id'];
            $data[$k]['zhandian_name'] = $v['zhandian'];
            $BaoXiuZhandian = Db::name('baoxiudan')->where('zhandian_id', $v['id'])->where($map)->select();
            $BaoxiuWancheng = Db::name('baoxiudan')->where('zhandian_id', $v['id'])->where($map)->where('type', 2)->select();
            $BaoxiuWeiWancheng = Db::name('baoxiudan')->where('zhandian_id', $v['id'])->where($map)->where('type', 1)->select();
            if (empty($BaoXiuZhandian)) {
                $data[$k]['baoxiu_num'] = 0;
                $data[$k]['BaoXiuStr'] = '';
                $data[$k]['Round'] = '无记录';
                $data[$k]['WRound'] = '无记录';

            } else {
                $data[$k]['baoxiu_num'] = count($BaoXiuZhandian);
                foreach ($BaoXiuZhandian as $k1 => $v1) {
                    $StrArr[$k1] = $v1['colum'];
                }
                $Round = round(count($BaoxiuWancheng) / count($BaoXiuZhandian) * 100, 2);
                $WRound = round(count($BaoxiuWeiWancheng) / count($BaoXiuZhandian) * 100, 2);
                $data[$k]['BaoXiuStr'] = implode($StrArr, ',');
                $data[$k]['Round'] = $Round . '%';
                $data[$k]['WRound'] = $WRound . '%';
            }
        }
        $this->view->assign("row", $data);
        return $this->view->fetch();
    }


    public function exportDayInner()
    {
        //月 起止时间戳
        $start_time = mktime(0, 0, 0, date('m'), 1, date('Y'));
        $end_time = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        $map['createtime'] = ['between', [$start_time, $end_time]];
        $ZhanDianArr = Db::name('zhandian')->select();
        foreach ($ZhanDianArr as $k => $v) {
            $data[$k]['id'] = $v['id'];
            $data[$k]['zhandian_name'] = $v['zhandian'];
            $BaoXiuZhandian = Db::name('baoxiudan')->where('zhandian_id', $v['id'])->where($map)->select();
            $BaoxiuWancheng = Db::name('baoxiudan')->where('zhandian_id', $v['id'])->where($map)->where('type', 2)->select();
            $BaoxiuWeiWancheng = Db::name('baoxiudan')->where('zhandian_id', $v['id'])->where($map)->where('type', 1)->select();
            if (empty($BaoXiuZhandian)) {
                $data[$k]['baoxiu_num'] = 0;
                $data[$k]['BaoXiuStr'] = '';
                $data[$k]['Round'] = '无记录';
                $data[$k]['WRound'] = '无记录';

            } else {
                $data[$k]['baoxiu_num'] = count($BaoXiuZhandian);
                foreach ($BaoXiuZhandian as $k1 => $v1) {
                    $StrArr[$k1] = $v1['colum'];
                }
                $Round = round(count($BaoxiuWancheng) / count($BaoXiuZhandian) * 100, 2);
                $WRound = round(count($BaoxiuWeiWancheng) / count($BaoXiuZhandian) * 100, 2);
                $data[$k]['BaoXiuStr'] = implode($StrArr, ',');
                $data[$k]['Round'] = $Round . '%';
                $data[$k]['WRound'] = $WRound . '%';
            }
        }
        $Time = date('Y/m/d');
        $filename = $Time;
        vendor('PHPExcel.PHPExcel');
        $objPHPExcel = new \PHPExcel();
        //设置保存版本格式
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
        //设置表头
        $objPHPExcel->getActiveSheet()->setCellValue('A1', '站点');
        $objPHPExcel->getActiveSheet()->setCellValue('B1', '报修单数量');
        $objPHPExcel->getActiveSheet()->setCellValue('C1', '报修完成率');
        $objPHPExcel->getActiveSheet()->setCellValue('D1', '报修未完成率');
        $objPHPExcel->getActiveSheet()->setCellValue('E1', '故障设备名称');

        //改变此处设置的长度数值
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(12);
        //输出表格
        foreach ($data as $key => &$val) {
            $count = count($data);
            $i = $key + 2;//表格是从2开始的
            if ($count >= 1) {
                $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $val['zhandian_name']);
                $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $val['baoxiu_num']);
                $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $val['Round']);
                $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $val['WRound']);
                $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $val['BaoXiuStr']);
            }
        }
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename=' . $filename . '.xls');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
    }
}