<?php
namespace app\api\controller;

use app\common\model\CleanTask;
use app\common\model\Daily;
use app\common\model\DailyTask;
use app\common\model\DeviceTask;
use app\common\model\PatrolTask;
use app\common\util\AppMsg;
use app\hander\HelpHander;
use think\Controller;
use think\Db;


class Headoffice extends Controller
{

    public function orgCount(){
        $date = date('Ymd');
        $todoCount = Db::name('todo')
            ->where('create_yyyymmdd',$date)
            ->where('del',0)
            ->count();
        $userCount = Db::name('user')
            ->where('del',0)
            ->where('type',0)
            ->count();
        $orgCount = Db::name('org')
            ->where('enable',1)
            ->where('del',0)
            ->where('type',2)
            ->count();
        $map[] = ['u.del','=',0];
        $roles = Db::name('roles')
            ->where('parent_id',11)
            ->where('del',0)
            ->where('enable',1)
            ->column('id');
        if(empty($roles)){
            $escortCount = 0;
        }else{
            $map[] = ['r.roles_id','in',$roles];
            $escortCount = Db::name('worker')
                 ->alias('a')
                ->join('user u','a.user_id=u.id')
                ->join('user_org o','a.user_id=o.user_id')
                ->join('user_roles r','a.user_id=r.user_id')
                ->field('u.real_name,u.mobile,u.create_time,u.head_image,a.*')
                ->where($map)
                ->count();
        }
        //今日完成
        $todoCount1 =Db::name('todo')
            ->where('create_yyyymmdd',$date)
            ->where('create_yyyymmdd',$date)
            ->where('todo_mode',3)
            ->where('del',0)
            ->count();
        $todoCount2 = $todoCount - $todoCount1 ;
        $bl = $todoCount>0?round($todoCount1/$todoCount*100,0):0;
        $data = [
            'todoCount'=>$todoCount,
            'userCount'=>$userCount,
            'orgCount'=>$orgCount,
            'escortCount'=>$escortCount,
            'todoCompletion'=>$bl.'%',];
        HelpHander::success($data);
    }
    public function todoCompletion(){
        $date = date('Ymd');
        $todoCount = Db::name('todo')
            ->where('create_yyyymmdd',$date)
            ->where('del',0)
            ->count();
        $todoCount1 = Db::name('todo')
            ->where('create_yyyymmdd',$date)
            ->where('create_yyyymmdd',$date)
            ->where('todo_mode',3)
            ->where('del',0)
            ->count();
        $todoCount2 = $todoCount - $todoCount1 ;
        $data = [
            ['name'=>'已完成','value'=>$todoCount1],
            ['name'=>'未完成','value'=>$todoCount2>0?$todoCount2:0],
        ];
        HelpHander::success($data);
    }
    public function userCount(){
        $org= Db::name('org')
            ->field('id,name')
            ->where('enable',1)
            ->where('del',0)
            ->where('type',2)
            ->select();
        foreach ($org as $k=>$v){
            $org[$k]['value'] = Db::name('user')
                ->alias('u')
                ->join('user_org uo','uo.user_id=u.id')
                ->where('uo.org_id',$v['id'])
                ->where('u.enable',1)
                ->where('u.del',0)
                ->count();
        }
       HelpHander::success($org);
    }
    //满意度排名
    public function orgRanking(){
        $org= Db::name('org')
            ->field('id,name')
            ->where('enable',1)
            ->where('del',0)
            ->where('type',2)
            ->select();
        foreach ($org as $k=>$v){
            $score = Db::name('comment')
                ->where('org_id',$v['id'])
                ->select();
            $num = 0;
            foreach ($score as $kk=>$vv){
                $num +=$vv['score'];
            }
            $org[$k]['score'] = $num;
        }
        $column = array_column($org,'score');
        array_multisort($column,SORT_DESC,$org);
        $data  = array_slice($org,0,3);
        HelpHander::success($data);
    }

