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.

491 lines
20 KiB
PHTML

5 years ago
<?php
namespace Admin\Controller;
/**
5 years ago
* 游戏流水聚合接口
* @author chenzhi
5 years ago
*/
class SpendCountSetController extends \Think\Controller
{
public $beginThismonth;
public $endThismonth;
public $date;
public $nowdata;
public $model;
public $usermodel;
5 years ago
public $continue=false;//单元测试,开启后可访问独立函数测试
5 years ago
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('t'),date('Y'))-1;
$this->date = date('Y')."-".((date('m')-1) > 9 ? (date('m')-1) : "0".(date('m')-1));
$this->nowdata =time();
5 years ago
$this->model =M("spend_count",'tab_');
$this->usermodel =M("spend_user_count",'tab_');
5 years ago
}
5 years ago
/**
* TODO:仅供测试,测试结束后删除
* 更新某月数据
5 years ago
*/
public function setMonthSpendCount()
5 years ago
{
$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->setSpendCount();
5 years ago
}
5 years ago
/**
* 每个月的统计接口
*/
5 years ago
public function setSpendCount()
{
set_time_limit(0);
$t1 = microtime(true);
5 years ago
//判断是否已经聚合
$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");
5 years ago
}
$this->model->startTrans();
$this->usermodel->startTrans();
5 years ago
$this->continue =true;
//执行游戏表统计
$this->setGameCount();
5 years ago
//执行用户表统计
$this->setUserCount();
//执行
5 years ago
$this->model->commit();
$this->usermodel->commit();
$t2 = microtime(true);
die("success runtime:".round($t2-$t1,3).'s');
5 years ago
}
/**
* 获取游戏聚合
*/
public function setGameCount()
{
$this->getCashData();
$this->getBalanceData();
$this->getBingData();
$this->getInsideData();
$this->getRoot();
# code...
}
5 years ago
//获取游戏现金流水
public function getCashData()
{
if(!$this->continue){
die("api error");
}
$map = array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"spend.pay_status"=>1,
"pay_way"=>array("GT",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) cash_count,
'{$this->date}' as count_date,'{$this->nowdata}' as create_time";
//获取现金
$cashRes = 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($cashRes)){
5 years ago
$dbres = $this->model->addAll($cashRes);
5 years ago
if(!$dbres){
$this->model->rollback();
die("error");
}
}
# code...
}
//获取平台币聚合
public function getBalanceData()
{
if(!$this->continue){
die("api error");
}
$map = array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"spend.pay_status"=>1,
"pay_way"=> array("LT",1)
);
$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) balance_coin_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'],
"promote_id"=>$balanceRes[$i]['promote_id'],
"count_date"=>$this->date
);
5 years ago
$dbres = $this->model->where($tempmap)->find();
5 years ago
if(!$dbres){
//不存在
5 years ago
$tempdbres = $this->model->add($balanceRes[$i]);
5 years ago
if(!$tempdbres){
$this->model->rollback();
die("error");
}
}else{
$dbres["balance_coin_count"] = $balanceRes[$i]['balance_coin_count'];
5 years ago
$tempdbres = $this->model->save($dbres);
5 years ago
if(!$tempdbres){
$this->model->rollback();
die("error");
}
}
}
}
# code...
}
//获取绑定币充值
public function getBingData()
{
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++) {
# code...
$tempmap = array(
"game_id"=>$balanceRes[$i]['game_id'],
"promote_id"=>$balanceRes[$i]['promote_id'],
"count_date"=>$this->date
);
5 years ago
$dbres = $this->model->where($tempmap)->find();
5 years ago
if(!$dbres){
//不存在
5 years ago
$tempdbres =$this->model->add($bindRes[$i]);
5 years ago
if(!$tempdbres){
$this->model->rollback();
die("error");
}
}else{
$dbres["bind_coin_count"] = $bindRes[$i]['bind_coin_count'];
5 years ago
$tempdbres = $this->model->save($dbres);
5 years ago
if(!$tempdbres){
$this->model->rollback();
die("error");
}
}
}
}
}
//获取内充流水
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
);
5 years ago
$dbres = $this->model->where($tempmap)->find();
5 years ago
if(!$dbres){
//不存在
5 years ago
$tempdbres = $this->model->add($insideRes[$i]);
5 years ago
if(!$tempdbres){
$this->model->rollback();
die("error");
}
}else{
$savedata = array("inside_cash_count"=>$insideRes[$i]['inside_cash_count']);
5 years ago
$tempdbres = $this->model->where($tempmap)->save($savedata);
5 years ago
if(!$tempdbres){
$this->model->rollback();
die("error");
}
}
}
}
// return true;
}
public function getRoot()
5 years ago
{
if(!$this->continue){
die("api error");
}
$map = array(
"parent_id"=>array("GT",0),
"count_date"=>$this->date
);
$user = $this->model->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->model->where($where)->save($root);
if(!$tempdbres){
$this->model->rollback();
die("setRoot error");
}
}
}
5 years ago
/**
* 获取角色聚合
*/
public function setUserCount()
{
//获取会长信息
$this->getRootUser();
$this->getParentUser();
$this->getPromoteUser();
$this->getInsideData2();
5 years ago
}
5 years ago
/** --------以下为辅助函数---------- **/
5 years ago
//获取会长信息
public function getRootUser()
{
if(!$this->continue) die("api error");
5 years ago
//获取所有数据
$month = $this->date;
$from = "
(
SELECT root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_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 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 FROM tab_spend_count WHERE parent_id > 0 and root_id = 0 and count_date='{$month}' group by parent_id
5 years ago
)
";
$subQueryAll = M()->table($from." a")
->field('root_id,root_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count')
5 years ago
->group("a.root_id")
// ->having("cash_count > 0 OR balance_coin_count > 0")
5 years ago
->select();
for ($i=0; $i<count($subQueryAll); $i++) {
$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'],
"all_count"=>$subQueryAll[$i]['cash_count']-0+$subQueryAll[$i]['balance_coin_count'],
5 years ago
"count_date"=>$this->date,
"create_time"=>$this->nowdata
);
$tempdbres = $this->usermodel->add($adddata);
if(!$tempdbres){
$this->usermodel->rollback();
die("getRootUser error");
}
5 years ago
}
}
5 years ago
//获取组长信息
public function getParentUser()
{
if(!$this->continue) die("api error");
5 years ago
$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 FROM tab_spend_count WHERE root_id > 0 and count_date='{$month}' group by parent_id
5 years ago
UNION
SELECT promote_id,promote_account,parent_id,parent_name,sum(cash_count) cash_count,sum(balance_coin_count) balance_coin_count FROM tab_spend_count WHERE parent_id >0 and root_id = 0 and count_date='{$month}' group by promote_id
5 years ago
)
";
$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')
5 years ago
->group("a.parent_id")
// ->having("cash_count > 0 or balance_coin_count > 0 ")
5 years ago
->select();
for ($i=0; $i<count($subQueryAll); $i++) {
$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'],
"all_count"=>$subQueryAll[$i]['cash_count']-0+$subQueryAll[$i]['balance_coin_count'],
5 years ago
"count_date"=>$this->date,
"create_time"=>$this->nowdata
);
$tempdbres = $this->usermodel->add($adddata);
if(!$tempdbres){
$this->usermodel->rollback();
die("getParentUser error");
}
5 years ago
}
}
5 years ago
//获取组员
public function getPromoteUser()
{
if(!$this->continue) die("api error");
5 years ago
$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 FROM tab_spend_count WHERE parent_id > 0 and root_id > 0 and count_date='{$month}' group by promote_id
5 years ago
)
";
$subQueryAll = M()->table($from." a")
->field('*')
->group("a.promote_id")
// ->having("cash_count > 0 or balance_coin_count > 0")
5 years ago
->select();
for ($i=0; $i<count($subQueryAll); $i++) {
$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'],
"all_count"=>$subQueryAll[$i]['cash_count']-0+$subQueryAll[$i]['balance_coin_count'],
5 years ago
"count_date"=>$this->date,
"create_time"=>$this->nowdata
);
$tempdbres = $this->usermodel->add($adddata);
if(!$tempdbres){
$this->usermodel->rollback();
die("getPromoteUser error");
}
5 years ago
}
# 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")
->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 = 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']);
}
}
}
// return true;
}
//按角色修正inside_cash_count数据
public 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']);
$tempdbres = $this->usermodel->where($tempmap)->save($savedata);
if(!$tempdbres){
$this->usermodel->rollback();
die("setInsideCount error");
}
# code...
}
5 years ago
5 years ago
}