<?php

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


    class Ask_model extends MY_Model
    {

        function __construct()
        {
            parent::__construct();
            $this->memkey='myask'.GB_CITY;
        }

         /**
         * 获取用户提问
         * @param type $uid
         * @return array
         */
        function getquelist($uid,$offset=0,$pagesize=0)
        {
           $key = $this->memkey.'getasklist'.$uid.$offset.$pagesize;
           if($this->input->get('update') == 1)
           {
              $this->memcached_class->delete($key);
           }
            $ask_list = $this->memcached_class->get($key);
            if(empty($ask_list) )
            {
               $ask_list =  $this->get_data(array('form_name' => 'qa_question','where'=>array('q_userid'=>$uid,'q_esta !='=>2),'select'=>array('q_id','q_message','q_dateline','q_esta'),'order_by'=>'q_dateline desc','limit'=>$offset,'offset'=>$pagesize),'esfhousebak');
               $this->memcached_class->add($key, $ask_list,10);
            }
            return $ask_list;
        }

        /**
         * 获取用户提问
         * @param type $uid
         * @return array
         */
        function getquelistnum($uid)
        {
           $ask_listnum = 0;
           if( $uid > 0 )
           {
                $ask_list =  $this->get_data(array('form_name' => 'qa_question','where'=>array('q_userid'=>$uid,'q_esta !='=>2),'select'=>array('count(q_id) as num')),'esfhousebak');

                $ask_listnum = intval( $ask_list[0]['num'] );
           }

           return $ask_listnum;
        }


        /**
         * 获取是否有新答案
         * @param type $qid
         * @return array
         */
        function getaskread($qid)
        {
           $ask_num =  $this->get_data(array('form_name' => 'qa_answer','where'=>array('a_qid'=>$qid,'a_esta'=>1,'a_read'=>0),'select'=>array('count(a_id) as num')),'esfhousebak');
           return $ask_num[0]['num'];
        }

        /**
         * 获取某个用户的所有未读回复条数
         * @param type $uid
         * @return int
         */
        function getnewanswerbyuid($uid)
        {
		    $all_num = 0;

            $q_list = $this->getquelist($uid);
			
            if(!empty($q_list) && is_array($q_list)){
             foreach($q_list as $key=>$value)
             {
               $num = 0;
               $num = $this->getaskread($value['q_id']);
               $all_num += $num;
             }
            }
          return $all_num;
        }

         /**
         * 获取问题的回答条数
         * @param type $qid
         * @return array
         */
        function getaskqnum($qid)
        {
            $key = $this->memkey.'getaskqnum'.$qid;
            if($this->input->get('update') == 1)
            {
              $this->memcached_class->delete($key);
            }
            $q_num = $this->memcached_class->get($key);
            if(empty($q_num) )
            {
               $q_num =  $this->get_data(array('form_name' => 'qa_answer','where'=>array('a_qid'=>$qid,'a_esta'=>1),'select'=>array('count(a_id) as num')),'esfhousebak');
               $this->memcached_class->add($key, $q_num,60);
            }
            return  isset($q_num[0]['num'])?$q_num[0]['num']:0;
        }

         /**
         * 获取优秀专家
         * @param type $qid
         * @return array
         */
        function getexperts()
        {

          $nowtime	= time();
          $starttime	= $nowtime - 7*86400;
            $key = $this->memkey.'getexperts';
            //if($this->input->get('update') == 1)
            //{
              $this->memcached_class->delete($key);
            //}
            $experts = $this->memcached_class->get($key);
            if(empty($experts) )
            {
                $experts=$this->get_data(array('form_name' => 'log_wenda_brokercredit','where'=>array('dateline >'=>$starttime,'dateline <'=>$nowtime),'select'=>array('lc_uid,SUM(lc_value) AS credit'),'order_by'=>'credit desc','group_by'=>'lc_uid','limit'=>3),'esfhousebak');

                $this->memcached_class->add($key, $experts,60);
            }
            return $experts;
        }

    }

