<?php
namespace app\admin\controller;

use think\Db;
use think\Exception;

class CockpitStatistics extends Auth
{

    public function index(){

        return $this->fetch();
    }

    public function todoCompletion(){
        $mode = input('mode',1);
        $month = date('Ym');
        //总工单
        $totalCount = Db::name('todo')
            ->where('del',0)
            ->where('org_id',$this->orgId)
            ->where('create_yyyymm',$month)
            ->where('work_type_mode',$mode)
            ->count();
        //完成的工单
        $count1 = Db::name('todo')
            ->where('del',0)
            ->where('org_id',$this->orgId)
            ->where('create_yyyymm',$month)
            ->where('work_type_mode',$mode)
            ->where('todo_mode',3)
            ->count();

        //主动完成工单
        $count2 = Db::name('todo')
            ->alias('t')
            ->join('orders o','o.id=t.order_id')
            ->where('o.del',0)
            ->where('o.source_type',3)
            ->where('o.create_yyyymm',$month)
            ->where('o.org_id',$this->orgId)
            ->where('t.del',0)
            ->where('t.org_id',$this->orgId)
            ->where('t.create_yyyymm',$month)
            ->where('t.work_type_mode',$mode)
            ->where('t.todo_mode',3)
            ->count();
        //被动完成的工单
        $count3 = Db::name('todo')
            ->alias('t')
            ->join('orders o','o.id=t.order_id')
            ->where('o.del',0)
            ->whereIn('o.source_type',[1,2])
            ->where('o.create_yyyymm',$month)
            ->where('o.org_id',$this->orgId)
            ->where('t.del',0)
            ->where('t.org_id',$this->orgId)
            ->where('t.create_yyyymm',$month)
            ->where('t.work_type_mode',$mode)
            ->where('t.todo_mode',3)
            ->count();

        $bl1 = $bl3 = $bl5 = 0;
        $bl2 = $bl4 = $bl6 = 100;

        if($totalCount > 0){
            $remain = $totalCount-$count1;
            $bl1 = round($count1/$totalCount*100,2);
            $bl2 = round($remain/$totalCount*100,2);

            $remain2 = $totalCount-$count2;
            $bl3 = round($count2/$totalCount*100,2);
            $bl4 = round($remain2/$totalCount*100,2);

            $remain3 = $totalCount-$count3;
            $bl5 = round($count3/$totalCount*100,2);
            $bl6 = round($remain3/$totalCount*100,2);
        }

        $data =[
            'count1'=>['title'=>'总完成率','bl1'=>$bl1,'bl2'=>$bl2],
            'count2'=>['title'=>'主动完成率','bl1'=>$bl3,'bl2'=>$bl4],
            'count3'=>['title'=>'被动完成率','bl1'=>$bl5,'bl2'=>$bl6],
            'title'=>['总完成率','主动完成率','被动完成率'],
        ];
        $this->success('','',$data);
    }

    public function todoResponseTime(){
        $mode = input('mode',1);
        $month = date('Ym');
        $lastMonth = date('Ym',strtotime("-1 month"));

        $todo = Db::name('todo')
            ->field('xy_time')
            ->where('del',0)
            ->where('org_id',$this->orgId)
            ->where('create_yyyymm',$month)
            ->where('work_type_mode',$mode)
            ->select();
        $nums = 0;
        foreach ($todo as $k=>$v){
            $nums +=$v['xy_time'];
        }
        $count = round($nums/60/60,1);

        $lastTodo = Db::name('todo')
            ->field('xy_time')
            ->where('del',0)
            ->where('org_id',$this->orgId)
            ->where('create_yyyymm',$lastMonth)
            ->where('work_type_mode',$mode)
            ->select();

        $nums2 = 0;
        foreach ($lastTodo as $k=>$v){
            $nums2 +=$v['xy_time'];
        }
        $count2 = round($nums2/60/60,1);

        $data = [
            'count'=>$count,
            'count2'=>$count-$count2,
        ];
        $this->success('','',$data);
    }

    public function repairTodo(){
        $list = Db::name('order_type')
            ->field('id,title as name')
            ->where('org_id',$this->orgId)
            ->where('parent_id',0)
            ->where('del',0)
            ->where('enable',1)
            ->limit(5)
            ->select();
        foreach ($list as $k=>$v){
            $repair = Db::name('order_type')
                ->field('id')
                ->where('org_id',$this->orgId)
                ->where('parent_id',$v['id'])
                ->where('del',0)
                ->where('enable',1)
                ->select();
            $ids = [];
            foreach ($repair as $kk=>$vv){
                $ids[] = $vv['id'];
            }
            $todo = Db::name('todo')
                ->alias('t')
                ->field('t.wc_time')
                ->join('order_repair or','or.order_id=t.order_id')
                ->whereIn('or.type_id',$ids)
                ->where('t.del',0)
                ->where('t.org_id',$this->orgId)
                ->where('t.create_yyyymm',date('Ym'))
                ->select();
            $nums = 0;
            foreach ($todo as $key=>$val){
                $nums +=$val['wc_time'];
            }

            $list[$k]['value'] = round($nums/60/60,1);
        }
        $this->success('','',$list);
    }

