<?php

/**
 * 版权所有，盗版必查水表
 */

/**
 * 竞投小助手
 *
 * @author Administrator
 */
class GrowthBidHelper {
    private $broker;
    private $growthBidFollow;
    private $growthBidRecord;
    private $growthBidRecordHistory;
    private $growthBid;
    
    public function __construct($broker) {
        $this->broker = $broker;
        $this->growthBidRecord = new GrowthBidBlockExpertRecordOn();
        $this->growthBidRecordHistory = new GrowthBidBlockExpertRecordHistory();
        $this->growthBid = new GrowthBidBlockExpert();
        $this->growthBidFollow = new GrowthBidFollow();
    }
    
    /**
     * 查找关注的小区的列表
     * @return type
     */
    public function getAttentionBlocks() {
        $brokerId = $this->broker['id'];
        //关注的小区id
        $type = 1;
        $conf = "broker_id = '$brokerId' and type='$type'";
        $bidFollow = $this->growthBidFollow->findBy($conf);
        $blocks = array();
        if($bidFollow) {
            $block = new Block();
            foreach ($bidFollow as $value) {
                //$blockIds[] = $value['target_id'];
                $blocks[$value['target_id']] = $block->findOneBy($value['target_id']);
                $blocks[$value['target_id']]['follow_id'] = $value['id'];
            }
            return $blocks;
        }
        return $blocks;
    }
    
    public function getAttentionList() {
        //关注的竞投对象，此处为小区，包含id，小区名
        $dbback = Container::getInstance()->get('dbback');
        $blocks = $this->getAttentionBlocks();
        $blockIds = array_keys($blocks);
        $blockIds = implode("','", $blockIds);
        
        $gBidPeriods = new GrowthBidPeriods();
        $bidp = $gBidPeriods->currentBidPeriods(1);
        $bidpid = $bidp['id'];//竞投期次id
        //竞投
        
        $helper = array();
        foreach($blocks as $v) {
            $temp = array();
            $blockid = $v['id'];
            $temp['target_id'] = $blockid;
            $temp['target'] = '【小区专家】'.$v['blockname'];
            $temp['follow_id'] = $v['follow_id'];
            $temp['bid_type'] = 1;//竞投类型，1，表示小区专家
            //竞投记录
            //竞投期间，查本期，
            //还未开始，上期，和将要开始的期
            if($bidp['status']==2) {
                //下次竞投id,开始时间，上次竞投最高，最低分
                //$confBid = "bid_periods_id='$bidpid' and block_id='".$blockid."'";
                //$nextBids = $this->growthBid->findOneBy($confBid);
                //$temp['bid_id'] = $nextBids['id'];
                //查找这个小区的上期的竞投
                $confBid = "bid_periods_id<'$bidpid' and block_id='".$blockid."'";
                $attentionBids = $this->growthBid->findOneBy($confBid, array('bid_periods_id'=>'desc'));
                //print_r($attentionBids);
                $bidid = $attentionBids['id'];
                $temp['bid_id'] = $bidid;
                $temp = array_merge($temp, $this->getBidInfo($bidid,2));
                
                //
                $temp['status_description'] = "离下期开始<span class='time'>".date("m d,Y H:i:s",  strtotime($bidp['start_time'])).'</span>';
                //print_r($temp);
                //exit;
            }
            //竞投结束,还没有刷下期竞投
            if($bidp['status']==3) {
                $confBid = "bid_periods_id='$bidpid' and block_id='".$blockid."'";
                $attentionBids = $this->growthBid->findOneBy($confBid);
                //print_r($attentionBids);
                $bidid = $attentionBids['id'];
                $temp['bid_id'] = $bidid;
                
                //最高分，最低分，我的分
                //echo "select max(score),min(score) from $recordTable where bid_id=$bidid";exit;
                $temp = array_merge($temp, $this->getBidInfo($bidid,2));
                
                $temp['status_description'] = "竞投结束，等待下期消息";
            }
            //print_r($temp);
            $helper[] = $temp;
        }
        return $helper;
    }

