<?php
/**
 * Copyright (C) LJCMS All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2016.03.19
 */

class countOModel extends X {
    function badge_count_schedule(){
                    $user_id=$_SESSION['USER_AUTH_KEY'];
                    $data=date("Y-m-d");
                    $where      = " WHERE user_id='$user_id' AND `is_del` = 0 AND `start_time` >= '$data'";
                    $countsql   = "SELECT COUNT(*) AS my_count FROM ".DB_PREFIXOA."schedule ".$where;
                    $total      = parent::$obj->fetch_count($countsql);
                    return $total;
    }
function badge_count_task() {
	return $this->badge_count_no_finish_task() + $this->badge_count_dept_task() + $this->badge_count_no_assign_task();
}

function badge_count_no_finish_task() {
	//等我接受的任务
                $user_id=$_SESSION['USER_AUTH_KEY']; 
                $sql = "SELECT `task_id` FROM ".DB_PREFIXOA."task_log WHERE type='1' AND status<'20' AND executor='$user_id'";
                $task_list=parent::$obj->getall($sql);
                $task_todo_count = 0;
                if (!empty($task_list)) {
                        $task_ids="";
                        foreach ($task_list as $v){
                            $task_ids .= $v['task_id'].",";
                        }
                       $task_ids=substr($task_ids, 0, -1);
                       $sql = "SELECT COUNT(*) AS my_count FROM ".DB_PREFIXOA."task WHERE is_del='0' AND id in ($task_ids)";
                       $task_todo_count=parent::$obj->fetch_count($sql);
                }
                return $task_todo_count;
}

function badge_count_no_assign_task() {
	//等我接受的任务

	$sql = "select id from ".DB_PREFIXOA."task task where status=0 and not exists (select * from ".DB_PREFIXOA."task_log task_log where task.id=task_log.task_id)";
	$task_list=parent::$obj->getall($sql);
	if (empty($task_list)) {
		return 0;
	} else {
                       $task_ids="";
                        foreach ($task_list as $v){
                            $task_ids .= $v['id'].",";
                        }
                       $task_ids=substr($task_ids, 0, -1);
                       $sql = "SELECT COUNT(*) AS my_count FROM ".DB_PREFIXOA."task WHERE is_del='0' AND id in ($task_ids)";
                       $task_no_assign_count=parent::$obj->fetch_count($sql);
                         return $task_no_assign_count;

	}
}

function badge_count_dept_task() {
             $userid= $_SESSION['USER_AUTH_KEY']; 
             $sql = "SELECT * FROM ".DB_PREFIXOA."user WHERE id='".$userid."'";
             $user=parent::$obj->fetch_first($sql);
             $dept_id=$user['dept_id'];
             $sql = "SELECT `task_id` FROM ".DB_PREFIXOA."task_log WHERE type='2' AND status='0' AND executor='$dept_id'";
                $task_list=parent::$obj->getall($sql);
                $task_todo_count = 0;
                if (!empty($task_list)) {
                        $task_ids="";
                        foreach ($task_list as $v){
                            $task_ids .= $v['task_id'].",";
                        }
                       $task_ids=substr($task_ids, 0, -1);
                       $sql = "SELECT COUNT(*) AS my_count FROM ".DB_PREFIXOA."task WHERE is_del='0' AND id in ($task_ids)";
                       $task_todo_count=parent::$obj->fetch_count($sql);
                }
                return $task_todo_count;
}
    function badge_count_leave(){
                    $user_id=$_SESSION['USER_AUTH_KEY'];
                    $where      = " WHERE user_id='$user_id' AND `status` in (0,2)";
                    $countsql   = "SELECT COUNT(*) AS my_count FROM ".DB_PREFIXOA."leave ".$where;
                    $total      = parent::$obj->fetch_count($countsql);
                    return $total; 
    }
    function badge_count_leave_shen(){
               $dept = parent::model('dept', 'oa');
                $dept_id=$_SESSION['dept_id'];
                $deptinfo = $dept -> get_top_menu_data($dept_id);
                $searchsql=" WHERE status='0'";
                if($deptinfo['pid']!=0){
                    $user = parent::model('user', 'oa');
                    $where ="dept_id='$dept_id' AND is_del='0'";
                    $user_list=$user->get_List($where);
                    $search="";
                    foreach($user_list as $v){
                         $search .=$v['id'].",";
                    }
                    $search=substr($search,0,strlen($search)-1); 
                    $searchsql .=" AND user_id in ($search)";
                }
                    $countsql   = "SELECT COUNT(*) AS my_count FROM ".DB_PREFIXOA."leave ".$searchsql;
                    $total      = parent::$obj->fetch_count($countsql);
                    return $total; 
    }    
    function badge_count_todo(){
 	    
                   $user_id=$_SESSION['USER_AUTH_KEY'];
                 
                    $where      = " WHERE user_id='$user_id' AND `status` in (1,2)";
                    $countsql   = "SELECT COUNT(*) AS my_count FROM ".DB_PREFIXOA."todo ".$where;
                    $total      = parent::$obj->fetch_count($countsql);
                    return $total;
    }
    function badge_count_message(){
 	//获取最新消息
 
                    $user_id=$_SESSION['USER_AUTH_KEY'];
                 
                    $where      = " WHERE owner_id='$user_id' AND receiver_id='$user_id' AND is_read='0'";
                    $countsql   = "SELECT COUNT(*) AS my_count FROM ".DB_PREFIXOA."message ".$where;
                    $total      = parent::$obj->fetch_count($countsql);
                    return $total;
    }    
    function badge_count_system_folder($id) {
            //获取最新消息
               $sql = "SELECT * FROM ".DB_PREFIXOA."system_folder WHERE id='".$id."'";
               $controller=parent::$obj->fetch_first($sql);   
            $count = 0;
            switch ($controller['controller']) {
                    case 'Info' :
                            $count = 0;
                            break;
                    default :
                            break;
            }
            return $count;
    }
      function badge_count_work(){
                 $dept = parent::model('dept', 'oa');
                $dept_id=$_SESSION['dept_id'];
                $deptinfo = $dept -> get_top_menu_data($dept_id);
                $searchsql.="WHERE  status='0'";
                if($deptinfo['pid']!=0){
                    $user = parent::model('user', 'oa');
                    $where ="dept_id='$dept_id' AND is_del='0'";
                    $user_list=$user->get_List($where);
                    $search="";
                    foreach($user_list as $v){
                         $search .=$v['id'].",";
                    }
                    $search=substr($search,0,strlen($search)-1); 
                    $searchsql .=" AND user_id in ($search)";
                }  
                                  $countsql   = "SELECT COUNT(*) AS my_count FROM ".DB_PREFIXOA."work_log ".$searchsql;
                    $total      = parent::$obj->fetch_count($countsql);
                    return $total; 
    }

    
}