<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');


class ding_value_model extends CI_Model {
        
    function __construct(){
        parent::__construct();
        $this->HT = $this->load->database('HT', TRUE);
    }

	//扫码获取相应人的信息
	function get_dingding_user($unionid){
        $sql = "select 
						*
				  from 
						Dingding_User 
				 where 
						ddu_Unionid = ?";
        $query = $this->HT->query($sql, array($unionid));
		if ($query->num_rows() > 0){
            $row = $query->row();
            return $row;
        }
        else{
			return FALSE;
        }
    }
	
	//根据姓名获取唯一unionid
	public function get_dingding_unionid($name){
		$sql = "select ddu_Unionid from Dingding_User where ddu_Name = ?";
        $query = $this->HT->query($sql, array($name));
		if ($query->num_rows() > 0){
            $row = $query->row();
            return $row;
        }
        else{
			return FALSE;
        }
	}
	
	
	//添加用户
	function add_dingding_user($name,$unionid,$mobile,$email,$position,$avatar,$time){
		$sql = "INSERT INTO Dingding_User (ddu_Name,ddu_Unionid,ddu_Mobile,ddu_Email,ddu_Position,ddu_Avatar,ddu_Datetime) VALUES (N?,?,?,?,N?,?,?)";
		$query = $this->HT->query($sql,array($name,$unionid,$mobile,$email,$position,$avatar,$time));
		if ($query){
			return TRUE;
		}else{
			return FALSE;
		}             
	}
	
	//添加value事件
	function add_value($data){
		$sql = "INSERT INTO Dingding_Value (ddv_Type,ddv_Name,ddv_User_Unionid,ddv_Comment_Name,ddv_Comment_Unionid,ddv_Content,ddv_Identify,ddv_Createtime) VALUES (?,N?,?,N?,?,N?,?,?)";
		$query = $this->HT->query($sql,array($data['type'],$data['user'],$data['user_unionid'],$data['comment_name'],$data['comment_unionid'],$data['content'],$data['mail_identify'],$data['createtime']));
		if($query){
			return TRUE;
		}else{
			return FALSE;
		}        
	}
	
	//统计like人数
	function count_like($unionid){
		$year_start = strtotime(date('Y-01-01', time()));
		$year_end = strtotime(date('Y-12-31', time()));
		$sql = "SELECT COUNT(*) AS count FROM Dingding_Value WHERE ddv_Comment_Unionid = ? AND ddv_Type = 'like' AND ddv_Type = 'like' and ddv_Createtime > '{$year_start}' and ddv_Createtime < '{$year_end}'";
		$query = $this->HT->query($sql,$unionid);
		$row = $query->row();
        return $row;
	}
	
	//获取like的人是那些
	function whos_like($unionid){
		$year_start = strtotime(date('Y-01-01', time()));
		$year_end = strtotime(date('Y-12-31', time()));
		$sql = "SELECT ddv_Name FROM Dingding_Value WHERE ddv_Comment_Unionid = ? AND ddv_Type = 'like' and ddv_Createtime > '{$year_start}' and ddv_Createtime < '{$year_end}' ORDER BY ddv_Sn DESC";
		$query = $this->HT->query($sql,$unionid);
		$row = $query->result();
        return $row;
	}
	
	//获取value点赞的人
	function value_whos_like($identify){
		$sql = "SELECT ddv_Name FROM Dingding_Value WHERE ddv_Identify = ?  and ddv_Type = 'like' and ddv_Comment_Name = 'value邮件' ORDER BY ddv_Createtime DESC";
		$query = $this->HT->query($sql,$identify);
		$row = $query->result();
        return $row;
	}
	
	//获取value拍砖的人
	function value_whos_unlike($identify){
		$sql = "SELECT ddv_Name FROM Dingding_Value WHERE ddv_Identify = ? and ddv_Type = 'unlike' and ddv_Comment_Name = 'value邮件' ORDER BY ddv_Createtime DESC";
		$query = $this->HT->query($sql,$identify);
		$row = $query->result();
        return $row;
	}
	
	//根据Identify获取value邮件
	function get_mail($identify){
		$sql = "SELECT * FROM Dingding_Mail where ddm_Identify = ?";
		$query = $this->HT->query($sql, array($identify));
		if ($query->num_rows() > 0){
            $row = $query->row();
            return $row;
        }
        else{
			return FALSE;
        }
	}
	
