<?php
/**
 * 竞投结束后的在删除之前备份数据处理
 * 1，on表数据移动到history表
 * 2，对竞投成功的，扣除积分，且解冻相应积分
 * 3，加成长值
 * 4, 对竞投成功的，刷成功表
 * //每天8点半之后执行
 * 参数 type blockExpert or house 表示刷小区专家竞投或房源竞投
 */
set_time_limit(0);
error_reporting(7);
ini_set("display_errors",true);

$conf_basedir = "../../../../"; //相对主目录的位置
define('WEB_ROOT', $conf_basedir);
$conf_where = "admin"; //选择调用相应模块
$directory = basename(dirname(__FILE__)); //定义当前所在的目录，引入模块公用函数
include_once(  "./base.inc.php");
require_once '../function_crontab.php';

//$container = Container::getInstance();
//$dbback = $container->get('dbback');
$bidPeriods= new GrowthBidPeriods();

$arrType = array('blockExpert','house');
foreach($arrType as $type) {
    if($type=='blockExpert') { 
    /***********刷小区专家竞投***********************/
        $periods = $bidPeriods->currentBidPeriods(1);
        //print_r($periods);
        $periodsStatus = $periods['status'];//echo $periodsStatus;exit;
        //$periodsStatus = 3;
        if($periodsStatus==3) {//结束时刷历史表
            $resultStatus = bidOnHandle('growth_bid_block_expert_record_on','growth_bid_block_expert_record_on_bak');
        }
    /***********刷小区专家竞投结束***********************/
    }
    //exit;
    if($type=='house') { 
    /***********刷房源竞投***********************/
        $periods = $bidPeriods->currentBidPeriods(2);
        //print_r($periods);exit;
        $periodsStatus = $periods['status'];
        if($periodsStatus==3) {//结束时刷历史表
            $resultStatus = bidOnHandle('growth_bid_house_record_on','growth_bid_house_record_on_bak');
        }
    /***********刷房源竞投结束***********************/    
    }
}

function bidOnHandle($tName,$btName) {
    global $dbback, $db;
    //有多少数据要备份
    $sqlNumber = "select count(*) from $tName ";
    $number = $dbback->getOne($sqlNumber);
    if($number>0) {
        $tempnum = 500;
	    $fornum = ceil($number / $tempnum);
        $j = 0;
	    for($i=0; $i<$fornum; $i++) { 
            $startnum = $i * $tempnum;
            $sql = "insert into $btName select * from $tName limit $startnum, $tempnum";
            $db->execute($sql);
            $j = $j + $db->Affected_Rows();
        }
        if ($j == $number) {
            sendMessage('成功备份表'.$tName.'的数据'.$number.'行');
        } else {
            sendMessage('备份失败');     
        }
    }
}
?>
