diff --git a/Application/Admin/Controller/SpendCountSetController.class.php b/Application/Admin/Controller/SpendCountSetController.class.php index b59d2f9bd..d797ad0d5 100644 --- a/Application/Admin/Controller/SpendCountSetController.class.php +++ b/Application/Admin/Controller/SpendCountSetController.class.php @@ -12,6 +12,7 @@ class SpendCountSetController extends \Think\Controller public $date; public $nowdata; public $model; + public $usermodel; public $continue=false;//单元测试,开启后可访问独立函数测试 public function _initialize(){ //初始化 @@ -20,69 +21,63 @@ class SpendCountSetController extends \Think\Controller $this->date = date('Y')."-".((date('m')-1) > 9 ? (date('m')-1) : "0".(date('m')-1)); $this->nowdata =time(); $this->model =M("spend_count",'tab_'); + $this->usermodel =M("spend_user_count",'tab_'); } /** - * 初始化统计/用于之前数据的聚合 + * TODO:仅供测试,测试结束后删除 + * 更新某月数据 */ - public function initCount() + public function setMonthSpendCount() { - $countRes = M("spend_count","tab_")->field("count(*) date_count")->find()['date_count']; - if($countRes > 0){ - die("init error"); - } - $nowdate = date('Y')."-".(date('m') > 9 ? date('m') : "0".date('m')); - $nowdate = "2017-10"; - //获取所有的月份 pay_time - $res = M("spend",'tab_')->field("FROM_UNIXTIME(pay_time,'%Y-%m') AS pay_time")->buildSql(); - $res = M()->table($res.' a')->group('pay_time')->select(); - //执行数据统计 - for ($i=0; $i < count($res); $i++) { - # code... - if($res[$i]['pay_time'] != $nowdate){ - $month = $res[$i]['pay_time']; - $this->date = $month; - $tarry = explode('-',$month); - $this->beginThismonth=mktime(0,0,0,$tarry[1],1,$tarry[0]); - $this->endThismonth=mktime(0,0,0,$tarry[1]-0+1,1,$tarry[0])-1; - //执行 - $this->model->startTrans(); - $this->continue =true; - $this->getCashData(); - $this->getBalanceData(); - $this->getBingData(); - $this->getInsideData(); - $this->setRoot(); - //执行用户表统计 - $this->setUserCount(); + $month = I("count_date"); + if(empty($month)) die("参数错误"); - $this->model->commit(); - } - } - die("success"); - + $this->date = $month; + $tarry = explode('-',$month); + $this->beginThismonth=mktime(0,0,0,$tarry[1],1,$tarry[0]); + $this->endThismonth=mktime(0,0,0,$tarry[1]-0+1,1,$tarry[0])-1; + + $this->setSpendCount(); } + /** * 每个月的统计接口 */ public function setSpendCount() { + set_time_limit(0); + $t1 = microtime(true); //判断是否已经聚合 $countRes = M("spend_count","tab_")->field("count(*) date_count")->where("count_date = '{$this->date}'")->find()['date_count']; - if($countRes > 0){ - die("Repeated statistics"); + $countUserRes = M("spend_user_count","tab_")->field("count(*) date_count")->where("count_date = '{$this->date}'")->find()['date_count']; + if($countRes > 0 || $countUserRes >0){ + die("error:Repeated statistics"); } $this->model->startTrans(); + $this->usermodel->startTrans(); $this->continue =true; + //执行游戏表统计 + $this->setGameCount(); + //执行用户表统计 + $this->setUserCount(); + //执行 + $this->model->commit(); + $this->usermodel->commit(); + + $t2 = microtime(true); + die("success runtime:".round($t2-$t1,3).'s'); + } + /** + * 获取游戏聚合 + */ + public function setGameCount() + { $this->getCashData(); $this->getBalanceData(); $this->getBingData(); $this->getInsideData(); - $this->setRoot(); - //执行用户表统计 - $this->setUserCount(); - - $this->model->commit(); - die("success"); + $this->getRoot(); + # code... } //获取游戏现金流水 public function getCashData() @@ -265,7 +260,7 @@ class SpendCountSetController extends \Think\Controller } // return true; } - public function setRoot() + public function getRoot() { if(!$this->continue){ die("api error"); @@ -308,10 +303,11 @@ class SpendCountSetController extends \Think\Controller $this->getInsideData2(); } - /** --------以下为辅助函数--------- **/ + /** --------以下为辅助函数---------- **/ //获取会长信息 public function getRootUser() { + if(!$this->continue) die("api error"); //获取所有数据 $month = $this->date; $from = " @@ -342,13 +338,18 @@ class SpendCountSetController extends \Think\Controller "count_date"=>$this->date, "create_time"=>$this->nowdata ); - M("spend_user_count","tab_")->add($adddata); + $tempdbres = $this->usermodel->add($adddata); + if(!$tempdbres){ + $this->usermodel->rollback(); + die("getRootUser error"); + } } } //获取组长信息 public function getParentUser() { + if(!$this->continue) die("api error"); $month =$this->date; //获取数据 $from = " @@ -377,13 +378,18 @@ class SpendCountSetController extends \Think\Controller "count_date"=>$this->date, "create_time"=>$this->nowdata ); - M("spend_user_count","tab_")->add($adddata); + $tempdbres = $this->usermodel->add($adddata); + if(!$tempdbres){ + $this->usermodel->rollback(); + die("getParentUser error"); + } } } //获取组员 public function getPromoteUser() { + if(!$this->continue) die("api error"); $month = $this->date;; //获取数据 $from = " @@ -410,14 +416,18 @@ class SpendCountSetController extends \Think\Controller "count_date"=>$this->date, "create_time"=>$this->nowdata ); - M("spend_user_count","tab_")->add($adddata); + $tempdbres = $this->usermodel->add($adddata); + if(!$tempdbres){ + $this->usermodel->rollback(); + die("getPromoteUser error"); + } } # code... } //处理inside_cash_count public function getInsideData2() { - + if(!$this->continue) die("api error"); $insideRes = M() ->table("tab_deposit deposit") ->field("promote_id,promote_account,IFNULL(promote.parent_id,0) parent_id,parent_name,sum(pay_amount) inside_cash_count,'{$this->date}' as count_date,'{$this->nowdata}' as create_time") @@ -469,7 +479,11 @@ class SpendCountSetController extends \Think\Controller ); $dbres = M("spend_user_count","tab_")->where($tempmap)->find(); $savedata = array("inside_cash_count"=>$inside_cash_count-0+$dbres['inside_cash_count'],"all_count"=>$inside_cash_count-0+$dbres['all_count']); - M("spend_user_count","tab_")->where($tempmap)->save($savedata); + $tempdbres = $this->usermodel->where($tempmap)->save($savedata); + if(!$tempdbres){ + $this->usermodel->rollback(); + die("setInsideCount error"); + } # code... }