市场结算管理,定时器完成

master
zhengyongxing 5 years ago
parent 47939e4311
commit e63c52ba2c

@ -325,4 +325,194 @@ class TimingController extends AdminController {
}
public function caculateMarketList() {
$map = [];
$map['my_time'] = date('Y-m',strtotime('-1 month'));
if ($_REQUEST['count_date']) {
$map['my_time'] = $_REQUEST['count_date'];
}
if ($_REQUEST['count_date'] == 'all') {
$map['my_time'] = ['neq', date('Y-m',time())];
}
$spendMap['tab_spend.pay_status'] = 1;
//获取spend表中的数据同时根据会长id进行group分类
$data = M('Spend','tab_')
->field("FROM_UNIXTIME(pay_time,'%Y-%m') as my_time,sum(pay_amount) as pay_amount,game_id,tab_spend.game_name,SUBSTRING_INDEX(tab_spend.`game_name`,\"(\",1) as game_names,promote_id,promote_account,company_relation,company_belong,CASE WHEN SUBSTRING_INDEX(SUBSTRING_INDEX(`chain`,\"/\",2),\"/\",-1)='' THEN promote_id ELSE SUBSTRING_INDEX(SUBSTRING_INDEX(`chain`,\"/\",2),\"/\",-1) END as root_id,tab_game.ratio,tab_game.relation_game_id,tab_spend.sdk_version ")
->join("left join tab_promote on promote_id = tab_promote.id")
->join("left join tab_game on tab_game.id=tab_spend.game_id")
->where($spendMap)
->group("my_time,game_id,root_id")
->order("my_time Desc")
->select(false);
//关联表获取会长账号名
$data = M()->table('('.$data.') as a')
->field("my_time,pay_amount,a.game_id,game_name,game_names,a.promote_id,a.promote_account,a.company_relation,a.company_belong,CASE WHEN root_id is null THEN 0 ELSE root_id END as root_id,CASE WHEN account is null THEN '官方渠道' ELSE account END as account,tab_ratio.ratio as ratio,turnover_ratio,a.ratio as game_ratio,a.sdk_version")
->join("left join tab_promote on root_id = tab_promote.id")
->join("left join tab_promote_game_ratio as tab_ratio on tab_ratio.game_id = a.relation_game_id and tab_ratio.promote_id=root_id and tab_ratio.status=1")
->where($map)
->group("my_time,game_id,root_id")
->order("my_time Desc")
->select();
// dump($data);die();
foreach($data as $key => $value) {
$mytime = $value['my_time'];
$nextMonth = date('Y-m',strtotime("$mytime + 1 month"));
$game_id = $value['game_id'];
$gameInfo = M('game','tab_')->where(['id'=>$game_id])->getField('game_name');
$gameName = substr($gameInfo,0,strpos($gameInfo,'('));
$gameMap['game_name'] = ['like','%'.$gameName.'%'];
$gameIdArr = M('game','tab_')->field('id')->where($gameMap)->select();
$gameNameIdArr = [];
foreach($gameIdArr as $gameKey => $gameValue){
$gameNameIdArr[] = $gameValue['id'];
}
$promote_id = $value['promote_id'];
$pay_status = 1;
$promoteInfo = M('promote','tab_')->where(['id'=>$promote_id])->find();
$level = $promoteInfo['level'];
if($promote_id != 0) {
$promoteIdArr = [];
if ($level == 1) {
$rootPromoteId = $promote_id;
$promoteIdArr[] = $promote_id;
}else {
$chain = $promoteInfo['chain'];
$chainArr = explode('/',$chain);
$rootPromoteId = $chainArr[1];
}
$where1['chain'] = array('like', '%/' . $rootPromoteId. '/%');
$promoteIdInfo = M('promote','tab_')->field('id')->where($where1)->select();
foreach($promoteIdInfo as $key2 => $v1) {
$promoteIdArr[] = $v1['id'];
}
$where['promote_id'] = ['in',$promoteIdArr];
if (!$promoteIdArr) {
$where['promote_id']= 0;
}
}else {
$where['promote_id'] = 0;
}
$where['game_id'] = ['in',$gameNameIdArr];
$where['pay_status'] = 1;
$begTime = strtotime($mytime);
$endTime = strtotime($nextMonth);
$where['pay_time'] = ['between',[$begTime,$endTime]];
$spendInfo = M('spend','tab_')->field('pay_amount,pay_way')->where($where)->select();
$initNum = 0;
foreach($spendInfo as $key1 => $spendValue) {
$pay_way = $spendValue['pay_way'];
$channel_rate = $this->getChannelConfig($pay_way) / 100;
$initNum += $spendValue['pay_amount'] * $channel_rate;
}
$data[$key]['pay_amount'] = $value['pay_amount'] - $initNum;
$data[$key]['upstream'] = caculateUpstream($value['game_id'],$value['pay_amount']);
if ($value['ratio']) {
$data[$key]['downstream'] = $value['pay_amount'] * ($value['ratio']*0.01);
} else {
$data[$key]['downstream'] = $value['pay_amount'] * ($value['game_ratio']*0.01);
}
$value['turnover_ratio'] = json_decode($value['turnover_ratio'],true);
// dd($value);
if (is_array($value['turnover_ratio'])) {
foreach($value['turnover_ratio'] as $k =>$v) {
if(($value['pay_amount']>=$v['turnover']&&$v['instanceof']==1) || $value['pay_amount']>$v['turnover']) {
$data[$key]['downstream'] = $value['pay_amount'] * ($v['ratio']*0.01);
}
}
}
$data[$key]['highquality'] = number_format(($value['pay_amount'] - $initNum - $data[$key]['upstream']-$data[$key]['downstream']) * (1-0.0672),2,'.','');
if (!$data[$key]['company_belong']) {
$data[$key]['company_belong'] = getCompanyBlong(0);
} else {
$data[$key]['company_belong'] = getCompanyBlong($data[$key]['company_belong']);
}
$data[$key]['company_relation'] = $data[$key]['company_relation']?'只维护':'自主开发及维护';
}
foreach($data as $key => $value) {
$is_hav = M('market_balance','tab_')->where(['my_time'=>$value['my_time'],
'game_id'=>$value['game_id'],
'promote_id'=>$value['promote_id'],
'root_id'=>$value['root_id']])->find();
if (!$is_hav) {
M('market_balance','tab_')->add($value);
} else {
M('market_balance','tab_')
->where(['my_time'=>$value['my_time'],
'game_id'=>$value['game_id'],
'promote_id'=>$value['promote_id'],
'root_id'=>$value['root_id']])
->save(['pay_amount'=>$value['pay_amount'],
'ratio'=>$value['ratio'],
'turnover_ratio'=>$value['turnover_ratio'],
'game_ratio'=>$value['game_ratio'],
'upstream'=>$value['upstream'],
'downstream'=>$value['downstream'],
'highquality'=>$value['highquality']]);
}
}
echo 'success';
// dump($data);die();
}
public function getChannelConfig($pay_way = 0,$config=false) {
if(!$config) {
$payWayName = $this->pay_way_name[$pay_way];
$toolConfig = M('tool','tab_')->where(['name'=>$payWayName])->find();
$config = json_decode($toolConfig['config'],true);
if(empty($config['channel_rate'])){
$channel_rate = 0;
}else {
$channel_rate = $config['channel_rate'];
}
}else {
foreach($this->pay_way_name as $key => $value) {
$name = $value;
$toolConfig = M('tool','tab_')->where(['name'=>$name])->find();
$config = json_decode($toolConfig['config'],true);
if(empty($config['channel_rate'])){
$channel_rate[$key] = 0;
}else {
$channel_rate[$key] = $config['channel_rate'];
}
}
}
return $channel_rate;
}
}

Loading…
Cancel
Save