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

use app\hander\HelpHander;
use think\Controller;
use think\Db;
use think\helper\Str;
use think\Request;

class Onelevel extends Controller{

    private $orgs = []; // 用户拥有的组织id集合,只区分大区管理员和总公司
    private $orgName = ''; //大区名称
    //取组合数组
    private $spOrgId = 5; //国天sp 厦门中山医院
    private $orgs2 = []; //去除研发中心id 有研发中心 取国天sp 厦门中山医院

    private $bjOrgId = 11; //霸洁云 贵州省黔西市人民医院

    protected function initialize()
    {
        parent::initialize();

        $token = input('token','','trim');
        $token = think_decrypt($token);
        if(!$token){
            HelpHander::success("无权限");
        }
//        $ts = explode('|',$token);
//        if($ts[1] != config('app.ucode')){
//            HelpHander::success("无权限");
//        }

        $map[] = ['type','=',2];
        $map[] = ['del','=',0];
        $map[] = ['enable','=',1];
        $orgids = Db::name('org')
            ->where($map)
            ->column('id');
        $this->orgs = $orgids?$orgids:[-1];

        foreach ($orgids as $k=>$v){
            if($v == 3){
                unset($orgids[$k]);
            }elseif($v == 24){
                unset($orgids[$k]);
            }
        }
        $this->orgs2 = array_values($orgids);

    }

    public function todayTodoInfo(){
        $date = date('Ymd');

        if(in_array(3,$this->orgs) && !in_array(24,$this->orgs)){
            $map[] = ['CREATE_YYYYMMDD','=',$date];
            $map[] = ['DEL_REF','=',0];
            $count0 = gtspdb('todo')->where($map)->where('ORG_ID',$this->spOrgId)->count();

            $where[] = ['create_yyyymmdd','=',$date];
            $where[] = ['del','=',0];
            $count1 = Db::name('todo')->where($where)->whereIn('org_id',$this->orgs2)->count();

            $count = $count0 + $count1;

            $map[] = ['TODO_MODE','=',3];
            $where[] = ['todo_mode','=',3];
            $overCount0 = gtspdb('todo')->where($map)->where('ORG_ID',$this->spOrgId)->count();
            $overCount1 = Db::name('todo')->where($where)->whereIn('org_id',$this->orgs2)->count();
            $overCount = $overCount0+$overCount1;
            $list = [0,0,0,0,0,0];
            $overBl = 0;
            if($count > 0){
                $overBl = round($overCount/$count*100,0);
                $len =  strlen($count);
                if($len == 1){
                    $count = '00000'.$count;
                }elseif($len == 2){
                    $count = '0000'.$count;
                }elseif($len == 3){
                    $count = '000'.$count;
                }elseif($len == 4){
                    $count = '00'.$count;
                }elseif($len == 5){
                    $count = '0'.$count;
                }else{
                    $count = '999999';
                }
                $list = str_split($count);
            }
        }elseif (!in_array(3,$this->orgs) && in_array(24,$this->orgs)){
            $map[] = ['create_yyyymmdd','=',$date];
            $map[] = ['del','=',0];
            $count0 = bjydb('todo')->where($map)->where('org_id',$this->bjOrgId)->count();

            $where[] = ['create_yyyymmdd','=',$date];
            $where[] = ['del','=',0];
            $count1 = Db::name('todo')->where($where)->whereIn('org_id',$this->orgs2)->count();

            $count = $count0 + $count1;

            $map[] = ['todo_mode','=',3];
            $where[] = ['todo_mode','=',3];
            $overCount0 = bjydb('todo')->where($map)->where('org_id',$this->bjOrgId)->count();
            $overCount1 = Db::name('todo')->where($where)->whereIn('org_id',$this->orgs2)->count();
            $overCount = $overCount0+$overCount1;
            $list = [0,0,0,0,0,0];
            $overBl = 0;
            if($count > 0){
                $overBl = round($overCount/$count*100,0);
                $len =  strlen($count);
                if($len == 1){
                    $count = '00000'.$count;
                }elseif($len == 2){
                    $count = '0000'.$count;
                }elseif($len == 3){
                    $count = '000'.$count;
                }elseif($len == 4){
                    $count = '00'.$count;
                }elseif($len == 5){
                    $count = '0'.$count;
                }else{
                    $count = '999999';
                }
                $list = str_split($count);
            }
        }else if(in_array(3,$this->orgs) && in_array(24,$this->orgs)){
            $map[] = ['CREATE_YYYYMMDD','=',$date];
            $map[] = ['DEL_REF','=',0];
            $count0 = gtspdb('todo')->where($map)->where('ORG_ID',$this->spOrgId)->count();

            $where[] = ['create_yyyymmdd','=',$date];
            $where[] = ['del','=',0];
            $count1 = Db::name('todo')->where($where)->whereIn('org_id',$this->orgs2)->count();

            $count2 = bjydb('todo')->where($where)->whereIn('org_id',$this->bjOrgId)->count();

            $count = $count0 + $count1 + $count2;

            $map[] = ['TODO_MODE','=',3];
            $where[] = ['todo_mode','=',3];

            $overCount0 = gtspdb('todo')->where($map)->where('ORG_ID',$this->spOrgId)->count();
            $overCount1 = Db::name('todo')->where($where)->whereIn('org_id',$this->orgs2)->count();
            $overCount2 = bjydb('todo')->where($where)->where('org_id',$this->bjOrgId)->count();
            $overCount = $overCount0+$overCount1+$overCount2;

            $list = [0,0,0,0,0,0];
            $overBl = 0;
            if($count > 0){
                $overBl = round($overCount/$count*100,0);
                $len =  strlen($count);
                if($len == 1){
                    $count = '00000'.$count;
                }elseif($len == 2){
                    $count = '0000'.$count;
                }elseif($len == 3){
                    $count = '000'.$count;
                }elseif($len == 4){
                    $count = '00'.$count;
                }elseif($len == 5){
                    $count = '0'.$count;
                }else{
                    $count = '999999';
                }
                $list = str_split($count);
            }
        }else{
            $map[] = ['create_yyyymmdd','=',$date];
            $map[] = ['del','=',0];
            $count = Db::name('todo')->where($map)->where('org_id','in',$this->orgs)->count();

            $map[] = ['todo_mode','=',3];
            $overCount = Db::name('todo')->where($map)->where('org_id','in',$this->orgs)->count();
            $list = [0,0,0,0,0,0];
            $overBl = 0;
            if($count > 0){
                $overBl = round($overCount/$count*100,0);
                $len =  strlen($count);
                if($len == 1){
                    $count = '00000'.$count;
                }elseif($len == 2){
                    $count = '0000'.$count;
                }elseif($len == 3){
                    $count = '000'.$count;
                }elseif($len == 4){
                    $count = '00'.$count;
                }elseif($len == 5){
                    $count = '0'.$count;
                }else{
                    $count = '999999';
                }
                $list = str_split($count);
            }

        }
        $data = [
            'list'=>$list,
            'bl'=>$overBl
        ];

        HelpHander::success($data);
    }

