<?php
namespace app\api\controller\v1;
use app\api\controller\Base;
use app\common\model\Config;
use app\hander\HelpHander;
use think\Db;

class AppIcon extends Base
{
    public function lists(){
        $orgId = input('orgId',0);
        $token = input('token','','trim');
        $rolesId = input('rolesId',0);
        $type = input('type',0);
        $ret = [];
        $rolesIds = db('roles')->where('id',$rolesId)->value('appauths');
        $orgapps = Db::name('org')->where('id',$orgId)->value('appauths');
        $rolesIds = $rolesIds?explode(',',$rolesIds):[];
        $orgapps = $orgapps?explode(',',$orgapps):[];

        $ret['appIcons'] = [];
        if($rolesIds || in_array($rolesId,[1,2])){
            $map = [];
            if(in_array($rolesId,[1,2])){
                if($orgapps){
                    $map[] = ['id','in',$orgapps];
                }else{
                    $map[] = ['id','=',0];
                }
            }else{
                $ids = array_unique(array_intersect($rolesIds,$orgapps));
                if($ids){
                    $map[] = ['id','in',$ids];
                }else{
                    $map[] = ['id','=',0];
                }
            }
            $map[] = ['enable','=',1];
            $map[] = ['type','=',$type];
            $map[] = ['del','=',0];
            if($type == 0){ //0=首页 1=统计页
                $map[] = ['pid','=',0];
            }
            $appIcon = db('app_icon')
                ->field('id,name,mode,path,url,des')
                ->where($map)
                ->order(['sort'=>'desc','id'=>'asc'])
                ->select();

            foreach ($appIcon as $k=>$v){
                if($v['url']){
                    if(strstr($v['url'],'?') === false){
                        $appIcon[$k]['url'] = $v['url'].'?token='.$token.'&orgId='.$orgId;
                    }else{
                        $appIcon[$k]['url'] = $v['url'].'&token='.$token.'&orgId='.$orgId;
                    }
                }
                $c = 0;
                if($v['id']==10){//专项保洁
                    $c = model('CleanTask')
                        ->taskCount2(0,$this->userId,$orgId);
                }elseif ($v['id']==7){//设备维保
                    $c = model('Device')
                        ->taskCount($this->userId,$orgId);
                }elseif ($v['id']==17){//巡更任务
                    $c = model('PatrolTask')
                        ->taskCount($this->userId,$orgId,1);
                }elseif ($v['id']==18){//巡检任务
                    $c = model('PatrolTask')
                        ->taskCount($this->userId,$orgId,4);
                }elseif ($v['id']==19){//巡查任务
                    $c = model('PatrolTask')
                        ->taskCount($this->userId,$orgId,3);
                }elseif ($v['id']==20){//巡视任务
                    $c = model('PatrolTask')
                        ->taskCount($this->userId,$orgId,2);
                }
                $appIcon[$k]['name'] = $c>0?$v['name'].'('.$c.')':$v['name'];

            }
            $ret['appIcons'] = $appIcon;
        }
        $orgGrabOrder = getOrgGrabOrder($this->userId,$this->orgId);
        $ret['org_grab_order'] = $orgGrabOrder;
        HelpHander::success($ret,'成功');
    }


    public function notice(){
        $ret['bannerList'] = Db::name('banner')
            ->where('del',0)
            ->where('enable',1)
            ->where('type',0)
            ->select();
        $notice = Db::name('notice')
            ->where('org_id',$this->orgId)
            ->where('user_id',$this->userId)
            ->where('del',0)
            ->field('id,title')
            ->order('id','desc')
            ->find();
        if(!$notice){
            $notice =   Db::name('notice_common')
                ->where('org_id',$this->orgId)
                ->where('del',0)
                ->field('id,title')
                ->order('id','desc')
                ->find();
        }
        $ret['notice']  = $notice?$notice:null;
        HelpHander::success($ret,'成功');
    }

    public function wxlist(){
        $token = input('token','','trim');
        $modes = ['1','2','3','4','12','15','42','48'];
        $appicons = Db::name('org')->where('id',$this->orgId)->value('appauths');

        $map[] = ['enable','=',1];
        $map[] = ['type','=',0];
        $map[] = ['pid','=',0];
        $map[] = ['del','=',0];
        $map[] = ['mode','in',$modes];
        if($appicons){
            $map[] = ['id','in',explode(',',$appicons)];
        }

        $appIcon = db('app_icon')
            ->field('id,name,mode,path,url,des')
            ->where($map)
            ->order(['sort'=>'desc','id'=>'asc'])
            ->select();
        foreach ($appIcon as $k=>$v){
            if($v['url']){
                if(strstr($v['url'],'?') === false){
                    $appIcon[$k]['url'] = $v['url'].'?token='.$token.'&orgId='.$this->orgId;
                }else{
                    $appIcon[$k]['url'] = $v['url'].'&token='.$token.'&orgId='.$this->orgId;
                }
            }
        }
        HelpHander::success($appIcon?$appIcon:[],'成功');
    }
}