	//根据Identify获取value邮件中value的人
	function get_value($identify,$user_unionid){
		$sql = "select * from Dingding_user_mail left join Dingding_User on(ddum_Name = ddu_Name) where ddum_Identify = ? and ddu_Unionid != '$user_unionid'";
		$query = $this->HT->query($sql, array($identify));
		if ($query->num_rows() > 0){
            $row = $query->result();
            return $row;
        }
        else{
			return FALSE;
        }
	}
	
	//统计unlike人数
	function count_unlike($unionid){
		$year_start = strtotime(date('Y-01-01', time()));
		$year_end = strtotime(date('Y-12-31', time()));
		$sql = "SELECT COUNT(*) AS count FROM Dingding_Value WHERE ddv_Comment_Unionid = ? AND ddv_Type = 'unlike' and ddv_Createtime > '{$year_start}' and ddv_Createtime < '{$year_end}'";
		$query = $this->HT->query($sql,$unionid);
		$row = $query->row();
        return $row;
	}
	
	//获取unlike的人是那些
	function whos_unlike($unionid){
		$year_start = strtotime(date('Y-01-01', time()));
		$year_end = strtotime(date('Y-12-31', time()));
		$sql = "SELECT ddv_Name FROM Dingding_Value WHERE ddv_Comment_Unionid = ? AND ddv_Type = 'unlike' and ddv_Createtime > '{$year_start}' and ddv_Createtime < '{$year_end}' ORDER BY ddv_Sn DESC";
		$query = $this->HT->query($sql,$unionid);
		$row = $query->result();
        return $row;
	}
	
	//获取所有点评了的人的列表
	function all_comment($unionid){
		$year_start = strtotime(date('Y-01-01', time()));
		$year_end = strtotime(date('Y-12-31', time()));
		$sql = "SELECT * 
				  FROM Dingding_Value 
			 left join Dingding_User on ddu_Unionid = ddv_User_Unionid 
			 left join Dingding_Mail on ddv_Identify = ddm_Identify
				 where ddv_Comment_Unionid = '$unionid' 
				 and ddv_Createtime > '{$year_start}' and ddv_Createtime < '{$year_end}'
			  ORDER BY ddv_Sn DESC";
		$query = $this->HT->query($sql,$unionid);
		$row = $query->result();
        return $row;
	}
	/*
	//获取所有点评了的人的列表
	function all_comment($unionid){
		$sql = "SELECT * FROM Dingding_Value WHERE ddv_Comment_Unionid = ? ORDER BY ddv_Sn DESC";
		$query = $this->HT->query($sql,$unionid);
		$row = $query->result();
        return $row;
	}
	*/
	//获取所有员工的姓名以及邮箱（钉钉邮箱）
	function all_email($data = null){
		if($data){
			$where = "where ddu_Name in ($data)";
		}else{
			$where = '';
		}
		$sql = "SELECT ddu_Name,ddu_Email FROM Dingding_User ".$where;
		$query = $this->HT->query($sql);
		$row = $query->result();
		return $row;
	}
	
	//根据名字确定是否存在
	public function has_user($user_name){
        $sql="SELECT * FROM Dingding_User WHERE ddu_Name='$user_name' ";
        $query = $this->HT->query($sql);
        return $query->result();
    }
	