    public function todayTodoType(){
        $date = date('Ymd');

        $map[] = ['create_yyyymmdd','=',$date];
        $map[] = ['del','=',0];

        if(in_array(3,$this->orgs) && !in_array(24,$this->orgs)){
            $map[] = ['create_yyyymmdd','=',$date];
            $map[] = ['del','=',0];
            $map[] = ['org_id','in',$this->orgs2];


            $where[] = ['CREATE_YYYYMMDD','=',$date];
            $where[] = ['ORG_ID','=',$this->spOrgId];
            $where[] = ['DEL_REF','=',0];

            $c1 = Db::name('todo')->where($map)->where('work_type_mode',1)->count();
            $c2 = gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',1)->count();
            $count = $c1 + $c2;

            $c3 = Db::name('todo')->where($map)->where('work_type_mode',2)->count();
            $c4 = gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',2)->count();
            $count2 = $c3 + $c4;
            $c5 = Db::name('todo')->where($map)->where('work_type_mode',3)->count();
            $c6 = gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',3)->count();
            $count3 = $c5 + $c6;
            $c7 = Db::name('todo')->where($map)->where('work_type_mode',4)->count();
            $c8 = gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',4)->count();
            $count4 = $c7 + $c8;

            $map[] = ['todo_mode','=',3];
            $where[] = ['TODO_MODE','=',3];

            //保修
            $o1 =  Db::name('todo')->where($map)->where('work_type_mode',1)->count();
            $o2 =  gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',1)->count();
            $bxCount = $o1 + $o2;
            //保洁
            $o3 =  Db::name('todo')->where($map)->where('work_type_mode',2)->count();
            $o4 =  gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',2)->count();
            $bjCount = $o3 + $o4;
            //运送
            $o5 =  Db::name('todo')->where($map)->where('work_type_mode',3)->count();
            $o6 =  gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',3)->count();
            $ysCount = $o5 + $o6;
            //隐患
            $o7 =  Db::name('todo')->where($map)->where('work_type_mode',4)->count();
            $o8 = gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',4)->count();
            $yhCount = $o7 + $o8;
        }else if(!in_array(3,$this->orgs) && in_array(24,$this->orgs)){
            $map[] = ['create_yyyymmdd','=',$date];
            $map[] = ['del','=',0];
            $map[] = ['org_id','in',$this->orgs2];


            $where[] = ['create_yyyymmdd','=',$date];
            $where[] = ['del','=',0];
            $where[] = ['org_id','=',$this->bjOrgId];


            $c1 = Db::name('todo')->where($map)->where('work_type_mode',1)->count();
            $c2 = bjydb('todo')->where($where)->where('work_type_mode',1)->count();
            $count = $c1 + $c2;

            $c3 = Db::name('todo')->where($map)->where('work_type_mode',2)->count();
            $c4 = bjydb('todo')->where($where)->where('work_type_mode',2)->count();
            $count2 = $c3 + $c4;
            $c5 = Db::name('todo')->where($map)->where('work_type_mode',3)->count();
            $c6 = bjydb('todo')->where($where)->where('work_type_mode',3)->count();
            $count3 = $c5 + $c6;
            $c7 = Db::name('todo')->where($map)->where('work_type_mode',4)->count();
            $c8 = bjydb('todo')->where($where)->where('work_type_mode',4)->count();
            $count4 = $c7 + $c8;

            $map[] = ['todo_mode','=',3];
            $where[] = ['todo_mode','=',3];

            //保修
            $o1 =  Db::name('todo')->where($map)->where('work_type_mode',1)->count();
            $o2 =  bjydb('todo')->where($where)->where('work_type_mode',1)->count();
            $bxCount = $o1 + $o2;
            //保洁
            $o3 =  Db::name('todo')->where($map)->where('work_type_mode',2)->count();
            $o4 =  bjydb('todo')->where($where)->where('work_type_mode',2)->count();
            $bjCount = $o3 + $o4;
            //运送
            $o5 =  Db::name('todo')->where($map)->where('work_type_mode',3)->count();
            $o6 =  bjydb('todo')->where($where)->where('work_type_mode',3)->count();
            $ysCount = $o5 + $o6;
            //隐患
            $o7 =  Db::name('todo')->where($map)->where('work_type_mode',4)->count();
            $o8 = bjydb('todo')->where($where)->where('work_type_mode',4)->count();
            $yhCount = $o7 + $o8;
        }elseif(in_array(3,$this->orgs) && in_array(24,$this->orgs)){
            $map[] = ['create_yyyymmdd','=',$date];
            $map[] = ['del','=',0];
            $map[] = ['org_id','in',$this->orgs2];


            $where[] = ['CREATE_YYYYMMDD','=',$date];
            $where[] = ['ORG_ID','=',$this->spOrgId];
            $where[] = ['DEL_REF','=',0];

            $map2[] = ['create_yyyymmdd','=',$date];
            $map2[] = ['del','=',0];
            $map2[] = ['org_id','in',$this->bjOrgId];

            $c1 = Db::name('todo')->where($map)->where('work_type_mode',1)->count();
            $c2 = gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',1)->count();
            $c22 = bjydb('todo')->where($map2)->where('work_type_mode',1)->count();
            $count = $c1 + $c2 + $c22;

            $c3 = Db::name('todo')->where($map)->where('work_type_mode',2)->count();
            $c4 = gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',2)->count();
            $c44 = bjydb('todo')->where($map2)->where('work_type_mode',2)->count();
            $count2 = $c3 + $c4 + $c44;

            $c5 = Db::name('todo')->where($map)->where('work_type_mode',3)->count();
            $c6 = gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',3)->count();
            $c66 = bjydb('todo')->where($map2)->where('work_type_mode',3)->count();
            $count3 = $c5 + $c6 + $c66;

            $c7 = Db::name('todo')->where($map)->where('work_type_mode',4)->count();
            $c8 = gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',4)->count();
            $c88 = bjydb('todo')->where($map2)->where('work_type_mode',4)->count();
            $count4 = $c7 + $c8 + $c88;

            $map[] = ['todo_mode','=',3];
            $where[] = ['TODO_MODE','=',3];
            $map2[] = ['todo_mode','=',3];

            //保修
            $o1 =  Db::name('todo')->where($map)->where('work_type_mode',1)->count();
            $o2 =  gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',1)->count();
            $o22 =  bjydb('todo')->where($map2)->where('work_type_mode',1)->count();
            $bxCount = $o1 + $o2 + $o22;
            //保洁
            $o3 =  Db::name('todo')->where($map)->where('work_type_mode',2)->count();
            $o4 =  gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',2)->count();
            $o44 =  bjydb('todo')->where($map2)->where('work_type_mode',2)->count();
            $bjCount = $o3 + $o4 + $o44;
            //运送
            $o5 =  Db::name('todo')->where($map)->where('work_type_mode',3)->count();
            $o6 =  gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',3)->count();
            $o66 =  bjydb('todo')->where($map2)->where('work_type_mode',3)->count();
            $ysCount = $o5 + $o6 + $o66;
            //隐患
            $o7 =  Db::name('todo')->where($map)->where('work_type_mode',4)->count();
            $o8 = gtspdb('todo')->where($where)->where('WORK_TYPE_MODE',4)->count();
            $o88 = bjydb('todo')->where($map2)->where('work_type_mode',4)->count();
            $yhCount = $o7 + $o8 + $o88;
        }else{
            $map[] = ['create_yyyymmdd','=',$date];
            $map[] = ['del','=',0];

            $count = Db::name('todo')->where($map)->where('work_type_mode',1)->where('org_id','in',$this->orgs)->count();
            $count2 = Db::name('todo')->where($map)->where('work_type_mode',2)->where('org_id','in',$this->orgs)->count();
            $count3 = Db::name('todo')->where($map)->where('work_type_mode',3)->where('org_id','in',$this->orgs)->count();
            $count4 = Db::name('todo')->where($map)->where('work_type_mode',4)->where('org_id','in',$this->orgs)->count();


            $map[] = ['todo_mode','=',3];

            //保修
            $bxCount =  Db::name('todo')->where($map)->where('work_type_mode',1)->where('org_id','in',$this->orgs)->count();
            //保洁
            $bjCount =  Db::name('todo')->where($map)->where('work_type_mode',2)->where('org_id','in',$this->orgs)->count();
            //运送
            $ysCount =  Db::name('todo')->where($map)->where('work_type_mode',3)->where('org_id','in',$this->orgs)->count();
            //隐患
            $yhCount =  Db::name('todo')->where($map)->where('work_type_mode',4)->where('org_id','in',$this->orgs)->count();


        }

        $bxBl = $bjBl = $ysBl = $yhBl =0;
        if($count > 0){
            $bxBl  = round($bxCount/$count*100,0);

        }
        if($count2 >0){
            $bjBl  = round($bjCount/$count2*100,0);
        }
        if($count3 >0){
            $ysBl  = round($ysCount/$count3*100,0);
        }
        if($count4 >0){
            $yhBl  = round($yhCount/$count4*100,0);
        }
        $data = [
            'bx'=>['count'=>$bxCount,'bl'=>$bxBl],
            'bj'=>['count'=>$bjCount,'bl'=>$bjBl],
            'ys'=>['count'=>$ysCount,'bl'=>$ysBl],
            'yh'=>['count'=>$yhCount,'bl'=>$yhBl],
        ];
        HelpHander::success($data);
    }