    public function orgList(){
        $org= Db::name('org')
            ->field('id as org_id,name,lat,lng')
            ->where('enable',1)
            ->where('del',0)
            ->where('type',2)
            ->select();
        HelpHander::success($org);
    }
    //各项目医废统计
    public function wasteCount(){
        $month = date('Ym');
        $year= date('Y');
        $org= Db::name('org')
            ->field('id,name')
            ->where('enable',1)
            ->where('del',0)
            ->where('type',2)
            ->select();

        foreach ($org as $k=>$v){
            $count =Db::name('waste_record')
                ->where('org_id',$v['id'])
                ->where('create_yyyymm',$month)
                ->where('del',0)
                ->select();

            $count2 = Db::name('waste_record')
                ->where('org_id',$v['id'])
                ->where('create_yyyy',$year)
                ->where('del',0)
                ->select();
            $num = 0;
            $num2 = 0;
            foreach ($count as $kk=>$vv){
                $num +=$vv['weight'];
            }
            foreach ($count2 as $kay=>$val){
                $num2 +=$val['weight'];
            }
            $org[$k]['month_weight'] = $num / 1000;

            $org[$k]['year_weight'] = $num2 / 1000;

        }
        $data = [
            'x'=>array_column($org,'name'),
            'y'=>array_column($org,'month_weight'),
            'y2'=>array_column($org,'year_weight'),
        ];

        HelpHander::success($data);
    }
    //各项目工单统计
    public function todoCount(){
        $month = date('Ym');
        $org= Db::name('org')
            ->field('id,name')
            ->where('enable',1)
            ->where('del',0)
            ->where('type',2)
            ->select();

        foreach ($org as $k=>$v){
            $org[$k]['count'] = Db::name('todo')
                ->where('org_id',$v['id'])
                ->where('create_yyyymm',$month)
                ->where('del',0)
                ->count();
        }
        HelpHander::success($org);
    }


    //各项目陪护单统计
    public function phCount(){
        $month = date('Ym');
        $year= date('Y');
        $org= Db::name('org')
            ->field('id,name')
            ->where('enable',1)
            ->where('del',0)
            ->where('type',2)
            ->select();
        foreach ($org as $k=>$v){
            $count =Db::name('ph_orders')
                ->where('org_id',$v['id'])
                ->where('create_time','>=',beginMonth())
                ->where('create_time','<=',endMonth())
                ->count();

            $count2 = Db::name('waste_record')
                ->where('org_id',$v['id'])
                ->where('create_time','>=',beginYear())
                ->where('create_time','<=',endYear())
                ->count();

            $org[$k]['month_weight'] = $count;

            $org[$k]['year_weight'] = $count2;

        }
        $data = [
            'x'=>array_column($org,'name'),
            'y'=>array_column($org,'month_weight'),
            'y2'=>array_column($org,'year_weight'),
        ];

        HelpHander::success($data);
    }

    public function todoTimeLength(){
        $org= Db::name('org')
            ->field('id,name')
            ->where('enable',1)
            ->where('del',0)
            ->where('type',2)
            ->select();
        foreach ($org as $k=>$v){
            $num = Db::name('todo')
                ->where('org_id',$v['id'])
                ->where('del',0)
                ->sum('xy_time');

            $num2 = Db::name('todo')
                ->where('org_id',$v['id'])
                ->where('del',0)
                ->sum('wc_time');

            $org[$k]['xy_time'] = round($num/60,1);

            $org[$k]['wc_time'] = round($num2/60,1);
        }
        $data = [
            'x'=>array_column($org,'name'),
            'y'=>array_column($org,'xy_time'),
            'y2'=>array_column($org,'wc_time'),
        ];
        HelpHander::success($data);
    }
    //巡查图片
    public function patrolImg(){
        $data = Db::name('patrol_record')
            ->where('patrol_mode',3)
            ->where('images','<>','')
            ->where('org_id',cur_org_id())
            ->limit(20)
            ->select();

        foreach ($data as $k=>$v){
            $img = explode(',',$v['images']);
            if($img){
                $data[$k]['images'] = $img[0];
            }
        }
        if(count($data)>3){
            $key = array_rand($data,4);
            $list[] = $data[$key[0]];
            $list[] = $data[$key[1]];
            $list[] = $data[$key[2]];
            $list[] = $data[$key[3]];
        }
        $eList = isset($list)?$list:$data;
        HelpHander::success($eList);
    }
    //
    public function patrolRecordCount(){

        $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 = [];
        foreach ($dlist as $k=>$v){
            $tt = date('Ymd',strtotime($v));
            $count = Db::name('patrol_record')
                ->where('create_yyyymmdd',$tt)
                ->where('org_id',cur_org_id())
                ->where('patrol_mode',4)
                ->count();
            $count2 =Db::name('patrol_record')
                ->where('create_yyyymmdd',$tt)
                ->where('org_id',cur_org_id())
                ->where('patrol_mode',2)
                ->count();
            $count3 =Db::name('patrol_record')
                ->where('create_yyyymmdd',$tt)
                ->where('org_id',cur_org_id())
                ->where('patrol_mode',3)
                ->count();
            $date[] =  date('m-d',strtotime($v));
            $yCount[] = $count;
            $y2Count[] = $count2;
            $y3Count[] = $count3;
        }
        $data = [
            'date'=>$date,
            'count1'=>$yCount,
            'count2'=>$y2Count,
            'count3'=>$y3Count,
        ];
        HelpHander::success($data);
    }
}