<?php
/**
 * +----------------------------------------------------------------------
 * 统计中心
 * +----------------------------------------------------------------------
 * 官网：https://www.sw-x.cn
 * +----------------------------------------------------------------------
 * 作者：小黄牛 <1731223728@qq.com>
 * +----------------------------------------------------------------------
 * 开源协议：http://www.apache.org/licenses/LICENSE-2.0
 * +----------------------------------------------------------------------
*/


namespace app\controller\admin\index;
use app\controller\SuperClass\Admin;

class Tongji extends Admin
{

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/index/tongji", method="GET", title="统计中心")
     * @Param(name="type", value="1")
    */
    public function run() {
        $param = \x\Request::get();
        if ($param['type'] == 2) {
            // 按项目分
            return $this->tongji2();
        }
        $DB = new \x\Db();
        
        $list = $DB->name('auth_admin')->where('status', 1)->select();
        foreach ($list as $k => $v) {
            $list[$k]['num'] = $DB->name('task_user')->where('admin_id', $v['id'])->where('status <> 3')->count();
            // 计算推迟比例
            $max = $DB->name('task_user')->where('admin_id', $v['id'])->where('status', 3)->count();
            $num = $DB->name('task_user')->where('admin_id', $v['id'])->where('status = 3 && final_end_time > end_time')->count();
            $rate = 0;
            if ($max) {
                $r = 100 / $max;
                $rate = round($num*$r, 2);
            }
            $list[$k]['rate'] = $rate;


            // 查询在线任务
            $where = [];
            $where[] = ['B.status', '<>', 3];
            $where[] = ['B.admin_id', '=', $v['id']];
            $array = $DB->name('task')->alias('A')
                ->join('task_user B', 'B.task_id=A.id')
                ->join('edition C', 'A.edition_id=C.id')
                ->join('item D', 'C.item_id=D.id')
                ->where($where)
                ->order('C.item_id ASC, A.edition_id DESC, A.id DESC')
                ->field('A.task_title, B.id, B.start_time, B.end_time, B.final_start_time, B.final_end_time, B.status, C.code, D.title')
                ->select();
            foreach ($array as $kk=>$vv) {
                $array[$kk]['start_time'] = !empty($vv['start_time']) ? date('Y-m-d H:i', $vv['start_time']) : '';
                $array[$kk]['end_time'] = !empty($vv['end_time']) ? date('Y-m-d H:i', $vv['end_time']) : '';
                $array[$kk]['final_start_time'] = !empty($vv['final_start_time']) ? date('Y-m-d H:i', $vv['final_start_time']) : '';
                $array[$kk]['final_end_time'] = !empty($vv['final_end_time']) ? date('Y-m-d H:i', $vv['final_end_time']) : '';
                $array[$kk]['status_name'] = task_status($vv['status']);;

                if ($vv['status'] == 1 && ($vv['start_time']-3600) < time()) {
                    $array[$kk]['start_time'] = '<div style="background: #ef4136;color:#fff;border-radius: 10px;padding-left:5px" title="即将过期">'.$array[$kk]['start_time'].'</div>';
                }
    
                if ($vv['status'] == 2 && ($vv['end_time']-3600) < time()) {
                    $array[$kk]['end_time'] = '<div style="background: #d93a49;color:#fff;border-radius: 10px;;padding-left:5px" title="即将过期">'.$array[$kk]['end_time'].'</div>';
                    $array[$kk]['outtime'] = 1;
                } else {
                    $array[$kk]['outtime'] = 0;
                }
            }
            $list[$k]['list'] = $array;
        }
        $this->assign('list', $list);

        $DB->return();

        return $this->display('admin/index/tongji');
    }

    public function tongji2() {
        $DB = new \x\Db();
        // 项目
        $item_list = $DB->name('item')->order('id ASC')->select();
        $return_list = [];
        foreach ($item_list as $k => $v) {
            // 查询在线任务
            $where = [];
            $where[] = ['B.status', '<>', 3];
            $where[] = ['C.item_id', '=', $v['id']];
            $array = $DB->name('task')->alias('A')
                ->join('task_user B', 'B.task_id=A.id')
                ->join('edition C', 'A.edition_id=C.id')
                ->join('item D', 'C.item_id=D.id')
                ->join('auth_admin E', 'B.admin_id=E.id')
                ->where($where)
                ->order('C.item_id ASC, A.edition_id DESC, A.id DESC')
                ->field('B.task_id, A.task_title, B.id, B.start_time, B.end_time, B.final_start_time, B.final_end_time, B.status, C.code, D.title, E.nick, E.logo, E.color')
                ->select();
            $ren = [];
            foreach ($array as $val) {
                if (!isset($ren[$val['nick']])) {
                    $ren[$val['nick']] = '<font color="'.$val['color'].'">'.$val['nick'].'</font>';
                }
            }
            $name = '';
            foreach ($ren as $val) {
                $name .= $val.'、';
            }
            $item_list[$k]['ren'] = rtrim($name, '、');

            $list = [];
            foreach ($array as $kk=>$vv) {
                if (!isset($list[$vv['task_id']])) {
                    $list[$vv['task_id']] = $vv['task_id'];
                }

                $array[$kk]['start_time'] = !empty($vv['start_time']) ? date('Y-m-d H:i', $vv['start_time']) : '';
                $array[$kk]['end_time'] = !empty($vv['end_time']) ? date('Y-m-d H:i', $vv['end_time']) : '';
                $array[$kk]['final_start_time'] = !empty($vv['final_start_time']) ? date('Y-m-d H:i', $vv['final_start_time']) : '';
                $array[$kk]['final_end_time'] = !empty($vv['final_end_time']) ? date('Y-m-d H:i', $vv['final_end_time']) : '';
                $array[$kk]['status_name'] = task_status($vv['status']);;

                if ($vv['status'] == 1 && ($vv['start_time']-3600) < time()) {
                    $array[$kk]['start_time'] = '<div style="background: #ef4136;color:#fff;border-radius: 10px;padding-left:5px" title="即将过期">'.$array[$kk]['start_time'].'</div>';
                }
    
                if ($vv['status'] == 2 && ($vv['end_time']-3600) < time()) {
                    $array[$kk]['end_time'] = '<div style="background: #d93a49;color:#fff;border-radius: 10px;;padding-left:5px" title="即将过期">'.$array[$kk]['end_time'].'</div>';
                    $array[$kk]['outtime'] = 1;
                } else {
                    $array[$kk]['outtime'] = 0;
                }
            }

            $item_list[$k]['num'] = count($list);
            $item_list[$k]['list'] = $array;

            if (count($list) > 0) {
                $return_list[] = $item_list[$k];
            }
        }
        $this->assign('item_list', $return_list);
        
        return $this->display('admin/index/tongji3');
    }
}