    public function moduleList(){

        if(in_array(3,$this->orgs) && !in_array(24,$this->orgs)){
            $map[] = ['del','=',0];
            $map[] = ['enable','=',1];
            $map[] = ['type','=',2];

            $orgList = Db::name('org')
                ->field('id,name')
                ->where('id','in',$this->orgs2)
                ->where($map)
                ->select();
            $bxCount = $bjCount = $ysCount = $yhCount = $pzCount = $xgCount = $phCount = 0;
            foreach ($orgList as $k=>$v){
                $bx = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',1)->find();
                if($bx){
                    $bxCount += 1;
                }
                $bj = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',2)->find();
                if($bj){
                    $bjCount += 1;
                }
                $ys = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',3)->find();
                if($ys){
                    $ysCount += 1;
                }
                $yh = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',4)->find();
                if($yh){
                    $yhCount += 1;
                }
                $xg = Db::name('patrol_record')->where('org_id',$v['id'])->where('patrol_mode',1)->count();
                if($xg){
                    $xgCount += 1;
                }
                $pzCount = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',15)->count();
                $phCount = Db::name('ph_todo')->where('org_id',$v['id'])->count();
            }

            $bx = gtspdb('todo')->where('ORG_ID',$this->spOrgId)->where('DEL_REF',0)->where('WORK_TYPE_MODE',1)->count();
            $bj = gtspdb('todo')->where('ORG_ID',$this->spOrgId)->where('DEL_REF',0)->where('WORK_TYPE_MODE',2)->count();
            $ys = gtspdb('todo')->where('ORG_ID',$this->spOrgId)->where('DEL_REF',0)->where('WORK_TYPE_MODE',3)->count();
            $yh = gtspdb('todo')->where('ORG_ID',$this->spOrgId)->where('DEL_REF',0)->where('WORK_TYPE_MODE',4)->count();
            $xg = gtspdb('patrol_record')->where('ORG_ID',$this->spOrgId)->where('PATROL_MODE',1)->count();

            $data = [
                ['name'=>'保修','value'=>$bxCount+$bx],
                ['name'=>'保洁','value'=>$bjCount+$bj],
                ['name'=>'运送','value'=>$ysCount+$ys],
                ['name'=>'隐患预警','value'=>$yhCount+$yh],
                ['name'=>'巡更','value'=>$xgCount+$xg],
                ['name'=>'陪护','value'=>$phCount],
                ['name'=>'品质整改','value'=>$pzCount],
            ];
        }elseif(!in_array(3,$this->orgs) && in_array(24,$this->orgs)){
            $map[] = ['del','=',0];
            $map[] = ['enable','=',1];
            $map[] = ['type','=',2];

            $orgList = Db::name('org')
                ->field('id,name')
                ->where('id','in',$this->orgs2)
                ->where($map)
                ->select();
            $bxCount = $bjCount = $ysCount = $yhCount = $pzCount = $xgCount = $phCount = 0;
            foreach ($orgList as $k=>$v){
                $bxCount = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',1)->count();
                $bjCount = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',2)->count();

                $ysCount = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',3)->count();

                $yhCount = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',4)->count();

                $xgCount = Db::name('patrol_record')->where('org_id',$v['id'])->where('patrol_mode',1)->count();

                $pzCount = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',15)->count();
                $phCount = Db::name('ph_todo')->where('org_id',$v['id'])->count();
            }

            $bx = bjydb('todo')->where('org_id',$this->bjOrgId)->where('del',0)->where('work_type_mode',1)->count();
            $bj = bjydb('todo')->where('org_id',$this->bjOrgId)->where('del',0)->where('work_type_mode',2)->count();
            $ys = bjydb('todo')->where('org_id',$this->bjOrgId)->where('del',0)->where('work_type_mode',3)->count();
            $yh = bjydb('todo')->where('org_id',$this->bjOrgId)->where('del',0)->where('work_type_mode',4)->count();
            $xg = bjydb('patrol_record')->where('org_id',$this->bjOrgId)->where('patrol_mode',1)->count();

            $data = [
                ['name'=>'保修','value'=>$bxCount+$bx],
                ['name'=>'保洁','value'=>$bjCount+$bj],
                ['name'=>'运送','value'=>$ysCount+$ys],
                ['name'=>'隐患预警','value'=>$yhCount+$yh],
                ['name'=>'巡更','value'=>$xgCount+$xg],
                ['name'=>'陪护','value'=>$phCount],
                ['name'=>'品质整改','value'=>$pzCount],
            ];
        }elseif(in_array(3,$this->orgs) && in_array(24,$this->orgs)){
            $map[] = ['del','=',0];
            $map[] = ['enable','=',1];
            $map[] = ['type','=',2];

            $orgList = Db::name('org')
                ->field('id,name')
                ->where('id','in',$this->orgs2)
                ->where($map)
                ->select();
            $bxCount = $bjCount = $ysCount = $yhCount = $pzCount = $xgCount = $phCount = 0;
            foreach ($orgList as $k=>$v){
                $bxCount = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',1)->count();
                $bjCount = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',2)->count();

                $ysCount = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',3)->count();

                $yhCount = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',4)->count();

                $xgCount = Db::name('patrol_record')->where('org_id',$v['id'])->where('patrol_mode',1)->count();

                $pzCount = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',15)->count();
                $phCount = Db::name('ph_todo')->where('org_id',$v['id'])->count();
            }

            $bx = bjydb('todo')->where('org_id',$this->bjOrgId)->where('del',0)->where('work_type_mode',1)->count();
            $bj = bjydb('todo')->where('org_id',$this->bjOrgId)->where('del',0)->where('work_type_mode',2)->count();
            $ys = bjydb('todo')->where('org_id',$this->bjOrgId)->where('del',0)->where('work_type_mode',3)->count();
            $yh = bjydb('todo')->where('org_id',$this->bjOrgId)->where('del',0)->where('work_type_mode',4)->count();
            $xg = bjydb('patrol_record')->where('org_id',$this->bjOrgId)->where('patrol_mode',1)->count();

            $bx2 = bjydb('todo')->where('org_id',$this->bjOrgId)->where('del',0)->where('work_type_mode',1)->count();
            $bj2 = bjydb('todo')->where('org_id',$this->bjOrgId)->where('del',0)->where('work_type_mode',2)->count();
            $ys2 = bjydb('todo')->where('org_id',$this->bjOrgId)->where('del',0)->where('work_type_mode',3)->count();
            $yh2 = bjydb('todo')->where('org_id',$this->bjOrgId)->where('del',0)->where('work_type_mode',4)->count();
            $xg2 = bjydb('patrol_record')->where('org_id',$this->bjOrgId)->where('patrol_mode',1)->count();

            $data = [
                ['name'=>'保修','value'=>$bxCount+$bx+$bx2],
                ['name'=>'保洁','value'=>$bjCount+$bj+$bj2],
                ['name'=>'运送','value'=>$ysCount+$ys+$ys2],
                ['name'=>'隐患预警','value'=>$yhCount+$yh+$yh2],
                ['name'=>'巡更','value'=>$xgCount+$xg+$xg2],
                ['name'=>'陪护','value'=>$phCount],
                ['name'=>'品质整改','value'=>$pzCount],
            ];
        }else{
            $map[] = ['del','=',0];
            $map[] = ['enable','=',1];
            $map[] = ['type','=',2];

            $orgList = Db::name('org')
                ->field('id,name')
                ->where('id','in',$this->orgs)
                ->where($map)
                ->select();
            $bxCount = $bjCount = $ysCount = $yhCount = $pzCount = $xgCount = $phCount = 0;
            foreach ($orgList as $k=>$v){
                $bx = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',1)->find();
                if($bx){
                    $bxCount += 1;
                }
                $bj = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',2)->find();
                if($bj){
                    $bjCount += 1;
                }
                $ys = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',3)->find();
                if($ys){
                    $ysCount += 1;
                }
                $yh = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',4)->find();
                if($yh){
                    $yhCount += 1;
                }
                $pz = Db::name('todo')->where('org_id',$v['id'])->where('del',0)->where('work_type_mode',15)->find();
                if($pz){
                    $pzCount += 1;
                }
                $xg = Db::name('patrol_record')->where('org_id',$v['id'])->where('patrol_mode',1)->count();
                if($xg){
                    $xgCount += 1;
                }
                $ph = Db::name('ph_todo')->where('org_id',$v['id'])->count();
                if($ph){
                    $phCount += 1;
                }
            }
            $data = [
                ['name'=>'保修','value'=>$bxCount],
                ['name'=>'保洁','value'=>$bjCount],
                ['name'=>'运送','value'=>$ysCount],
                ['name'=>'隐患预警','value'=>$yhCount],
                ['name'=>'品质整改','value'=>$pzCount],
                ['name'=>'巡更','value'=>$xgCount],
                ['name'=>'陪护','value'=>$phCount],
            ];
        }



        HelpHander::success($data);

    }

