You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.5 KiB
PHP
74 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace Admin\Controller;
|
|
use Think\Controller;
|
|
/**
|
|
* 子站同步表
|
|
* @example 按日期同步: php wm.php SubSpendSet/setSubSpend/time/2019-09-31
|
|
* @example 重算更新: php wm.php SubSpendSet/recountSubSpend/begin/2019-01-01/end/2019-09-31
|
|
* @author cz
|
|
*/
|
|
class SubSynTableController extends Controller
|
|
{
|
|
const CHECK_SUM_SQL = "checksum table ";
|
|
const TRUNCATE_SQL = "truncate table ";
|
|
const LIMIT = 100;
|
|
public $SubSynTableModel;
|
|
|
|
|
|
public function _initialize()
|
|
{
|
|
if(!IS_SUBSITE) die("仅能在子站脚本下运行");
|
|
$this->SubSynTableModel = M("syn_table","sub_",SUBSITE_DB);//指定子库
|
|
}
|
|
|
|
public function run(){
|
|
$synList = $this->SubSynTableModel->field("id,table_name,table_prefix,check_sum")->select();
|
|
foreach ($synList as $k => $v) {
|
|
$this->doSyn($v);
|
|
}
|
|
}
|
|
|
|
public function synOneTable($table_prefix,$tab_name)
|
|
{
|
|
$synInfo = $this->SubSynTableModel->where("table_name = '{$tab_name}' and table_prefix = '{$table_prefix}'")->field("id,table_name,table_prefix,check_sum")->find();
|
|
$this->doSyn($synInfo);
|
|
}
|
|
|
|
protected function doSyn($dbarr)
|
|
{
|
|
//获取最新check_sum
|
|
$table_prefix = $dbarr['table_prefix'];
|
|
$table_name = $dbarr['table_name'];
|
|
$res = M()->query(self::CHECK_SUM_SQL.$table_prefix.$table_name);
|
|
$now = time();
|
|
$check_sum = $res[0]['checksum'];
|
|
if($check_sum !== $dbarr['check_sum']){
|
|
$this->clearTable($table_prefix,$table_name);
|
|
$this->synTable($table_prefix,$table_name);
|
|
$dbarr['syn_time'] = $now;
|
|
$dbarr['check_sum'] = $check_sum;
|
|
}
|
|
$dbarr['update_time'] = $now;
|
|
$this->SubSynTableModel->save($dbarr);
|
|
echo $table_prefix.$table_name." success".PHP_EOL;
|
|
}
|
|
protected function clearTable($table_prefix,$table_name)
|
|
{
|
|
return SM()->query(self::TRUNCATE_SQL.$table_prefix.$table_name);
|
|
}
|
|
protected function synTable($table_prefix,$table_name)
|
|
{
|
|
$count = M($table_name,$table_prefix)->count();
|
|
if(empty($count)) return true;
|
|
$pageCount = ceil($count/self::LIMIT);
|
|
for ($i=1; $i <= $pageCount; $i++) {
|
|
$tres = M($table_name,$table_prefix)->page($i,self::LIMIT)->select();
|
|
$addRes = M($table_name,$table_prefix,SUBSITE_DB)->addAll($tres);
|
|
echo "[{$i}/{$pageCount}] {$addRes}".PHP_EOL;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
}
|