<?php
namespace app\api\controller\screen;

use app\api\controller\screen\Index;
use app\hander\HelpHander;


use think\Db;
use think\helper\Time;

class Todo extends Index
{
    //各类型当天工单总数
    public function cateTodoCount(){
        //保洁
        $bjCount = Db::name('todo')
            ->where('del',0)
            ->where('create_yyyymmdd',date('Ymd'))
            ->where('work_type_mode',2)
            ->count();
        $bjCount2 = Db::name('todo')
            ->where('del',0)
            ->where('todo_mode',3)
            ->where('create_yyyymmdd',date('Ymd'))
            ->where('work_type_mode',2)
            ->count();
        //报修
        $bxCount = Db::name('todo')
            ->where('del',0)
            ->where('create_yyyymmdd',date('Ymd'))
            ->where('work_type_mode',1)
            ->count();
        $bxCount2 = Db::name('todo')
            ->where('del',0)
            ->where('todo_mode',3)
            ->where('create_yyyymmdd',date('Ymd'))
            ->where('work_type_mode',1)
            ->count();
        //隐患
        $yhCount = Db::name('todo')
            ->where('del',0)
            ->where('create_yyyymmdd',date('Ymd'))
            ->where('work_type_mode',4)
            ->count();
        $yhCount2 = Db::name('todo')
            ->where('del',0)
            ->where('todo_mode',3)
            ->where('create_yyyymmdd',date('Ymd'))
            ->where('work_type_mode',4)
            ->count();
        //运送
        $ysCount = Db::name('todo')
            ->where('del',0)
            ->where('create_yyyymmdd',date('Ymd'))
            ->where('work_type_mode',3)
            ->count();
        $ysCount2 = Db::name('todo')
            ->where('del',0)
            ->where('todo_mode',3)
            ->where('create_yyyymmdd',date('Ymd'))
            ->where('work_type_mode',3)
            ->count();
        //陪护
        $phCount = Db::name('ph_todo')
            ->where('create_time','>=',date('Y-m-d').' 00:00:00')
            ->where('create_time','<=',date('Y-m-d').' 23:59:59')
            ->count();
        $phCount2 = Db::name('ph_todo')
            ->where('create_time','>=',date('Y-m-d').' 00:00:00')
            ->where('create_time','<=',date('Y-m-d').' 23:59:59')
            ->where('status',2)
            ->count();

        $data = [
            'bj'=>$bjCount.'/'.$bjCount2,
            'bx'=>$bxCount.'/'.$bxCount2,
            'yh'=>$yhCount.'/'.$yhCount2,
            'ys'=>$ysCount.'/'.$ysCount2,
            'ph'=>$phCount.'/'.$phCount2,
        ];
        HelpHander::success($data);
    }

