<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

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



    /**
     * 报表统计
     */
    public function tongji(){
        $starttime = strtotime(date('Y-m-d',strtotime("-7 day")));  //默认7天前
        $endtime   = strtotime(date('Y-m-d')) - 1;

        $starttime = strtotime(date('Y-m-d'));  //默认7天前
        $endtime   = strtotime(date('Y-m-d')) + 86399;

        //接收
        $choicedatetime = input('choicedatetime','');
        if(!empty($choicedatetime)){
            $choicedatetime = explode(' - ',$choicedatetime);
            $starttime = strtotime($choicedatetime[0]);
            $endtime   = strtotime($choicedatetime[1]) + 86399;
        }
        $this->view->assign('defaultdatetime',date('Y-m-d',$starttime).' - '.date('Y-m-d',$endtime));

        //dump($starttime);
        //dump($endtime);

        //所有应该参加考试的人
        $exam_paper = Db::name('exam_paper')->where('start_time','BETWEEN',[$starttime,$endtime])->where('deletetime',NULL)->column('id,user_ids');
//        dump($exam_paper);
        $new_exam_paper = [];
        foreach($exam_paper as $key => $val){
            $new_exam_paper = array_merge($new_exam_paper,explode(',',$val));
        }
//        dump($new_exam_paper);
        $new_exam_paper = array_count_values($new_exam_paper);
//        dump($new_exam_paper);

        //未通过考试的人
        $nopass = Db::name('exam_grade')->where('paper_id','IN',array_keys($exam_paper))->where('is_pass',0)->column('user_id');
//        dump($nopass);
        $nopass = array_count_values($nopass);
//        dump($nopass);

        //参加考试的人
        $exam_grade = Db::name('exam_grade')->where('paper_id','IN',array_keys($exam_paper))->column('user_id');
//        dump($exam_grade);
        $exam_grade = array_count_values($exam_grade);
//        dump($exam_grade);

        //应该参加培训的人
        $train_active = Db::name('train_active')->where('starttime','BETWEEN',[$starttime,$endtime])->where('deletetime',NULL)->column('id,user_ids');
//        dump($train_active);
        $new_train_active = [];
        foreach($train_active as $key => $val){
            $new_train_active = array_merge($new_train_active,explode(',',$val));
        }
//        dump($new_train_active);
        $new_train_active = array_count_values($new_train_active);
//        dump($new_train_active);

        //参加签到的人
        $user_train = Db::name('user_train')->where('train_id','IN',array_keys($train_active))->column('user_id');
//        dump($user_train);
        $user_train = array_count_values($user_train);
//        dump($user_train);

        //参加签到却迟到的人
        $user_train_late = Db::name('user_train')->where('train_id','IN',array_keys($train_active))->where('status',2)->column('user_id');
        $user_train_late = array_count_values($user_train_late);


        //所有用户
        $userlist = Db::name('user')->order('idcard_status desc')->select();
        foreach($userlist as $key => $user){
            $user['realname'] = '';
            if($user['idcard_status'] == 1){
                $user['realname'] = $user['nickname'];
            }

            //应参加考试次数
            $user['paper_count'] = isset($new_exam_paper[$user['id']]) ? $new_exam_paper[$user['id']] : 0;
            //不及格次数
            $user['no_pass'] = isset($nopass[$user['id']]) ? $nopass[$user['id']] : 0;
            //考试次数
            $user['grade_count'] = isset($exam_grade[$user['id']]) ? $exam_grade[$user['id']] : 0;
            //缺考次数
            $user['no_grade'] = $user['paper_count'] - $user['grade_count'];
            if($user['no_grade'] < 0){ $user['no_grade'] = 0;}

            //应该签到的次数
            $user['train_count'] = isset($new_train_active[$user['id']]) ? $new_train_active[$user['id']] : 0;
            //签到的次数
            $user['sign_count'] = isset($user_train[$user['id']]) ? $user_train[$user['id']] : 0;
            //未到的次数
            $user['no_sign'] = $user['train_count'] - $user['sign_count'];
            if($user['no_sign'] < 0){ $user['no_sign'] = 0;}
            //迟到次数
            $user['late'] = isset($user_train_late[$user['id']]) ? $user_train_late[$user['id']] : 0;

            //匿名用户
            if($user['idcard_status'] != 1){
                $user['no_pass'] = 0;
                $user['grade_count'] = 0;
                $user['no_grade'] = 0;

                $user['sign_count'] = 0;
                $user['no_sign'] = 0;
                $user['late'] = 0;
            }

            //
            $userlist[$key] = $user;

            //如果数据都是0，去掉
            if($user['paper_count'] == 0 && $user['train_count'] == 0){
                unset($userlist[$key]);
            }
        }

        $this->assign('datalist',$userlist);

        $this->view->engine->layout(false);
        return $this->view->fetch();
    }
}
