<?php

/**
 * core\lib\growth\GrowthCreditDayCount.php
 */

/**
 * 当天通过不同方式获取积分的累计
 *
 * @author wxj
 */
class GrowthCreditDayCount {
	/**
     *
     * @var TINY_ADODB 主数据库 
     */    
    private $db;
    
    /**
     *
     * @var type 从数据库
     */
    private $dbback;
    
    /**
     *
     * @var type 缓存对象
     */
    private $mc;
    /**
     *
     * @var 全局服务容器 
     */
    private $container;
    /**
     *
     * @var String 表名 
     */
    private $table;
    /**
     *
     * @var array 表中包含的字段 
     */
    public static $fields = array('id','ctime','way_alias','broker_id','day_credit');

    
    /**
     * 构造函数，初始化表名等
     * @param String $table 表名，默认为空
     */
    public function __construct($table = null) {
        $container = Container::getInstance();
        $this->db = $container->get('db');
        $this->dbback = $container->get('dbback');
        $this->mc = $container->get('mc');
        $this->table = "growth_credit_day_count";
        
    }
    
    /**
     * 根据id查找每日累计积分
     * @param int $id GrowthCreditDayCount表中id
     * @return array 成功返回一条包含累计积分的数组
     */
    public function findById($id) {
       $sql = SqlCombinHelper::select_sql($this->table, implode(",", self::$fields), 'id ='.$id);
       return $this->dbback->getRow($sql);
    }
    
    /**
     * 根据获取方式别名获得每日积分
     * @param string $wayAlias 获取方式的别名
     * @return type
     */
    public function findAllByWayIAlias($wayAlias) {
       $sql = SqlCombinHelper::select_sql($this->table, implode(",", self::$fields), "way_alias ='$wayAlias'");
       return $this->dbback->getAll($sql);
    }
    
    /**
     * 根据经纪人id 和日期获得每日积分,设置$ctime参数可以限制时间
     * @param int $brokerId 经纪人id
     * @param String $ctime 日期，格式如2014-02-20
     * @return type
     */
    public function findByBrokerId($brokerId,$ctime=null) {
        $where = 'broker_id ='.$brokerId;
        if($ctime) $where .= " and ctime = $ctime";
        $sql = SqlCombinHelper::select_sql($this->table, implode(",", self::$fields), $where);
       return $this->dbback->getAll($sql);
    }
    
    /**
     * 根据日期获得每日积分,如果设置$wayAlias参数将限制获取方式
     * @param string $ctime 格式如：2014-02-20
     * @param String $wayAlias 获取方式别名
     * @return type
     */
    public function findByCtime($ctime,$wayAlias = null) {
        $where = '1';
        $where .= " and ctime = '$ctime'";
        if($wayAlias!==null)  {$where .= " and way_alias = '$wayAlias'";}
        $sql = SqlCombinHelper::select_sql($this->table, implode(",", self::$fields), $where);
       return $this->dbback->getAll($sql);
    }
    
    /**
     * 根据时间，获取方式id和经纪人id获得每日积分
     * @param string $ctime 日期，格式如：2014-02-20
     * @param String $wayAlias 获取方式别名
     * @param int $brokerId 经纪人id
     * @return type
     */
    public function findByTimeWayAliasBrokerId($ctime,$wayAlias,$brokerId) {
        $sql = SqlCombinHelper::select_sql($this->table, implode(",", self::$fields), "ctime ='".$ctime."' and way_alias='".$wayAlias."' and broker_id=$brokerId");
       return $this->dbback->getRow($sql);
    }
    
	/**
     * 根据时间，类别数组和经纪人id获得每日积分总和
     * @param string $ctime 日期，格式如：2014-02-20
     * @param array $wayAliasArr 获取方式别名
     * @param int $brokerId 经纪人id
     * @return int 总的积分
     */
    public function findArrayByTimeWayAliasBrokerId($ctime,$wayAliasArr,$brokerId) {		
        for($i=0;$i<$j=count($wayAliasArr);$i++){
            $i == $j-1 ? $wayAlias .="'".$wayAliasArr[$i]."'" : $wayAlias .="'".$wayAliasArr[$i]."',";			
		}
        $sql = SqlCombinHelper::select_sql($this->table, "sum(day_credit) as sum_credit", "ctime ='".$ctime."' and way_alias in (".$wayAlias.") and broker_id=$brokerId");
       return $this->dbback->getOne($sql);
    }
    

