1, "pay_way"=>['neq',0], "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->DepositModel = M("Deposit","tab_");//指定子库 $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]]; $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])$/"; 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(); $this->pageSetDeposit(); } echo "sub_spend update and syn ok".PHP_EOL; } //分页插入spend同步数据 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); 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() { 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]); } }