<?php
// +----------------------------------------------------------------------
// | 主页
// +----------------------------------------------------------------------
// | Copyright (c) 2018 https://blog.junphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小黄牛 <1731223728@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\home;
use app\controller\SuperClass\Reception;

class Index extends Reception
{

    /**
     * @RequestMapping(route="/", 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();
        }
        $this->assign('list', $list);

        $item = $DB->name('item')->where('status', 1)->select();
        $this->assign('item', $item);


        $DB->return();

        return $this->display();
    }

    /**
     * @RequestMapping(route="/get_edition", method="POST", title="获取版本号")
    */
    public function get_edition() {
        $param = \x\Request::post();

        $DB = new \x\Db();
        
        $list = $DB->name('edition')->where('item_id', $param['id'])->order('id ASC')->select();
        foreach ($list as $k => $v) {
            if ($v['start_time']) {$list[$k]['start_time'] = date('Y-m-d H:i', $v['start_time']);}else{$list[$k]['start_time']='无';}
            if ($v['test_time']) {$list[$k]['test_time'] = date('Y-m-d H:i', $v['test_time']);}else{$list[$k]['test_time']='无';}
            if ($v['publish_time']) {$list[$k]['publish_time'] = date('Y-m-d H:i', $v['publish_time']);}else{$list[$k]['publish_time']='无';}

            if ($v['final_start_time']) {$list[$k]['final_start_time'] = date('Y-m-d H:i', $v['final_start_time']);}else{$list[$k]['final_start_time']='无';}
            if ($v['final_test_time']) {$list[$k]['final_test_time'] = date('Y-m-d H:i', $v['final_test_time']);}else{$list[$k]['final_test_time']='无';}
            if ($v['final_publish_time']) {$list[$k]['final_publish_time'] = date('Y-m-d H:i', $v['final_publish_time']);}else{$list[$k]['final_publish_time']='无';}

            $list[$k]['day'] = $this->_time(($v['publish_time']-$v['start_time']), $v['start_time']);

            if ($v['final_start_time'] && $v['final_publish_time']) {
                $list[$k]['final_day'] = $this->_time(($v['final_publish_time']-$v['final_start_time']), $v['final_start_time']);
            } else {
                $list[$k]['final_day'] = '未知';
            }

            $list[$k]['type_start'] = 1;
            $list[$k]['type_test'] = 1;
            $list[$k]['type_publish'] = 1;

            if ($v['final_start_time'] && $v['final_start_time'] > $v['start_time']) {
                $list[$k]['type_start'] = 2;
            }
            if ($v['final_test_time'] && $v['final_test_time'] > $v['test_time']) {
                $list[$k]['type_test'] = 2;
            }
            if ($v['final_publish_time'] && $v['final_publish_time'] > $v['final_publish_time']) {
                $list[$k]['type_publish'] = 2;
            }

            $list[$k]['list'] = $DB->name('task')->where('edition_id', $v['id'])->field('id, task_title')->order('id ASC')->select();
        }
        $DB->return();

        return $this->returnJson('00', '查询成功', $list);
    }