    public function commentComplainList(){
        $date =  [
            date('Y',strtotime('-5 years')),
            date('Y',strtotime('-4 years')),
            date('Y',strtotime('-3 years')),
            date('Y',strtotime('-2 years')),
            date('Y',strtotime('-1 years')),
            date('Y'),
        ];
        $list = $list2 = [];
        if(in_array(3,$this->orgs) && in_array(24,$this->orgs) ){
            foreach ($date as $k=>$v){
                $c1 = Db::name('comment')->where('org_id','in',$this->orgs2)->where('create_yyyy',$v)->sum('score');
                $c2 = gtspdb('comment')->where('ORG_ID',$this->spOrgId)->where('CREATE_YYYY',$v)->sum('SCORE');
                $c22 = bjydb('comment')->where('org_id',$this->bjOrgId)->where('create_yyyy',$v)->sum('score');
                $list[] = $c1+$c2+$c22;
                $c3 = Db::name('complain')->where('org_id','in',$this->orgs2)->where('create_yyyy',$v)->where('del',0)->count();
                $c4 = gtspdb('complain')->where('ORG_ID',$this->spOrgId)->where('CREATE_YYYY',$v)->where('DEL_REF',0)->count();
                $c44 = bjydb('complain')->where('org_id',$this->bjOrgId)->where('create_yyyy',$v)->where('del',0)->count();
                $list2[] = $c3+$c4+$c44;
            }
        }elseif(in_array(3,$this->orgs) && !in_array(24,$this->orgs) ){
            foreach ($date as $k=>$v){
                $c1 = Db::name('comment')->where('org_id','in',$this->orgs2)->where('create_yyyy',$v)->sum('score');
                $c2 = gtspdb('comment')->where('ORG_ID',$this->spOrgId)->where('CREATE_YYYY',$v)->sum('SCORE');
                $list[] = $c1+$c2;
                $c3 = Db::name('complain')->where('org_id','in',$this->orgs2)->where('create_yyyy',$v)->where('del',0)->count();
                $c4 = gtspdb('complain')->where('ORG_ID',$this->spOrgId)->where('CREATE_YYYY',$v)->where('DEL_REF',0)->count();
                $list2[] = $c3+$c4;
            }
        }elseif(!in_array(3,$this->orgs) && in_array(24,$this->orgs) ){
            foreach ($date as $k=>$v){
                $c1 = Db::name('comment')->where('org_id','in',$this->orgs2)->where('create_yyyy',$v)->sum('score');
                $c2 = bjydb('comment')->where('org_id',$this->bjOrgId)->where('create_yyyy',$v)->sum('score');
                $list[] = $c1+$c2;
                $c3 = Db::name('complain')->where('org_id','in',$this->orgs2)->where('create_yyyy',$v)->where('del',0)->count();
                $c4 = bjydb('complain')->where('org_id',$this->bjOrgId)->where('create_yyyy',$v)->where('del',0)->count();
                $list2[] = $c3+$c4;
            }
        }else{
            foreach ($date as $k=>$v){
                $list[] = Db::name('comment')->where('org_id','in',$this->orgs2)->where('create_yyyy',$v)->sum('score');
                $list2[] = Db::name('complain')->where('org_id','in',$this->orgs2)->where('create_yyyy',$v)->where('del',0)->count();
            }

        }

        $data = [
            'title'=>$date,
            'list1'=>$list,
            'list2'=>$list2,
        ];

        HelpHander::success($data);

    }