	/**
     * 根据时间，获取方式经纪人每日积分总和
     * @param string $ctime 日期，格式如：2014-02-20    
     * @param int $brokerId 经纪人id
     * @return int 总的积分
     */
    public function findCreditSumByTimeBrokerId($ctime,$brokerId) {		
    
        $sql = SqlCombinHelper::select_sql($this->table, "sum(day_credit) as sum_credit", "ctime ='".$ctime."' and broker_id=$brokerId");
       return $this->dbback->getOne($sql);
    }

    




    /**
     * 根据时间，获取方式id和经纪人id获得每日积分
     * @param string $ctime 日期，格式如：2014-02-20
     * @param String $wayAlias 获取方式别名
     * @param int $brokerId 经纪人id
     * @return type
     */
    public function findByUniqueIndex($ctime,$wayAlias,$brokerId) {
        $sql = SqlCombinHelper::select_sql($this->table, implode(",", self::$fields), 'ctime ='.$ctime." and way_alias=$wayAlias and broker_id=$brokerId");
       return $this->dbback->getRow($sql);
    }
    
    /**
     * 插入一条数据
     * @param array $fields 包含'ctime','way_alias','broker_id','day_credit'的数组
     * @return boolean 成功返回true，失败返回false
     */
    public function insert($fields) {
        $sql = SqlCombinHelper::insert_sql($this->table, $fields);
        echo $sql;
        exit;

        if($this->db->execute($sql)) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 根据经纪人编号、方式别名和统计时间获取相关操作数据
     * @param type $ctime 统计时间
     * @param type $wayAlias 方式别名
     * @param type $brokerId 经纪人编号
     * @return array
     */
    public function findOneByUniqueIndex($ctime,$wayAlias,$brokerId) {
        $sql = SqlCombinHelper::select_sql($this->table, self::$fields, 
                "ctime = '$ctime' AND way_alias='$wayAlias'"
                . " AND broker_id=$brokerId");
        return $this->dbback->getRow($sql);
    }
    
    public function insertData($ctime,$wayAlias,$brokerId,$dayCredit) {
        $arrFieldsValue = array();
        $arrFieldsValue['broker_id'] = $brokerId;
        $arrFieldsValue['way_alias'] = $wayAlias;
        $arrFieldsValue['day_credit'] = $dayCredit;
        $arrFieldsValue['ctime'] = $ctime;
        $sql = SqlCombinHelper::insert_sql($this->table, $arrFieldsValue);
        return $this->db->execute($sql);
    }
    
    public function addScoreToOneByUniqueIndex($ctime,$wayAlias,$brokerId, $score) {
        $score = $score > 0 ? '+' . $score : $score;
        $arrField['day_credit'] =  $score;
        $sql = SqlCombinHelper::updateSelf_sql($this->table, $arrField, 
                "ctime = '$ctime' AND way_alias='$wayAlias'"
                . " AND broker_id=$brokerId");
        return $this->db->execute($sql);
    }

    
    /**
     * 增加每日积分
     * @param string $ctime 时间格式如：2014-02-20
     * @param String $wayAlias 获取积分方式的别名
     * @param Integer $brokerId 经纪人id
     * @param Integer $score 要增加的积分值
     */
    public function increase($ctime,$wayAlias,$brokerId,$score) {
        $arrRow = $this->findOneByUniqueIndex($ctime, $wayAlias, $brokerId);
        if (count($arrRow) == 0) {
            $status = $this->insertData($ctime, $wayAlias, $brokerId, $score);
        } else {
            $status = $this->addScoreToOneByUniqueIndex($ctime, $wayAlias, 
                    $brokerId, $score);
        }
        return $status;
    }

	/**
	**获取当天获取积分的人数
	**@param string $ctime  时间格式如：2014-02-23
	**@return int 返回人数
	**
	**/
	public function findBrokerNumber($ctime=null){
		$ctime = $ctime != null ? $ctime : date('Y-m-d');
		$sql = SqlCombinHelper::select_sql($this->table, "COUNT( DISTINCT (broker_id) ) AS num", "ctime = '$ctime'");
        return $this->dbback->getOne($sql);
	}

	/**
	**获取某个城市当天获取积分少于某个积分的人
	**@param string $ctime  时间格式如：2014-02-23
	**@return int 返回人数
	**
	**/
	public function findBelowCreditBroker($conf_cityflag,$score,$ctime=null){
		$ctime = $ctime != null ? $ctime : date('Y-m-d');
    
        $sql = SqlCombinHelper::select_sql($this->table, "broker_id", "ctime = '$ctime' GROUP BY broker_id
HAVING SUM( day_credit ) <$score");

		$brokerArr = $this->dbback->getAll($sql);
        return $brokerArr;
	}


   /**
	**获取小区完善当天获取积分的人数
	**@param string $ctime  时间格式如：2014-02-23
	**@return int 返回人数
	**
	**/
	public function findBrokerImproveNumber($ctime,$wayAliasArr){
		$j=count($wayAliasArr);
		for($i=0;$i<$j;$i++){
            $i == $j-1 ? $wayAlias .="'".$wayAliasArr[$i]."'" : $wayAlias .="'".$wayAliasArr[$i]."',";			
		}
		$sql = SqlCombinHelper::select_sql($this->table, "count(DISTINCT(broker_id)) as num", "ctime ='".$ctime."' and way_alias in (".$wayAlias.") ");
        return $this->dbback->getOne($sql);
	}

   /**
	**获取小区完善当天获取积分比我少的经纪人个数
	**@param string $ctime  时间格式如：2014-02-23  
	**@param array    $wayAliasArr 获取途径数组 
	**@param int   $myCredit 我获取的积分
	**return  int  
	**/
	public function findImproveBelowCreditBroker($ctime,$wayAliasArr,$myCredit){
		$j=count($wayAliasArr);
		for($i=0;$i<$j;$i++){
            $i == $j-1 ? $wayAlias .="'".$wayAliasArr[$i]."'" : $wayAlias .="'".$wayAliasArr[$i]."',";			
		}
	   $sql = SqlCombinHelper::select_sql($this->table, "count(id) as num", "ctime = '$ctime' and way_alias in (".$wayAlias.") GROUP BY broker_id HAVING SUM(day_credit) < $myCredit");
	   $brokerArr = $this->dbback->getAll($sql);
       $result = count($brokerArr);
       return $result;
	}


  	/**
     * 当前经纪人小区完善获取积分总值
     * @param type $brokerId  经纪人id
     * @param type $wayAliasArr 获取途径数组 
     */
    public function getImproveCreditCountByUid($brokerId,$wayAliasArr) {
		$j=count($wayAliasArr);
		for($i=0;$i<$j;$i++){
            $i == $j-1 ? $wayAlias .="'".$wayAliasArr[$i]."'" : $wayAlias .="'".$wayAliasArr[$i]."',";			
		}
        $sql = SqlCombinHelper::select_sql($this->table,'sum(day_credit) as num', "broker_id='$brokerId'   and way_alias in (".$wayAlias.")");
	    $result = $this->dbback->getOne($sql);
	 
		return $result;
    }


	/**
     * 参与小区完善获取积分的经纪人总数
     * @param type $wayAliasArr 获取途径数组 
     */
    public function getImproveCreditCountAllByUid($wayAliasArr) {
		$j=count($wayAliasArr);
		for($i=0;$i<$j;$i++){
            $i == $j-1 ? $wayAlias .="'".$wayAliasArr[$i]."'" : $wayAlias .="'".$wayAliasArr[$i]."',";			
		}
        $sql = SqlCombinHelper::select_sql($this->table,'count(DISTINCT(broker_id)) as num', " way_alias in (".$wayAlias.")");

	    $result = $this->dbback->getOne($sql);
		return $result;
    }
	

   /**
	**获取小区完善总积分比我低的经纪人数量 
	**@param array    $wayAliasArr 获取途径数组 
	**@param int   $myCredit 我获取的积分
	**return  int  
	**/
	public function getImproveCreditBelow($wayAliasArr,$myCredit){
		$j=count($wayAliasArr);
		for($i=0;$i<$j;$i++){
            $i == $j-1 ? $wayAlias .="'".$wayAliasArr[$i]."'" : $wayAlias .="'".$wayAliasArr[$i]."',";			
		}
	   $sql = SqlCombinHelper::select_sql($this->table, "count(id) as num", "  way_alias in (".$wayAlias.") GROUP BY broker_id HAVING SUM(day_credit) < $myCredit");
	   $result = $this->dbback->getAll($sql);
	   $num = count($result);
       return $num;
	}
	
}

?>
