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.

422 lines
17 KiB
PHTML

<?php
namespace Admin\Controller;
/**
* 游戏流水聚合接口
* @author chenzhi
*/
class FinancialSummarySetController extends \Think\Controller
{
public $beginThismonth;
public $endThismonth;
public $date;
public $nowdata;
public $month;//月份
public $year;//年份
public $adddata;//要添加的数据
//数据库
public $model;
public $FinancialSummaryModel;
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;
$temp = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
$this->year= date('Y',$temp);
$this->month= date('m',$temp);
$this->date = $this->year."-".$this->month;
$this->nowdata =time();
$this->model =M();
$this->FinancialSummaryModel =M("FinancialSummary",'tab_');
$this->DepositModel =M("Deposit",'tab_');
$this->SpendModel = M("Spend",'tab_');
$this->GameSupersignModel = M("GameSupersign",'tab_');
$this->CoinPayOrderModel = M("CoinPayOrder",'tab_');//会长充值平台币表
}
/**
* 更新某月数据
*/
public function setMonthFinancialSummary()
{
$month = I("count_date");
if(empty($month)) die("参数错误");
$this->date = $month;
$tarry = explode('-',$month);
$this->year= $tarry[0];
$this->month= $tarry[1];
$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->setFinancialSummary();
}
/**
* 强制重新聚合
*/
public function reCount()
{
$recount = I("recount");
if(empty($recount) || $recount != 1){return ;}
# code...
//清理之前的聚合
$temp =array(
"count_date"=>$this->date
);
$this->gamemodel->where($temp)->delete();
$this->usermodel->where($temp)->delete();
$this->monthmodel->where($temp)->delete();
echo "重置成功执行重新生成:";
}
/**
* 每个月的统计接口
*/
public function setFinancialSummary()
{
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->continue =true;
//按收入类型
$this->setIncomeMoney();
//按合作方类型
$this->setPromoteMoney();
//支付渠道/支付类型
$this->setPayWayMoney();
//执行
dump($this->adddata);
$this->model->commit();
$t2 = microtime(true);
die("success runtime:".round($t2-$t1,3).'s');
}
/**
* 获取类型为1的数据,按收入类型
*/
public function setIncomeMoney()
{
$this->getCashAndBalanCoinSpend();
$this->getBalanceCoinIncome();
$this->getGameSupersignIncome();
$this->getTestUserIncome();
# code...
}
//现金充值及平台币消耗
protected function getCashAndBalanCoinSpend()
{
$temparr = array(
"type"=>1,
"count_month"=>$this->month,
"count_year"=>$this->year,
"create_time"=>$this->nowdata
);
$map = array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"pay_status"=>1
);
$res = $this->SpendModel
->field("IFNULL(SUM(CASE WHEN pay_way > 0 THEN pay_amount ELSE 0 END),0) as cash_count,IFNULL(SUM(CASE WHEN pay_way = 0 THEN pay_amount ELSE 0 END),0) as balance_coin_count")
->where($map)
->find();
$cash_spend = $temparr;
$cash_spend["key_name"] = "cash_spend";
$cash_spend["sort"] = 1;
$cash_spend["sum_money"] = $res['cash_count'];
$this->adddata[]=$cash_spend;
$balance_coin_spend = $temparr;
$balance_coin_spend["key_name"] = "balance_coin_spend";
$balance_coin_spend["sort"] = 2;
$balance_coin_spend["sum_money"] = $res['balance_coin_count'];
$this->adddata[]=$balance_coin_spend;
}
//平台币充值(deposit/CoinPayOrder)玩家及会长充值的平台币总额
protected function getBalanceCoinIncome()
{
$temparr = array(
"key_name"=>'balance_coin_income',
"type"=>1,
"sort"=>3,
"count_month"=>$this->month,
"count_year"=>$this->year,
"create_time"=>$this->nowdata
);
$map = array(
"create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"pay_status"=>1
);
$temparr['sum_money'] = $this->DepositModel->field("IFNULL(sum(pay_amount),0) pay_amount")->where($map)->find()['pay_amount'];
//获取会长充值的平台币
$map1= array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"order_status"=>array("EGT",1)
);
$COMoney= $this->CoinPayOrderModel->field("IFNULL(sum(pay_amount),0) pay_amount")->where($map1)->find()['pay_amount'];
$temparr['sum_money'] = number_format(($temparr['sum_money']*100+$COMoney*100)/100, 2, '.', '');
$this->adddata[]=$temparr;
}
//超级签购买收入
protected function getGameSupersignIncome()
{
$temparr = array(
"key_name"=>'game_supersign_income',
"type"=>1,
"sort"=>4,
"count_month"=>$this->month,
"count_year"=>$this->year,
"create_time"=>$this->nowdata
);
$map = array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"pay_status"=>1
);
$temparr['sum_money'] = $this->GameSupersignModel->field("IFNULL(sum(pay_price),0) pay_price")->where($map)->find()['pay_price'];
$this->adddata[]=$temparr;
}
//测试账号收入
protected function getTestUserIncome()
{
$temparr = array(
"key_name"=>'test_user_income',
"type"=>1,
"sort"=>5,
"count_month"=>$this->month,
"count_year"=>$this->year,
"create_time"=>$this->nowdata
);
//获取测试白名单
$testlist = M("TestWhiteList",'tab_')->field("user_id")->select();
if($testlist){
$testlist = implode(",",array_column($testlist, 'user_id'));
}else{
$testlist = false;
}
//获取现金充值
$map = array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"pay_status"=>1,
"pay_way"=>array("GT",0)
);
if($testlist) $map['user_id']=array("in",$testlist);
$cash_spend = $this->SpendModel
->field("IFNULL(SUM(pay_amount),0) as pay_amount")
->where($map)
->find()['pay_amount'];
//获取平台币充值
$map1 = array(
"create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"pay_status"=>1
);
if($testlist) $map1['user_id']=array("in",$testlist);
$deposit = $this->DepositModel->field("IFNULL(sum(pay_amount),0) pay_amount")->where($map1)->find()['pay_amount'];
$temparr['sum_money'] = number_format(($cash_spend*100+$deposit*100)/100, 2, '.', '');
$this->adddata[]=$temparr;
}
/**
* 获取类型为2,3的数据/按合作方类型
*/
public function setPromoteMoney()
{
$map = array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"pay_status"=>1
);
$res = $this->SpendModel
->alias('s')
->field("IFNULL(SUM(CASE WHEN pay_way > 0 THEN pay_amount ELSE 0 END),0) as cash_count,
IFNULL(SUM(CASE WHEN pay_way = 0 THEN pay_amount ELSE 0 END),0) as balance_coin_count,
IFNULL(SUM(CASE WHEN pay_way = -1 THEN pay_amount ELSE 0 END),0) as bind_coin_count,
promote.company_belong")
->where($map)
->join("tab_promote promote ON s.promote_id = promote.id", "left")
->group("company_belong")
->select();
$inside_count = 0;
$outer_count = 0;
$inside_nobind_count = 0;
$outer_nobind_count = 0;
foreach ($res as $k => $v) {
if($v['company_belong'] == 1){
$outer_count = $v['cash_count']*100+$v['balance_coin_count']*100+$v['bind_coin_count']*100;
$outer_nobind_count = $v['cash_count']*100+$v['balance_coin_count']*100;
}else{
//有可能存在官方渠道company_belong=null
$inside_count += ($v['cash_count']*100+$v['balance_coin_count']*100+$v['bind_coin_count']*100);
$inside_nobind_count += ($v['cash_count']*100+$v['balance_coin_count']*100);
}
}
//处理数据
$this->adddata[] = array(
"key_name"=>'inside_count',
"type"=>2,"sort"=>1,"count_month"=>$this->month,"count_year"=>$this->year,"create_time"=>$this->nowdata,
"sum_money"=>number_format($inside_count/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'outer_count',
"type"=>2,"sort"=>2,"count_month"=>$this->month,"count_year"=>$this->year,"create_time"=>$this->nowdata,
"sum_money"=>number_format($outer_count/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'inside_count',
"type"=>3,"sort"=>1,"count_month"=>$this->month,"count_year"=>$this->year,"create_time"=>$this->nowdata,
"sum_money"=>number_format($inside_nobind_count/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'outer_count',
"type"=>3,"sort"=>2,"count_month"=>$this->month,"count_year"=>$this->year,"create_time"=>$this->nowdata,
"sum_money"=>number_format($outer_nobind_count/100, 2, '.', '')
);
}
/**
* 获取类型为4,5的数据/按支付渠道类型,支付类型类型
*/
public function setPayWayMoney()
{
$moneyarry=[
"wx"=>0,
"zfb"=>0,
"gfzfb"=>0,
"sqzfb"=>0,
"sqkj"=>0
];
$this->getSpendPayWay($moneyarry);
$this->getGameSupersignPayWay($moneyarry);
$this->getDepositPayWay($moneyarry);
$this->getCoinPayOrderPayWay($moneyarry);
//统计数据
$moneyarry['zfb'] = ($moneyarry['gfzfb']-0+$moneyarry['sqzfb']);
$this->adddata[] = array(
"key_name"=>'gfwx_count',
"type"=>4,"sort"=>1,"count_month"=>$this->month,"count_year"=>$this->year,"create_time"=>$this->nowdata,
"sum_money"=>number_format($moneyarry['wx']/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'gfzfb_count',
"type"=>4,"sort"=>2,"count_month"=>$this->month,"count_year"=>$this->year,"create_time"=>$this->nowdata,
"sum_money"=>number_format($moneyarry['gfzfb']/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'sqzfb_count',
"type"=>4,"sort"=>3,"count_month"=>$this->month,"count_year"=>$this->year,"create_time"=>$this->nowdata,
"sum_money"=>number_format($moneyarry['sqzfb']/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'sqkj_count',
"type"=>4,"sort"=>4,"count_month"=>$this->month,"count_year"=>$this->year,"create_time"=>$this->nowdata,
"sum_money"=>number_format($moneyarry['sqkj']/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'wx_count',
"type"=>5,"sort"=>1,"count_month"=>$this->month,"count_year"=>$this->year,"create_time"=>$this->nowdata,
"sum_money"=>number_format($moneyarry['wx']/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'zfb_count',
"type"=>5,"sort"=>2,"count_month"=>$this->month,"count_year"=>$this->year,"create_time"=>$this->nowdata,
"sum_money"=>number_format($moneyarry['zfb']/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'kj_count',
"type"=>5,"sort"=>3,"count_month"=>$this->month,"count_year"=>$this->year,"create_time"=>$this->nowdata,
"sum_money"=>number_format($moneyarry['sqkj']/100, 2, '.', '')
);
}
//获取spend表
protected function getSpendPayWay(&$moneyarry){
$map = array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"pay_status"=>1,
"pay_way"=>array("IN","1,2,3,9,15")
);
$dbres=$this->SpendModel
->field("
IFNULL(SUM(CASE WHEN pay_way = 2 THEN pay_amount ELSE 0 END),0) as wxsm_count,
IFNULL(SUM(CASE WHEN pay_way = 3 THEN pay_amount ELSE 0 END),0) as wxapp_count,
IFNULL(SUM(CASE WHEN pay_way = 1 THEN pay_amount ELSE 0 END),0) as zfbgf_count,
IFNULL(SUM(CASE WHEN pay_way = 9 THEN pay_amount ELSE 0 END),0) as zfbsq_count,
IFNULL(SUM(CASE WHEN pay_way = 15 THEN pay_amount ELSE 0 END),0) as kjsq_count
")
->where($map)
->find();
$moneyarry['wx'] += ($dbres['wxsm_count']*100+$dbres['wxapp_count']*100);
$moneyarry['gfzfb'] += $dbres['zfbgf_count']*100;
$moneyarry['sqzfb'] += $dbres['zfbsq_count']*100;
$moneyarry['sqkj'] += $dbres['kjsq_count']*100;
}
//获取超级签购买
protected function getGameSupersignPayWay(&$moneyarry)
{
$map = array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"pay_status"=>1
);
$dbres=$this->GameSupersignModel
->field("
IFNULL(SUM(CASE WHEN pay_way = 2 THEN pay_price ELSE 0 END),0) as wx_count,
IFNULL(SUM(CASE WHEN pay_way = 1 THEN pay_price ELSE 0 END),0) as zfb_count
")
->where($map)
->find();
$moneyarry['wx'] += $dbres['wx_count'];
$moneyarry['gfzfb'] += $dbres['zfb_count'];
}
//玩家购买平台币
protected function getDepositPayWay(&$moneyarry)
{
$map = array(
"create_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"pay_status"=>1,
"pay_way"=>array("IN","1,2,3,9,15")
);
$dbres=$this->DepositModel
->field("
IFNULL(SUM(CASE WHEN pay_way = 2 THEN pay_amount ELSE 0 END),0) as wxsm_count,
IFNULL(SUM(CASE WHEN pay_way = 3 THEN pay_amount ELSE 0 END),0) as wxapp_count,
IFNULL(SUM(CASE WHEN pay_way = 1 THEN pay_amount ELSE 0 END),0) as zfbgf_count,
IFNULL(SUM(CASE WHEN pay_way = 9 THEN pay_amount ELSE 0 END),0) as zfbsq_count,
IFNULL(SUM(CASE WHEN pay_way = 15 THEN pay_amount ELSE 0 END),0) as kjsq_count
")
->where($map)
->find();
$moneyarry['wx'] += ($dbres['wxsm_count']*100+$dbres['wxapp_count']*100);
$moneyarry['gfzfb'] += $dbres['zfbgf_count']*100;
$moneyarry['sqzfb'] += $dbres['zfbsq_count']*100;
$moneyarry['sqkj'] += $dbres['kjsq_count']*100;
}
//会长充值平台币
protected function getCoinPayOrderPayWay(&$moneyarry)
{
$map= array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"order_status"=>array("EGT",1)
);
$dbres= $this->CoinPayOrderModel
->field("
IFNULL(SUM(CASE WHEN pay_way = 2 THEN pay_amount ELSE 0 END),0) as wx_count,
IFNULL(SUM(CASE WHEN pay_way = 1 THEN pay_amount ELSE 0 END),0) as zfb_count
")
->where($map)
->find();
$moneyarry['wx'] += $dbres['wx_count'];
$moneyarry['gfzfb'] += $dbres['zfb_count'];
}
}