    //任务数据总览
    public function taskList(){
        $mode = input('mode/d',1);
        if(!in_array($mode,[1,2,3,4,5])){
            HelpHander::error('参数错误');
        }
        $data = [];
        $header = ['状态', '任务类型', '始发地', '目的地', '需求时间', '执行人', '接单时间', '完成时间'];
        if($mode == 5){
            $header = ['状态', '开始时间', '结束时间', '工作天数', '创建时间'];
            $todo = Db::name('ph_todo')
                ->where('org_id',$this->orgId)
//                ->where('status','in',[1,2])
                ->order('id desc')
                ->limit(30)
                ->select();
            foreach ($todo as $k=>$v){
                $data[$k]['status'] = $v['status'];
                $data[$k]['start'] = date('H:i',strtotime($v['start']));
                $data[$k]['end'] = date('H:i',strtotime($v['end']));
                $data[$k]['day'] = $v['day'];
                $data[$k]['create_time'] = date('H:i',strtotime($v['create_time']));
            }


        }else{
            if($mode == 1){
                $header = ['状态', '执行人', '报修事项', '内容', '接单时间','完成时间'];
            }else if($mode == 2||$mode == 4){
                $header = ['状态', '执行人', '内容', '接单时间','完成时间'];

            }
            $todo = Db::name('todo')
                ->where('org_id',$this->orgId)
                ->where('work_type_mode',$mode)
                ->where('del',0)
                ->order('id desc')
                ->limit(30)
                ->field('id,order_id,to_user_id,todo_mode,create_time,confirm_time,todo_content,done_time')
                ->select();

            foreach ($todo as $k=>$v){
                $userName = Db::name('user')->where('id',$v['to_user_id'])->value('real_name');
                if($mode == 1){ // 报修
                    $type = Db::name('order_repair')
                        ->alias('or')
                        ->leftJoin('order_type ot','ot.id = or.type_id')
                        ->where('or.order_id',$v['order_id'])
                        ->value('title');
                  $data[$k]['todo_mode'] = $v['todo_mode'];
                  $data[$k]['user_name'] = $userName?$userName:'';
                  $data[$k]['type'] = $type?$type:'';
                  $data[$k]['content'] = $v['todo_content'];
                  $data[$k]['confirm_time'] = $v['confirm_time']?date('H:i',strtotime($v['confirm_time'])):'';
                  $data[$k]['done_time'] = $v['done_time']?date('H:i',strtotime($v['done_time'])):'';

                }else if($mode == 3){ //运送
                    $convey = Db::name('order_convey')
                        ->alias('oc')
                        ->join('convey_cate cc','cc.id = oc.type')
                        ->where('oc.order_id',$v['order_id'])
                        ->field('oc.*,cc.title as cate_title')
                        ->find();
                    $userName = Db::name('user')->where('id',$v['to_user_id'])->value('real_name');
                    $start = Db::name('address')->where('id',$convey['start'])->value('title');
                    $end = Db::name('address')->where('id',$convey['end'])->value('title');

                    $data[$k]['todo_mode'] = $v['todo_mode'];
                    $data[$k]['type'] = $convey?$convey['cate_title']:'';
                    $data[$k]['start'] = $start?$start:'';
                    $data[$k]['end'] = $end?$end:'';
                    $data[$k]['xq_time'] = $convey?date('H:i',strtotime($convey['xq_time'])):'';
                    $data[$k]['user_name'] = $userName?$userName:'';
                    $data[$k]['confirm_time'] = $v['confirm_time']?date('H:i',strtotime($v['confirm_time'])):'';
                    $data[$k]['done_time'] = $v['done_time']?date('H:i',strtotime($v['done_time'])):'';

                }else{ // 保洁,应急
                    $data[$k]['todo_mode'] = $v['todo_mode'];
                    $data[$k]['user_name'] = $userName?$userName:'';
                    $data[$k]['content'] = $v['todo_content'];
                    $data[$k]['confirm_time'] = $v['confirm_time']?date('H:i',strtotime($v['confirm_time'])):'';
                    $data[$k]['done_time'] = $v['done_time']?date('H:i',strtotime($v['done_time'])):'';

                }
            }

        }


        $lists = [];
        if($mode == 1){
            foreach ($data as $k=>$v){
                $status = Db::name('todo_mode')->where('id',$v['todo_mode'])->value('out_content');
                if($v['todo_mode'] == 1){
                    $lists[$k][] = '<span style="color:#FFDA0A;">新订单</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['user_name'].'</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['type'].'</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['content'].'</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['confirm_time'].'</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['done_time'].'</span>';
                }else if($v['todo_mode'] == 2){
                    $lists[$k][] = '<span style="color:#0FC2DD;">进行中</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['user_name'].'</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['type'].'</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['content'].'</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['confirm_time'].'</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['done_time'].'</span>';
                }else{
                    $lists[$k][] = $status;
                    $lists[$k][] = $v['user_name'];
                    $lists[$k][] = $v['type'];
                    $lists[$k][] = $v['content'];
                    $lists[$k][] = $v['confirm_time'];
                    $lists[$k][] = $v['done_time'];
                }
            }
        }else if($mode == 2 || $mode == 4){
            foreach ($data as $k=>$v){
                $status = Db::name('todo_mode')->where('id',$v['todo_mode'])->value('out_content');
                if($v['todo_mode'] == 1){
                    $lists[$k][] = '<span style="color:#FFDA0A;">新订单</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['user_name'].'</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['content'].'</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['confirm_time'].'</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['done_time'].'</span>';
                }else if($v['todo_mode'] == 2){
                    $lists[$k][] = '<span style="color:#0FC2DD;">进行中</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['user_name'].'</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['content'].'</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['confirm_time'].'</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['done_time'].'</span>';
                }else{
                    $lists[$k][] = $status;
                    $lists[$k][] = $v['user_name'];
                    $lists[$k][] = $v['content'];
                    $lists[$k][] = $v['confirm_time'];
                    $lists[$k][] = $v['done_time'];
                }
            }
        }else if($mode == 3){
            foreach ($data as $k=>$v){
                $status = Db::name('todo_mode')->where('id',$v['todo_mode'])->value('out_content');
                if($v['todo_mode'] == 1){
                    $lists[$k][] = '<span style="color:#FFDA0A;">新订单</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['type'].'</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['start'].'</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['end'].'</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['xq_time'].'</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['user_name'].'</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['confirm_time'].'</span>';
                    $lists[$k][] = '<span style="color:#FFDA0A;">'.$v['done_time'].'</span>';
                }elseif($v['todo_mode'] == 2){
                    $lists[$k][] = '<span style="color:#0FC2DD;">进行中</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['type'].'</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['start'].'</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['end'].'</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['xq_time'].'</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['user_name'].'</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['confirm_time'].'</span>';
                    $lists[$k][] = '<span style="color:#0FC2DD;">'.$v['done_time'].'</span>';
                }else{
                    $lists[$k][] = $status;
                    $lists[$k][] = $v['type'];
                    $lists[$k][] = $v['start'];
                    $lists[$k][] = $v['end'];
                    $lists[$k][] = $v['xq_time'];
                    $lists[$k][] = $v['user_name'];
                    $lists[$k][] = $v['confirm_time'];
                    $lists[$k][] = $v['done_time'];
                }
            }
        }else if($mode == 5){
            foreach ($data as $k=>$v){
                $status = '';
               if($v['status'] == 0){
                   $status = '作废';
               }else if($v['status'] == 1){
                   $status = '服务中';
               }else if($v['status'] == 2){
                   $status = '已结束';
               }
                $lists[$k][] = $status;
                $lists[$k][] = $v['start'];
                $lists[$k][] = $v['end'];
                $lists[$k][] = $v['day'];
                $lists[$k][] = $v['create_time'];

            }

        }

        HelpHander::success(['data'=>$lists,'header'=>$header]);
    }

