<?php

/**
 * core\lib\block\BlockLevelStat.php
 */

/**
 * 根据等级数据，更新小区等级（热度，完善度）
 *
 * @author Wxj
 */
class BlockLevelStat {
    /**
     *
     * @var Container 包含db，memcache等
     */
    public $container;
    /**
     *
     * @var BlockLevelHeat 小区热度类
     */
    private $blockLevelHeat;
    /**
     *
     * @var BlockLevelInfo 小区完善度等级类
     */
    private $blockLevelInfo;
    
    /**
     *
     * @var int 划分小区等级时的pv限制 
     */
    private $pvLimit;
    /**
     *
     * @var int 划分小区等级时的房源量限制 
     */
    private $houseNumberLimit;
    /**
     *
     * @var int 小区资料完善等级未完善项数目的限制
     */
    private $notFillitemsLimit;
    /**
     *
     * @var int 小区资料完善等级图片数量的限制
     */
    private $imageNumberLimit;
    
    /**
     * 构造函数初始化
     * @throws Exception 如果没有等级设置怎出现异常
     */
    public function __construct() {
        $this->container = Container::getInstance();
        $this->blockLevelHeat = new BlockLevelHeat();
        $this->blockLevelInfo = new BlockLevelInfo();
        $blockLhSetting = new BlockLevelHeatSetting();
        $blhsData = $blockLhSetting->findAll();
        if($blhsData) {         
            $this->houseNumberLimit = $blhsData;
        } else {
            throw new Exception('no block level setting');
        }
        $blockLiSetting = new BlockLevelInfoFullSetting();
        $blisData = $blockLiSetting->findAll();
        if($blhsData) {
            $this->notFillitemsLimit = $blisData[0]['not_filled_items_number'];
            $this->imageNumberLimit = $blisData[0]['image_number'];
        } else {
            throw new Exception('no block level setting');
        }
    }
    
    /**
     * 根据房源量计算出小区对应的等级
     * @param int $min_house_number 最小房源量
     * @param int $max_house_number 最大房源量
     * @return string 返回 A,B,C,D之一
     */
    public function statToLevelHeat($house_number) {
        if($this->houseNumberLimit){
            foreach($this->houseNumberLimit as $value){
                if($value['max_house_number']!=0){
                    if($house_number>=$value['min_house_number']&&$value['max_house_number']>$house_number){
                         return $value['level'];
                    }
                }else{
                    if($house_number>=$value['min_house_number']){
                        return $value['level'];
                    }
                }
            }
        }      
      
    }
    
    /**
     * 查询计算房源量数据
     */
    private function calHouseNumber() {
        $dbback = $this->container->get('dbback');
        //上个月
        $stat = strtotime("-2 month");
        $mouth = Date('m',$stat);
        $mouthMaxDay = date("t",$stat);
        $year = date('Y',$stat);
        $datalineStart = $year.$mouth."01";
        $datalineEnd = $year.$mouth.$mouthMaxDay;
        $sql = "select blockid,blockname,SUM(h_num) as house_number,COUNT(blockid) as days from block_stat WHERE dataline>=$datalineStart and dataline<=$datalineEnd  GROUP BY blockid";
        return $dbback->getAll($sql);
    }

    /**
     * 根据小区未完善项的数量和小区图片的数量返回小区资料完善紧急程度等级
     * @param int $notFullItemNumber 小区未完善项数
     * @param int $imageNumber 图片数量
     * @return string 小区紧急程度等级
     */
    public function blockToLevelInfo($notFullItemNumber,$imageNumber) {
        if($notFullItemNumber>=0&&$notFullItemNumber<=$this->notFillitemsLimit) {
            if($imageNumber>=$this->imageNumberLimit) {
                return BlockLevelInfoFullSetting::$levels[1];
            } else {
                return BlockLevelInfoFullSetting::$levels[2];
            }
        } else {
            if($imageNumber>=$this->imageNumberLimit) {
                return BlockLevelInfoFullSetting::$levels[3];
            } else {
                return BlockLevelInfoFullSetting::$levels[4];
            }
        }
    }
    
