<?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 Tongji2 extends Admin
{

    /**
     * @AopBefore(class="app\aop\AdminLogin", function="before")
     * @RequestMapping(route="/admin/index/tongji2", method="GET", title="统计中心")
    */
    public function run() {
        $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;
            // 今天周几
            $w = date("w")+1+7; // 最多+14天
            $time = strtotime(date('Y-m-d', time()))-(86400*$w);
            
            // 查询在线任务
            $where = [];
            $where[] = ['B.status', '=', 3];
            $where[] = ['B.admin_id', '=', $v['id']];
            $where[] = ['B.final_end_time', '>=', $time];
            $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('B.final_end_time 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) {
                if ($vv['final_start_time'] > $vv['start_time']) {
                    $s = ($vv['final_start_time']-$vv['start_time']);
                    if ($s >= 86400) {
                        $day = ceil($s/86400).'天';
                    } else if ($s >= 3600) {
                        $day = ceil($s/3600).'小时';
                    } else {
                        $day = ceil($s/60).'分钟';
                    }
                    $array[$kk]['final_start_time'] = '<div style="background: #ef4136;color:#fff;border-radius: 10px;padding-left:5px" title="约推迟：'.$day.'">'.date('Y-m-d H:i', $vv['final_start_time']).'</div>';
                } else {
                    $array[$kk]['final_start_time'] = date('Y-m-d H:i', $vv['final_start_time']);
                }
                if ($vv['final_end_time'] > $vv['end_time']) { 
                    $s = ($vv['final_end_time']-$vv['end_time']);
                    if ($s >= 86400) {
                        $day = ceil($s/86400).'天';
                    } else if ($s >= 3600) {
                        $day = ceil($s/3600).'小时';
                    } else {
                        $day = ceil($s/60).'分钟';
                    }
                    $array[$kk]['final_end_time'] = '<div style="background: #d93a49;color:#fff;border-radius: 10px;padding-left:5px" title="约推迟：'.$day.'">'.date('Y-m-d H:i', $vv['final_end_time']).'</div>';
                    $array[$kk]['outtime'] = 1;
                } else {
                    $array[$kk]['final_end_time'] = date('Y-m-d H:i', $vv['final_end_time']);
                    $array[$kk]['outtime'] = 0;
                }
            }
            $list[$k]['list'] = $array;
        }
        $this->assign('list', $list);

        $DB->return();

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

