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

use app\hander\HelpHander;
use think\Db;

class User extends Index
{
    // 项目大屏--员工数据分析
    public function userCate(){
        $lists = model('WorkTypeMode')->getWorkerUserStatistics($this->orgId);
        $data = [];
        foreach ($lists as $v){
            $data[] = [
                'name' => $v['name'],
                'value' => $v['count']
            ];
        }
        HelpHander::success($data);
    }

    // 项目大屏--员工好评度排名
    public function userPraise(){
        $allUids = model('WorkTypeMode')->getWorkerUserIds($this->orgId);
        $users = [];
        if($allUids){
            $users = Db::name('user')
                ->where('id','in',$allUids)
                ->order('score desc,id asc')
                ->field('id,real_name')
                ->limit(10)
                ->select();
            $users = $users?$users:[];
        }
        $data = [];
        $host = config("app.app_host");
        foreach ($users as $k=>$v){
            if($k == 0){
                $top = '<img src="'.$host.'/img/org-top1.png" width="35" style="margin-top:5px">';
            }else if($k == 1){
                $top = '<img src="'.$host.'/img/org-top2.png" width="35" style="margin-top:5px">';
            }else if($k == 2){
                $top = '<img src="'.$host.'/img/org-top3.png" width="35" style="margin-top:5px">';
            }else{
                $top = 'TOP'.($k+1);
            }
            $data[] = [$top,$v['real_name']];
        }
        HelpHander::success($data);
    }

    // 项目大屏--员工实时在岗情况
    public function userPosition(){
        $lists = Db::name('todo')
            ->where('todo_mode',3)
            ->where('org_id',$this->orgId)
            ->where('create_yyyymmdd',date('Ymd'))
            ->field('count(*) as count,to_user_id')
            ->group('to_user_id')
            ->distinct(true)
            ->order('count desc,to_user_id asc')
            ->select();
        $data = [];
        foreach ($lists as $k=>$v){
            $addr = Db::name('convey_plan_record')
                ->alias('cpr')
                ->join('address a','a.id = cpr.addr_id')
                ->where('user_id',$v['to_user_id'])
                ->value('title');
            $username = Db::name('user')->where('id',$v['to_user_id'])->value('real_name');
            $data[] = [$username?$username:'',$v['count'],$addr?$addr:''];
        }
        HelpHander::success($data);
    }

    public function userDep(){
        $depList = Db::name('dep')->where('enable',1)->where('org_id',$this->orgId)->where('del',0)->limit(6)->order('id desc')->select();
        $data = [];
        foreach ($depList as $v){
            $data[] = [
                'name' => $v['title'],
                'value' => Db::name('user_dep')
                    ->alias('ud')
                    ->join('user u','u.id=ud.user_id')
                    ->where('ud.dep_id',$v['id'])
                    ->where('u.del',0)
                    ->count(),
            ];
        }
        HelpHander::success($data);

    }

