diff --git a/Application/Admin/Controller/SubSpendSetController.class.php b/Application/Admin/Controller/SubSpendSetController.class.php index ea24935e1..38fde0482 100644 --- a/Application/Admin/Controller/SubSpendSetController.class.php +++ b/Application/Admin/Controller/SubSpendSetController.class.php @@ -11,13 +11,17 @@ use Think\Controller; class SubSpendSetController extends Controller { const LIMIT = 100; + const DEPOSIT_LIMIT = 30; + const ECHO = FALSE; public $SubSpendModel; + public $DepositModel; public $MainSpendModel; public $SubKvModel; public $LastSynTime; public $SynTime; public $SynWhere = [ "pay_status"=>1, + "pay_way"=>['neq',0], "partner_type"=>["in",[0,PARTNER_TYPE]] ]; @@ -26,6 +30,7 @@ class SubSpendSetController extends Controller $this->SubSpendModel = M("spend","tab_",SUBSITE_DB);//指定子库 $this->MainSpendModel = M("spend","tab_");//指定子库 $this->SubKvModel = M("Kv","sub_",SUBSITE_DB);//指定子库 + $this->DepositModel = M("Deposit","tab_");//指定子库 $this->LastSynTime = $this->SubKvModel->where("`key` = 'sub_spend_syn_time'")->getField("value"); } @@ -45,16 +50,15 @@ class SubSpendSetController extends Controller die("时间不能小于最后同步时间"); } $this->SynWhere['payed_time'] = ['between', [$this->LastSynTime-0+1,$this->SynTime]]; - - if($this->pageSetSpend()){ - $this->updateLastSynTime(); - }; + $this->pageSetSpend(); + $this->pageSetDeposit(); + $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])$/"; + $patten = "/^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])$/"; if (!preg_match($patten, $begin)) { die("开始时间格式错误"); } @@ -70,13 +74,15 @@ class SubSpendSetController extends Controller $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(); + $this->pageSetDeposit(); } echo "sub_spend update and syn ok".PHP_EOL; } - //分页插入同步数据 + //分页插入spend同步数据 protected function pageSetSpend() { $count = $this->getSynCount(); @@ -85,10 +91,68 @@ class SubSpendSetController extends Controller 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; + if(self::ECHO){ + echo "[{$i}/{$pageCount}] {$addRes}".PHP_EOL; + } } return true; } + protected function pageSetDeposit(){ + $rechargeAmount = $this->getDepositAmount(); + $echoRechargeAmount = $rechargeAmount; + $depositCount = $this->getDepositCount(); + $consumeAmount = $depositCount['pay_amount']; + $count = $depositCount['count']; + $DepositWhere = $this->getDepositWhere(); + $pageCount = ceil($count/self::DEPOSIT_LIMIT); + for ($i=1; $i <= $pageCount; $i++) { + //预先统计 + $tres = $this->MainSpendModel->where($DepositWhere)->page($i,self::DEPOSIT_LIMIT)->select(); + $preAmount = $this->getDepositPageAmount($i); + if ($rechargeAmount - $preAmount >= 0) { + $addRes = $this->SubSpendModel->addAll($tres); + $rechargeAmount -= $preAmount; + if(self::ECHO){ + echo "[{$rechargeAmount}/{$echoRechargeAmount}] $preAmount".PHP_EOL; + } + }else{ + foreach ($tres as $key => $value) { + $preAmount = $value['pay_amount']; + if ($rechargeAmount - $preAmount < 0) { + return true; + } + $addRes = $this->SubSpendModel->add($value); + $rechargeAmount -= $preAmount; + if(self::ECHO){ + echo "[{$rechargeAmount}/{$echoRechargeAmount}] $preAmount".PHP_EOL; + } + } + } + } + return true; + } + //获取平台币相关统计 + protected function getDepositAmount() + { + return $this->DepositModel->where($this->SynWhere)->field("SUM(pay_amount) pay_amount")->find()['pay_amount']; + } + protected function getDepositCount() + { + return $this->MainSpendModel->where($this->getDepositWhere())->field("SUM(pay_amount) pay_amount,count(id) count")->find(); + } + protected function getDepositPageAmount($page){ + return $this->MainSpendModel + ->where($this->getDepositWhere()) + ->field("SUM(pay_amount) pay_amount") + ->page($i,self::DEPOSIT_LIMIT) + ->find()['pay_amount']; + } + protected function getDepositWhere(){ + $DepositWhere = $this->SynWhere; + $DepositWhere['pay_way'] = 0; + return $DepositWhere; + } + //删除本地已同步数据 protected function delSubSpend() {