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.

640 lines
26 KiB
PHTML

<?php
namespace Admin\Controller;
use Think\Exception;
/**
* 游戏流水聚合接口
* @author chenzhi
*/
class FinancialSummarySetController extends \Think\Controller
{
public $beginThismonth;
public $endThismonth;
public $date;
public $nowdata;
public $month;//月份
public $year;//年份
public $adddata;//要添加的数据
public $taskid=false;
public $result='';
//数据库
public $FinancialSummaryModel;
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',time()).'-'.(date('m',time())-1).'-01');
$this->year= date('Y',$temp);
$this->month= date('m',$temp);
$this->date = $this->year."-".$this->month;
$this->nowdata =time();
$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_');//会长充值平台币表
$this->TestOrderModel = M("TestOrder",'tab_');//测试订单录入
}
/**
* 更新某月数据
*/
public function setMonthFinancialSummary($count_date)
{
$month = $count_date;
if(empty($month)) die("参数错误");
$this->adddata = [];//置空否者会脚本进来会重复计算
$this->date = $month;
$tarry = explode('-',$month);
$this->year= $tarry[0];
if(strlen($tarry[1]) < 2) $tarry[1]="0".$tarry[1];
$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_year"=>$this->year,
"count_month"=>$this->month
);
$this->FinancialSummaryModel->where($temp)->delete();
echo "重置成功执行重新生成:";
}
/**
* 每个月的统计接口
*/
public function setFinancialSummary()
{
set_time_limit(0);
$t1 = microtime(true);
//判断是否已经聚合
$countRes = $this->FinancialSummaryModel->field("IFNULL(COUNT(*),0) count")->where("count_year = '{$this->year}' and count_month= '{$this->month}'")->find()['count'];
if($countRes > 0){
die("error:Repeated statistics,msg:{$this->date}已经聚合过");
}
echo "{$this->date}开始统计".PHP_EOL;
echo "--收入类型统计中".PHP_EOL;
$this->setIncomeMoney();
echo "--合作方类型统计中".PHP_EOL;
$this->setPromoteMoney();
//
echo "--支付渠道统计中".PHP_EOL;
$this->setChannelMoney();
echo "--支付类型统计中".PHP_EOL;
$this->setPaywayMoney();
echo "--支付商户统计中".PHP_EOL;
$this->setMerchantMoney();
echo "--数据保存".PHP_EOL;
$this->addDb();
$t2 = microtime(true);
echo ("success {$this->date}:ok runtime:".round($t2-$t1,3).'s'.PHP_EOL);
if($this->taskid){
$this->result = "success {$this->date}:ok runtime:".round($t2-$t1,3).'s';
}
}
/**
* 获取类型为1的数据,按收入类型
*/
public function setIncomeMoney()
{
$this->getCashAndBalanCoinSpend();
$this->getBalanceCoinIncome();
$this->getGameSupersignIncome();
$this->getTestUserIncome();
$this->getRewardRecordIncome();
}
//现金充值及平台币消耗
protected function getCashAndBalanCoinSpend()
{
$map = array(
"payed_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["key_name"] = "cash_spend";
$cash_spend["type"] = 1;
$cash_spend["sort"] = 1;
$cash_spend["sum_money"] = $res['cash_count'];
$this->adddata[]=$cash_spend;
$balance_coin_spend["key_name"] = "balance_coin_spend";
$balance_coin_spend["type"] = 1;
$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,
);
$map = array(
"payed_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,
);
$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
);
//获取测试白名单
$testlist = M("TestWhiteList",'tab_')->field("user_id")->select();
if($testlist){
$testlist = implode(",",array_column($testlist, 'user_id'));
}else{
$testlist = false;
}
//获取现金充值
$map = array(
"payed_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(
"payed_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'];
//测试订单录入
$map2 = array(
"pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth))
);
$test_pay_amount = $this->TestOrderModel->where($map2)->field("IFNULL(sum(pay_amount),0) pay_amount")->find()['pay_amount'];
$temparr['sum_money'] = number_format(($cash_spend*100 + $deposit*100 + $test_pay_amount*100 )/100, 2, '.', '');
4 years ago
$this->adddata[]=$temparr;
}
//奖罚收入
protected function getRewardRecordIncome()
{
$map = array(
"confirm_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
);
$rrres = M("RewardDetail","tab_")
->field("
IFNULL(SUM(CASE WHEN type = 1 THEN amount ELSE 0 END),0) as reward_count,
IFNULL(SUM(CASE WHEN type = 2 THEN amount ELSE 0 END),0) as fine_count
")
->where($map)
->find();
//奖励负,惩罚正
$this->adddata[] = array(
"key_name"=>'reward_count',
"type"=>1,
"sort"=>6,
'sum_money'=>$rrres['reward_count']
);
$this->adddata[] = array(
"key_name"=>'fine_count',
"type"=>1,
"sort"=>7,
'sum_money'=>$rrres['fine_count']
);
}
/**
* 获取类型为2,3的数据/按合作方类型
*/
public function setPromoteMoney()
{
$map = array(
"payed_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;//外团
$outer_branch_count = 0;//外团分发
$other_group_count = 0;//其他
$inside_nobind_count = 0;
$outer_nobind_count = 0;
$outer_branch_nobind_count = 0;
$other_group_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;
}elseif($v['company_belong'] == 2){
//外团分发
$outer_branch_count = $v['cash_count']*100+$v['balance_coin_count']*100+$v['bind_coin_count']*100;
$outer_branch_nobind_count = $v['cash_count']*100+$v['balance_coin_count']*100;
}elseif($v['company_belong'] == 3){
//其他
$other_group_count = $v['cash_count']*100+$v['balance_coin_count']*100+$v['bind_coin_count']*100;
$other_group_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,
"sum_money"=>number_format($inside_count/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'outer_count',
"type"=>2,"sort"=>2,
"sum_money"=>number_format($outer_count/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'outer_branch_count',
"type"=>2,"sort"=>3,
"sum_money"=>number_format($outer_branch_count/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'other_group_count',
"type"=>2,"sort"=>4,
"sum_money"=>number_format($other_group_count/100, 2, '.', '')
);
//不含绑定币
$this->adddata[] = array(
"key_name"=>'inside_count',
"type"=>3,"sort"=>1,
"sum_money"=>number_format($inside_nobind_count/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'outer_count',
"type"=>3,"sort"=>2,
"sum_money"=>number_format($outer_nobind_count/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'outer_branch_count',
"type"=>3,"sort"=>3,
"sum_money"=>number_format($outer_branch_nobind_count/100, 2, '.', '')
);
$this->adddata[] = array(
"key_name"=>'other_group_count',
"type"=>3,"sort"=>4,
"sum_money"=>number_format($other_group_nobind_count/100, 2, '.', '')
);
}
4 years ago
public function setChannelMoney(){
$Channel = $this->getChannelCount($Channel);
foreach( $Channel as $k=>$v){
$this->adddata[] = array(
"key_name"=>$k,
"type"=>4,
"sort"=>$k,
"sum_money"=>number_format($v, 2, '.', '')
);
}
4 years ago
}
//获取Channel
protected function getChannelCount(){
$map = array(
"s.payed_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"s.pay_status"=>1,
"s.merchant_id"=>["GT","0"]
);
$spres=$this->SpendModel
->alias("s")
->field("SUM(s.pay_amount) pay_amount,p.channel")
->where($map)
->group("p.channel")
->join("left join tab_payment_merchant p ON p.id = s.merchant_id")
->select();
4 years ago
//超级签
$ss_map = [
"s.pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"s.pay_status"=>1
];
$ssres=$this->GameSupersignModel
->alias("s")
->field("SUM(s.pay_price) pay_amount,p.channel")
->join("left join tab_payment_merchant p ON p.id = s.merchant_id")
->group("p.channel")
->where($ss_map)
->select();
4 years ago
//玩家购买平台币
$dp_map =[
"s.payed_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"s.pay_status"=>1
];
$dpres=$this->DepositModel
->alias("s")
->field("SUM(s.pay_amount) pay_amount,p.channel")
->join("left join tab_payment_merchant p ON p.id = s.merchant_id")
->group("p.channel")
->where($dp_map)
->select();
4 years ago
//会长充值平台币
$co_map =[
"s.pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"s.order_status"=>array("EGT",1)
];
$cores=$this->CoinPayOrderModel
->alias("s")
->field("SUM(s.pay_amount) pay_amount,p.channel")
->join("left join tab_payment_merchant p ON p.id = s.merchant_id")
->group("p.channel")
->where($co_map)
->select();
//测试的订单
$to_map =[
"s.pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"s.merchant_id"=>["GT","0"]
];
$tores=$this->TestOrderModel
->alias("s")
->field("SUM(s.pay_amount) pay_amount,p.channel")
->join("left join tab_payment_merchant p ON p.id = s.merchant_id")
->group("p.channel")
->where($to_map)
->select();
$dbres = array_merge($spres,$ssres,$dpres,$cores,$tores);
$Channel = [];
foreach ($dbres as $k=>$v){
$Channel["c".$v['channel']] += $v['pay_amount'];
}
return $Channel;
}
public function setPaywayMoney(){
$Ways = $this->getPaywayCount();
foreach($Ways as $k=>$v){
$this->adddata[] = array(
"key_name"=>$k,
"type"=>5,
"sort"=>$k,
"sum_money"=>number_format($v, 2, '.', '')
);
}
}
//获取Channel
protected function getPaywayCount(){
$map = array(
"s.payed_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"s.pay_status"=>1,
"s.merchant_id"=>["GT","0"]
);
$spres=$this->SpendModel
->alias("s")
->field("SUM(s.pay_amount) pay_amount,s.merchant_way")
->where($map)
->group("s.merchant_way")
->select();
//超级签
$ss_map = [
"s.pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"s.pay_status"=>1
];
$ssres=$this->GameSupersignModel
->alias("s")
->field("SUM(s.pay_price) pay_amount,s.merchant_way")
->group("s.merchant_way")
->where($ss_map)
->select();
//玩家购买平台币
$dp_map =[
"s.payed_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"s.pay_status"=>1
];
$dpres=$this->DepositModel
->alias("s")
->field("SUM(s.pay_amount) pay_amount,s.merchant_way")
->group("s.merchant_way")
->where($dp_map)
->select();
//会长充值平台币
$co_map =[
"s.pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"s.order_status"=>array("EGT",1)
];
$cores=$this->CoinPayOrderModel
->alias("s")
->field("SUM(s.pay_amount) pay_amount,s.merchant_way")
->group("s.merchant_way")
->where($co_map)
->select();
//测试的订单
$to_map =[
"s.pay_time"=> array('BETWEEN',array($this->beginThismonth, $this->endThismonth)),
"s.merchant_id"=>["GT","0"]
];
$tores=$this->TestOrderModel
->alias("s")
->field("SUM(s.pay_amount) pay_amount,s.merchant_way")
->group("s.merchant_way")
->where($to_map)
->select();
$dbres = array_merge($spres,$ssres,$dpres,$cores,$tores);
$Ways = [];
foreach ($dbres as $k=>$v){
$Ways["w".$v['merchant_way']] += $v['pay_amount'];
}
return $Ways;
}
public function addDb()
{
foreach ($this->adddata as $k=> &$v) {
$v["count_month"] = $this->month;
$v["count_year"] = $this->year;
$v["create_time"] = $this->nowdata;
}
$res = $this->FinancialSummaryModel->addAll($this->adddata);
if(!$res){
echo "DB save error";
}else{
}
}
public function setMerchantMoney() {
$map = [];
$map['payed_time'] = ['BETWEEN',[$this->beginThismonth, $this->endThismonth]];
//游戏充值金额
$data = M("spend use index(pay_channel)","tab_")
->field("SUM(CASE WHEN merchant.id is not null and tab_test_white_list.user_id is null THEN pay_amount ELSE 0 END) as cash_amount,
SUM(CASE WHEN merchant.id is not null and tab_test_white_list.user_id is not null THEN pay_amount ELSE 0 END) as test_amount,
0 coin_amount,0 offcial_amount,0 spread_amount,0 supersign_amount,0 test_order_amount,merchant_id,name,channel,identifier,
payed_time,game_id,main_name,merchant_way,merchant.account")
->join("left join tab_test_white_list on tab_spend.user_id=tab_test_white_list.user_id")
->join("left join tab_payment_merchant merchant on merchant.id=tab_spend.merchant_id")
->where(['pay_status'=>1,'tab_spend.pay_way'=>['egt',1],'merchant.status'=>1,'merchant.type'=>1])
->where($map)
->group("merchant_id")
->select(false);
//平台币充值金额计算
$coinData = M('deposit','tab_')
// ->join("inner join tab_deposit on tab_pay_info.order_id=tab_deposit.pay_order_number")
->join("left join tab_test_white_list on tab_test_white_list.user_id=tab_deposit.user_id")
->join("left join tab_payment_merchant merchant on merchant.id=tab_deposit.merchant_id")
->field("0 cash_amount,SUM(CASE WHEN merchant.id is not null and pay_source=2 and tab_test_white_list.user_id is not null THEN pay_amount ELSE 0 END) as test_amount,
SUM(CASE WHEN merchant.id is not null and tab_test_white_list.user_id is null and pay_source=2 THEN pay_amount ELSE 0 END) as coin_amount,
0 offcial_amount,0 spread_amount,0 supersign_amount,0 test_order_amount,merchant_id,name,channel,identifier,payed_time,0 game_id,main_name,merchant_way,merchant.account")
->where(['pay_status'=>1,'tab_deposit.pay_way'=>['egt',1],'merchant.status'=>1,'merchant.type'=>1])
->where($map)
->group("merchant_id")
->select(false);
//官网金额
$officalData = M('deposit','tab_')
->join("left join tab_test_white_list on tab_test_white_list.user_id=tab_deposit.user_id")
->join("left join tab_payment_merchant merchant on merchant.id=tab_deposit.merchant_id")
->field("0 cash_amount,SUM(CASE WHEN merchant.id is not null and pay_source=0 and tab_test_white_list.user_id is not null THEN pay_amount ELSE 0 END) as test_amount,
0 as coin_amount,SUM(CASE WHEN merchant.id is not null and tab_test_white_list.user_id is null and pay_source=0 THEN pay_amount ELSE 0 END) as offcial_amount,
0 spread_amount,0 supersign_amount,0 test_order_amount,merchant_id,name,channel,identifier,payed_time,0 as game_id,main_name,merchant_way,merchant.account")
->where(['pay_status'=>1,'tab_deposit.pay_way'=>['egt',1],'merchant.status'=>1,'merchant.type'=>1])
->where($map)
->group("merchant_id")
->select(false);
$map['pay_time'] = $map['payed_time'];
unset($map['payed_time']);
//推广后台
$spreadData = M("coin_pay_order","tab_")
->field("0 cash_amount,0 test_amount,0 coin_amount,0 offcial_amount,
SUM(CASE WHEN merchant.id is not null THEN pay_amount ELSE 0 END) as spread_amount,0 supersign_amount,
0 test_order_amount,merchant_id,name,channel,identifier,pay_time payed_time,0 game_id,main_name,merchant_way,merchant.account")
->join("left join tab_payment_merchant merchant on merchant.id=tab_coin_pay_order.merchant_id")
->where(['order_status'=>1,'tab_coin_pay_order.pay_way'=>['egt',1],'merchant.status'=>1,'merchant.type'=>1])
->where($map)
->group("merchant_id")
->select(false);
//超级签数据
$superSignData = M('game_supersign','tab_')
->field("0 cash_amount,
SUM(CASE WHEN merchant.id is not null and tab_test_white_list.user_id is not null THEN pay_price ELSE 0 END) as test_amount,
0 coin_amount,0 offcial_amount,0 spread_amount,
SUM(CASE WHEN merchant.id is not null and tab_test_white_list.user_id is null THEN pay_price ELSE 0 END) as supersign_amount,
0 test_order_amount,merchant_id,name,channel,identifier,pay_time payed_time,game_id,main_name,merchant_way,merchant.account")
->join("left join tab_payment_merchant merchant on merchant.id=tab_game_supersign.merchant_id")
->join("left join tab_test_white_list on tab_test_white_list.user_id=tab_game_supersign.user_id")
->where(['pay_status'=>1,'tab_game_supersign.pay_way'=>['egt',1],'merchant.status'=>1,'merchant.type'=>1])
->where($map)
->group("merchant_id")
->select(false);
//测试环境内网平台订单
$testOrder = M('test_order', 'tab_')
->where(['tab_test_order.pay_way'=>['egt',1]])
->field('0 cash_amount,0 test_amount,0 coin_amount,0 offcial_amount,0 spread_amount,0 supersign_amount,
SUM(CASE WHEN merchant.id is not null THEN pay_amount ELSE 0 END) as test_order_amount,merchant_id,name,
channel,identifier,pay_time payed_time,game_id,main_name,merchant_way,merchant.account')
->join("left join tab_payment_merchant merchant on merchant.id=tab_test_order.merchant_id")
->where(['merchant.status'=>1,'merchant.type'=>1])
->where($map)
->group("merchant_id")
->select(false);
$sql = "{$data} UNION ALL {$coinData} UNION ALL {$officalData} UNION ALL {$spreadData} UNION ALL {$superSignData} UNION ALL {$testOrder}";
$data = M()->table("({$sql})a")
->field("sum(cash_amount) cash_amount,sum(test_amount) test_amount,
sum(coin_amount) coin_amount,sum(offcial_amount) offcial_amount,
sum(spread_amount) spread_amount,sum(supersign_amount) supersign_amount,
sum(test_order_amount) test_order_amount,merchant_id,account,merchant_way,main_name,
name,channel,identifier")
->where("name is not null")
->group("merchant_id")
->select();
foreach ($data as $key => $value) {
$sum = $value['cash_amount']+$value['test_amount']+$value['coin_amount']+
$value['offcial_amount']+$value['spread_amount']+$value['supersign_amount']+$value['test_order_amount'];
$this->adddata[] = array(
"key_name"=>$value['identifier'],
"type"=>6,"sort"=>8,
"sum_money"=>number_format($sum, 2, '.', '')
);
}
}
}