    //新项目大屏---在岗人员情况
    public function onGuardUserData(){
        $type = input('type','');
        $pid = 0;
        if($type == 1){
            $pid = 7;
        }elseif ($type == 2){
            $pid = 8;
        }elseif ($type == 3){
            $pid = 6;
        }elseif ($type == 4){
            $pid = 5;
        }elseif ($type == 5){
            $pid = 9;
        }elseif ($type == 6){
            $pid = 11;
        }

        $rolesId = Db::name('roles')
            ->where('parent_id',$pid)
            ->where('del',0)
            ->where('type',1)
            ->where('enable',1)
            ->where('org_id',$this->orgId)
            ->column('id');

        $userCount = Db::name('user')
            ->alias('u')
            ->field('u.id,u.real_name,ur.roles_id')
            ->join('user_roles ur','ur.user_id=u.id')
            ->where('u.del',0)
            ->where('u.enable',1)
            ->whereIn('ur.roles_id',$rolesId)
            ->count();

        $rolesUser = Db::name('user')
            ->alias('u')
            ->field('u.id,u.real_name,ur.roles_id')
            ->join('user_roles ur','ur.user_id=u.id')
            ->where('u.del',0)
            ->where('u.enable',1)
            ->where('u.work',1)
            ->whereIn('ur.roles_id',$rolesId)
            ->select();
        foreach ($rolesUser as $k=>$v){
            $roles = Db::name('roles')->where('id',$v['roles_id'])->find();
            $rolesUser[$k]['roles_name'] = Db::name('roles')->where('id',$roles['parent_id'])->value('name');
            $rolesUser[$k]['dep_name'] = Db::name('user_dep')
                ->alias('ud')
                ->join('dep d','d.id=ud.dep_id')
                ->where('ud.user_id',$v['id'])
                ->where('d.org_id',$this->orgId)
                ->value('d.title');
        }

        $list = [];
        foreach ($rolesUser as $k=>$v){
            $list[$k][] = $v['roles_name'];
            $list[$k][] = $v['dep_name'];
            $list[$k][] = $v['real_name'];
        }

        $data = [
            'userCount'=>$userCount,
            'rolesCount'=>count($rolesUser),
            'list'=>$list
        ];
        HelpHander::success($data);

    }
    //人员在岗数据总览
    public function orgUserCount(){
        $userCount = Db::name('user')
            ->alias('u')
            ->join('user_org uo','u.id=uo.user_id')
            ->where('uo.org_id',$this->orgId)
            ->where('u.type',0)
            ->where('enable',1)
            ->where('del',0)
            ->count();
        $userPostCount =  Db::name('user')
            ->alias('u')
            ->join('user_org uo','u.id=uo.user_id')
            ->where('uo.org_id',$this->orgId)
            ->where('u.work',1)
            ->where('u.type',0)
            ->where('del',0)
            ->where('enable',1)
            ->count();
        $data = [
            ['name'=>'总人数','value'=>$userCount],
            ['name'=>'在岗人员','value'=>$userPostCount]
        ];
        HelpHander::success($data);
    }

    public function rolesUserData(){

        $data = [
            ['pid'=>8,'name'=>'保洁','value'=>0],
            ['pid'=>7,'name'=>'维修','value'=>0],
            ['pid'=>5,'name'=>'保安','value'=>0],
            ['pid'=>6,'name'=>'运送','value'=>0],
            ['pid'=>11,'name'=>'陪护','value'=>0],
        ];

        foreach ($data as $k=>$v){
            $data[$k]['pid'] = Db::name('roles')
                ->where('parent_id',$v['pid'])
                ->where('del',0)
                ->where('type',1)
                ->where('enable',1)
                ->where('org_id',$this->orgId)
                ->column('id');
        }

        foreach ($data as $k=>$v){
            $data[$k]['value'] = Db::name('user')
                ->alias('u')
                ->join('user_roles ur','ur.user_id=u.id')
                ->whereIn('ur.roles_id',$v['pid'])
                ->where('u.del',0)
                ->where('u.enable',1)
                ->count();
            unset($data[$k]['pid']);
        }
        HelpHander::success($data);
    }

    public function bjUserCountData(){
        //保洁
        $bjRolesId = Db::name('roles')
            ->where('parent_id',8)
            ->where('del',0)
            ->where('type',1)
            ->where('enable',1)
            ->where('org_id',$this->orgId)
            ->column('id');
        $bjCount = Db::name('user')
            ->alias('u')
            ->join('user_roles ur','ur.user_id=u.id')
            ->whereIn('ur.roles_id',$bjRolesId)
            ->where('u.del',0)
            ->where('u.enable',1)
            ->count();
        $bjWorkCount = Db::name('user')
            ->alias('u')
            ->join('user_roles ur','ur.user_id=u.id')
            ->whereIn('ur.roles_id',$bjRolesId)
            ->where('u.del',0)
            ->where('u.enable',1)
            ->where('u.work',1)
            ->count();
        $bl = $bl2 = 0;
        if($bjCount > 0){
            $bl = round($bjWorkCount/$bjCount*100,1);
            $bl2  = 100 - $bl;
        }

        $data = [
            'count'=>['bjCount'=>$bjCount,'bjWorkCount'=>$bjWorkCount],
            'count2'=>[['value'=>$bl],['value'=>$bl2]],
            'bl'=>$bl
        ];

        HelpHander::success($data);
    }