    public function projectCount(){

        if(in_array(3,$this->orgs) && !in_array(24,$this->orgs)){
            $count = Db::name('org')->where('parent_id',1)->where('del',0)->where('enable',1)->where('type',1)->count();

            $count1 =  Db::name('org')->where('del',0)->where('enable',1)->where('type',2)->count();

            $c1 = Db::name('user')
                ->alias('u')
                ->join('user_org uo','uo.user_id=u.id')
                ->join('org o','o.id=uo.org_id')
                ->where('u.del',0)
                ->where('u.type',0)
                ->where('o.enable',1)
                ->where('o.del',0)
                ->count();

            $c2 = gtspdb('user')
                ->alias('u')
                ->join('user_org uo','uo.USER_ID=u.USER_ID')
                ->where('u.DEL_REF',0)
                ->where('u.TYPE',0)
                ->where('uo.ORG_ID','in',$this->spOrgId)
                ->where('uo.ORG_TYPE',0)
                ->count();

            $count2 = $c1 + $c2;

            $oc1 = Db::name('orders')
                ->where('del',0)
                ->where('org_id','in',$this->orgs2)
                ->count();

            $oc2 = gtspdb('orders')
                ->where('DEL_REF',0)
                ->where('ORG_ID',$this->spOrgId)
                ->count();

            $orderCount = $oc1 + $oc2;

            $tc1= Db::name('todo')
                ->where('del',0)
                ->where('org_id','in',$this->orgs2)
                ->count();

            $tc2 = gtspdb('todo')
                ->where('DEL_REF',0)
                ->where('ORG_ID',$this->spOrgId)
                ->count();

            $todoCount =  $tc1 + $tc2;


            $t1 = Db::name('patrol_task')
                ->where('del',0)
                ->where('org_id','in',$this->orgs2)
                ->count();
            $t2 = gtspdb('patrol_task')
                ->where('DEL_REF',0)
                ->where('ORG_ID',$this->spOrgId)
                ->count();
            $taskCount =  $t1 + $t2;



        }elseif(!in_array(3,$this->orgs) && in_array(24,$this->orgs)){
            $count = Db::name('org')->where('parent_id',1)->where('del',0)->where('enable',1)->where('type',1)->count();

            $count1 =  Db::name('org')->where('del',0)->where('enable',1)->where('type',2)->count();

            $c1 = Db::name('user')
                ->alias('u')
                ->join('user_org uo','uo.user_id=u.id')
                ->join('org o','o.id=uo.org_id')
                ->where('u.del',0)
                ->where('u.type',0)
                ->where('o.enable',1)
                ->where('o.del',0)
                ->count();

            $c2 = bjydb('user')
                ->alias('u')
                ->join('user_org uo','uo.user_id=u.id')
                ->join('org o','o.id=uo.org_id')
                ->where('u.del',0)
                ->where('u.type',0)
                ->where('o.enable',1)
                ->where('o.del',0)
                ->where('o.id',$this->bjOrgId)
                ->count();

            $count2 = $c1 + $c2;

            $oc1 = Db::name('orders')
                ->where('del',0)
                ->where('org_id','in',$this->orgs2)
                ->count();

            $oc2 = bjydb('orders')
                ->where('del',0)
                ->where('org_id',$this->bjOrgId)
                ->count();

            $orderCount = $oc1 + $oc2;

            $tc1= Db::name('todo')
                ->where('del',0)
                ->where('org_id','in',$this->orgs2)
                ->count();

            $tc2 = bjydb('todo')
                ->where('del',0)
                ->where('org_id',$this->bjOrgId)
                ->count();

            $todoCount =  $tc1 + $tc2;


            $t1 = Db::name('patrol_task')
                ->where('del',0)
                ->where('org_id','in',$this->orgs2)
                ->count();
            $t2 = bjydb('patrol_task')
                ->where('del',0)
                ->where('org_id',$this->bjOrgId)
                ->count();
            $taskCount =  $t1 + $t2;

        }elseif(in_array(3,$this->orgs) && in_array(24,$this->orgs)){
            $count = Db::name('org')->where('parent_id',1)->where('del',0)->where('enable',1)->where('type',1)->count();

            $count1 =  Db::name('org')->where('del',0)->where('enable',1)->where('type',2)->count();

            $c1 = Db::name('user')
                ->alias('u')
                ->join('user_org uo','uo.user_id=u.id')
                ->join('org o','o.id=uo.org_id')
                ->where('u.del',0)
                ->where('u.type',0)
                ->where('o.enable',1)
                ->where('o.del',0)
                ->count();

            $c2 = gtspdb('user')
                ->alias('u')
                ->join('user_org uo','uo.USER_ID=u.USER_ID')
                ->where('u.DEL_REF',0)
                ->where('u.TYPE',0)
                ->where('uo.ORG_ID','in',$this->spOrgId)
                ->where('uo.ORG_TYPE',0)
                ->count();
            $c22 = bjydb('user')
                ->alias('u')
                ->join('user_org uo','uo.user_id=u.id')
                ->join('org o','o.id=uo.org_id')
                ->where('u.del',0)
                ->where('u.type',0)
                ->where('o.enable',1)
                ->where('o.del',0)
                ->where('o.id',$this->bjOrgId)
                ->count();

            $count2 = $c1 + $c2 + $c22;

            $oc1 = Db::name('orders')
                ->where('del',0)
                ->where('org_id','in',$this->orgs2)
                ->count();

            $oc2 = gtspdb('orders')
                ->where('DEL_REF',0)
                ->where('ORG_ID',$this->spOrgId)
                ->count();

            $oc3 = bjydb('orders')
                ->where('del',0)
                ->where('org_id',$this->bjOrgId)
                ->count();

            $orderCount = $oc1 + $oc2 + $oc3;

            $tc1= Db::name('todo')
                ->where('del',0)
                ->where('org_id','in',$this->orgs2)
                ->count();

            $tc2 = gtspdb('todo')
                ->where('DEL_REF',0)
                ->where('ORG_ID',$this->spOrgId)
                ->count();

            $tc3 = bjydb('todo')
                ->where('del',0)
                ->where('org_id',$this->bjOrgId)
                ->count();

            $todoCount =  $tc1 + $tc2 + $tc3;


            $t1 = Db::name('patrol_task')
                ->where('del',0)
                ->where('org_id','in',$this->orgs2)
                ->count();
            $t2 = gtspdb('patrol_task')
                ->where('DEL_REF',0)
                ->where('ORG_ID',$this->spOrgId)
                ->count();
            $t3 = bjydb('patrol_task')
                ->where('del',0)
                ->where('org_id',$this->bjOrgId)
                ->count();
            $taskCount =  $t1 + $t2 + $t3;

        }else{
            $count = Db::name('org')->where('parent_id',1)->where('del',0)->where('enable',1)->where('type',1)->count();

            $count1 =  Db::name('org')->where('del',0)->where('enable',1)->where('type',2)->count();

            $count2 = Db::name('user')
                ->alias('u')
                ->join('user_org uo','uo.user_id=u.id')
                ->join('org o','o.id=uo.org_id')
                ->where('u.del',0)
                ->where('u.type',0)
                ->where('o.enable',1)
                ->where('o.del',0)
                ->count();

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

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

            $task1 = Db::name('patrol_task')
                ->where('del',0)
                ->count();

            $task2 = Db::name('device_task')
                ->where('del',0)
                ->count();

            $taskCount =  $task1 + $task2;

        }



        $data = [
            'count1'=>$count,
            'count2'=>$count1,
            'count3'=>$count2,
            'count4'=>$orderCount,
            'count5'=>$todoCount,
            'count6'=>$taskCount,
            'count7'=>23
        ];
        HelpHander::success($data);
    }

    public function cityList(){

        $org = Db::name('org')
            ->alias('o')
            ->field('o.city_id,c.title')
            ->join('city c','c.id=o.city_id')
            ->where('o.del',0)
            ->where('o.enable',1)
            ->where('o.type',2)
            ->group('o.city_id')
            ->select();

        foreach ($org as $k=>$v){
            $list = Db::name('org')->where('del',0)->where('enable',1)->where('type',2)->where('city_id',$v['city_id'])->select();
            foreach ($list as $kk=>$vv){
                $list[$kk]['id'] = think_encrypt($vv['id'].'|'.time());
            }
            $org[$k]['list'] = $list?$list:[];
        }

        HelpHander::success($org);
    }