    public function cleanTypeTask(){
        $list = Db::name('clean_type')
            ->field('id,title')
            ->where('parent_id',0)
            ->where('org_id',$this->orgId)
            ->where('del',0)
            ->where('enable',1)
            ->limit(5)
            ->select();
        $title = [];
        foreach ($list as $k=>$v){
            $title[] = $v['title'];
            $type = Db::name('clean_type')
                ->where('parent_id',$v['id'])
                ->where('org_id',$this->orgId)
                ->where('del',0)
                ->where('enable',1)
                ->column('id');

            $form = Db::name('clean_form')
                ->where('org_id',$this->orgId)
                ->where('del',0)
                ->where('enable',1)
                ->whereIn('type_id',$type)
                ->column('id');

//            $task = Db::name('clean_task')
//                ->alias('ct')
//                ->join('clean_task_form ctf','ctf.task_id=ct.id')
//                ->whereIn('ctf.form_id',$form)
//                ->where('ct.org_id',$this->orgId)
//                ->where('ct.del',0)
//                ->count();
            $taskCount = Db::name('clean_task')
                ->alias('ct')
                ->join('clean_task_form ctf','ctf.task_id=ct.id')
                ->whereIn('ctf.form_id',$form)
                ->where('ct.org_id',$this->orgId)
                ->where('ct.del',0)
                ->whereIn('ct.status',[2,3])
                ->count();

            $list[$k]['value'] =$taskCount;
        }
        $data = [
            'list'=>$list,
            'title'=>$title,
        ];
        $this->success('','',$data);
    }

    public function mateGoods(){
        $where[] = ['create_time','>=',date('Y-m').'-01 00:00:00'];
        $where[] = ['create_time','<=',date('Y-m').'-31 00:00:00'];

        $list = Db::name('mate_goods')
            ->field('id,title')
            ->where('org_id',$this->orgId)
            ->where('del',0)
            ->where('enable',1)
            ->limit(6)
            ->select();
        $title = $count = [];
        foreach ($list as $k=>$v){
            $title[]= $v['title'];
            $nums = Db::name('todo_mate_item')
                ->where('items_id',$v['id'])
                ->where($where)
                ->sum('total');
            $count[] = $nums;
        }

        $data = [
            'list'=>$count,
            'title'=>$title,
        ];
        $this->success('','',$data);
    }

    public function orgUser(){
        $list = [
            ['id'=>3,'name'=>'客户'],
            ['id'=>4,'name'=>'综合'],
            ['id'=>5,'name'=>'保安'],
            ['id'=>6,'name'=>'运送'],
            ['id'=>7,'name'=>'维修'],
            ['id'=>8,'name'=>'保洁'],
            ['id'=>9,'name'=>'调度'],
            ['id'=>10,'name'=>'管理层'],
        ];
        foreach ($list as $k=>$v){
            $roles = Db::name('roles')
                ->where('org_id',$this->orgId)
                ->where('enable',1)
                ->where('del',0)
                ->where('parent_id',$v['id'])
                ->column('id');
            $user = Db::name('user')
                ->alias('u')
                ->join('user_org uo','uo.user_id=u.id')
                ->join('user_roles ur','ur.user_id=uo.user_id')
                ->whereIn('ur.roles_id',$roles)
                ->where('uo.org_id',$this->orgId)
                ->where('u.enable',1)
                ->where('u.del',0)
                ->count();
            $list[$k]['value'] = $user;
        }

        $this->success('','',$list);
    }

    public function wasteCount(){
        $waste = Db::name('waste_record')
            ->where('org_id',$this->orgId)
            ->where('create_yyyymm',date('Ym'))
            ->where('status',2)
            ->where('del',0)
            ->column('weight');
        $nums = 0;
        foreach ($waste as $k=>$v){
            $nums +=$v;
        }
        $count = round($nums/1000,2);
        $type = 0;
        $record = Db::name('waste_record')->where('org_id',$this->orgId)->where('del',0)->find();
        if(!$record){
            $type = 1; //功能未应用
        }

        $sdate = date('Y-m-d H:i:s',strtotime("-3 day"));
        $edate = date('Y-m-d H:i:s');


        $record3 = Db::name('waste_record')
            ->where('org_id',$this->orgId)
            ->where('del',0)
            ->where('create_time','>',$sdate)
            ->where('create_time','<',$edate)
            ->order('id desc')
            ->find();
        if($record3){
            $type = 3; //绿色
        }
        $record2 = Db::name('waste_record')
            ->where('org_id',$this->orgId)
            ->where('del',0)
            ->where('create_time','>',$sdate)
            ->order('id desc')
            ->find();
        if(!$record3 && !$record2){
            $type = 2; //红色
        }

        $data = [
            'count' =>$count,
            'type' =>$type,

        ];
        $this->success('','',$data);
    }

    public function dailyDataCount(){
        $sDate = date('Ymd',strtotime( "-3 month"));
        $eDate = date('Ymd');

        $count = Db::name('daily_record')
            ->where('org_id',$this->orgId)
            ->where('create_yyyymmdd','>',$sDate)
            ->where('create_yyyymmdd','<=',$eDate)
            ->count();

        $count2 = Db::name('comment')
            ->where('org_id',$this->orgId)
            ->where('type',0)
            ->where('create_yyyymmdd','>',$sDate)
            ->where('create_yyyymmdd','<=',$eDate)
            ->count();

        $count3 = Db::name('comment')
            ->where('org_id',$this->orgId)
            ->where('type',0)
            ->where('create_yyyymm',date('Ym'))
            ->count();

        $count4 = Db::name('comment')
            ->where('org_id',$this->orgId)
            ->where('type',0)
            ->where('create_yyyymm',date('Ym'))
            ->avg('score');

        $bl = '100%';
        if($count > 0){
            $bl = round($count2/$count*100,1).'%';
        }

        $data = [
            'bl'=>$bl,
            'count2'=>$count2.'/'.$count,
            'count3'=>$count3,
            'count4'=>round($count4,1),

        ];
        $this->success('','',$data);
    }


}