    /**
     * 
     * @param int $bidid
     * @param int $type 1,表示当前表，2，表示历史表
     * @return array
     */
    public function getBidInfo($bidid,$type=1) {
        $dbback = Container::getInstance()->get('dbback');
        $brokerId = $this->broker['id'];
        if($type==1) {
            $recordTable =$this->growthBidRecord->getTable();
        } 
        if($type==2) {
            $recordTable =$this->growthBidRecordHistory->getTable();
        }
        $temp = array();
        
        $x = $dbback->getRow("select max(score) as max,min(score) as min from $recordTable where bid_id=$bidid");
        //var_dump($x);
        if($x) {
            $temp = array_merge($temp, $x);
        }
        //echo "select score,status  from $recordTable where broker_id = $brokerId and bid_id=$bidid";exit;
        $x1 = $dbback->getRow("select score,status  from $recordTable where broker_id = $brokerId and bid_id=$bidid");
        //var_dump($x1);
        if($x1) {
            $temp['myscore'] = $x1['score']?$x1['score']:'';
            $temp['mystatus'] = $x1['status'];//1,成功，-1，失败
            $temp['mystatus_str'] = $x1['status']==1?'成功':'失败';
        } else {
            $temp['myscore'] = '';
            $temp['mystatus'] = 0;//1,成功，-1，失败,0,未参加
            $temp['mystatus_str'] = '';
        }
        return $temp;
    }

    /**
     * 已废弃，请调用GrowthBidPeriods 类中的同名方法
     */
    public function currentBidPeriods() {
        //最新一期的id
        $growBidPeriods = new GrowthBidPeriods();
        $confgbp = "type=1";
        $order = "ctime desc";
        $limit = "0,1";
        $bidp = $growBidPeriods->findBy($confgbp, $order, $limit);
        if($bidp) {
            $bidp= $bidp[0];
        }
        $bidpid = $bidp['id'];
        //判断本期的状态
        $time = time();
        $start_time = strtotime($bidp['start_time']);
        $end_time = strtotime($bidp['end_time']);
        if($time>=$start_time&&$time<$end_time) {//进行中
            $bidp['status'] = '1';
        } else if($time<$start_time){
            $bidp['status'] = '2';//未开始
        } elseif($time>=$end_time) {
            $bidp['status'] = '3';//竞投结束
        }
        //print_r($bidp);exit;
        return $bidp;
    }
    
    
   /**
     * 关注竞投
     * @param int $type 关注的对象的类型，1，小区，。。。。。
     * @param int $targetId 关注的对象的id
     * @return mixed 成功返回true，失败返回false,超过关注数量返回-1
     */
    public function attent($targetId,$type=1) {
        //
        $brokerId = $this->broker['id'];
        $growthBidFollow = new GrowthBidFollow();
        $conf = "broker_id = '$brokerId' and target_id='$targetId' and type='$type'";
        $bidFollow = $growthBidFollow->findBy($conf);
        if($this->checkAttention()>=10) {
            return -1;
        }
        if($bidFollow) {

        } else {
            $fieldData = array('broker_id'=>$brokerId,'type'=>'1','target_id'=>$targetId,);
            if($growthBidFollow->insert($fieldData)) {
                return true;
            } else {
                return false;
            }
        }
    }
    
    /**
     * 取消已经关注的竞投
     * @param int $type 关注的对象的类型，1，小区，。。。。。
     * @param int $targetId 关注的对象的id
     * @return boolean 成功返回true，失败返回false
     */
    public function cancelAttention($targetId,$type=1) {
        $brokerId = $this->broker['id'];
        $growthBidFollow = new GrowthBidFollow();
        $conf = "broker_id = '$brokerId' and target_id='$targetId' and type='$type'";
        $fieldData = array('del'=>'1');
        if($growthBidFollow->update($fieldData, $conf)) {
            return true;;
        } else {
            return false;
        }
    }
    
    /**
     * 返回已关注的数量
     * @return int 已关注的数量
     */
    public function checkAttention() {
        $brokerId = $this->broker['id'];
        $growthBidFollow = new GrowthBidFollow();
        $conf = "broker_id = '$brokerId'";
        return $bidFollowNumber = $growthBidFollow->findNumberBy($conf);
    }
    
}

?>
