<?php
namespace app\h5\controller;

use app\common\model\WorkTypeMode;
use app\hander\HelpHander;
use think\App;
use think\Controller;
use think\Db;
use think\helper\Time;

class Statistics extends Controller
{
    private $orgId = 0;

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

        $this->orgId = input('orgId/d',0);
//        halt($this->orgId);
        if($this->orgId <= 0){
            $this->redirect(url('index/errorPage'));
        }
    }

    protected function checkToken(){
        $token = input('token','','trim');
        if(!$token){
            if(request()->isAjax()){
                $this->error('登录信息失效');
            }else{
                $this->redirect(url('index/errorPage'));
            }
        }
        $user = Db::name('token')->where('token',$token)->find();
        if(!$user) {
            if(request()->isAjax()){
                $this->error('登录信息失效');
            }else{
                $this->redirect(url('index/errorPage'));
            }
        }
        return $user['user_id'];
    }

    public function orders(){
        $type = input('type/d',1);
        $mode = input('mode/d',1);
        if($mode == 1){
            $list = [
                ['id' => [5],'type'=>'已完成订单'],
                ['id' => [1],'type'=>'待处理订单'],
                ['id' => [4],'type'=>'已派发订单'],
                ['id' => [2,3],'type'=>'已取消订单'],
                ['id' => [6],'type'=>'已评价订单'],
                ['id' => [-1],'type'=>'挂起订单'],
            ];
        }else{
            $list = [
                ['id' => [5],'type'=>'已完成订单'],
                ['id' => [1],'type'=>'待处理订单'],
                ['id' => [4],'type'=>'已派发订单'],
                ['id' => [2,3],'type'=>'已取消订单'],
                ['id' => [6],'type'=>'已评价订单'],
            ];
        }

        $map[] = ['del','=',0];
        $map[] = ['org_id','=',$this->orgId];
        $map[] = ['work_type_mode','=',$mode];

        if($type == 1){ //日统计
            $map[] = ['create_yyyymmdd','=',date('Ymd')];
        }else if($type == 2){ //月统计
            $map[] = ['create_yyyymm','=',date('Ym')];
        }

        $allcount = 0;
        if($mode == 1){
            foreach ($list as $k=>$v){
                if(in_array(4,$v['id'])){
                    $a1 = 0;
                    $order =  Db::name('orders')->where($map)->where('order_mode','in',$v['id'])->select();
                    foreach ($order as $kk=>$vv){
                        $todo = Db::name('todo')->where('order_id',$vv['id'])->where('pause',1)->where('todo_mode',2)->where('del',0)->find();
                        if($todo){
                            $a1 +=1;
                        }
                    }
                    $cost = count($order)-$a1;
                }elseif(in_array(-1,$v['id'])){
                    $a2 = 0;
                    $order =  Db::name('orders')->where($map)->where('order_mode',4)->select();
                    foreach ($order as $kk=>$vv){
                        $todo = Db::name('todo')->where('order_id',$vv['id'])->where('del',0)->where('todo_mode',2)->where('pause',1)->find();
                        if($todo){
                            $a2 +=1;
                        }
                    }
                    $cost = $a2;
                }else{
                    $cost =  Db::name('orders')->where($map)->where('order_mode','in',$v['id'])->count();
                }

                $list[$k]['cost'] = $cost;
                $list[$k]['a'] = '1';
                $allcount += $list[$k]['cost'];
            }
        }else{
            foreach ($list as $k=>$v){
                $list[$k]['cost'] =  Db::name('orders')->where($map)->where('order_mode','in',$v['id'])->count();
                $list[$k]['a'] = '1';
                $allcount += $list[$k]['cost'];
            }
        }


        $this->assign('list',$list);
        $this->assign('allcount',$allcount);
        $this->assign('orgId',$this->orgId);
        $this->assign('type',$type);
        $this->assign('mode',$mode);

        return $this->fetch();
    }

    public function users() {
        $lists = Db::name('roles')->where('id','in',(new WorkTypeMode())->workerRoles)->field('id,name')->select();
        $lists = $lists?$lists:[];
        $allcount = 0;
        foreach ($lists as $k=>$v){
            $lists[$k]['color'] = '#325699';
            $roleIds = model('Roles')->getRoleIds($v['id'],$this->orgId);
            $map = [];
            $map[] = ['u.del','=',0];
            $map[] = ['u.enable','=',1];
            if($roleIds){
                $map[] = ['ur.roles_id','in',$roleIds];
            }else{
                $map[] = ['ur.roles_id','=',-1];
            }
            $count = Db::name('user_roles')
                ->alias('ur')
                ->join('user u','u.id = ur.user_id')
                ->where($map)
                ->count();
            $lists[$k]['cost'] = $count;
            $lists[$k]['a'] = '1';
            $allcount += $count;
        }

        $this->assign('list',$lists);
        $this->assign('orgId',$this->orgId);
        $this->assign('allcount',$allcount);

        return $this->fetch();
    }

    public function comment() {
        $type = input('type/d',1);

        if($type == 1){ //日统计
            $map[] =['c.create_yyyymmdd','=',date('Ymd')];
        }else if($type == 2){ //月统计
            $map[] =['c.create_yyyymm','=',date('Ym')];
        }
        $map[] = ['c.org_id','=',$this->orgId];
        $tlist = [
            ['work_type_mode'=> 0,'name'=>'一键呼叫'],
            ['work_type_mode'=> 1,'name'=>'报修'],
            ['work_type_mode'=> 2,'name'=>'保洁'],
            ['work_type_mode'=> 3,'name'=>'运送'],
            ['work_type_mode'=> 4,'name'=>'应急'],
            ['work_type_mode'=> 15,'name'=>'品质整改']
        ];
        $list = [];
        foreach ($tlist as $k=>$v){
            $ss = Db::name('comment')
                ->alias('c')
                ->join('orders o','o.id = c.from_id')
                ->where('c.type',$v['work_type_mode'])
                ->where('o.del',0)
                ->where($map)
                ->field('count(*) as count,sum(score) as score,c.type')
                ->find();
            $list[] = array(
                'year' => $v['name'],
                'sales' => $ss&&$ss['count']>0?round($ss['score']/$ss['count'],1):0,
                'count' => $ss['count'],
                'work_type_mode'=>$ss['type']?$ss['type']:$v['work_type_mode'],
            );
        }

        $this->assign('list',$list);
        $this->assign('orgId',$this->orgId);
        $this->assign('type',$type);

        return $this->fetch();
    }

    public function daily() {
        $type = input('type/d',0);

        $map[] = ['org_id','=',$this->orgId];
        if($type == 0){ //日统计
            $map[] = ['create_yyyymmdd','=',date('Ymd')];
        } else if($type == 1){ //月统计
            $map[] = ['create_yyyymm','=',date('Ym')];
        }
        $alladdr = Db::name('daily_record')->where($map)->group('daily_id')->count();
        $allcount = Db::name('daily_record')->where($map)->count();
        $this->assign('allcount',$allcount);
        $this->assign('alladdr',$alladdr);
        $this->assign('orgId',$this->orgId);
        $this->assign('type',$type);

        return $this->fetch();
    }

    public function device() {
        $type = input('type/d',0);
        $map[] = ['org_id','=',$this->orgId];

        if($type == 0){ //日统计
            $map[] = ['create_yyyymmdd','=',date('Ymd')];
        } else if($type == 1){ //月统计
            $map[] = ['create_yyyymm','=',date('Ym')];
        }

        $device = Db::name('device_record')->where($map)->group('device_id')->count();

        $allcount = Db::name('device_record')->where($map)->count();

        $this->assign('allcount',$allcount);
        $this->assign('device',$device);
        $this->assign('orgId',$this->orgId);
        $this->assign('type',$type);

        return $this->fetch();
    }

    public function patrol() {
        $type = input('type/d',0);
        $mode = input('mode/d',1);
        $map[] = ['org_id','=',$this->orgId];
        $map[] = ['del','=',0];
        $map[] = ['patrol_mode','=',$mode];

        if($type == 0){ //日统计
            $map[] = ['create_yyyymmdd','=',date('Ymd')];
        } else if($type == 1){ //月统计
            $map[] = ['create_yyyymm','=',date('Ym')];
        }

        $tt = 3;
        if($mode == 2){
            $tt = 4;
        }else if($mode == 3){
            $tt = 5;
        }else if($mode == 4){
            $tt = 6;
        }
        $bluetooth = model('Address')->getListByTypeCount($tt,$this->orgId);

        $complete = Db::name('patrol_task')->where($map)->where('status', 2)->count();

        $incomplete = Db::name('patrol_task')->where($map)->where('status', 3)->count();

        $allcount = Db::name('patrol_task')->where($map)->count();

        $ratio = 0;
        if($allcount > 0) {
            $ratio = round($complete / $allcount*100,2);
        }

        $mode_name = Db::name('patrol_mode')->where('id', $mode)->field('name')->find();

        $this->assign('allcount',$allcount);
        $this->assign('bluetooth',$bluetooth);
        $this->assign('complete',$complete);
        $this->assign('incomplete',$incomplete);
        $this->assign('orgId',$this->orgId);
        $this->assign('type',$type);
        $this->assign('ratio',$ratio);
        $this->assign('mode',$mode);
        $this->assign('mode_name',$mode_name);

        return $this->fetch();
    }

    public function ordersList(){
        $page = input('page',1);
        $size = input('size',20);
        $type = input('type',-1);//订单状态
        $mode = input('mode',0);//订单类型
        $day = input('day',0);//日,月,总计


        //全部订单
        if($type == -1){
            $where[]=['order_mode','in',[1,2,3,4,5,6]];
        }
        //已完成订单
        if($type == 0){
            $where[]=['order_mode','in',[5]];
        }
        //待处理订单
        if($type == 1){
            $where[]=['order_mode','in',[1]];
        }
        //已派发订单
        if($type == 2){
            $where[]=['order_mode','in',[4]];
        }
        //已取消订单
        if($type == 3){
            $where[]=['order_mode','in',[2,3]];
        }
        //已评价订单
        if($type == 4){
            $where[]=['order_mode','in',[6]];
        }

        if($type == 5){ //挂起订单
            $where[]=['order_mode','in',[4]];
        }

        if($day == 1){    //日订单
            $where[] = ['create_yyyymmdd','=',date('Ymd')];
        }else if($day == 2){  //月订单
            $where[] = ['create_yyyymm','=',date('Ym')];
        }
        //订单类型
        if(in_array($mode,array(1,2,3,4))){
            $where[] = ['work_type_mode','=',$mode];
        }else{
            $where[] = ['work_type_mode','>',0];
        }

        $andWhere = array(
            'del' => 0,
            'org_id' => $this->orgId
        );

        if($page > 1){
            $list = Db::name('orders')
                ->field('id,dep_id,work_type_mode,order_mode,user_id,create_time,org_id')
                ->where($where)
                ->where($andWhere)
                ->page($page,$size)
                ->order('id desc')
                ->select();

            foreach ($list as $k=>$v){
                if($type == 2 && $v['order_mode'] == 4){
                    $list[$k]['out_content'] = Db::name('order_mode')->where('id',$v['order_mode'])->value('out_content');
                    $list[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
                    $dep = Db::name('dep')->where('id',$v['dep_id'])->value('title');
                    $list[$k]['dep_name'] = $dep?$dep :'' ;

                    $todo = Db::name('todo')->where('order_id',$v['id'])->where('del',0)->where('todo_mode',2)->where('pause',1)->find();
                    if($todo){
                        unset($list[$k]);
                    }

                }elseif($type == 5 && $v['order_mode'] == 4){
                    $list[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
                    $dep = Db::name('dep')->where('id',$v['dep_id'])->value('title');
                    $list[$k]['dep_name'] = $dep?$dep :'' ;

                    $list[$k]['out_content'] = '挂起';
                    $todo = Db::name('todo')->where('order_id',$v['id'])->where('del',0)->where('todo_mode',2)->where('pause',1)->find();
                    if(!$todo){
                        unset($list[$k]);
                    }

                }else if($type == -1){
                    if($v['order_mode'] == 4){
                        $todo = Db::name('todo')->where('order_id',$v['id'])->where('del',0)->where('todo_mode',2)->where('pause',1)->find();

                        $list[$k]['out_content'] = $todo ? '挂起': Db::name('order_mode')->where('id',$v['order_mode'])->value('out_content');
                    }else{
                        $list[$k]['out_content'] = Db::name('order_mode')->where('id',$v['order_mode'])->value('out_content');
                    }
                    $list[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
                    $dep = Db::name('dep')->where('id',$v['dep_id'])->value('title');
                    $list[$k]['dep_name'] = $dep?$dep :'' ;


                }else{
                    $list[$k]['out_content'] = Db::name('order_mode')->where('id',$v['order_mode'])->value('out_content');
                    $list[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
                    $dep = Db::name('dep')->where('id',$v['dep_id'])->value('title');
                    $list[$k]['dep_name'] = $dep?$dep :'' ;

                }
            }

            $this->success('','',array_values($list));
        }else{
            $list = Db::name('orders')
                ->field('id,dep_id,work_type_mode,order_mode,user_id,create_time,org_id')
                ->where($where)
                ->where($andWhere)
                ->page($page,$size)
                ->order('id desc')
                ->select();
            foreach ($list as $k=>$v){
                if($type == 2 && $v['order_mode'] == 4){
                    $list[$k]['out_content'] = Db::name('order_mode')->where('id',$v['order_mode'])->value('out_content');
                    $list[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
                    $dep = Db::name('dep')->where('id',$v['dep_id'])->value('title');
                    $list[$k]['dep_name'] = $dep?$dep :'' ;

                    $todo = Db::name('todo')->where('order_id',$v['id'])->where('del',0)->where('todo_mode',2)->where('pause',1)->find();
                    if($todo){
                        unset($list[$k]);
                    }

                }elseif($type == 5 && $v['order_mode'] == 4){
                    $list[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
                    $dep = Db::name('dep')->where('id',$v['dep_id'])->value('title');
                    $list[$k]['dep_name'] = $dep?$dep :'' ;

                    $list[$k]['out_content'] = '挂起';
                    $todo = Db::name('todo')->where('order_id',$v['id'])->where('del',0)->where('todo_mode',2)->where('pause',1)->find();
                    if(!$todo){
                        unset($list[$k]);
                    }

                }else if($type == -1){
                    if($v['order_mode'] == 4){
                        $todo = Db::name('todo')->where('order_id',$v['id'])->where('del',0)->where('todo_mode',2)->where('pause',1)->find();

                        $list[$k]['out_content'] = $todo ? '挂起': Db::name('order_mode')->where('id',$v['order_mode'])->value('out_content');
                    }else{
                        $list[$k]['out_content'] = Db::name('order_mode')->where('id',$v['order_mode'])->value('out_content');
                    }
                    $list[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
                    $dep = Db::name('dep')->where('id',$v['dep_id'])->value('title');
                    $list[$k]['dep_name'] = $dep?$dep :'' ;


                }else{
                    $list[$k]['out_content'] = Db::name('order_mode')->where('id',$v['order_mode'])->value('out_content');
                    $list[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
                    $dep = Db::name('dep')->where('id',$v['dep_id'])->value('title');
                    $list[$k]['dep_name'] = $dep?$dep :'' ;

                }
            }


            $this->assign('mode',$mode);
            $this->assign('orgId',$this->orgId);
            $this->assign('type',$type);
            $this->assign('day',$day);
            $this->assign('list',array_values($list));
            return $this->fetch();
        }
    }

    public function ordersDetails(){
        $id = input('id',0);
        $info = model('Orders')->detail($id);
        $info['create_time'] = date('Y-m-d H:i',strtotime($info['create_time']));
        $info['images'] = $info['images']?explode(',',$info['images']):[];
        $info['videos'] = $info['videos']?explode(',',$info['videos']):[];

        $this->assign('info',$info);
        return $this->fetch();
    }

    public function ordersSubDetails(){
        $id = input('id',0);
        $info = Db::name('todo')->where('id',$id)->find();
        $info['sign'] = $info['sign']?explode(',',$info['sign']):[];
        $info['cons'] = [];
        if ($info['work_type_mode'] == 1) {
            $todo_mate = Db::name('todo_mate')
                ->where('order_id',$info['order_id'])
                ->select();
            if(!empty($todo_mate)){
                $cons = Db::name('todo_mate_item')
                    ->alias('a')
                    ->join('mate_goods b','b.id = a.items_id')
                    ->where('a.todo_mate_id','in',array_column($todo_mate,'id'))
                    ->field('a.*,b.title')
                    ->select();
                foreach ($cons as $k1=>$v1){
                    $pInfo = Db::name('mate_goods')
                        ->where('id',$v1['pid'])
                        ->find();
                    $info['cons'][] = [
                        'title' =>$v1['title'],
                        'total' =>$v1['total'],
                        'money' =>$v1['money'],
                        'total_money' =>$v1['total_money'],
                    ];

                }
            }
        }
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function videoPlay(){
        $src = input('url');
        $this->assign('url',$src);
        return $this->fetch();
    }
    //巡更任务列表
    public function patrolList() {
        $page = input('page',1);
        $size = input('size',20);
        $type = input('type',0);//巡更状态
        $mode = input('mode',0);//巡更类型
        $day = input('day',0);//日,月,总计

        if($type==2){
            $where[] = ['status','=',2];
        }
        if($type==3){
            $where[] = ['status','=',3];
        }
        $where[] = ['org_id','=',$this->orgId];
        $where[] = ['del','=',0];

        if($day == 0){    //日订单
            $where[] = ['create_yyyymmdd','=',date('Ymd')];
        }else if($day == 1){  //月订单
            $where[] = ['create_yyyymm','=',date('Ym')];
        }else if($day == 2){  //总订单

        }
        //订单类型
        if(in_array($mode,array(1,2,3,4))){
            $where[] = ['patrol_mode','=',$mode];
        }else{
            $where[] = ['patrol_mode','>',0];
        }

        if($page > 1){
            $list = Db::name('patrol_task')
                ->field('id,title,start_time,end_time,status,patrol_mode,org_id')
                ->where($where)
                ->order('id desc')
                ->page($page,$size)
                ->select();

            foreach ($list as $k=>$v){
                $user = Db::name('patrol_task_user')
                    ->alias('ptu')
                    ->join('user u','u.id=ptu.user_id')
                    ->where('ptu.patrol_task_id',$v['id'])
                    ->column('u.real_name');
                $list[$k]['real_name']=$user?implode(',',$user):'';

                $dep = Db::name('patrol_task_user')
                    ->alias('ptu')
                    ->join('user_dep ud','ud.user_id=ptu.user_id')
                    ->join('dep d','d.id=ud.dep_id')
                    ->where('ptu.patrol_task_id',$v['id'])
                    ->column('d.title');
                $list[$k]['dep']=$dep?implode(',',$dep):'';
            }
            HelpHander::success($list);
        }else{

            $list = Db::name('patrol_task')
                ->field('id,title,start_time,end_time,status,patrol_mode,org_id')
                ->where($where)
                ->order('id desc')
                ->page($page,$size)
                ->select();
            foreach ($list as $k=>$v){
                $user = Db::name('patrol_task_user')
                    ->alias('ptu')
                    ->join('user u','u.id=ptu.user_id')
                    ->where('ptu.patrol_task_id',$v['id'])
                    ->column('u.real_name');
                $list[$k]['real_name']=$user?implode(',',$user):'';

                $dep = Db::name('patrol_task_user')
                    ->alias('ptu')
                    ->join('user_dep ud','ud.user_id=ptu.user_id')
                    ->join('dep d','d.id=ud.dep_id')
                    ->where('ptu.patrol_task_id',$v['id'])
                    ->column('d.title');
                $list[$k]['dep']=$dep?implode(',',$dep):'';
            }
            $this->assign('list',$list);
            $this->assign('mode',$mode);
            $this->assign('type',$type);
            $this->assign('day',$day);
            $this->assign('orgId',$this->orgId);
            return $this->fetch();
        }
    }

    public function patrolDetails(){
        $id = input('id',0);
        $info = Db::name('patrol_task')
            ->where('id',$id)
            ->where('del',0)
            ->find();
        $info['interrupt_img']= $info['interrupt_img']?explode(',',$info['interrupt_img']):[];
        $info['patrol_type']= Db::name('patrol_mode')->where('id',$info['patrol_mode'])->value('name');
        //巡更人员
        $taskUser= Db::name('patrol_task_user')
            ->alias('ptu')
            ->join('user u','u.id=ptu.user_id')
            ->where('ptu.patrol_task_id',$info['id'])
            ->column('u.real_name');
        $info['patrol_user']=$taskUser?implode(',',$taskUser):'';
        //巡更地点
        $address = Db::name('patrol_task_addr')
            ->alias('pta')
            ->field('a.id,a.title,pta.id as patrol_form_id,pta.patrol_task_id')
//            ->join('patrol_addr_form paf','paf.id=pta.patrol_form_id')
            ->join('address a','a.id=pta.address_id')
            ->where('pta.patrol_task_id',$id)
            ->select();
        foreach ($address as $kk=>$vv){
            $record = Db::name('patrol_record')
                ->where('patrol_task_id',$vv['patrol_task_id'])
                ->where('patrol_addr_id',$vv['id'])
                ->where('patrol_addr_form_id',$vv['patrol_form_id'])
                ->find();
            $address[$kk]['status'] = $record?1:0;
        }
        $info['patrol_address']=$address?$address:[];
        $this->assign('info',$info);
        return $this->fetch();
    }

    public function patrolSonDetails(){
        $addrFormId = input('addrFormId',0);
        $id = input('id',0);
        $task = Db::name('patrol_task')
            ->field('end_time,patrol_mode')
            ->where('id',$id)
            ->where('del',0)
            ->find();
        //巡更人员
        $taskUser = Db::name('patrol_task_user')
            ->alias('ptu')
            ->join('user u','u.id=ptu.user_id')
            ->where('ptu.patrol_task_id',$id)
            ->column('u.real_name');

        $info = Db::name('patrol_record')
            ->field('images,content,check_json')
            ->where('patrol_task_id',$id)
            ->where('patrol_addr_form_id',$addrFormId)
            ->find();
        $info['img'] = $info['images']?explode(',',$info['images']):[];


        $info['forms'] =$info['check_json']?json_decode($info['check_json'],true):[];
        $info['patrol_user']=$taskUser?implode(',',$taskUser):'';
        $info['patrol_time']=$task['end_time'];
        $info['patrol_type']= Db::name('patrol_mode')->where('id',$task['patrol_mode'])->value('name');

        $this->assign('info',$info);
        return $this->fetch();
    }
    //用户列表
    public function userList(){
        $page = input('page',1);
        $size = input('size',10);
        $rolesId = input('rolesId',0);
        if($page > 1){
            if($rolesId == 0){
                $rolesIds =  Db::name('roles')
                    ->whereIn('parent_id',[4,5,6,7,8,9,10,11])
                    ->where('org_id',$this->orgId)
                    ->where('del',0)
                    ->where('enable',1)
                    ->column('id');
                $list = Db::name('user')
                    ->alias('u')
                    ->field('u.id,u.real_name,u.head_image,u.create_time,u.mobile')
                    ->join('user_roles ur','ur.user_id=u.id')
                    ->where('u.del',0)
                    ->where('u.enable',1)
                    ->whereIn('ur.roles_id',$rolesIds)
                    ->order('u.id desc')
                    ->page($page,$size)
                    ->select();
            }else{
                $rolesIds =  Db::name('roles')
                    ->where('parent_id',$rolesId)
                    ->where('org_id',$this->orgId)
                    ->where('del',0)
                    ->where('enable',1)
                    ->column('id');
                $list = Db::name('user')
                    ->alias('u')
                    ->field('u.id,u.real_name,u.head_image,u.create_time,u.mobile')
                    ->join('user_roles ur','ur.user_id=u.id')
                    ->where('u.del',0)
                    ->where('u.enable',1)
                    ->whereIn('ur.roles_id',$rolesIds)
                    ->order('u.id desc')
                    ->page($page,$size)
                    ->select();
            }
            HelpHander::success($list);
        }else{

            if($rolesId == 0){
                $rolesIds =  Db::name('roles')
                    ->whereIn('parent_id',[4,5,6,7,8,9,10,11])
                    ->where('org_id',$this->orgId)
                    ->where('del',0)
                    ->where('enable',1)
                    ->column('id');
                $list = Db::name('user')
                    ->alias('u')
                    ->field('u.id,u.real_name,u.head_image,u.create_time,u.mobile')
                    ->join('user_roles ur','ur.user_id=u.id')
                    ->where('u.del',0)
                    ->where('u.enable',1)
                    ->whereIn('ur.roles_id',$rolesIds)
                    ->order('u.id desc')
                    ->page($page,$size)
                    ->select();
            }else{
                $rolesIds =  Db::name('roles')
                    ->where('parent_id',$rolesId)
                    ->where('org_id',$this->orgId)
                    ->where('del',0)
                    ->where('enable',1)
                    ->column('id');
                $list = Db::name('user')
                    ->alias('u')
                    ->field('u.id,u.real_name,u.head_image,u.create_time,u.mobile')
                    ->join('user_roles ur','ur.user_id=u.id')
                    ->where('u.del',0)
                    ->where('u.enable',1)
                    ->whereIn('ur.roles_id',$rolesIds)
                    ->order('u.id desc')
                    ->page($page,$size)
                    ->select();
            }

        }
        $this->assign('rolesId',$rolesId);
        $this->assign('orgId',$this->orgId);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function commentList() {
        $page = input('page',1);
        $size = input('size',20);
        $day = input('day',1);//日,月,总计
        $mode = input('mode',0);

        $where[] = ['o.del','=',0];
        $where[] = ['o.org_id','=',$this->orgId];
        $where[] = ['c.type','=',$mode];

        if($day == 1){    //日订单
            $where[] = ['c.create_yyyymmdd','=',date('Ymd')];
        }else if($day == 2){  //月订单
            $where[] = ['c.create_yyyymm','=',date('Ym')];
        }

        if($page > 1){
            $list = Db::name('comment')
                ->alias('c')
                ->field('c.*')
                ->join('orders o','o.id=c.from_id')
                ->where($where)
                ->order('c.id desc')
                ->page($page,$size)
                ->select();
            foreach ($list as $k=>$v){
                $list[$k]['user_name']= Db::name('user')->where('id',$v['user_id'])->value('real_name');
            }
            HelpHander::success($list);
        }else{
            $list = Db::name('comment')
                ->alias('c')
                ->field('c.*')
                ->join('orders o','o.id=c.from_id')
                ->where($where)
                ->order('c.id desc')
                ->page($page,$size)
                ->select();
            foreach ($list as $k=>$v){
                $list[$k]['user_name']= Db::name('user')->where('id',$v['user_id'])->value('real_name');
            }

        }
        $this->assign('list',$list);
        $this->assign('day',$day);
        $this->assign('mode',$mode);
        $this->assign('orgId',$this->orgId);
        return $this->fetch();
    }

    public function commentDetails(){
        $id = input('id',0);
        $info = Db::name('comment')->where('id',$id)->find();
        $info['user_name'] = Db::name('user')->where('id',$info['user_id'])->value('real_name');

        $this->assign('info',$info);
        return $this->fetch();
    }
    //日常工作统计
    public function dailyList() {
        $page = input('page',1);
        $size = input('size',20);
        $day = input('day',0);//日,月,总计

        if($day == 0){    //日订单
            $where[] = ['create_yyyymmdd','=',date('Ymd')];
        }else if($day == 1){  //月订单
            $where[] = ['create_yyyymm','=',date('Ym')];
        }
        $where[] = ['org_id','=',$this->orgId];
        $dailyIds = Db::name('daily_record')->where($where)->group('daily_id')->column('daily_id');
        if($dailyIds){
            $map[] = ['id','in',$dailyIds];
        }else{
            $map[] = ['id','in',[0]];
        }
        if($page>1){
            $list = Db::name('daily')
                ->field('id,title,enable')
                ->where('org_id',$this->orgId)
                ->where($map)
                ->order('id desc')
                ->page($page,$size)
                ->select();
            foreach ($list as $k=>$v){
                $user =  Db::name('daily_user')
                    ->alias('du')
                    ->join('user u','u.id=du.user_id')
                    ->where('du.daily_id',$v['id'])
                    ->where('u.del',0)
                    ->where('u.enable',1)
                    ->column('u.real_name');

                $list[$k]['work_user']=$user?implode(',',$user):'';
            }
            HelpHander::success($list);
        }else{
            $list = Db::name('daily')
                ->field('id,title,enable')
                ->where('org_id',$this->orgId)
                ->where($map)
                ->order('id desc')
                ->page($page,$size)
                ->select();
            foreach ($list as $k=>$v){
                $user =  Db::name('daily_user')
                    ->alias('du')
                    ->join('user u','u.id=du.user_id')
                    ->where('du.daily_id',$v['id'])
                    ->where('u.del',0)
                    ->where('u.enable',1)
                    ->column('u.real_name');

                $list[$k]['work_user']=$user?implode(',',$user):'';
            }
        }
        $this->assign('list',$list);
        $this->assign('day',$day);
        $this->assign('orgId',$this->orgId);
        return $this->fetch();

    }

    public function dailyRecordList() {
        $page = input('page',1);
        $size = input('size',20);
        $day = input('day',0);//日,月,总计

        if($day == 0){    //日订单
            $where[] = ['create_yyyymmdd','=',date('Ymd')];
        }else if($day == 1){  //月订单
            $where[] = ['create_yyyymm','=',date('Ym')];
        }
        $where[] = ['org_id','=',$this->orgId];
        if($page>1){
            $list = Db::name('daily_record')
                ->field('id,user_id,create_time,org_id')
                ->where($where)
                ->order('id desc')
                ->page($page,$size)
                ->select();
            foreach ($list as $k=>$v){
                $list[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
            }
            HelpHander::success($list);
        }else{
            $list = Db::name('daily_record')
                ->field('id,user_id,create_time,org_id')
                ->where($where)
                ->order('id desc')
                ->page($page,$size)
                ->select();
            foreach ($list as $k=>$v){
                $list[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
            }
        }
        $this->assign('list',$list);
        $this->assign('day',$day);
        $this->assign('orgId',$this->orgId);
        return $this->fetch();
    }

    public function dailyRecordDetails(){
        $id = input('id',0);
        $info = Db::name('daily_record')->where('id',$id)->find();
        $info['img'] = $info['images']?explode(',',$info['images']):[];
        //任务内容
        $daily = Db::name('daily')->where('id',$info['daily_id'])->find();
        $info['daily_content'] = $daily['content'] ? $daily['content']:'';
        $info['daily_title'] = $daily['title'] ? $daily['title']:'';
        $info['user_name'] = Db::name('user')->where('id',$info['user_id'])->value('real_name');
        $info['forms'] = json_decode($info['check_json'],true);

        $this->assign('info',$info);
        return $this->fetch();
    }

    //设备台账统计
    public function deviceList() {
        $page = input('page',1);
        $size = input('size',20);
        $day = input('day',0);//日,月,总计

        if($day == 0){    //日订单
            $where[] = ['create_yyyymmdd','=',date('Ymd')];
        }else if($day == 1){  //月订单
            $where[] = ['create_yyyymm','=',date('Ym')];
        }
        $where[] = ['org_id','=',$this->orgId];

        $deviceIds = Db::name('device_record')->where($where)->group('device_id')->column('device_id');
        if($deviceIds){
            $map[] = ['id','in',$deviceIds];
        }else{
            $map[] = ['id','in',[0]];
        }
        if($page>1){
            $list = Db::name('device')
                ->field('id,title,enable')
                ->where('org_id',$this->orgId)
                ->where($map)
                ->order('id desc')
                ->page($page,$size)
                ->select();
            foreach ($list as $k=>$v){
                $user =  Db::name('device_user')
                    ->alias('du')
                    ->join('user u','u.id=du.user_id')
                    ->where('du.device_id',$v['id'])
                    ->where('u.del',0)
                    ->where('u.enable',1)
                    ->column('u.real_name');

                $list[$k]['work_user']=$user?implode(',',$user):'';
            }
            HelpHander::success($list);
        }else{
            $list = Db::name('device')
                ->field('id,title,enable')
                ->where('org_id',$this->orgId)
                ->where($map)
                ->order('id desc')
                ->page($page,$size)
                ->select();
            foreach ($list as $k=>$v){
                $user =  Db::name('device_user')
                    ->alias('du')
                    ->join('user u','u.id=du.user_id')
                    ->where('du.device_id',$v['id'])
                    ->where('u.del',0)
                    ->where('u.enable',1)
                    ->column('u.real_name');

                $list[$k]['work_user']=$user?implode(',',$user):'';
            }
        }
        $this->assign('list',$list);
        $this->assign('day',$day);
        $this->assign('orgId',$this->orgId);
        return $this->fetch();
    }

    public function deviceRecordList() {
        $page = input('page',1);
        $size = input('size',20);
        $day = input('day',0);//日,月,总计

        if($day == 0){    //日订单
            $where[] = ['create_yyyymmdd','=',date('Ymd')];
        }else if($day == 1){  //月订单
            $where[] = ['create_yyyymm','=',date('Ym')];
        }
        $where[] = ['org_id','=',$this->orgId];
        if($page>1){
            $list = Db::name('device_record')
                ->field('id,user_id,create_time,org_id')
                ->where($where)
                ->order('id desc')
                ->page($page,$size)
                ->select();
            foreach ($list as $k=>$v){
                $list[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
            }
            HelpHander::success($list);
        }else{
            $list = Db::name('device_record')
                ->field('id,user_id,create_time,org_id')
                ->where($where)
                ->order('id desc')
                ->page($page,$size)
                ->select();
            foreach ($list as $k=>$v){
                $list[$k]['user_name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
            }
        }
        $this->assign('list',$list);
        $this->assign('day',$day);
        $this->assign('orgId',$this->orgId);
        return $this->fetch();
    }

    public function deviceRecordDetails(){
        $id = input('id',0);
        $info = Db::name('device_record')->where('id',$id)->find();
        $info['img'] = $info['images']?explode(',',$info['images']):[];
        //任务内容
        $daily = Db::name('device')->where('id',$info['device_id'])->find();
        $info['device_content'] = $daily['content'] ? $daily['content']:'';
        $info['device_title'] = $daily['title'] ? $daily['title']:'';
        $info['user_name'] = Db::name('user')->where('id',$info['user_id'])->value('real_name');
        $info['forms'] = json_decode($info['check_json'],true);

        $this->assign('info',$info);
        return $this->fetch();
    }

    public function complain(){
        $type = input('type',0);

        $week = Time::week();

        $sw = date('Ymd',$week[0]);
        $ew = date('Ymd',$week[1]);

        if($type == 0){
            $map[] = ['create_yyyymmdd','>=',$sw];
            $map[] = ['create_yyyymmdd','<=',$ew];
        }elseif ($type == 1){
            $map[] = ['create_yyyymm','=',date('Ym')];
        }
        $map[] = ['org_id','=',$this->orgId];
        $map[] = ['del','=',0];

        $data = [
            ['name'=>'已处理','status'=>2],
            ['name'=>'处理中','status'=>1],
            ['name'=>'待处理','status'=>0],
        ];

        foreach ($data as $k=>$v){
            $count = 0;
            if($v['status'] == 2){
                $complain =  Db::name('complain')->where($map)->select();
                foreach ($complain as $kk=>$vv){
                    if($vv['status'] == 1 && $vv['order_id'] > 0){
                        $order = Db::name('orders')->where('id',$vv['order_id'])->find();
                        if(in_array($order['order_mode'],[3,5,6])){
                            $count +=1;
                        }
                    }elseif ($vv['status'] == 1 && $vv['order_id'] == 0){
                           $count +=1;
                    }
                }
            }elseif ($v['status'] == 1){
                $complain =  Db::name('complain')->where($map)->select();
                foreach ($complain as $kk=>$vv){
                    if($vv['status'] == 1 && $vv['order_id'] > 0){
                        $order = Db::name('orders')->where('id',$vv['order_id'])->find();
                        if(!in_array($order['order_mode'],[3,5,6])){
                            $count +=1;
                        }
                    }
                }
            }elseif ($v['status'] == 0){
                $count =  Db::name('complain')->where($map)->where('status',0)->count();
            }

            $data[$k]['cost'] = $count;
        }

        $count =  Db::name('complain')->where($map)->count();

        $this->assign('list',$data);
        $this->assign('count',$count);
        $this->assign('orgId',$this->orgId);
        $this->assign('type',$type);
        return $this->fetch();
    }

    public function complainList(){
        $type = input('type/d',0);
        $status= input('status',-1);

        $week = Time::week();

        $sw = date('Ymd',$week[0]);
        $ew = date('Ymd',$week[1]);

        if($type == 0){
            $map[] = ['create_yyyymmdd','>=',$sw];
            $map[] = ['create_yyyymmdd','<=',$ew];
        }elseif ($type == 1){
            $map[] = ['create_yyyymm','=',date('Ym')];
        }
        $map[] = ['org_id','=',$this->orgId];
        $map[] = ['del','=',0];

        $list = [];
        if($status == 0){
            $list =  Db::name('complain')->where($map)->where('status',0)->order('id desc')->select();
            foreach ($list as $k=>$v){
                $list[$k]['statusTxt'] = '待处理';
            }
        }elseif ($status == -1){
            $list = Db::name('complain')->where($map)->order('id desc')->select();
            foreach ($list as $k=>$v){
                $statusTxt = '';
                if($v['status'] == 1 && $v['order_id'] == 0){
                    $statusTxt = '已处理';
                }elseif($v['status'] == 1 && $v['order_id'] > 0){
                    $order = Db::name('orders')->where('id',$v['order_id'])->find();
                    if(in_array($order['order_mode'],[3,5,6])){
                        $statusTxt = '已处理';
                    }else{
                        $statusTxt = '处理中';
                    }
                }elseif($v['status'] == 0){
                    $statusTxt = '待处理';
                }
                $list[$k]['statusTxt'] = $statusTxt;
            }

        } else{
            $complain =  Db::name('complain')->where($map)->select();
            foreach ($complain as $k=>$v){
                $statusTxt = '';
                if($v['status'] == 1 && $v['order_id'] == 0){
                    $statusTxt = '已处理';
                }else if($v['status'] == 1 && $v['order_id'] > 0){
                    $order = Db::name('orders')->where('id',$v['order_id'])->find();
                    if(in_array($order['order_mode'],[3,5,6])){
                        $statusTxt = '已处理';
                    }else{
                        $statusTxt = '处理中';
                    }
                }elseif($v['status'] == 0){
                    $statusTxt = '待处理';
                }
                $complain[$k]['statusTxt'] = $statusTxt;
            }

            foreach ($complain as $k=>$v){
                if($status == 2){
                    if($v['status'] == 1 && $v['order_id'] > 0){
                        $order = Db::name('orders')->where('id',$v['order_id'])->find();
                        if(in_array($order['order_mode'],[3,5,6])){
                            $list[$k] = $v;
                        }
                    }elseif($v['status'] == 1 && $v['order_id'] == 0){
                        $list[$k] = $v;
                    }
                }elseif ($status == 1){
                    if($v['status'] == 1 && $v['order_id'] > 0){
                        $order = Db::name('orders')->where('id',$v['order_id'])->find();
                        if(!in_array($order['order_mode'],[3,5,6])){
                            $list[$k] = $v;
                        }
                    }
                }
            }

        }

        foreach ($list as $k=>$v){
            if($v['hide'] == 1){
                $list[$k]['user_name'] = '匿名';
            }else{
                $list[$k]['user_name'] = Db::name('user')->where('id',$v['create_user_id'])->value('real_name');
            }

        }

        $this->assign('list',$list);
        $this->assign('orgId',$this->orgId);
        $this->assign('type',$type);

        return $this->fetch();
    }

    public function complainDetail(){
        $id = input('id',0);
        $info = Db::name('complain')->where('id',$id)->find();
        $info['images'] = !empty($info['images'])?explode(',',$info['images']):[];
        $info['create_time'] = date('Y-m-d H:i',strtotime($info['create_time']));
        $statusTxt = '';
        if($info['status'] == 1 && $info['order_id'] == 0){
            $statusTxt = '已处理';
        }elseif($info['status'] == 1 && $info['order_id'] > 0){
            $order = Db::name('orders')->where('id',$info['order_id'])->find();
            if(in_array($order['order_mode'],[3,5,6])){
                $statusTxt = '已处理';
            }else{
                $statusTxt = '处理中';
            }
        }elseif($info['status'] == 0){
            $statusTxt = '待处理';
        }
        $info['statusTxt'] = $statusTxt;

        if($info['hide'] == 1){
            $info['user_name'] = '匿名';
            $info['mobile'] = '';
        }else{
            $info['user_name'] = Db::name('user')->where('id',$info['create_user_id'])->value('real_name');
            $info['mobile'] = Db::name('user')->where('id',$info['create_user_id'])->value('mobile');
        }
        $info['dep_name'] = db('dep')->where('id',$info['dep_id'])->value('title');


        $this->assign('info',$info);

        return $this->fetch();
    }


    public function waste() {
        $type = input('type/d',0);
        $map[] = ['org_id','=',$this->orgId];

        if($type == 0){ //日统计
            $map[] = ['create_yyyymmdd','=',date('Ymd')];
        } else if($type == 1){ //月统计
            $map[] = ['create_yyyymm','=',date('Ym')];
        }
        $map[] = ['del','=',0];
        $wasteTypes = model("WasteType")->list1($this->orgId);
        foreach ($wasteTypes as $k=>$v){
            $count = Db::name('waste_record')->where($map)->where('cateid',$v['waste_id'])->sum('weight');
            $wasteTypes[$k]['count'] = round($count/1000,2)."kg";
            if($v['type'] == 1){
                $count = Db::name('waste_record')->where($map)->where('cateid',$v['waste_id'])->sum('num');
                $wasteTypes[$k]['count'] = round($count/1000,2)."kg".'('.$count.$v['unit'].')';
            }


        }

        $this->assign('wasteTypes',$wasteTypes);
        $this->assign('orgId',$this->orgId);
        $this->assign('type',$type);

        return $this->fetch();
    }
}