    public function quarterScore(){


        $org = Db::name('org')
            ->field('id,name')
            ->where('parent_id',1)
            ->where('del',0)
            ->where('enable',1)
            ->where('type',1)
            ->select();

        foreach ($org as $k=>$v){
            $org[$k]['orgs'] =  $this->getAllNextId($v['id']);
        }

        //获取当前季度
        $season = ceil((date('n'))/3);
        //当前季度开始时间戳
        $startTime = date('Y-m-d H:i:s', mktime(0,0,0,$season*3-3+1,1,date('Y')));
        //获取当前季度结束时间戳
        $overTime = date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0,0,0,$season*3,1,date("Y"))),date('Y')));

        $where[] = ['create_yyyymm','>=',date('Ym',strtotime($startTime))];
        $where[] = ['create_yyyymm','<=',date('Ym',strtotime($overTime))];
        $where2[] = ['CREATE_YYYYMM','>=',date('Ym',strtotime($startTime))];
        $where2[] = ['CREATE_YYYYMM','<=',date('Ym',strtotime($overTime))];
        foreach ($org as $k=>$v){
            if(in_array(3,$v['orgs']) && !in_array(24,$v['orgs'])){
                $c1 = gtspdb('comment')->where('ORG_ID',$this->spOrgId)->where($where2)->sum('SCORE');
                $c2 = Db::name('comment')->whereIn('org_id',$v['orgs'])->where('org_id','<>',3)->where($where)->sum('score');

                $org[$k]['score'] = $c1 + $c2;
            }elseif(!in_array(3,$v['orgs']) && in_array(24,$v['orgs'])){
                $c1 = bjydb('comment')->where('org_id',$this->bjOrgId)->where($where)->sum('SCORE');
                $c2 = Db::name('comment')->whereIn('org_id',$v['orgs'])->where('org_id','<>',3)->where($where)->sum('score');

                $org[$k]['score'] = $c1 + $c2;
            }elseif(in_array(3,$v['orgs']) && in_array(24,$v['orgs'])){
                $c1 = bjydb('comment')->where('org_id',$this->bjOrgId)->where($where)->sum('SCORE');
                $c2 = Db::name('comment')->whereIn('org_id',$v['orgs'])->where('org_id','<>',3)->where($where)->sum('score');

                $c3 = gtspdb('comment')->where('ORG_ID',$this->spOrgId)->where($where2)->sum('SCORE');

                $org[$k]['score'] = $c1 + $c2 + $c3;
            }else{
                $org[$k]['score'] = Db::name('comment')->whereIn('org_id',$v['orgs'])->where($where)->sum('score');
            }
        }
        $list = list_sort_by($org,'score','desc');

        $top1 = isset($list[0])?$list[0]:null;
        $top2 = isset($list[1])?$list[1]:null;
        $top3 = isset($list[2])?$list[2]:null;
        $top4 = isset($list[3])?$list[3]:null;
        $top5 = isset($list[4])?$list[4]:null;


        HelpHander::success(['top1'=>$top1,'top2'=>$top2,'top3'=>$top3,'top4'=>$top4,'top5'=>$top5]);
    }


    public function quarterScore2(){
        $map[] = ['del','=',0];
        $map[] = ['enable','=',1];
        $map[] = ['type','=',2];

        $org = Db::name('org')
            ->field('id,name')
            ->where('parent_id',1)
            ->where('del',0)
            ->where('enable',1)
            ->where('type',1)
            ->select();

        foreach ($org as $k=>$v){
            $org[$k]['orgs'] =  $this->getAllNextId($v['id']);
        }

        //获取当前季度
        $season = ceil((date('n'))/3);
        //当前季度开始时间戳
        $startTime = date('Y-m-d H:i:s', mktime(0,0,0,$season*3-3+1,1,date('Y')));
        //获取当前季度结束时间戳
        $overTime = date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0,0,0,$season*3,1,date("Y"))),date('Y')));


        $where[] = ['create_yyyymm','>=',date('Ym',strtotime($startTime))];
        $where[] = ['create_yyyymm','<=',date('Ym',strtotime($overTime))];
        foreach ($org as $k=>$v){
            if(in_array(3,$v['orgs']) ){
                $org[$k]['price'] = 5.3;
            }else{
                $org[$k]['price'] = 0;
            }

        }
        $list = list_sort_by($org,'score','desc');

        $top1 = isset($list[0])?$list[0]:null;
        $top2 = isset($list[1])?$list[1]:null;
        $top3 = isset($list[2])?$list[2]:null;
        $top4 = isset($list[3])?$list[3]:null;
        $top5 = isset($list[4])?$list[4]:null;

        HelpHander::success(['top1'=>$top1,'top2'=>$top2,'top3'=>$top3,'top4'=>$top4,'top5'=>$top5]);
    }

    public function fcompanyList(){
        $map[] = ['del','=',0];
        $map[] = ['enable','=',1];
        $map[] = ['type','=',2];

        $org = Db::name('org')
            ->field('id,name')
            ->where('parent_id',1)
            ->where('del',0)
            ->where('enable',1)
            ->where('type',1)
            ->select();

        foreach ($org as $k=>$v){
            $org[$k]['orgs'] =  $this->getAllNextId($v['id']);
        }

        $host = request()->domain(true);
        foreach ($org as $k=>$v){
            if(in_array(3,$v['orgs'])){
                $org[$k]['day'] = 256;
                $org[$k]['nums'] = 1;
                $org[$k]['nums2'] = 1;
                $org[$k]['bl'] = 100;
                $org[$k]['imgs'] = [
                    $host.'/screen/images/nh1.png',
                    $host.'/screen/images/nh2.png',
                    $host.'/screen/images/nh3.png',
                ];
            }else{
                $org[$k]['day'] = 0;
                $org[$k]['nums'] = 0;
                $org[$k]['nums2'] = 0;
                $org[$k]['bl'] = 0;
                $org[$k]['imgs'] = [];
            }

        }

        HelpHander::success($org);
    }

    public function nlxhList(){
        $org = Db::name('org')
            ->field('id,name')
            ->where('parent_id',1)
            ->where('del',0)
            ->where('enable',1)
            ->where('type',1)
            ->select();

        foreach ($org as $k=>$v){
            $org[$k]['orgs'] =  $this->getAllNextId($v['id']);
            if(isset($k) && $k==0){
                $org[$k]['value'] =  556;
            }else if(isset($k) && $k==1){
                $org[$k]['value'] =  875;
            }else if(isset($k) && $k==2){
                $org[$k]['value'] =  345;
            }else if(isset($k) && $k==3){
                $org[$k]['value'] =  234;
            }else if(isset($k) && $k==4){
                $org[$k]['value'] =  797;
            }else if(isset($k) && $k==5) {
                $org[$k]['value'] = 887;
            }
        }



        //获取当前季度
        $season = ceil((date('n'))/3);
        //当前季度开始时间戳
        $startTime = date('Y-m-d H:i:s', mktime(0,0,0,$season*3-3+1,1,date('Y')));
        //获取当前季度结束时间戳
        $overTime = date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0,0,0,$season*3,1,date("Y"))),date('Y')));


        $list = array_slice(list_sort_by($org,'value','desc'),0,5);

        HelpHander::success($list);

    }


    //巡查图片
    public function patrolImg(){
        if(in_array(3,$this->orgs) && !in_array(24,$this->orgs)){
            $data = gtspdb('patrol_record')
                ->where('IMAGES','<>','')
                ->where('ORG_ID',$this->spOrgId)
                ->limit(20)
                ->order('PATROL_RECORD_ID desc')
                ->select();

            foreach ($data as $k=>$v){
                $img = explode(',',$v['IMAGES']);
                if($img){
                    $data[$k]['images'] = $img[0];
                }
            }
            if(count($data)>2){
                $key = array_rand($data,3);
                $list[] = $data[$key[0]];
                $list[] = $data[$key[1]];
                $list[] = $data[$key[2]];
            }

        }elseif(!in_array(3,$this->orgs) && in_array(24,$this->orgs)){
            $data = bjydb('patrol_record')
                ->where('images','<>','')
                ->where('org_id',$this->bjOrgId)
                ->limit(20)
                ->order('id desc')
                ->select();

            foreach ($data as $k=>$v){
                $img = explode(',',$v['images']);
                if($img){
                    $data[$k]['images'] = $img[0];
                }
            }
            if(count($data)>2){
                $key = array_rand($data,3);
                $list[] = $data[$key[0]];
                $list[] = $data[$key[1]];
                $list[] = $data[$key[2]];
            }

        }elseif(in_array(3,$this->orgs) && in_array(24,$this->orgs)){
            $data1 = bjydb('patrol_record')
                ->where('images','<>','')
                ->where('org_id',$this->bjOrgId)
                ->limit(20)
                ->order('id desc')
                ->select();
            foreach ($data1 as $k=>$v){
                $img = explode(',',$v['images']);
                if($img){
                    $data1[$k]['images'] = $img[0];
                }
            }

            $data2 = gtspdb('patrol_record')
                ->where('IMAGES','<>','')
                ->where('ORG_ID',$this->spOrgId)
                ->limit(20)
                ->order('PATROL_RECORD_ID desc')
                ->select();

            foreach ($data2 as $k=>$v){
                $img = explode(',',$v['IMAGES']);
                if($img){
                    $data2[$k]['images'] = $img[0];
                }
            }
            $data =  array_merge($data1,$data2);

            if(count($data)>2){
                $key = array_rand($data,3);
                $list[] = $data[$key[0]];
                $list[] = $data[$key[1]];
                $list[] = $data[$key[2]];
            }

        }else{
            $data = Db::name('patrol_record')
                ->where('images','<>','')
                ->whereIn('org_id',$this->orgs2)
                ->limit(20)
                ->order('id desc')
                ->select();

            foreach ($data as $k=>$v){
                $img = explode(',',$v['images']);
                if($img){
                    $data[$k]['images'] = $img[0];
                }
            }
            if(count($data)>2){
                $key = array_rand($data,3);
                $list[] = $data[$key[0]];
                $list[] = $data[$key[1]];
                $list[] = $data[$key[2]];
            }

        }
        $eList = isset($list)?$list:$data;
        HelpHander::success($eList);
    }

    public function patrolRecordList(){
        $dlist = [
            date('Y-m-d',strtotime('-6 days')),
            date('Y-m-d',strtotime('-5 days')),
            date('Y-m-d',strtotime('-4 days')),
            date('Y-m-d',strtotime('-3 days')),
            date('Y-m-d',strtotime('-2 days')),
            date('Y-m-d',strtotime('-1 days')),
            date('Y-m-d'),
        ];
        $date = [];
        $yCount = [];
        $y2Count = [];
        $y3Count = [];
        $y4Count = [];
        if(in_array(3,$this->orgs) && !in_array(24,$this->orgs)){
            foreach ($dlist as $k=>$v){
                $tt = date('Ymd',strtotime($v));
                $c1 = gtspdb('patrol_record')
                    ->where('ORG_ID',$this->spOrgId)
                    ->where('CREATE_YYYYMMDD',$tt)
                    ->where('PATROL_MODE',1)
                    ->count();
                $c2 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs2)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',1)
                    ->count();
                $count = $c1 + $c2;
                $c3 = gtspdb('patrol_record')
                    ->where('ORG_ID',$this->spOrgId)
                    ->where('CREATE_YYYYMMDD',$tt)
                    ->where('PATROL_MODE',2)
                    ->count();
                $c4 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs2)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',2)
                    ->count();
                $count2 = $c3 + $c4;
                $c5 = gtspdb('patrol_record')
                    ->where('ORG_ID',$this->spOrgId)
                    ->where('CREATE_YYYYMMDD',$tt)
                    ->where('PATROL_MODE',3)
                    ->count();
                $c6 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs2)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',3)
                    ->count();
                $count3 = $c5 + $c6;
                $c7 = gtspdb('patrol_record')
                    ->where('ORG_ID',$this->spOrgId)
                    ->where('CREATE_YYYYMMDD',$tt)
                    ->where('PATROL_MODE',4)
                    ->count();
                $c8 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',4)
                    ->count();
                $count4 = $c7 + $c8;
                $date[] =  date('m-d',strtotime($v));
                $yCount[] = $count;
                $y2Count[] = $count2;
                $y3Count[] = $count3;
                $y4Count[] = $count4;
            }

        }elseif(!in_array(3,$this->orgs) && in_array(24,$this->orgs)){
            foreach ($dlist as $k=>$v){
                $tt = date('Ymd',strtotime($v));
                $c1 = bjydb('patrol_record')
                    ->where('org_id',$this->bjOrgId)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',1)
                    ->count();
                $c2 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs2)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',1)
                    ->count();
                $count = $c1 + $c2;
                $c3 = bjydb('patrol_record')
                    ->where('org_id',$this->bjOrgId)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',2)
                    ->count();
                $c4 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs2)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',2)
                    ->count();
                $count2 = $c3 + $c4;
                $c5 = bjydb('patrol_record')
                    ->where('org_id',$this->bjOrgId)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',3)
                    ->count();
                $c6 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs2)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',3)
                    ->count();
                $count3 = $c5 + $c6;
                $c7 = bjydb('patrol_record')
                    ->where('org_id',$this->bjOrgId)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',1)
                    ->count();
                $c8 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',4)
                    ->count();
                $count4 = $c7 + $c8;
                $date[] =  date('m-d',strtotime($v));
                $yCount[] = $count;
                $y2Count[] = $count2;
                $y3Count[] = $count3;
                $y4Count[] = $count4;
            }

        }elseif(in_array(3,$this->orgs) && in_array(24,$this->orgs)){
            foreach ($dlist as $k=>$v){
                $tt = date('Ymd',strtotime($v));
                $c1 = gtspdb('patrol_record')
                    ->where('ORG_ID',$this->spOrgId)
                    ->where('CREATE_YYYYMMDD',$tt)
                    ->where('PATROL_MODE',1)
                    ->count();
                $c2 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs2)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',1)
                    ->count();
                $c22 = bjydb('patrol_record')
                    ->where('org_id',$this->bjOrgId)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',1)
                    ->count();
                $count = $c1 + $c2 + $c22;
                $c3 = gtspdb('patrol_record')
                    ->where('ORG_ID',$this->spOrgId)
                    ->where('CREATE_YYYYMMDD',$tt)
                    ->where('PATROL_MODE',2)
                    ->count();
                $c4 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs2)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',2)
                    ->count();
                $c44 = bjydb('patrol_record')
                    ->where('org_id',$this->bjOrgId)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',2)
                    ->count();
                $count2 = $c3 + $c4 + $c44;
                $c5 = gtspdb('patrol_record')
                    ->where('ORG_ID',$this->spOrgId)
                    ->where('CREATE_YYYYMMDD',$tt)
                    ->where('PATROL_MODE',3)
                    ->count();
                $c6 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs2)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',3)
                    ->count();
                $c66 = bjydb('patrol_record')
                    ->where('org_id',$this->bjOrgId)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',3)
                    ->count();
                $count3 = $c5 + $c6 + $c66;
                $c7 = gtspdb('patrol_record')
                    ->where('ORG_ID',$this->spOrgId)
                    ->where('CREATE_YYYYMMDD',$tt)
                    ->where('PATROL_MODE',4)
                    ->count();
                $c8 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',4)
                    ->count();
                $c88 = bjydb('patrol_record')
                    ->where('org_id',$this->bjOrgId)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',4)
                    ->count();
                $count4 = $c7 + $c8 + $c88;
                $date[] =  date('m-d',strtotime($v));
                $yCount[] = $count;
                $y2Count[] = $count2;
                $y3Count[] = $count3;
                $y4Count[] = $count4;
            }

        }else{
            foreach ($dlist as $k=>$v){
                $tt = date('Ymd',strtotime($v));
                $count = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',1)
                    ->count();
                $count2 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',2)
                    ->count();
                $count3 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',3)
                    ->count();
                $count4 = Db::name('patrol_record')
                    ->whereIn('org_id',$this->orgs)
                    ->where('create_yyyymmdd',$tt)
                    ->where('patrol_mode',4)
                    ->count();
                $date[] =  date('m-d',strtotime($v));
                $yCount[] = $count;
                $y2Count[] = $count2;
                $y3Count[] = $count3;
                $y4Count[] = $count4;
            }

        }
        $data = [
            'date'=>$date,
            'count1'=>$yCount,
            'count2'=>$y2Count,
            'count3'=>$y3Count,
            'count4'=>$y4Count,
        ];
        HelpHander::success($data);
    }


    // 获取所有下级的id集合
    public function getAllNextId($id,$data=[]){
        $pids = DB::name('org')->where('parent_id',$id)->column('id');
        if(count($pids)>0){
            foreach($pids as $v){
                $data[] = $v;
                $data = $this->getAllNextId($v,$data); //注意写$data 返回给上级
            }
        }
        if(count($data)>0){
            return $data;
        }else{
            return [];
        }
    }

    public function getOrgs(){
        $img1 = request()->domain(true).'/screen/images/hz01.png';
        $img2 = request()->domain(true).'/screen/images/hz02.png';
        $lists1 = [
            [
                'id'=>think_encrypt('3|'.time()),
                'title'=>'新乡市中心医院',
                'img'=> $img1
            ],
            [
//                'id'=>think_encrypt('24|'.time()),
                'id'=>'',
                'title'=>'医院1',
                'img'=>$img2
            ],
            [
//                'id'=>think_encrypt('26|'.time()),
                'id'=>'',
                'title'=>'医院2',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院3',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院4',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院5',
                'img'=>$img2
            ],
        ];

        $lists2 = [
            [
                'id'=>0,
                'title'=>'医院11',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院12',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院13',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院14',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院15',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院16',
                'img'=>$img2
            ],
        ];

        $lists3 = [
            [
                'id'=>0,
                'title'=>'医院12',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院13',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院14',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院15',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院16',
                'img'=>$img2
            ],
        ];
        $lists4 = [
            [
                'id'=>0,
                'title'=>'医院6',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院7',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院8',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院9',
                'img'=>$img2
            ],
            [
                'id'=>0,
                'title'=>'医院10',
                'img'=>$img2
            ],

        ];

        HelpHander::success([
            'lists1'=>$lists1,
            'lists2'=>$lists2,
            'lists3'=>$lists3,
            'lists4'=>$lists4,
        ]);
    }

    public function dailyData(){
        $lists = [
            [
                'id'=>2,
                'title'=>'济南',
            ],
            [
                'id'=>8,
                'title'=>'青岛',
            ],
            [
                'id'=>10,
                'title'=>'烟台',
            ],
            [
                'id'=>11,
                'title'=>'威海',
            ],
            [
                'id'=>12,
                'title'=>'东营',
            ],
            [
                'id'=>13,
                'title'=>' 淄博',
            ],
        ];

        $orgs = [];
        $pzdata = [];
        $rwdata = [];
        $max = 0;
        foreach ($lists as $k=>$v){
            $map = [];
            $orgs[] = $v['title'];
            if($v['id'] == 0){
                $pzdata[] = mt_rand(0,10);
                $rwdata[] = mt_rand(0,$max);
            }else{
                $orgIds = $this->getAllNextId($v['id'],[]);
                if($orgIds){
                    $map[] = ['org_id','in',$orgIds];
                }else{
                    $map[] = ['org_id','=',-1];
                }
                $pzcount = Db::name('daily_record')->where($map)->where('order_id','>',0)->count();
                $pzdata[] = $pzcount;

                $rwcount = Db::name('daily_record')->where($map)->count();
                if($rwcount > $max){
                    $max = $rwcount;
                }
                $rwdata[] = $rwcount;
            }
        }

        HelpHander::success([
            'orgs'=>$orgs,
            'pzdata'=>$pzdata,
            'rwdata'=>$rwdata,
        ]);
    }

    public function todoData(){
        $lists = [
            [
                'id'=>2,
                'title'=>'济南',
            ],
            [
                'id'=>8,
                'title'=>'青岛',
            ],
            [
                'id'=>10,
                'title'=>'烟台',
            ],
            [
                'id'=>11,
                'title'=>'威海',
            ],
            [
                'id'=>12,
                'title'=>'东营',
            ],
            [
                'id'=>13,
                'title'=>' 淄博',
            ],
        ];

        $orgs = [];
        $y1 = [];
        $y2 = [];
        $max1 = 0;
        $max2 = 0;
        foreach ($lists as $k=>$v){
            $map = [];
            $orgs[] = $v['title'];
            if($v['id'] == 0){
                $y1[] = mt_rand(0,$max1);
                $y2[] = mt_rand(0,$max2);
            }else{
                $orgIds = $this->getAllNextId($v['id'],[]);
                if($orgIds){
                    $map[] = ['org_id','in',$orgIds];
                }else{
                    $map[] = ['org_id','=',-1];
                }
                $pzcount = Db::name('todo')->where($map)->where('todo_mode',3)->where('del',0)->count();
                $xycount = Db::name('todo')->where($map)->where('todo_mode',3)->where('del',0)->sum('xy_time');
                $wccount = Db::name('todo')->where($map)->where('todo_mode',3)->where('del',0)->sum('wc_time');
                $y2c = $pzcount>0?round(($xycount/60)/$pzcount,1):0;
                $y2[] = $y2c;

                if($y2c > $max2){
                    $max2 = $y2c;
                }
                $y1c = $pzcount>0?round(($wccount/60)/$pzcount,1):0;
                $y1[] = $y1c;
                if($y1c > $max1){
                    $max1 = $y1c;
                }
            }
        }

        HelpHander::success([
            'orgs'=>$orgs,
            'y1'=>$y1,
            'y2'=>$y2,
        ]);
    }

}