<?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 messageOModel extends X {
        
        public function getList($items){
                    $where      = " WHERE 1=1".$items['searchsql'];
                    $start      = ($items['page']-1)*$items['pagesize'];
                    $countsql   = "SELECT COUNT(*) AS my_count FROM ".DB_PREFIXOA."message ".$where;
                    $total      = parent::$obj->fetch_count($countsql);
                    $sql        = "SELECT * FROM ".DB_PREFIXOA."message ".$where." ORDER BY create_time desc LIMIT ".$start.", ".$items['pagesize']."";
                    $array = parent::$obj->getall($sql);
                    return array($total, $array);
        }
        
        public function get_List($items){
                //$sql = "SELECT * FROM ".DB_PREFIXOA."todo WHERE ".$where."ORDER BY priority desc,sort asc";
            	$user_id = $_SESSION['USER_AUTH_KEY'];
                $start      = ($items['page']-1)*$items['pagesize'];
		$sql = "";
		$sql .= "select   t2.*, t3.count, t3.count- t3.is_read unread ";
		$sql .= "  from   " . DB_PREFIXOA . "message t2, ";
		$sql .= "         ( select  max(id) id, count(*) count, SUM(t1.is_read) is_read ";
		$sql .= "              from   (select   id, ";
		$sql .= "                               sender_id, ";
		$sql .= "                               receiver_id, ";
		$sql .= "                               create_time, ";
		$sql .= "                               is_read ";
		$sql .= "                        from " . DB_PREFIXOA . "message ";
		$sql .= "                       where   is_del = '0' ";
		$sql .= "                               and owner_id = '$user_id' ";
		$sql .= "                               and receiver_id = '$user_id' ";
		$sql .= "                      union ";
		$sql .= "                      select   id, ";
		$sql .= "                               a.receiver_id, ";
		$sql .= "                               a.sender_id, ";
		$sql .= "                               create_time, ";
		$sql .= "                                1 is_read ";
		$sql .= "                        from " . DB_PREFIXOA . "message a ";
		$sql .= "                       where   a.is_del = 0 and owner_id =  '$user_id' and sender_id =  '$user_id') ";
		$sql .= "                     t1 ";
		$sql .= "             where   t1.receiver_id = '$user_id' ";
		$sql .= "          group by   t1.sender_id) t3 ";
		$sql .= " where  t3.id = t2.id order by t3.is_read,t2.create_time desc LIMIT ".$start.", ".$items['pagesize']."";
                $array = parent::$obj->getall($sql);
		return $array;
        }

  
        public function get_data($id,$userid){
	        $sql = "SELECT * FROM ".DB_PREFIXOA."todo WHERE id='".$id."' AND user_id='$userid'";
                return parent::$obj->fetch_first($sql);
	}


        public function get_read($sql){
             $list = parent::$obj->getall($sql);
            return $list;
        }

                public function get_menu_list(){
            $sql='SELECT `id`,`pid`,`name`,`is_del` FROM `oa_dept` ORDER BY sort asc';
            $list = parent::$obj->getall($sql);
            return $list;
        }

        public function get_DeptGrade_list(){
             $sql=' SELECT `id`,`name` FROM `oa_dept_grade` WHERE ( is_del=0 ) ORDER BY sort asc';
             $list = parent::$obj->getall($sql);
             return $list;
        }

                public function doEdit($id,$arr){
                unset($arr['id']);
                unset($arr['opmode']);   
                 unset($arr['ajax']);   
                $result = parent::$obj->update(DB_PREFIXOA.'message', $arr, 'id='.$id.'');
                return $result;
        }  
        public function doAdd($array) {
           return parent::$obj->insert(DB_PREFIXOA.'message', $array); 
      }
      public function del($id){
        $sql = "DELETE FROM ".DB_PREFIXOA."todo WHERE id='".intval($id)."'";
        return parent::$obj->query($sql);
      }
        
      public function popup($dept){
          $sql="SELECT User.id AS id,User.emp_no AS emp_no,User.name AS name,User.letter AS letter,User.dept_id AS dept_id,User.position_id AS position_id,User.email AS email,User.duty AS duty,User.office_tel AS office_tel,User.mobile_tel AS mobile_tel,User.pic AS pic,User.birthday AS birthday,User.sex AS sex,User.password AS password,User.is_del AS is_del,Position.name AS position_name,Dept.name AS dept_name FROM oa_user User  JOIN oa_position Position ON Position.id=User.position_id  JOIN oa_dept Dept ON Dept.id=User.dept_id WHERE User.dept_id IN ($dept) AND User.is_del = 0 ";
                          $array = parent::$obj->getall($sql);
		return $array;
      }
}
?>
