Explorar o código

用户答题统计

lizhen_gitee hai 4 meses
pai
achega
6c678a4246

+ 66 - 1
application/admin/controller/user/User.php

@@ -3,7 +3,8 @@
 namespace app\admin\controller\user;
 
 use app\common\controller\Backend;
-
+use think\Db;
+use app\common\library\ExcelCsv;
 /**
  * 用户管理
  *
@@ -69,4 +70,68 @@ class User extends Backend
         return $this->view->fetch();
     }
 
+    /**
+     * 统计
+     */
+    public function tongji(){
+        $list_exam = Db::name('user_question_log')->alias('log')
+            ->field('log.user_id,count(log.id) as right_num,user.nickname,user.mobile,user.bind_jigou_id,vote_jigou.title')
+            ->join('user','log.user_id = user.id','LEFT')
+            ->join('vote_jigou','user.bind_jigou_id = vote_jigou.id','LEFT')
+            ->where('log.is_right',1)
+            ->group('log.user_id')->order('right_num desc')
+            ->select();
+
+        $list_vote = Db::name('vote_record')->field('user_id,sum(vote) as vote_num')->group('user_id')->order('vote_num desc')->select();
+
+        foreach($list_exam as $key => $val){
+            $val['num'] = $key + 1;
+            $val['vote_num'] = 0;
+            foreach($list_vote as $k => $v){
+                if($val['user_id'] == $v['user_id']){
+                    $val['vote_num'] = $v['vote_num'];
+                }
+            }
+            $list_exam[$key] = $val;
+        }
+
+        //只要前300名,后面的砍掉
+        if(count($list_exam) > 300){
+            $list_exam = array_chunk($list_exam,300)[0];
+        }
+//        dump($list_exam);
+//        dump($list_vote);
+
+        //导出excel
+        $action = input('action','');
+        if($action == 'export'){
+            //表头
+            $excel_header = [
+                'num'         => '排名',
+                'user_id'     => '用户ID',
+                'nickname'    => '用户昵称',
+                'mobile'      => '用户手机',
+                'right_num'   => '答对次数',
+                'title'       => '绑定机构',
+                'vote_num'    => '投出票数',
+            ];
+
+            //内容
+            $excel_result = $list_exam;
+
+            //文件输出
+
+
+            $fileName = date('Y-m-d-H-i-s');
+            $ExcelCsv = new ExcelCsv($excel_header, $excel_result);
+            $data = $ExcelCsv->collection();
+            $ExcelCsv->download($fileName, $excel_header, $data);
+        }
+
+        $this->assign('lists',$list_exam);
+        $this->view->engine->layout(false);
+        return $this->view->fetch();
+
+    }
+
 }

+ 73 - 0
application/admin/view/user/user/tongji.html

@@ -0,0 +1,73 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>统计</title>
+    {include file="common/meta" /}
+</head>
+<style>
+    /* 默认情况下,屏幕上显示 */
+    .hide-on-print {
+        display: block;
+        text-align: right;
+        margin-bottom: 20px;
+        padding-top: 20px;
+    }
+
+    .table-bordered {
+        border: 1px solid #ddd;
+    }
+
+
+    .table-bordered > thead > tr > th, .table-bordered > tbody > tr > th, .table-bordered > tfoot > tr > th, .table-bordered > thead > tr > td, .table-bordered > tbody > tr > td, .table-bordered > tfoot > tr > td {
+        border: 1px solid #ddd;
+        text-align: center;
+
+    }
+
+    /* 打印时隐藏 */
+    @media print {
+        .hide-on-print {
+            display: none;
+        }
+        .form-horizontal {
+            display: none;
+        }
+    }
+</style>
+
+<body  style="width: 760px;margin: 0 auto">
+
+    <div class="hide-on-print">
+        <a type="button" href="?action=export" class="btn btn-primary dropdown-toggle">导出</a>
+    </div>
+
+    <table id="left_table" class="table table-striped table-bordered table-hover table-nowrap">
+
+        <tr>
+            <td>排名</td>
+            <td>用户ID</td>
+            <td>用户昵称</td>
+            <td>用户手机</td>
+            <td>答对次数</td>
+            <td>绑定机构</td>
+            <td>投出票数</td>
+        </tr>
+
+        {volist name='lists' id='vo'}
+        <tr>
+            <td>{$vo.num}</td>
+            <td>{$vo.user_id}</td>
+            <td>{$vo.nickname}</td>
+            <td>{$vo.mobile}</td>
+            <td>{$vo.right_num}</td>
+            <td>{$vo.title}</td>
+            <td>{$vo.vote_num}</td>
+        </tr>
+        {/volist}
+    </table>
+
+
+
+</body>
+</html>
+

+ 13 - 0
application/common.php

@@ -5,6 +5,7 @@
 use think\exception\HttpResponseException;
 use think\Response;
 use fast\Random;
+use Hyperf\Utils\Collection;
 
 if (!function_exists('__')) {
 
@@ -981,4 +982,16 @@ if(!function_exists('birthtime_to_age')) {
 
         return $age;
     }
+}
+if (! function_exists('collect')) {
+    /**
+     * Create a collection from the given value.
+     *
+     * @param null|mixed $value
+     * @return Collection
+     */
+    function collect($value = null)
+    {
+        return new Collection($value);
+    }
 }

+ 79 - 0
application/common/library/ExcelCsv.php

@@ -0,0 +1,79 @@
+<?php
+namespace app\common\library;
+
+class ExcelCsv{
+    private $row;
+    private $data;
+
+    public function __construct($row,$data){
+        $this->row[] = $row;
+        $this->data = $data;
+    }
+
+    public function collection()
+    {
+        $row = $this->row;
+        $data = $this->data;
+        //设置表头
+        foreach ($row[0] as $key => $value) {
+            $key_arr[] = $key;
+        }
+
+        //输入数据
+        foreach ($data as $key => &$value) {
+            $js = [];
+            for ($i=0; $i < count($key_arr); $i++) {
+                $js = array_merge($js,[ $key_arr[$i] => $value[ $key_arr[$i] ] ]);
+            }
+            array_push($row, $js);
+            unset($val);
+        }
+
+        $collect =  collect($row);
+
+        $row = [];
+        $list = [];
+        foreach ($collect as $key=>$item){
+            $val = [];
+            foreach ($item as $v){
+                $val[] = $v;
+            }
+            if ($key == 0){
+                $row[] = $val;
+            }else{
+                $list[] = $val;
+            }
+        }
+
+        return $list;
+    }
+
+    public function download($fileName,$row,$list)
+    {
+        $fileName = $fileName.'.csv';
+        //设置文件头
+        header('Content-Description: File Transfer');
+        header('Content-Type: application/vnd.ms-excel');
+        header('Content-Disposition: attachment; filename="' . $fileName . '"');
+        header('Expires: 0');
+        header('Cache-Control: must-revalidate');
+        header('Pragma: public');
+
+        $fp = fopen('php://output', 'a');//打开output流
+        mb_convert_variables('GBK', 'UTF-8', $row);
+        fputcsv($fp, $row);
+
+        foreach ($list as $val) {
+            $val1 = [];
+            foreach ($val as $export_obj) {
+                $val1[] = iconv('utf-8', 'GB18030', $export_obj);
+            }
+            fputcsv($fp, $val1);
+            //刷新缓冲
+            ob_flush();
+            flush();
+        }
+        fclose($fp);
+        exit();
+    }
+}