    public function wxUserCountData(){
        //维修
        $rolesId = Db::name('roles')
            ->where('parent_id',7)
            ->where('del',0)
            ->where('type',1)
            ->where('enable',1)
            ->where('org_id',$this->orgId)
            ->column('id');
        $count = Db::name('user')
            ->alias('u')
            ->join('user_roles ur','ur.user_id=u.id')
            ->whereIn('ur.roles_id',$rolesId)
            ->where('u.del',0)
            ->where('u.enable',1)
            ->count();
        $workCount = Db::name('user')
            ->alias('u')
            ->join('user_roles ur','ur.user_id=u.id')
            ->whereIn('ur.roles_id',$rolesId)
            ->where('u.del',0)
            ->where('u.enable',1)
            ->where('u.work',1)
            ->count();
        $bl = $bl2 = 0;
        if($count > 0){
            $bl = round($workCount/$count*100,1);
            $bl2  = 100 - $bl;
        }

        $data = [
            'count'=>['count'=>$count,'workCount'=>$workCount],
            'count2'=>[['value'=>$bl],['value'=>$bl2]],
            'bl'=>$bl
        ];

        HelpHander::success($data);
    }

    public function baUserCountData(){
        //保安
        $rolesId = Db::name('roles')
            ->where('parent_id',5)
            ->where('del',0)
            ->where('type',1)
            ->where('enable',1)
            ->where('org_id',$this->orgId)
            ->column('id');
        $count = Db::name('user')
            ->alias('u')
            ->join('user_roles ur','ur.user_id=u.id')
            ->whereIn('ur.roles_id',$rolesId)
            ->where('u.del',0)
            ->where('u.enable',1)
            ->count();
        $workCount = Db::name('user')
            ->alias('u')
            ->join('user_roles ur','ur.user_id=u.id')
            ->whereIn('ur.roles_id',$rolesId)
            ->where('u.del',0)
            ->where('u.enable',1)
            ->where('u.work',1)
            ->count();
        $bl = $bl2 = 0;
        if($count > 0){
            $bl = round($workCount/$count*100,1);
            $bl2  = 100 - $bl;
        }

        $data = [
            'count'=>['count'=>$count,'workCount'=>$workCount],
            'count2'=>[['value'=>$bl],['value'=>$bl2]],
            'bl'=>$bl
        ];

        HelpHander::success($data);
    }

    public function phUserCountData(){
        //陪护
        $rolesId = Db::name('roles')
            ->where('parent_id',11)
            ->where('del',0)
            ->where('type',1)
            ->where('enable',1)
            ->where('org_id',$this->orgId)
            ->column('id');
        $count = Db::name('user')
            ->alias('u')
            ->join('user_roles ur','ur.user_id=u.id')
            ->whereIn('ur.roles_id',$rolesId)
            ->where('u.del',0)
            ->where('u.enable',1)
            ->count();
        $workCount = Db::name('user')
            ->alias('u')
            ->join('user_roles ur','ur.user_id=u.id')
            ->whereIn('ur.roles_id',$rolesId)
            ->where('u.del',0)
            ->where('u.enable',1)
            ->where('u.work',1)
            ->count();
        $bl = $bl2 = 0;
        if($count > 0){
            $bl = round($workCount/$count*100,1);
            $bl2  = 100 - $bl;
        }
        $workUserIds = Db::name('user')
            ->alias('u')
            ->join('user_roles ur','ur.user_id=u.id')
            ->whereIn('ur.roles_id',$rolesId)
            ->where('u.del',0)
            ->where('u.enable',1)
            ->where('u.work',1)
            ->column('u.id');
        $isWorkCount = Db::name('ph_todo')->whereIn('user_id',$workUserIds)->where('org_id',$this->orgId)->where('status',1)->group('user_id')->count();
        $noWorkCount = $workCount - $isWorkCount;
        if($noWorkCount < 1 ){
            $noWorkCount = 0;
        }

        $data = [
            'count'=>['count'=>$count,'workCount'=>$workCount,'noWorkCount'=>$noWorkCount],
            'count2'=>[['value'=>$bl],['value'=>$bl2]],
            'bl'=>$bl
        ];

        HelpHander::success($data);
    }

