@ -60,6 +60,8 @@ class SpendCountSetController extends \Think\Controller
$this->setGameCount();
//执行用户表统计
$this->setUserCount();
//执行月份统计
$this->setMonthCount();
//执行
$this->model->commit();
$this->usermodel->commit();
@ -74,7 +76,6 @@ class SpendCountSetController extends \Think\Controller
{
$this->getCashData();
$this->getBalanceData();
$this->getBingData();
$this->getInsideData();
$this->getRoot();
# code...
@ -122,7 +123,7 @@ class SpendCountSetController extends \Think\Controller
$map = array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"spend.pay_status"=>1,
"pay_way"=> array("LT",1 )
"pay_way"=> array("EQ",0 )
);
$field = "IFNULL(game.partner_id,0) partner_id,partner.partner partner_name,
spend.promote_id,spend.promote_account,spend.game_id,spend.game_name,
@ -168,29 +169,33 @@ class SpendCountSetController extends \Think\Controller
# code...
}
//获取绑定币充值
public function getBing Data()
public function getInside Data()
{
if(!$this->continue){
die("api error");
}
$bindRes = M()
->table("tab_bind_recharge bind")
->field("IFNULL(game.partner_id,0) partner_id,partner.partner partner_name,
bind.game_id,bind.game_name,bind.promote_id,bind.promote_account,
IFNULL(promote.parent_id,0) parent_id,promote.parent_name,sum(real_amount) bind_coin_count,
'{$this->date}' as count_date,'{$this->nowdata}' as create_time")
->where(array(
"bind.create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"bind.pay_status"=>1
))
->join("tab_promote promote ON bind.promote_id = promote.id","left")
->join("tab_game game ON bind.game_id = game.id","left")
->join("tab_partner partner ON partner.id = game.partner_id","left")
->group('bind.promote_id,bind.game_id')
->select();
//聚合表
if(!empty($bindRes)){
for ($i=0; $i < count ( $ bindRes ) ; $ i + + ) {
$map = array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"spend.pay_status"=>1,
"pay_way"=> array("LT",0)
);
$field = "IFNULL(game.partner_id,0) partner_id,partner.partner partner_name,
spend.promote_id,spend.promote_account,spend.game_id,spend.game_name,
IFNULL(promote.parent_id,0) parent_id,promote.parent_name,sum(pay_amount) inside_cash_count,
'{$this->date}' as count_date,'{$this->nowdata}' as create_time";
$balanceRes = M()
->table("tab_spend spend")
->field($field)
->where($map)
->join("tab_promote promote ON spend.promote_id = promote.id","left")
->join("tab_game game ON spend.game_id = game.id","left")
->join("tab_partner partner ON partner.id = game.partner_id","left")
->group('spend.promote_id,spend.game_id')
->select();
//集中两个表
if(!empty($balanceRes)){
for ($i=0; $i < count ( $ balanceRes ) ; $ i + + ) {
# code...
$tempmap = array(
"game_id"=>$balanceRes[$i]['game_id'],
@ -200,13 +205,13 @@ class SpendCountSetController extends \Think\Controller
$dbres = $this->model->where($tempmap)->find();
if(!$dbres){
//不存在
$tempdbres =$this->model->add($bind Res[$i]);
$tempdbres = $this->model->add($balance Res[$i]);
if(!$tempdbres){
$this->model->rollback();
die("error");
}
}else{
$dbres["bind_coin_count"] = $bindRes[$i]['bind_coin _count'];
$dbres["inside_cash_count"] = $balanceRes[$i]['inside_cash _count'];
$tempdbres = $this->model->save($dbres);
if(!$tempdbres){
$this->model->rollback();
@ -216,50 +221,7 @@ class SpendCountSetController extends \Think\Controller
}
}
}
//获取内充流水
public function getInsideData()
{
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")
->where(array(
"deposit.create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"pay_status"=>1
))
->join("tab_promote promote ON deposit.promote_id = promote.id","left")
->group('promote_id')
->select();
//聚合表
if(!empty($insideRes)){
for ($i=0; $i < count ( $ insideRes ) ; $ i + + ) {
# code...
$tempmap = array(
"promote_id"=>$insideRes[$i]['promote_id'],
"count_date"=>$this->date
);
$dbres = $this->model->where($tempmap)->find();
if(!$dbres){
//不存在
$tempdbres = $this->model->add($insideRes[$i]);
if(!$tempdbres){
$this->model->rollback();
die("error");
}
}else{
$savedata = array("inside_cash_count"=>$insideRes[$i]['inside_cash_count']);
$tempdbres = $this->model->where($tempmap)->save($savedata);
if(!$tempdbres){
$this->model->rollback();
die("error");
}
}
}
}
// return true;
}
public function getRoot()
{
if(!$this->continue){
@ -291,6 +253,8 @@ class SpendCountSetController extends \Think\Controller
}
}
}
/** --------以下为设定角色聚合---------- **/
/**
* 获取角色聚合
*/
@ -300,10 +264,9 @@ class SpendCountSetController extends \Think\Controller
$this->getRootUser();
$this->getParentUser();
$this->getPromoteUser();
$this->getInsideData2 ();
$this->getUser InsideData();
}
/** --------以下为辅助函数---------- **/
//获取会长信息
public function getRootUser()
{
@ -424,51 +387,162 @@ class SpendCountSetController extends \Think\Controller
}
# code...
}
//处理inside_cash_count
public function getInsideData2 ()
//渠道角色统计仅计算推广员的平台币发放与收回
public function getUser InsideData()
{
if(!$this->continue) die("api error");
// 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 ")
->table("tab_promote_coin ")
->field("* ")
->where(array(
"deposit.create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"pay_status"=>1
"create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"source_id"=>0,
"status"=>1
))
->join("tab_promote promote ON deposit.promote_id = promote.id","left")
->group('promote_id')
->select();
//聚合表
if(!empty($insideRes)){
for ($i=0; $i < count ( $ insideRes ) ; $ i + + ) {
# code...
$tempmap = array(
"promote_id"=>$insideRes[$i]['promote_id'],
"count_date"=>$this->date
);
$dbres = M("spend_user_count","tab_")->where($tempmap)->find();
//修正数据
if($dbres['root_id'] > 0 & & $dbres['parent_id'] > 0){//是推广员
//修正root
$this->setInsideCount($dbres['root_id'],$insideRes[$i]['inside_cash_count']);
//修正Parent
$this->setInsideCount($dbres['parent_id'],$insideRes[$i]['inside_cash_count']);
//修正自己
$this->setInsideCount($dbres['promote_id'],$insideRes[$i]['inside_cash_count']);
}elseif($dbres['root_id'] == 0 & & $dbres['parent_id'] > 0){//是组长
//修正root
$this->setInsideCount($dbres['parent_id'],$insideRes[$i]['inside_cash_count']);
//修正Parent
$this->setInsideCount($dbres['promote_id'],$insideRes[$i]['inside_cash_count']);
}else{//是会长
//修正root
$this->setInsideCount($dbres['promote_id'],$insideRes[$i]['inside_cash_count']);
if($insideRes[$i]['type'] > 1){
$insideRes[$i]['num'] = ( 0- $insideRes[$i]['num']);
}
$this->createUserCount($insideRes[$i]['promote_id'],$insideRes[$i]['num']);
}
}
}
//建立user表空数据
protected function createUserCount($promote_id,$inside_cash_count)
{
$promote = M("Promote","tab_")->field("id,account,parent_id,parent_name,grand_id,grand_account")->where("id='".$promote_id."'")->find();
if(empty($promote)){
return false;
}
$temprootarr=array(
"parent_id"=>0,
"root_id"=>0,
"inside_cash_count"=>$inside_cash_count,
"all_count"=>$inside_cash_count,
"count_date"=>$this->date,
"create_time"=>$this->nowdata
);
$tempparentarr=array(
"root_id"=>0,
"inside_cash_count"=>$inside_cash_count,
"all_count"=>$inside_cash_count,
"count_date"=>$this->date,
"create_time"=>$this->nowdata
);
$temppromotearr=array(
"inside_cash_count"=>$inside_cash_count,
"all_count"=>$inside_cash_count,
"count_date"=>$this->date,
"create_time"=>$this->nowdata
);
if($promote["parent_id"] == 0){
//会长
$dbres = M("spend_user_count","tab_")->where(array(
"promote_id"=>$promote["id"],
"count_date"=>$this->date
))->find();
if(empty($dbres)){
$temprootarr["promote_id"]= $promote['id'];
$temprootarr["promote_account"]= $promote['account'];
$this->usermodel->add($temprootarr);
}else{
//叠加
$this->setInsideCount($promote['id'],$inside_cash_count);
}
}elseif($promote["parent_id"] > 0 & & $promote["grand_id"] == 0){
//是组长
//添加会长
$dbres = M("spend_user_count","tab_")->where(array(
"promote_id"=>$promote["parent_id"],
"count_date"=>$this->date
))->find();
if(empty($dbres)){
$temprootarr["promote_id"]= $promote['parent_id'];
$temprootarr["promote_account"]= $promote['parent_name'];
$this->usermodel->add($temprootarr);
}else{
//叠加
$this->setInsideCount($promote['parent_id'],$inside_cash_count);
}
//添加自己
$dbres = M("spend_user_count","tab_")->where(array(
"promote_id"=>$promote["id"],
"count_date"=>$this->date
))->find();
if(empty($dbres)){
$tempparentarr['promote_id']=$promote['id'];
$tempparentarr['promote_account']=$promote['account'];
$tempparentarr['parent_id']=$promote["parent_id"];
$tempparentarr['parent_name']=$promote["parent_name"];
$this->usermodel->add($tempparentarr);
}else{
//叠加
$this->setInsideCount($promote['id'],$inside_cash_count);
}
}else{
//推广员
//添加会长
$dbres = M("spend_user_count","tab_")->where(array(
"promote_id"=>$promote["grand_id"],
"count_date"=>$this->date
))->find();
if(empty($dbres)){
$temprootarr["promote_id"]= $promote['grand_id'];
$temprootarr["promote_account"]= $promote['grand_account'];
$this->usermodel->add($temprootarr);
}else{
//叠加
$this->setInsideCount($promote['grand_id'],$inside_cash_count);
}
//添加组长
$dbres = M("spend_user_count","tab_")->where(array(
"promote_id"=>$promote["parent_id"],
"count_date"=>$this->date
))->find();
if(empty($dbres)){
$tempparentarr["promote_id"]= $promote['parent_id'];
$tempparentarr["promote_account"]= $promote['parent_name'];
$tempparentarr['parent_id']=$promote["grand_id"];
$tempparentarr['parent_name']=$promote["grand_account"];
$this->usermodel->add($tempparentarr);
}else{
//叠加
$this->setInsideCount($promote['parent_id'],$inside_cash_count);
}
$dbres = M("spend_user_count","tab_")->where(array(
"promote_id"=>$promote["id"],
"count_date"=>$this->date
))->find();
if(empty($dbres)){
$temppromotearr['promote_id']=$promote['id'];
$temppromotearr['promote_account']=$promote['account'];
$temppromotearr['parent_id']=$promote["parent_id"];
$temppromotearr['parent_name']=$promote["parent_name"];
$temppromotearr['root_id']=$promote['grand_id'];
$temppromotearr['root_name']=$promote['grand_account'];
$this->usermodel->add($temppromotearr);
}else{
//叠加
$this->setInsideCount($promote['id'],$inside_cash_count);
}
//添加自己
}
// return true;
# code...
}
//按角色修正inside_cash_count数据
public function setInsideCount($promote_id,$inside_cash_count)
@ -480,11 +554,68 @@ 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']);
$tempdbres = $this->usermodel->where($tempmap)->save($savedata);
if(!$tempdbres){
if($tempdbres === false){
dump($dbres);
dump($savedata);
$this->usermodel->rollback();
die("setInsideCount error");
}
# code...
}
/** --------以下为设定月份聚合---------- **/
public function setMonthCount()
{
$initdata = $this->getMonthInit();
$balance = $this->getBalanceUser();
$bind = $this->getBindUser();
$initdata["inside_cash_count"] = $initdata["inside_cash_count"]-$balance-$bind;
$initdata["all_count"] = $initdata["inside_cash_count"]-0+$initdata["balance_coin_count"]-0+$initdata["cash_count"];
$initdata["count_date"] = $this->date;
$initdata["create_time"] = $this->nowdata;
M("spend_month_count","tab_")->add($initdata);
# code...
}
//获取除了内充的
protected function getMonthInit()
{
$tempmap = array(
"count_date"=>$this->date,
"parent_id"=>0
);
return M("spend_user_count","tab_")->field("sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) inside_cash_count")->where($tempmap)->find();
}
//设置 后台发放平台币给玩家 及 回收玩家 的最终值
protected function getBalanceUser()
{
$map = array(
"create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"status"=>1
);
$add = M("provide_user","tab_")->field("sum(amount) amount")->where($map)->find()['amount'];
$add || $add=0;
$jq = M("user_coin","tab_")->field("sum(num) num")->where(array(
"create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth))
))->find()['num'];
$jq || $jq=0;
return $add-$jq;
}
//设置 后台发放绑币给玩家 及 回收玩家绑币 的最终值
protected function getBindUser()
{
$map = array(
"create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"status"=>1
);
$add = M("provide","tab_")->field("sum(amount) amount")->where($map)->find()['amount'];
$add || $add=0;
$jq = M("deduct_bind_record","tab_")->field("sum(quantity) quantity")->where(array(
"create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth))
))->find()['quantity'];
$jq || $jq=0;
return $add-$jq;
}
}