    private function blockLevelHeatData($statDate) {
        
    }
    
    /**
     * 刷新小区热度等级
     */
    public function flushHeat() {
        $statData = $this->calHouseNumber();
        $levelData = array();
        if($statData) {
            $i = 0;
            $dbback = $this->container->get('dbback');
            foreach ($statData as $value) {
                $temp = array();
                //如果不是正式小区，没有等级
                $blockId = $value['blockid'];
                $sql = "select esta from block WHERE id='$blockId'";
                $esta = $dbback->getOne($sql);
                if($esta!=2) {
                    continue;
                }
                $temp['block_id'] = $blockId;
               
                $house_number = intval($value['house_number']/$value['days']);
                //$temp['pv'] = $pv;
                //$temp['house_number'] = $house_number;
                $temp['level'] = $this->statToLevelHeat($house_number);
                $levelData[] = $temp;
                /*$i++;
                if($i>10){
                    print_r($settingData);exit;
                }*/
            }
            //print_r($levelData);exit;
            $this->blockLevelHeat->refreshData($levelData);
            
        }
    }
    
    /**
     * 刷新小区资料完善度等级
     */
    public function flushInfo() {
        //SELECT bi_blockid as block_id,COUNT(id) as image_number FROM block_img GROUP BY bi_blockid LIMIT 0,10
    
        $notLockData = $this->blockNotLockItemNumber();
        $imageNumberData = $this->blockImageNumber();
        $levelData = array();
        $i = 0;
        if($notLockData) {
            foreach ($notLockData as $value) {
                $temp = array();
                $temp['block_id'] = $value['block_id'];
                if($value['lock_flag'] ==1) {
                    $notItemNumber = 0;
                } else {
                    $notItemNumber = $value['not_full_item_number'];
                }
                $image_number = $imageNumberData[$value['block_id']];
                //$temp['not_full_item_number'] = $notItemNumber;
                //$temp['image_number'] = $image_number;
                $temp['level'] = $this->blockToLevelInfo($notItemNumber, $image_number);
                $levelData[] = $temp;
                /**调试
                if($temp['block_id'] =='410') {
                    echo $notItemNumber;
                    print_r($temp);print_r($value);
                    echo $image_number;
                }
                 */
                /*
                $i++;
                if($i>10){
                    print_r($levelData);exit;
                }*/
            }
            $this->blockLevelInfo->refreshData($levelData);
        }
        
        //print_r($imageNumberData);
    }
    
    /**
     * 从小区表中查找小区资料未完善项
     * @return array 未完善项数目的数组
     */
    private function blockNotLockItemNumber() {
        $sql = SqlCombinHelper::select_sql("block", "id as block_id,lock_flag,lock_opennum as not_full_item_number", "esta = 2");
        $dbback = $this->container->get('dbback');
        $res = $dbback->getAll($sql);
        return $res;
        $data = array();
        if(is_array($res)&&!empty($res)) {
            foreach ($res as $value) {
                //$temp = array();
                if($value['lock_flag'] ==1) {
                    $data[$value['block_id']] = 0;
                } else {
                    $data[$value['block_id']] = $value['not_full_item_number'];
                }
            }
        }
        //print_r($data);
        return $data;
    }
    
    /**
     * 通过小区图片表获得小区图片数量
     * @return array 包含小区图片数量的数据
     */
    private function blockImageNumber() {
        $sql = "SELECT bi_blockid as block_id,COUNT(id) as image_number FROM block_img where bi_type in (51,52,53,54,55 ) GROUP BY bi_blockid";
        $dbback = $this->container->get('dbback');
        $res = $dbback->getAll($sql);
        $data = array();
        if(is_array($res)&&!empty($res)) {
            foreach ($res as $value) {
                $data[$value['block_id']] = $value['image_number'];
            }
        }
        return $data;
    }
}


?>