    /**
     * @RequestMapping(route="/get_task", method="POST", title="获取任务树")
    */
    public function get_task() {
        $param = \x\Request::post();

        $DB = new \x\Db();
        
        $list = $DB->name('task')->where('edition_id', $param['id'])->order('id ASC')->select();
        foreach ($list as $k => $v) {
            $user = $DB->name('task_user')->alias('A')->join('auth_admin B', 'A.admin_id=B.id')
                    ->where('A.task_id', $v['id'])
                    ->order('A.id ASC')
                    ->field('A.*, B.nick, B.color, B.logo, B.identity')
                    ->select();
            foreach ($user as $key => $val) {
                $user[$key]['day'] = $this->_time(($val['end_time']-$val['start_time']), $val['start_time']);
                if ($val['final_start_time'] && $val['final_end_time']) {
                    $user[$key]['final_day'] = $this->_time(($val['final_end_time']-$val['final_start_time']), $val['final_start_time']);
                } else {
                    $user[$key]['final_day'] = '未知';
                }

                if ($val['start_time']) {$user[$key]['start_time'] = date('Y-m-d H:i', $val['start_time']);}else{$user[$key]['start_time']='无';}
                if ($val['end_time']) {$user[$key]['end_time'] = date('Y-m-d H:i', $val['end_time']);}else{$user[$key]['end_time']='无';}

                if ($val['final_start_time']) {$user[$key]['final_start_time'] = date('Y-m-d H:i', $val['final_start_time']);}else{$user[$key]['final_start_time']='无';}
                if ($val['final_end_time']) {$user[$key]['final_end_time'] = date('Y-m-d H:i', $val['final_end_time']);}else{$user[$key]['final_end_time']='无';}

                $user[$key]['type_start'] = 1;
                $user[$key]['type_end'] = 1;

                if ($val['final_start_time'] && $val['final_start_time'] > $val['start_time']) {
                    $user[$key]['type_start'] = 2;
                }
                if ($val['final_end_time'] && $val['final_end_time'] > $val['end_time']) {
                    $user[$key]['type_end'] = 2;
                }
            }        

            $list[$k]['list'] = $user;
        }
        $DB->return();

        return $this->returnJson('00', '查询成功', $list);
    }

    
    /**
     * @RequestMapping(route="/get_user", method="POST", title="获取某个人的任务树")
    */
    public function get_user() {
        $param = \x\Request::post();

        $DB = new \x\Db();
        
        $list = $DB->name('task_user')->alias('A')
                ->join('auth_admin B', 'A.admin_id=B.id')
                ->join('task C', 'A.task_id=C.id')
                ->join('edition D', 'C.edition_id=D.id')
                ->join('item E', 'D.item_id=E.id')
                ->where('A.admin_id', $param['id'])
                ->where('A.status', '<>', 3)
                ->order('E.id ASC, C.edition_id DESC, A.task_id DESC')
                ->field('C.*, B.nick, B.color, B.logo, B.identity, A.start_time, A.final_start_time, A.end_time, A.final_end_time, A.duty, A.status, D.code, E.title')
                ->select();
        foreach ($list as $k => $v) {
            $list[$k]['day'] = $this->_time(($v['end_time']-$v['start_time']), $v['start_time']);
            if ($v['final_start_time'] && $v['final_end_time']) {
                $list[$k]['final_day'] = $this->_time(($v['final_end_time']-$v['final_start_time']), $v['final_start_time']);
            } else {
                $list[$k]['final_day'] = '未知';
            }

            if ($v['start_time']) {$list[$k]['start_time'] = date('Y-m-d H:i', $v['start_time']);}else{$list[$k]['start_time']='无';}
            if ($v['end_time']) {$list[$k]['end_time'] = date('Y-m-d H:i', $v['end_time']);}else{$list[$k]['end_time']='无';}

            if ($v['final_start_time']) {$list[$k]['final_start_time'] = date('Y-m-d H:i', $v['final_start_time']);}else{$list[$k]['final_start_time']='无';}
            if ($v['final_end_time']) {$list[$k]['final_end_time'] = date('Y-m-d H:i', $v['final_end_time']);}else{$list[$k]['final_end_time']='无';}

            $list[$k]['type_start'] = 1;
            $list[$k]['type_end'] = 1;

            if ($v['final_start_time'] && $v['final_start_time'] > $v['start_time']) {
                $list[$k]['type_start'] = 2;
            }
            if ($v['final_end_time'] && $v['final_end_time'] > $v['end_time']) {
                $list[$k]['type_end'] = 2;
            }
        }
        $DB->return();

        return $this->returnJson('00', '查询成功', $list);
    }

    // 计算天数
    private function _time($s, $start_time) {
        // 天数
        if ($s >= 86400) {
            $day = ceil($s/86400);
            $num = 0;
            $time = strtotime(date('Y-m-d', $start_time));
            // 不管，先过滤掉周末双休再说
            for ($i=1; $i <= $day; $i++) {
                $w = date("w", $time);
                if ($w == 0 || $w == 6) {
                    $num++;
                }
                $time += 86400;
            }
            return ($day-$num).' 个工作日';
        // 小时
        } else if ($s > 3600) {
            return ceil($s/3600).' 小时';
        // 分钟
        } else {
            return ceil($s/60).' 分钟';
        }
    }
}