    //今日工单总数 //项目工单总数 //项目订单总数 //项目任务总数
    public function todoCountData(){

        $todayCount = Db::name('todo')
            ->where('del',0)
            ->where('org_id',$this->orgId)
            ->where('create_yyyymmdd',date('Ymd'))
            ->count();

        $todoCount = Db::name('todo')
            ->where('del',0)
            ->where('org_id',$this->orgId)
            ->count();

        $orderCount = Db::name('orders')
            ->where('del',0)
            ->where('org_id',$this->orgId)
            ->count();

        $task1 = Db::name('device_task')
            ->where('del',0)
            ->where('org_id',$this->orgId)
            ->count();

        $task2 = Db::name('patrol_task')
            ->where('del',0)
            ->where('org_id',$this->orgId)
            ->count();

        $taskCount = $task1+$task2;

        $data = [
            'todayCount'=>$todayCount,
            'todoCount'=>$todoCount,
            'orderCount'=>$orderCount,
            'taskCount'=>$taskCount,
        ];

        HelpHander::success($data);
    }


    public function todayTodoData(){
        $date = date('Ymd');
        $count = Db::name('todo')
            ->where('create_yyyymmdd',$date)
            ->where('del',0)
            ->count();
        $count2 = Db::name('todo')
            ->where('create_yyyymmdd',$date)
            ->where('del',0)
            ->where('todo_mode',3)
            ->count();
        $map[] = ['del','=',0];
        $map[] = ['create_yyyymmdd','=',$date];
        $bjCount = Db::name('todo')->where('work_type_mode',2)->where($map)->count();
        $bj = Db::name('todo')->where('work_type_mode',2)->where('todo_mode',3)->where($map)->count();

        $ysCount = Db::name('todo')->where('work_type_mode',3)->where($map)->count();
        $ys = Db::name('todo')->where('work_type_mode',3)->where('todo_mode',3)->where($map)->count();

        $bxCount = Db::name('todo')->where('work_type_mode',1)->where($map)->count();
        $bx = Db::name('todo')->where('work_type_mode',1)->where('todo_mode',3)->where($map)->count();

        $yhCount = Db::name('todo')->where('work_type_mode',4)->where($map)->count();
        $yh = Db::name('todo')->where('work_type_mode',4)->where('todo_mode',3)->where($map)->count();
        $bl = $bjBl = $ysBl = $bxBl = $yhBl = 0;
        if($count > 0){
            $bl = round($count2/$count*100,0);
        }
        if($bjCount >0){
            $bjBl = round($bj/$bjCount*100,0);
        }
        if($ysCount >0){
            $ysBl = round($ys/$ysCount*100,0);
        }
        if($bxCount >0){
            $bxBl = round($bx/$bxCount*100,0);
        }
        if($yhCount >0){
            $yhBl = round($yh/$yhCount*100,0);
        }
        $count3 = 0;
        if($count == 0){
            $count3 = 100;
        }else{
            $count3 =  $count - $count2;
        }

        $data = [
            'count'=>$count,
            'bl'=>$bl,
            'bjBl'=>$bjBl,
            'bj'=>$bj,
            'ysBl'=>$ysBl,
            'ys'=>$ys,
            'bxBl'=>$bxBl,
            'bx'=>$bx,
            'yhBl'=>$yhBl,
            'yh'=>$yh,
            'list'=>[
                ['value'=>$count2],
                ['value'=>$count3],
            ]
        ];

        HelpHander::success($data);
    }

}