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.

108 lines
3.4 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 SubSpendSetController extends Controller
{
const LIMIT = 100;
public $SubSpendModel;
public $MainSpendModel;
public $SubKvModel;
public $LastSynTime;
public $SynTime;
public $SynWhere = [
"pay_status"=>1,
"partner_type"=>["in",[0,PARTNER_TYPE]]
];
public function _initialize()
{
$this->SubSpendModel = M("spend","tab_",SUBSITE_DB);//指定子库
$this->MainSpendModel = M("spend","tab_");//指定子库
$this->SubKvModel = M("Kv","sub_",SUBSITE_DB);//指定子库
$this->LastSynTime = $this->SubKvModel->where("`key` = 'sub_spend_syn_time'")->getField("value");
}
public function setSubSpend($time)
{
if($time == '') die("时间不能为空");
//判断日期格式
$patten = "/^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])$/";
if (!preg_match($patten, $time)) {
die("时间格式错误");
}
if(strtotime($time)+86399-24*3600 > time()){
die("时间不能大于当前");
}
$this->SynTime = strtotime($time)+86399;
if($this->SynTime <= $this->LastSynTime){
die("时间不能小于最后同步时间");
}
$this->SynWhere['payed_time'] = ['between', [$this->LastSynTime-0+1,$this->SynTime]];
if($this->pageSetSpend()){
$this->updateLastSynTime();
};
echo "sub_spend syn ok".PHP_EOL;
}
public function recountSubSpend($begin,$end)
{
$patten = "/^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])$/";
if (!preg_match($patten, $begin)) {
die("开始时间格式错误");
}
if (!preg_match($patten, $end)) {
die("结束时间格式错误");
}
if(strtotime($end) < strtotime($begin)){
die("结束时间不能比开始时间小");
}
if(strtotime($end)+86399 > $this->LastSynTime){
die("结束时间不能大于最后同步时间");
}
$end = strtotime($end)+86399;
$begin = strtotime($begin);
$this->SynWhere['payed_time'] = ['between', [$begin,$end]];
if($this->delSubSpend() !== false){
echo "delete sub_spend ok".PHP_EOL;
$this->pageSetSpend();
}
echo "sub_spend update and syn ok".PHP_EOL;
}
//分页插入同步数据
protected function pageSetSpend()
{
$count = $this->getSynCount();
if(empty($count)) return true;
$pageCount = ceil($count/self::LIMIT);
for ($i=1; $i <= $pageCount; $i++) {
$tres = $this->MainSpendModel->where($this->SynWhere)->page($i,self::LIMIT)->select();
$addRes = $this->SubSpendModel->addAll($tres);
echo "[{$i}/{$pageCount}] {$addRes}".PHP_EOL;
}
return true;
}
//删除本地已同步数据
protected function delSubSpend()
{
return $this->SubSpendModel->where($this->SynWhere)->delete();
}
//获取需要同步的数量
protected function getSynCount()
{
return $this->MainSpendModel->where($this->SynWhere)->count();
}
//更新最后同步时间
protected function updateLastSynTime()
{
return $this->SubKvModel->where("`key` = 'sub_spend_syn_time'")->save(['value'=>$this->SynTime]);
}
}