<?php

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


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

    public function add_mail($data)
    {
        $sql="INSERT INTO webht_mail (
            whm_subject,
            whm_content,
            whm_fromuser,
            whm_touser,
            whm_ccuser,
            whm_createtime,
            whm_value_key,
            whm_identify
            ) 
            VALUES (N?,N?,?,?,?,?,?,?)";

        $this->HT->query($sql, array($data['whm_subject'],$data['whm_content'],$data['whm_fromuser'],$data['whm_touser'],$data['whm_ccuser'],$data['whm_createtime'],$data['whm_value_key'],$data['whm_identify']));
        return $this->HT->last_id('webht_mail');
    }
    public function add_value_user($data)
    {
        $sql="INSERT INTO webht_user_mail (
                whum_whu_uname,
                whum_whm_identify,
                whum_createtime) 
                VALUES (?,?,?)";

        $this->HT->query($sql, array($data['whum_whu_uname'],$data['whum_whm_identify'],$data['whum_createtime']));
        return $this->HT->last_id('webht_user_mail');
    }
    public function add_like($data)
    {
        $sql="INSERT INTO webht_comment (
                whc_type,
                whc_whm_identify,
                whc_uid,
                whc_content,
                whc_createtime) 
                VALUES (?,?,?,?,?)";
                
        $this->HT->query($sql, array($data['whc_type'],$data['whc_whm_identify'],$data['whc_uid'],$data['whc_content'],$data['whc_createtime']));
        return $this->HT->last_id('webht_comment');
    }

    public function get_comment_list($whc_whm_identify,$whc_type='comment',$order='ASC',$whc_uid=false)
    {
        $map_order=" ORDER BY whc_createtime ASC ";
        if ($order!='ASC') {
            $map_order=" ORDER BY whc_createtime DESC ";
        }
        if ($whc_uid) {
            $map_order= " AND whc_uid='$whc_uid' ".$map_order;
        }
        $sql=" SELECT   whc_id,
                        whc_type,
                        whc_whm_identify,
                        whc_content,
                        whc_createtime,
                        whu_uid,
                        whu_uname,
                        whu_email
                 FROM   webht_comment 
                        LEFT JOIN webht_user ON whc_uid=whu_uid
                WHERE   whc_type=? AND 
                        whc_whm_identify=? 
                        $map_order ";

        $query = $this->HT->query($sql, array($whc_type,$whc_whm_identify));
        $result=$query->result();
        return $result;
    }

    public function get_space_comment($user,$user_sn,$whc_type,$get_count=false)
    {
        $map_sql="";
        if ($whc_type!='all') {
             $map_sql=" AND whc_type='$whc_type' ";
        }
        $order_sql=" ORDER BY whc_createtime DESC ";
        $filed_str='whc_id,whc_type,whc_whm_identify,whc_content,whc_createtime,whu_uid,whu_uname,whu_email,whm_subject,whm_fromuser,whm_createtime';
        if ($get_count) {
            $filed_str='count(whc_id) as count';
            $order_sql='';
        }
        $sql=" SELECT   $filed_str
                 FROM   webht_comment 
                        LEFT JOIN webht_user ON whu_uid=whc_uid
                        LEFT JOIN webht_user_mail ON whum_whm_identify=whc_whm_identify
                        LEFT JOIN webht_mail ON whum_whm_identify=whm_identify
                WHERE   (whum_whu_uname=? or whc_whm_identify=?)
                        $map_sql 
                $order_sql";

        $query = $this->HT->query($sql, array($user,$user_sn));
        $result=$query->result();
        return $result;
    }

    public function get_comment_count($whc_whm_identify,$whc_type=false,$whc_uid=false)
    {
        $maptype='';
        if ($whc_type) {
            $maptype=" AND whc_type='$whc_type' ";
        }
        $mapuid='';
        if ($whc_uid) {
            $mapuid=" AND whc_uid=$whc_uid ";
        }
        $today=strtotime(date('Y-m-d 00:00:00',time()));
        $sql=" SELECT   count(whc_id) as count
                 FROM   webht_comment 
                WHERE   whc_whm_identify=? AND whc_createtime>? $maptype $mapuid";

        $query = $this->HT->query($sql, array($whc_whm_identify,$today));
        $result=$query->result();
        return $result[0]->count;
    }

    public function get_mail_text($whm_identify)
    {
        $sql=" SELECT   whm_content,
                        whm_subject,
                        whm_fromuser,
                        whm_createtime,
                        whm_identify
                 FROM   webht_mail 
                WHERE   whm_identify=?";

        $query = $this->HT->query($sql, array($whm_identify));
        $result=$query->result();
        if ($result) {
            $result=$result[0];
        }
        return $result;
    }
    
    public function get_webhtuser_by_ip($ip,$filed='whu_uid')
    {
        $sql="SELECT $filed FROM webht_user WHERE whu_ip=?";
        $query = $this->HT->query($sql, array($ip));
        $result=$query->result();
        if ($result) {
            if ($filed=='*') {
                $result=$result[0];
            }else{
                $result=$result[0]->$filed;
            }
        }
        return $result;
    }

    public function get_webhtuser_by_name($uname,$filed='whu_uid')
    {
        $sql="SELECT $filed FROM webht_user WHERE whu_uname=?";
        $query = $this->HT->query($sql, array($uname));
        $result=$query->result();
        if ($result) {
            if ($filed=='*') {
                $result=$result[0];
            }else{
                $result=$result[0]->$filed;
            }
        }
        return $result;
    }

    public function get_webhtuser_by_filed($mapfiled,$map,$filed='whu_uid')
    {
        $sql="SELECT $filed FROM webht_user WHERE $mapfiled=?";
        $query = $this->HT->query($sql, array($map));
        $result=$query->result();
        if ($result) {
            if ($filed=='*') {
                $result=$result[0];
            }else{
                $result=$result[0]->$filed;
            }
        }
        return $result;
    }

    public function is_liked($data)
    {
        $sql="SELECT whc_id 
                FROM webht_comment 
               WHERE whc_type=? 
                     AND whc_whm_identify=? 
                     AND whc_uid=?";
        $query = $this->HT->query($sql, array($data['whc_type'],$data['whc_whm_identify'],$data['whc_uid']));
        $result=$query->result();
        return $result;
    }

    public function get_value_rank($from_date,$to_date,$valuetype){
        $valuetype_sql=" AND whm_value_key!=''";
        if ($valuetype) {
            $valuetype_sql=" AND whm_value_key like '%$valuetype%'";
        }
        $sql="SELECT        whum_whu_uname,
                            COUNT(0) as like_count 
                FROM        webht_comment 
                            LEFT JOIN webht_user_mail ON whc_whm_identify=whum_whm_identify 
                            LEFT JOIN webht_mail ON whm_identify=whc_whm_identify 
               WHERE        whc_type!='unlike' AND 
                            whm_value_key!='' AND 
                            whum_whu_uname!='' AND 
                            whm_createtime BETWEEN '$from_date' AND '$to_date' 
                            $valuetype_sql
               GROUP BY whum_whu_uname 
               ORDER BY like_count 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 whc_createtime BETWEEN '$from_date' AND '$to_date' ";
        }
        $sql="SELECT    TOP 15 
                        whu_uname,
                        whu_email,
                        COUNT(whc_id) as like_count 
                FROM    webht_comment 
                        LEFT JOIN webht_user_mail ON whc_whm_identify=whum_whm_identify
                        LEFT JOIN webht_user ON (whu_uname=whum_whu_uname or whu_email=whc_whm_identify+'@citsguilin.com')
               WHERE    whc_type='like' AND whu_uname is not null 
                        $datesql 
               GROUP    BY whu_uname,whu_email
               ORDER    BY like_count DESC";

        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }

    public function get_rank_mail($from_date,$to_date,$valuetype){
        $valuetype_sql="";
        if ($valuetype) {
            $valuetype_sql=" AND whm_value_key like '%$valuetype%'";
        }
        $sql="SELECT    whm_id,
                        whm_subject,
                        whm_fromuser,
                        whm_content,
                        whm_createtime,
                        whm_value_key,
                        whum_whu_uname,
                        whm_identify
                FROM    webht_mail 
                        LEFT JOIN webht_user_mail ON whm_identify=whum_whm_identify 
               WHERE    whm_createtime BETWEEN '$from_date' AND '$to_date' 
                        AND whm_value_key!='' 
                        $valuetype_sql
               ORDER BY whm_createtime ASC";
        $query = $this->HT->query($sql);
        $result=$query->result();
        return $result;
    }

    public function verify_user($whu_uname,$whu_ip){
        $query_sql="SELECT * FROM webht_user WHERE whu_uname like '%$whu_uname%' ";
        $query_result=$this->HT->query($query_sql);
        $userdata=$query_result->result();
        if (empty($userdata)) {
            return '0';
        }elseif (count($userdata)>1) {
            return '2';
        }else{
            $sql="UPDATE webht_user SET whu_ip = '$whu_ip',whu_status=1 WHERE whu_uname like '%$whu_uname%' ";
            $result = $this->HT->query($sql);//var_dump($this->HT->last_query());
            return '1';
        }
    }

    public function has_user($whu_uname){
        $sql="SELECT * FROM webht_user WHERE whu_uname='$whu_uname' ";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    public function user_list(){
        $sql="SELECT * FROM webht_user";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    public function delete_comment($whc_id)
    {
        $sql="DELETE FROM webht_comment WHERE whc_id=?";
        $query=$this->HT->query($sql, array($whc_id));
        return $query;
    }

    //每个价值观对应的被value次数最多的人
    public function value_key_rank($whm_value_key)
    {
        $sql="SELECT TOP 10 whum_whu_uname,count(whum_id) as value_count 
                from webht_user_mail whum
                     left join webht_mail on whum_whm_identify=whm_identify
               where whm_value_key!='' and whm_value_key like '%$whm_value_key%' 
               group by whum_whu_uname 
               order by value_count desc";
        $query=$this->HT->query($sql);
        return $query->result();
    }

}