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.

619 lines
24 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace Admin\Controller;
/**
* 游戏流水聚合接口
* @author chenzhi
*/
class SpendCountSetController extends \Think\Controller
{
public $beginThismonth;
public $endThismonth;
public $date;
public $nowdata;
public $model;
public $usermodel;
public $continue=false;//单元测试,开启后可访问独立函数测试
public function _initialize(){
//初始化
$this->beginThismonth = mktime(0,0,0,date('m')-1,1,date('Y'));
$this->endThismonth = mktime(0,0,0,date('m'),1,date('Y'))-1;
$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_');
$this->monthmodel =M("spend_month_count",'tab_');
$this->gamemodel =M("spend_count",'tab_');
}
/**
* TODO:仅供测试,测试结束后删除
* 更新某月数据
*/
public function setMonthSpendCount()
{
$month = I("count_date");
if(empty($month)) die("参数错误");
$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->reCount();
$this->setSpendCount();
}
/**
* 强制重新聚合
*/
public function reCount()
{
$recount = I("recount");
if(empty($recount) || $recount != 1){return ;}
# code...
//清理之前的聚合
$temp =array(
"count_date"=>$this->date
);
$this->model->where($temp)->delete();
$this->usermodel->where($temp)->delete();
$this->monthmodel->where($temp)->delete();
echo "重置成功执行重新生成:";
}
/**
* 每个月的统计接口
*/
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'];
$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,msg:{$this->date}已经聚合过");
}
$this->model->startTrans();
$this->usermodel->startTrans();
$this->continue =true;
//执行游戏表统计
$this->setGameCount();
//执行用户表统计
$this->setUserCount();
//执行月份统计
$this->setMonthCount();
//执行
$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->getInsideData();
$this->getRoot();
# code...
}
//获取游戏现金流水
protected function getCashData()
{
$this->createGameCount("cash_count");
}
//获取平台币聚合
protected function getBalanceData()
{
$this->createGameCount("balance_coin_count");
}
//获取绑定币充值
protected function getInsideData()
{
$this->createGameCount("inside_cash_count");
}
protected function createGameCount($type)
{
$map = array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"spend.pay_status"=>1
);
if($type == "cash_count"){
//现金统计
$map['pay_way'] = array("GT",0);
}elseif($type == "balance_coin_count"){
$map['pay_way']= array("EQ",0);
}elseif($type == "inside_cash_count"){
$map['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,sum(pay_amount) {$type},
IFNULL(promote.parent_id,0) parent_id,promote.parent_name,
'{$this->date}' as count_date,'{$this->nowdata}' as create_time";
$spendRes = 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($spendRes)){
return ;
}
//集中两个表
if($type == "cash_count"){
$dbres = $this->gamemodel->addAll($spendRes);
if(!$dbres){
$this->model->rollback();
die("error");
}
}else{
for ($i=0; $i < count($spendRes); $i++) {
$spendRes[$i][$type] || $spendRes[$i][$type]=0;
# code...
$tempmap = array(
"game_id"=>$spendRes[$i]['game_id'],
"promote_id"=>$spendRes[$i]['promote_id'],
"count_date"=>$this->date
);
$dbres = $this->gamemodel->where($tempmap)->find();
if(!$dbres){
//不存在
$tempdbres = $this->gamemodel->add($spendRes[$i]);
if(!$tempdbres){
$this->model->rollback();
die("error");
}
}else{
$dbres[$type] = $dbres[$type]-0+$spendRes[$i][$type];
$tempdbres = $this->gamemodel->save($dbres);
if($tempdbres === false){
$this->model->rollback();
die("error");
}
}
}
}
# code...
}
protected function getRoot()
{
if(!$this->continue){
die("api error");
}
$map = array(
"parent_id"=>array("GT",0),
"count_date"=>$this->date
);
$user = $this->gamemodel->field("parent_id,promote_id")->where($map)->group("promote_id")->select();
$Promote=M("promote","tab_");
for ($i=0; $i < count($user); $i++) {
# code...
$map['id']=$user[$i]['parent_id'];
$root=$Promote->field('IFNULL(parent_id,0) root_id,parent_name root_name')->where($map)->find();
if(!$root){
$root['root_id']=0;
$root['root_name']='官方渠道';
}
//保存
$where = array(
"promote_id"=>$user[$i]['promote_id'],
"count_date"=>$this->date
);
$tempdbres = $this->gamemodel->where($where)->save($root);
if(!$tempdbres){
$this->model->rollback();
die("setRoot error");
}
}
}
/** --------以下为设定角色聚合---------- **/
/**
* 获取角色聚合
*/
public function setUserCount()
{
//获取会长信息
$this->getRootUser();
$this->getParentUser();
$this->getPromoteUser();
$this->getUserInsideData();
}
//获取会长信息
protected function getRootUser()
{
if(!$this->continue) die("api error");
//获取所有数据
$month = $this->date;
$from = "
(
SELECT root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) bind_coin_count FROM tab_spend_count WHERE root_id > 0 and count_date='{$month}' group by root_id
UNION ALL
SELECT promote_id,promote_account,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) bind_coin_count FROM tab_spend_count WHERE parent_id = 0 and count_date='{$month}' group by promote_id
UNION ALL
SELECT parent_id,parent_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) bind_coin_count FROM tab_spend_count WHERE parent_id > 0 and root_id = 0 and count_date='{$month}' group by parent_id
)
";
$subQueryAll = M()->table($from." a")
->field('root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(bind_coin_count) bind_coin_count')
->group("a.root_id")
// ->having("cash_count > 0 OR balance_coin_count > 0")
->select();
for ($i=0; $i<count($subQueryAll); $i++) {
$subQueryAll[$i]['bind_coin_count'] || $subQueryAll[$i]['bind_coin_count']=0;
$subQueryAll[$i]['cash_count'] || $subQueryAll[$i]['cash_count']=0;
$subQueryAll[$i]['balance_coin_count'] || $subQueryAll[$i]['balance_coin_count']=0;
$adddata = array(
"promote_id"=>$subQueryAll[$i]['root_id'],
"promote_account"=>$subQueryAll[$i]['root_name'],
"parent_id"=>0,
"parent_name"=>'官方渠道',
"root_id"=>0,
"root_name"=>'官方渠道',
"cash_count"=>$subQueryAll[$i]['cash_count'],
"balance_coin_count"=>$subQueryAll[$i]['balance_coin_count'],
"bind_coin_count"=>$subQueryAll[$i]['bind_coin_count'],
"all_count"=>$subQueryAll[$i]['cash_count']-0+$subQueryAll[$i]['balance_coin_count']-0+$subQueryAll[$i]['bind_coin_count'],
"count_date"=>$this->date,
"create_time"=>$this->nowdata
);
$tempdbres = $this->usermodel->add($adddata);
if(!$tempdbres){
$this->usermodel->rollback();
die("getRootUser error");
}
}
}
//获取组长信息
protected function getParentUser()
{
if(!$this->continue) die("api error");
$month =$this->date;
//获取数据
$from = "
(
SELECT parent_id,parent_name,root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) bind_coin_count FROM tab_spend_count WHERE root_id > 0 and count_date='{$month}' group by parent_id
UNION
SELECT promote_id,promote_account,parent_id,parent_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) bind_coin_count FROM tab_spend_count WHERE parent_id >0 and root_id = 0 and count_date='{$month}' group by promote_id
)
";
$subQueryAll = M()->table($from." a")
->field('parent_id,parent_name,root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(bind_coin_count) bind_coin_count')
->group("a.parent_id")
// ->having("cash_count > 0 or balance_coin_count > 0 ")
->select();
for ($i=0; $i<count($subQueryAll); $i++) {
$subQueryAll[$i]['bind_coin_count'] || $subQueryAll[$i]['bind_coin_count']=0;
$subQueryAll[$i]['cash_count'] || $subQueryAll[$i]['cash_count']=0;
$subQueryAll[$i]['balance_coin_count'] || $subQueryAll[$i]['balance_coin_count']=0;
$adddata = array(
"promote_id"=>$subQueryAll[$i]['parent_id'],
"promote_account"=>$subQueryAll[$i]['parent_name'],
"parent_id"=>$subQueryAll[$i]['root_id'],
"parent_name"=>$subQueryAll[$i]['root_name'],
"root_id"=>0,
"root_name"=>'官方渠道',
"cash_count"=>$subQueryAll[$i]['cash_count'],
"balance_coin_count"=>$subQueryAll[$i]['balance_coin_count'],
"bind_coin_count"=>$subQueryAll[$i]['bind_coin_count'],
"all_count"=>$subQueryAll[$i]['cash_count']-0+$subQueryAll[$i]['balance_coin_count']-0+$subQueryAll[$i]['bind_coin_count'],
"count_date"=>$this->date,
"create_time"=>$this->nowdata
);
$tempdbres = $this->usermodel->add($adddata);
if(!$tempdbres){
$this->usermodel->rollback();
die("getParentUser error");
}
}
}
//获取组员
protected function getPromoteUser()
{
if(!$this->continue) die("api error");
$month = $this->date;;
//获取数据
$from = "
(
SELECT promote_id,promote_account,parent_id,parent_name,root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count,sum(inside_cash_count) bind_coin_count FROM tab_spend_count WHERE parent_id > 0 and root_id > 0 and count_date='{$month}' group by promote_id
)
";
$subQueryAll = M()->table($from." a")
->field('*')
->group("a.promote_id")
// ->having("cash_count > 0 or balance_coin_count > 0")
->select();
for ($i=0; $i<count($subQueryAll); $i++) {
$subQueryAll[$i]['bind_coin_count'] || $subQueryAll[$i]['bind_coin_count']=0;
$subQueryAll[$i]['cash_count'] || $subQueryAll[$i]['cash_count']=0;
$subQueryAll[$i]['balance_coin_count'] || $subQueryAll[$i]['balance_coin_count']=0;
$adddata = array(
"promote_id"=>$subQueryAll[$i]['promote_id'],
"promote_account"=>$subQueryAll[$i]['promote_account'],
"parent_id"=>$subQueryAll[$i]['parent_id'],
"parent_name"=>$subQueryAll[$i]['parent_name'],
"root_id"=>$subQueryAll[$i]['root_id'],
"root_name"=>$subQueryAll[$i]['root_name'],
"cash_count"=>$subQueryAll[$i]['cash_count'],
"balance_coin_count"=>$subQueryAll[$i]['balance_coin_count'],
"bind_coin_count"=>$subQueryAll[$i]['bind_coin_count'],
"all_count"=>$subQueryAll[$i]['cash_count']-0+$subQueryAll[$i]['balance_coin_count']-0+$subQueryAll[$i]['bind_coin_count'],
"count_date"=>$this->date,
"create_time"=>$this->nowdata
);
$tempdbres = $this->usermodel->add($adddata);
if(!$tempdbres){
$this->usermodel->rollback();
die("getPromoteUser error");
}
}
# code...
}
//渠道角色统计仅计算推广员的平台币发放与收回
protected function getUserInsideData()
{
// if(!$this->continue) die("api error");
//获取发送给推广员的数据
$insideRes = M()
->table("tab_promote_coin")
->field("*")
->where(array(
"create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"source_id"=>0,
"status"=>1
))
->select();
if(!empty($insideRes)){
for ($i=0; $i < count($insideRes); $i++) {
# code...
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);
}
//添加自己
}
# code...
}
//按角色修正inside_cash_count数据
protected function setInsideCount($promote_id,$inside_cash_count)
{
$tempmap = array(
"promote_id"=>$promote_id,
"count_date"=>$this->date
);
$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']);
$savedata = array("inside_cash_count"=>$inside_cash_count-0+$dbres['inside_cash_count']);
$tempdbres = $this->usermodel->where($tempmap)->save($savedata);
if($tempdbres === false){
$this->usermodel->rollback();
die("setInsideCount error");
}
# code...
}
/** --------以下为设定月份聚合---------- **/
/**
* @param 现金流水 cash_count 游戏中充值的现金
* @param 平台币流水 balance_coin_count 游戏中消耗的平台币
* @param 绑定币流水 bind_coin_count 游戏中绑定币的消耗
* @param 内充流水 inside_cash_count:(后台发放给玩家平台币-回收平台币 + (后台发放绑币给玩家 -回收的绑币)
* @param 平台币充值 balance_coin_deposit
* @return void
*/
public function setMonthCount()
{
$initdata = $this->getMonthInit();
$balance = $this->getBalanceUser();
$bind = $this->getBindUser();
$deposit = $this->getBalanceDeposit();
$initdata["inside_cash_count"] = $balance-0+$bind;
$initdata["balance_coin_deposit"] = $deposit;
$initdata["all_count"] = $initdata["inside_cash_count"]-0+$initdata["cash_count"]-0+$deposit;
$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(bind_coin_count) bind_coin_count")->where($tempmap)->find();
}
//获取平台币充值流水
protected function getBalanceDeposit()
{
$map = array(
"create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"pay_status"=>1
);
$deposit = M("deposit","tab_")->field("SUM(pay_amount) pay_amount")->where($map)->find()['pay_amount'];
$deposit || $deposit=0;
return $deposit;
}
//设置 后台发放平台币给玩家 及 回收玩家 的最终值
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;
}
}