	//新增value邮件
	 public function add_mail($data){
        $sql="INSERT INTO Dingding_Mail(
            ddm_subject,
            ddm_content,
            ddm_touser,
            ddm_ccuser,
			ddm_fromuser,
			ddm_value_key,
			ddm_identify,
            ddm_createtime
            ) 
            VALUES (N?,N?,N?,N?,N?,?,?,?)";
        $this->HT->query($sql, array($data['mail_subject'],$data['mail_content'],$data['mail_touser'],$data['mail_ccuser'],$data['mail_fromuser'],$data['mail_value_key'],$data['mail_identify'],$data['mail_createtime']));
        return $this->HT->last_id('Dingding_Mail');
    }
	
	//新增value的名单
	public function add_value_user($data){
        $sql="INSERT INTO Dingding_User_Mail (
                ddum_name,
				ddum_identify,
                ddum_createtime) 
                VALUES (?,?,?)";
        $this->HT->query($sql, array($data['value_user_name'],$data['value_user_identify'],$data['value_user_createtime']));
        return $this->HT->last_id('webht_user_mail');
    }
	
	//检测是否已经点赞(仅用于value邮件点赞)
	public function is_liked($data){
		$sql = "select * from Dingding_Value where ddv_Identify = ? and ddv_User_Unionid = ? and ddv_Comment_Name = 'value邮件'";
		$query = $this->HT->query($sql,array($data['mail_identify'],$data['user_unionid']));
		if($query->num_rows() >= 1){
			return true;
		}else{
			return false;
		}     
	}
	
	//获取用户最新一次被点赞的时间以及人
	public function get_user_time($unionid,$user_unionid,$identify=false){
		if($identify){
			$where = "and ddv_identify = '$identify'";
		}else{
			$where = "";
		}
		$sql = "select ddv_Name,ddv_Createtime from Dingding_Value where ddv_Comment_Unionid = '$unionid' and ddv_User_Unionid = '$user_unionid'$where  order by ddv_Createtime desc";
		$query = $this->HT->query($sql);
		if($query){
			return $query->row();
		}else{
			return false;
		}
	}
	
	//获取最新一次被评论的时间
	public function get_last_comment($comment_name,$comment_unionid){
		$sql = "select  
					top 1 * 
				from 
					Dingding_Value 
				where 
					ddv_Comment_Name = '$comment_name' 
				and
					ddv_Comment_Unionid = '$comment_unionid'
				and 
					ddv_Type in ('comment','hidden_comment') 
		   order by 
					ddv_Createtime desc";
		$query = $this->HT->query($sql);
        $result=$query->result();
        return $result;			
	}
	
	
	//获取点赞排行榜
	public function get_person_rank($from_date=false,$to_date=false){
        $datesql="";
        if ($from_date) {
            $datesql=" AND ddv_Createtime BETWEEN '$from_date' AND '$to_date' ";
        }
        $sql="SELECT    TOP 15 
                        ddv_Comment_Name,
                        ddv_Comment_Unionid,
                        COUNT(ddv_Comment_Name) as like_count 
                FROM    Dingding_Value
               WHERE    ddv_Type='like' AND ddv_Comment_Name != 'value邮件'
                        $datesql 
               GROUP    BY ddv_Comment_Name,ddv_Comment_Unionid
               ORDER    BY like_count DESC";

        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }
	
	//获取全年参与点赞的次数
	public function get_click_liek($from_date=false,$to_date=false){
		$datesql="";
        if ($from_date) {
            $datesql=" AND ddv_Createtime BETWEEN '$from_date' AND '$to_date' ";
        }
        $sql="SELECT  	COUNT(*) as like_count 
                FROM    Dingding_Value
               WHERE    ddv_Type='like' AND ddv_Comment_Name != 'value邮件'
                        $datesql 
						";

        $query = $this->HT->query($sql);
        $result=$query->row();
        return $result;
	}
	
	//更新用户信息
	function update_dingding_user($name,$unionid,$mobile,$email,$position,$avatar,$time){
		$sql = "UPDATE Dingding_User SET 
										ddu_Name = '$name',
										ddu_Mobile = '$mobile',
										ddu_Email = '$email',
										ddu_Position = '$position',
										ddu_Avatar = '$avatar',
										ddu_Datetime = '$time'
								WHERE ddu_Unionid = '$unionid'";
		$query = $this->HT->query($sql);
		if ($query){
			return true;
		}else{
			return FALSE;
		}            
	}
	
	//获取value邮件列表
	public function get_valuemail_list(){
		$year = strtotime(date('Y-01-01'));
		$sql = "select * from Dingding_Mail where ddm_Createtime > '$year' order by ddm_Createtime desc ";
		$query = $this->HT->query($sql);
		return $query->result();
    
	}
	
	//获取所有value邮件所用的价值观
	public function get_allvalueitems(){
		$sql = "select ddm_value_key from Dingding_Mail where ddm_value_key != ''";
		$query = $this->HT->query($sql);
		return $query->result();
	}
	
	public function delete_value($sn){
		$sql = "DELETE FROM Dingding_Value WHERE ddv_Sn = '{$sn}'";
		$query = $this->HT->query($sql);
	}
	
	public function test(){
		$sql = "delete from Dingding_Value where ddv_Sn in ('10903','10904','10905')";
		$query = $this->HT->query($sql);
	}
	
}