<?php
namespace app\common\model;

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

class UserAppIcon extends Model
{

    public function queryAppIconList($userId,$orgId){
        // 获取用户权限
        $roles = Db::name('user_roles')
            ->alias('ur')
            ->join('roles r','r.id = ur.roles_id')
            ->where('ur.user_id',$userId)
            ->where('r.org_id',$orgId)
            ->where('r.del',0)
            ->where('r.enable',1)
            ->column('r.appicons');
        $rolesapps = [];
        if($roles){
            foreach ($roles as $v){
                if($v){
                    $rolesapps = array_merge($rolesapps,explode(',',$v));
                }
            }
        }
        $rolesapps = array_unique($rolesapps);

        $appicons = $this->getUserIcons($userId,$orgId);

        $appiconsjj = array_intersect($rolesapps,$appicons);

        $lists = [];
        if($appiconsjj){
            $lists = Db::name('app_icon')
                ->where('id','in',$appiconsjj)
                ->where('del',0)
                ->where('enable',1)
                ->select();
        }

        $banner =  Db::name('banner')
            ->where('type',1)
            ->where('enable',1)
            ->where('del',0)
            ->order('sort asc,id asc')
            ->select();

        //$notice = model('Notice')->queryUserLastNotice($userId,$orgId);
        $notice = Db::name('leader_ship')
            ->where('del',0)
            ->where('enable',1)
            ->field('content')
            ->order('id desc')
            ->find();

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

        $iconList[] = [
            'id' => 0,
            'name' => '综合总览'
        ];

        foreach ($iconList as $k=>$v){
            $appicon = [];
            if($rolesapps){
                $appicon = Db::name('app_icon')
                    ->where('cate_id',$v['id'])
                    ->where('id','in',$rolesapps)
                    ->where('del',0)
                    ->where('enable',1)
                    ->select();
            }

            $iconList[$k]['appIconList'] = $appicon?$appicon:[];
        }

        $data = [
            'commonIcon' => $lists?$lists:[],
            'bannerList' => $banner?$banner:[],
            'iconList' => $iconList,
            'latestNotice' => $notice
        ];
        return $data;
    }

    public function queryAppIconListNew($userId,$orgId){
        // 获取用户权限
        $roles = Db::name('user_roles')
            ->alias('ur')
            ->join('roles r','r.id = ur.roles_id')
            ->where('ur.user_id',$userId)
            ->where('r.org_id',$orgId)
            ->where('r.del',0)
            ->where('r.enable',1)
            ->column('r.appicons');
        $rolesapps = [];
        if($roles){
            foreach ($roles as $v){
                if($v){
                    $rolesapps = array_merge($rolesapps,explode(',',$v));
                }
            }
        }
//        $rolesapps = array_unique($rolesapps);

//        $appicons = $this->getUserIcons($userId,$orgId);

        $appiconsjj = array_unique($rolesapps);
        $lists = [];
        if($appiconsjj){
            $lists = Db::name('app_icon')
                ->where('id','in',$appiconsjj)
                ->where('del',0)
                ->where('enable',1)
                ->select();

        }


        $banner =  Db::name('banner')
            ->where('type',1)
            ->where('enable',1)
            ->where('del',0)
            ->order('sort asc,id asc')
            ->select();

//        $notice = model('Notice')->queryUserLastNotice($userId,$orgId);

        $notice = Db::name('leader_ship')
            ->where('del',0)
            ->where('enable',1)
            ->field('content')
            ->order('id desc')
            ->find();

        $approval = model('Approval')->queryApprovalCateList($userId,$orgId);



        $lists = $lists?$lists:[];
        $modes = [12,13];
        $fcarr = $zharr = [];
        $token = input('token');
//        dump($lists);
        foreach ($lists as $k=>$v){
            if($v['url']){
                if(strstr($v['url'],'?') === false){
                    $v['url'] = $v['url'].'?token='.$token.'&userId='.$userId;
                }else{
                    $v['url'] = $v['url'].'&token='.$token.'&userId='.$userId;
                }
            }

            if(in_array($v['mode'],$modes)){
                $fcarr[] = $v;
            }else{
                $zharr[] = $v;
            }
        }

//        halt($zharr);
        $cateIcon = [
//            [
//                'name' => '房产管理',
//                'child' => $fcarr
//            ],
            [
                'name' => '综合总览',
                'child' => $zharr
            ],
        ];

        $data = [
            'commonIcon' => $lists?$lists:[],
            'bannerList' => $banner?$banner:[],
            'approval' => $approval,
            'latestNotice' => $notice,
            'cateIcon' => $cateIcon
        ];
        return $data;
    }

    public function getUserIcons($userId,$orgId){
        $appicons = Db::name('user_app_icon')->where('user_id',$userId)->where('org_id',$orgId)->value('appicons');
        return $appicons?explode(',',$appicons):[];
    }

    public function updateAppIcon($iconId,$operation,$userId,$orgId){
        $info = Db::name('user_app_icon')
            ->where('user_id',$userId)
            ->where('org_id',$orgId)
            ->find();
        if($operation == 0){ // 添加
            if($info){
                $appicons = $info['appicons']?explode(',',$info['appicons']):[];
                if(in_array($iconId,$appicons)){
                    HelpHander::error('已添加过,无需重新添加');
                }
                $appicons[] = $iconId;
                $ret = Db::name('user_app_icon')->where('id',$info['id'])->setField('appicons',implode(',',$appicons));
            }else{
                $ret = Db::name('user_app_icon')->insert(['user_id'=>$userId,'appicons'=>$iconId,'org_id'=>$orgId]);
            }
            if(!$ret){
                HelpHander::error('操作失败');
            }
        }else{ // 删除
            if($info){
                $appicons = $info['appicons']?explode(',',$info['appicons']):[];
                if(!$appicons){
                    HelpHander::error('已删除');
                }
                $appicons = array_diff($appicons,[$iconId]);
                $ret = Db::name('user_app_icon')->where('id',$info['id'])->setField('appicons',implode(',',$appicons));
                if(!$ret){
                    HelpHander::error('操作失败');
                }
            }else{
                HelpHander::error('已删除');
            }
        }
        return true;
    }

}