    public function ysUserCountData(){
        //运送
        $rolesId = Db::name('roles')
            ->where('parent_id',6)
            ->where('del',0)
            ->where('type',1)
            ->where('enable',1)
            ->where('org_id',$this->orgId)
            ->column('id');
        $count = Db::name('user')
            ->alias('u')
            ->join('user_roles ur','ur.user_id=u.id')
            ->whereIn('ur.roles_id',$rolesId)
            ->where('u.del',0)
            ->where('u.enable',1)
            ->count();
        $workCount = Db::name('user')
            ->alias('u')
            ->join('user_roles ur','ur.user_id=u.id')
            ->whereIn('ur.roles_id',$rolesId)
            ->where('u.del',0)
            ->where('u.enable',1)
            ->where('u.work',1)
            ->count();
        $bl = $bl2 = 0;
        if($count > 0){
            $bl = round($workCount/$count*100,1);
            $bl2  = 100 - $bl;
        }
        $workUserIds = Db::name('user')
            ->alias('u')
            ->join('user_roles ur','ur.user_id=u.id')
            ->whereIn('ur.roles_id',$rolesId)
            ->where('u.del',0)
            ->where('u.enable',1)
            ->where('u.work',1)
            ->column('u.id');
        $isWorkCount = Db::name('todo')->whereIn('to_user_id',$workUserIds)->where('org_id',$this->orgId)->where('todo_mode',2)->where('work_type_mode',3)->group('to_user_id')->where('del',0)->count();
        $noWorkCount = $workCount - $isWorkCount;
        if($noWorkCount < 1 ){
            $noWorkCount = 0;
        }

        $data = [
            'count'=>['count'=>$count,'workCount'=>$workCount,'noWorkCount'=>$noWorkCount],
            'count2'=>[['value'=>$bl],['value'=>$bl2]],
            'bl'=>$bl
        ];

        HelpHander::success($data);
    }

    public function userCount(){
        $userCount = 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','in',[0,2])
            ->where('o.enable',1)
            ->where('o.del',0)
            ->where('o.id',$this->orgId)
            ->count();

        HelpHander::success($userCount);
    }

    // 项目大屏--满意度排名
    public function userCommentScoreList(){
        $user = Db::name('comment')
            ->field('user_id')
            ->where('org_id',$this->orgId)
            ->group('user_id')
            ->select();
        foreach ($user as $k=>$v){
            $user[$k]['score'] = Db::name('comment')
                ->where('org_id',$this->orgId)
                ->where('user_id',$v['user_id'])
                ->sum('score');
            $user[$k]['name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
            $user[$k]['dep'] = Db::name('user_dep')
                ->alias('ud')
                ->join('dep d','d.id=ud.dep_id')
                ->where('ud.user_id',$v['user_id'])
                ->value('title');
            $rolesId = Db::name('user_roles')
                ->alias('ur')
                ->join('roles r','r.id=ur.roles_id')
                ->where('ur.user_id',$v['user_id'])
                ->value('r.parent_id');
            $user[$k]['roles'] = $rolesId ? Db::name('roles')->where('id',$rolesId)->value('name'):'';
            $user[$k]['task'] = Db::name('comment')
                ->where('org_id',$this->orgId)
                ->where('user_id',$v['user_id'])
                ->count();
        }

        $list = list_sort_by($user,'score','desc');
        $data = [];
        $host = config("app.app_host");
        foreach ($list as $k=>$v){
            if(isset($k) && $k==0){
                $top = '<img src="'.$host.'/img/org-top1.png" width="30" style="margin-top:5px" >';
            }elseif (isset($k) && $k==1){
                $top = '<img src="'.$host.'/img/org-top2.png" width="30" style="margin-top:5px" >';
            }elseif (isset($k) && $k==2){
                $top = '<img src="'.$host.'/img/org-top3.png" width="30" style="margin-top:5px">';
            }else{
                $top = 'TOP'.($k+1);
            }
            $data[] = [$top,$v['name'],$v['dep'],$v['roles'],$v['task'],$v['score']];
        }
        HelpHander::success($data);
    }
}