<?php

/**
 * 备份竞投相关的表
 * 参数type heat 或 info 分别表示刷热度等级和资料完善度等级
 */
set_time_limit(0);
error_reporting(7);
ini_set("display_errors",false);
$conf_basedir = "../../../../"; //相对主目录的位置
define('WEB_ROOT', $conf_basedir);
$conf_where = "admin"; //选择调用相应模块
$directory = basename(dirname(__FILE__)); //定义当前所在的目录，引入模块公用函数
include_once(  "./base.inc.php");
require_once '../function_crontab.php';

//

//print_r($tables);exit;
$container = Container::getInstance();
$db = $container->get('db');

$periodsIds = getBidPeriodsBack(1);//要备份的期次id数组
//exit;
//备份小区竞投
$tName = 'growth_bid_block_expert';//要备份的表名
$btName = $tName.'_bak';//备份到的表名
$conf = "bid_periods_id in ('".  implode("','", $periodsIds)."')";
bidBackCommon($tName,$btName,$conf);
echo "<br/><br/>";

//备份小区竞投记录
$tName = 'growth_bid_block_expert_record_history';//要备份的表名
$btName = $tName.'_bak';//备份到的表名
$conf = "bid_periods_id in ('".  implode("','", $periodsIds)."')";
bidBackCommon($tName,$btName,$conf);
echo "<br/><br/>";
//备份竞投期次
$tName = 'growth_bid_periods';//要备份的表名
$btName = $tName.'_bak';//备份到的表名
$conf = "id in ('".  implode("','", $periodsIds)."')";
bidBackCommon($tName,$btName,$conf);
echo "<br/><br/>";

//备份房源竞投
$periodsIds  = getBidPeriodsBack(2);//要备份的期次id数组
$tName = 'growth_bid_house';//要备份的表名
$btName = $tName.'_bak';//备份到的表名
$conf = "bid_periods_id in ('".  implode("','", $periodsIds)."')";
bidBackCommon($tName,$btName,$conf);
echo "<br/><br/>";

//备份房源竞投记录
$tName = 'growth_bid_house_record_history';//要备份的表名
$btName = $tName.'_bak';//备份到的表名
$conf = "bid_periods_id in ('".  implode("','", $periodsIds)."')";
bidBackCommon($tName,$btName,$conf);
echo "<br/><br/>";

$tName = 'growth_bid_periods';//要备份的表名
$btName = $tName.'_bak';//备份到的表名
$conf = "id in ('".  implode("','", $periodsIds)."')";
bidBackCommon($tName,$btName,$conf);
echo "<br/><br/>";

/**
 * 获得要备份的期次
 * @param type $type 表示是小区，还是房源
 * @param type $remainP 要备份多少期之前的，从0算
 * @return type 期次id数组
 */
function getBidPeriodsBack($type=1,$remainP=7) {
    $container = Container::getInstance();
    $db = $container->get('db');
    $tNameBidPeriode = "growth_bid_periods";
    $p8Sql = "select * from $tNameBidPeriode where type=$type order by ctime desc limit 7,1";
    $p8 = $db->getAll($p8Sql);
    if($p8){
        $p8 = $p8[0];
    }
    //print_r($p8);exit;
    $time8 = $p8['ctime'];
    $sqlPeriodsOlder = "select id from $tNameBidPeriode where type=$type and ctime <'$time8'";
    $pMore8 = $db->getAll($sqlPeriodsOlder);
    //print_r($pMore8);
    $periodsIds = array();
    if($pMore8) {
        foreach ($pMore8 as $value) {
            $periodsIds[] = $value['id'];
        }
    }
    return $periodsIds;
}

/**
 * 
 * @param type $tName
 * @param type $btName
 * @param type $conf
 */
function bidBackCommon($tName,$btName,$conf) {
    //return ;
    $container = Container::getInstance();
    $db = $container->get('db');
    
    $tables = array();
    $tablesSql = "SHOW TABLES";
    $tablesArr = $db->getAll($tablesSql);
    //数据库中的所有表
    if($tablesArr) {
        foreach ($tablesArr as $value) {
            $tables[] = $value['Tables_in_hf_house'];
        }
    }
    //没有备份表，从原表建新的备份表
    if(!in_array($btName, $tables)){
        $sqlCT = "create table $btName like $tName";
        $db->execute($sqlCT);
    }
    
    //有多少数据要备份
    $sqlNumber = "select count(*) from $tName where $conf";
    $number = $db->getOne($sqlNumber);
    if($number>0) {
        $sql = "insert into $btName select * from $tName where $conf";
        $db->execute($sql);
        if($db->Affected_Rows()==$number) {
            echo '成功备份表'.$tName.'的数据'.$number.'行';
            //删除原来数据
            $sqlD = "delete from $tName where $conf";
            $db->execute($sqlD);
        } else {
            echo $tName."备份失败";
            //删除插入的数据
            $sqlD = "delete from $btName where $conf";
            $db->execute($sqlD);
        }
    } else {
        echo $tName."没有数据要备份";
    }
    
}
?>
