<?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 taskOModel 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."task ".$where;
                    $total      = parent::$obj->fetch_count($countsql);
                    $sql        = "SELECT * FROM ".DB_PREFIXOA."task ".$where." ORDER BY id desc LIMIT ".$start.", ".$items['pagesize']."";
                    $array = parent::$obj->getall($sql);
                    return array($total, $array);
        }

          public function get_data($id){
	        $sql = "SELECT * FROM ".DB_PREFIXOA."task WHERE id='".$id."'";
                return parent::$obj->fetch_first($sql);
	}
        
        public function get_task_loginfo($where){
                $sql = "SELECT * FROM ".DB_PREFIXOA."task_log WHERE".$where." ORDER BY id desc";
                return parent::$obj->fetch_first($sql);
        }

        public function get_addinfo(){
                        $sql = "SELECT * FROM ".DB_PREFIXOA."task ORDER BY id desc";
                return parent::$obj->fetch_first($sql);
        }
        public function get_task_log($id){
            $sql = "SELECT * FROM ".DB_PREFIXOA."task_log WHERE task_id='$id' ORDER BY id desc";
            return parent::$obj->getall($sql);
        }

                public function doEdit($id,$arr){
                unset($arr['id']);
                unset($arr['opmode']);
                unset($arr['ajax']);           
                $result = parent::$obj->update(DB_PREFIXOA.'task', $arr, 'id='.$id.'');
                return $result;
        }  
        public function doEditlog($id,$arr){
                unset($arr['id']);
                unset($arr['ajax']);           
                $result = parent::$obj->update(DB_PREFIXOA.'task_log', $arr, 'id='.$id.'');
                return $result;
        }
        public function add_log($array) {
       
             return parent::$obj->insert(DB_PREFIXOA.'task_log', $array); 
      }
        public function doAdd($array) {
             unset($array['ajax']);
             unset($array['opmode']);
             $userid=$array['user_id']= $_SESSION['USER_AUTH_KEY']; 
             $sql = "SELECT * FROM ".DB_PREFIXOA."user WHERE id='".$userid."'";
             $user=parent::$obj->fetch_first($sql);
             $array['user_name']=$user['name'];
             $array['is_del']=0;
             $dept_id=$array['dept_id']=$user['dept_id'];
             $sql1 = "SELECT * FROM ".DB_PREFIXOA."dept WHERE id='".$dept_id."'";
             $dept=parent::$obj->fetch_first($sql1);
             $array['dept_name']=$dept['name'];
             $array['create_time']= time();
             $array['task_no']=date('Y') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
             return parent::$obj->insert(DB_PREFIXOA.'task', $array); 
      }
      public function del($id){
        $sql = "DELETE FROM ".DB_PREFIXOA."task_log WHERE task_id='".intval($id)."'";
        parent::$obj->query($sql);
        $sql = "DELETE FROM ".DB_PREFIXOA."task WHERE id='".intval($id)."'";
        return parent::$obj->query($sql);
      }
      
      public function popup($id){
          $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.position_id = '$id' AND User.is_del = 0 ";
        $array = parent::$obj->getall($sql);
		return $array;
          }
          
          public function addlog($data){
                $executor_list = $data['executor'];
		$executor_list = array_filter(explode(';', $executor_list));
				
		if (!empty($executor_list)) {
			foreach ($executor_list as $key => $val) {
				$tmp = explode('|', $val);
				$executor_name = $tmp[0];
				$executor = $tmp[1];

				if (strpos($executor, "dept_") !== false) {
					$type = 2;
					$executor = str_replace('dept_', '', $executor);					
//					$where['dept_id']=array('eq',$executor);					
//					$dept_user_list=M('User')->where($where)->getField('id',true);
//
//					foreach($dept_user_list as $val){
//						$auth=D("Role")->get_auth('Task',$val);
//						if($auth['admin']){
//							$user_list[]=$val;
//						}
//					}
				} else {
					$type = 1;
				//	$user_list[] = $executor;
				}

				$log_data['executor'] = $executor;
				$log_data['executor_name'] = $executor_name;
				$log_data['type'] = $type;
				$log_data['assigner'] = $data['user_id'];
				$log_data['task_id'] = $data['id'];
                                parent::$obj->insert(DB_PREFIXOA.'task_log', $log_data); 
			}
			
//			$push_data['type'] = '任务';
//			$push_data['action'] = '需要执行';
//			$push_data['title'] = "来自：" . get_dept_name()."-".get_user_name();
//			$push_data['content'] = "标题：" . $data['name'];
//			$push_data['url'] = U('Task/read',"id={$data['id']}&return_url=Task/index");
//			
//			send_push($push_data, $user_list);
		}		
          }
          
          public 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;
          }
          
          public 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;
        }
        public 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;

	}
       
}
        
}
?>
