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.

1166 lines
50 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
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use User\Api\UserApi as UserApi;
use function GuzzleHttp\Psr7\str;
/**
* 后台首页控制器
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
class TimingController extends AdminController {
public function _initialize(){
}
public function caculateDeviceInfo() {
$start = strtotime(date("Ymd",time()))-86400;
$end = $start+1;
// if (I('time')) {
// $start = strtotime(I('time'));
// $end = $start+1;
// }
if (I('start')&&I('end')) {
$start = strtotime(I('start'));
$end = strtotime(I('end'))+1;
} else if (I('start')&&!I('end')) {
$start = strtotime(I('start'));
$end = strtotime(I('start'))+1;
} else if (!I('start')&&I('end')) {
$start = strtotime(I('end'));
$end = strtotime(I('end'))+1;
}
if ($start>$end) {
echo '开始时间不能大于结束时间';
}
ini_set('memory_limit','1024M');
// $device = D('DeviceRecord');
// echo 1;
for($startTime = $start;$startTime<$end;$startTime=$startTime+86400) {
echo date("Y-m-d",$startTime).' 安卓 ';
//当日新增设备
$newSql = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->where(['version'=>1])->group('unique_code')->select(false);
$data['new_device'] = M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime,$startTime+86399])])->order('create_time')->count();
echo $data['new_device'].' ';
//当日活跃设备
$data['active_device'] = $this->active(['create_time'=>array('between',[$startTime,$startTime+86399]),'version'=>1]);
echo $data['active_device'].' ';
$data['time'] = $startTime;
$data['version'] = 1;
$data['today_new_device'] =json_encode($this->news_on_time(['create_time'=>['between',[$startTime,$startTime+86399]]],'news',5,'time,version','time',1));
// echo M('device_record','tab_')->_sql()."\n";
$data['today_active_device'] =json_encode($this->active_on_time(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>1],'active',5,'time,version'));
// echo M('device_record','tab_')->_sql()."\n";
$data['today_model_device'] = json_encode($this->model(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>1]));
$data['model'] = json_encode($this->modelDevice(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>1]));
$duration = $this->duration(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])]);
$newSqls = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->group('unique_code')->select(false);
$data['duration7'] = ($duration/M('device_record','tab_')->table('(' . $newSqls . ') as a')->where(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])])->order('create_time')->count())/7;
$getStatistics = M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>1])->find();
echo "\n";
if (!$getStatistics) {
M('device_statistics','tab_')->add($data);
} else {
M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>1])->save($data);
}
echo date("Y-m-d",$startTime).' 苹果 ';
//当日新增设备
$newSql = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->where(['version'=>2])->group('unique_code')->select(false);
$data['new_device'] = M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime,$startTime+86399])])->order('create_time')->count();
echo $data['new_device'].' ';
//当日活跃设备
$data['active_device'] = $this->active(['create_time'=>array('between',[$startTime,$startTime+86399]),'version'=>2]);
echo $data['active_device'].' ';
$data['time'] = $startTime;
$data['version'] = 2;
$data['today_new_device'] =json_encode($this->news_on_time(['create_time'=>['between',[$startTime,$startTime+86399]]],'news',5,'time,version','time',2));
// echo M('device_record','tab_')->_sql();die();
$data['today_active_device'] =json_encode($this->active_on_time(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>2],'active',5,'time,version'));
$data['today_model_device'] = json_encode($this->model(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>2]));
$data['model'] = json_encode($this->modelDevice(['create_time'=>['between',[$startTime,$startTime+86399]],'version'=>2]));
// $duration = $this->duration(['create_time'=>array('between',[$startTime-86400*7,$startTime-1]),'version'=>2]);
// $data['duration7'] = ($duration/M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])])->order('create_time')->count())/7;
$data['duration7'] = 0;
$getStatistics = M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>2])->find();
echo "\n";
if (!$getStatistics) {
M('device_statistics','tab_')->add($data);
} else {
M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>2])->save($data);
}
}
}
public function caculateTodayDevice() {
$start = strtotime(date("Ymd",time()));
$end = $start+1;
if (I('start')&&I('end')) {
$start = strtotime(I('start'));
$end = strtotime(I('end'))+1;
} else if (I('start')&&!I('end')) {
$start = strtotime(I('start'));
$end = strtotime(I('start'))+1;
} else if (!I('start')&&I('end')) {
$start = strtotime(I('end'));
$end = strtotime(I('end'))+1;
}
if ($start>$end) {
echo '开始时间不能大于结束时间';
}
ini_set('memory_limit','1024M');
// $device = D('DeviceRecord');
// echo 1;
for($startTime = $start;$startTime<$end;$startTime=$startTime+86400) {
echo date("Y-m-d",$startTime).' 安卓 ';
//当日新增设备
$newSql = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->where(['version'=>1])->group('unique_code')->select(false);
$data['new_device'] = M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime,$startTime+86399])])->order('create_time')->count();
$data['time'] = $startTime;
echo $data['new_device'].' ';
$duration = $this->duration(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])]);
$newSqls = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->group('unique_code')->select(false);
$data['duration7'] = ($duration/M('device_record','tab_')->table('(' . $newSqls . ') as a')->where(['create_time'=>array('between',[$startTime-86400*7,$startTime-1])])->order('create_time')->count())/7;
$data['version'] = 1;
$getStatistics = M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>1])->find();
echo "\n";
if (!$getStatistics) {
M('device_statistics','tab_')->add($data);
} else {
M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>1])->save($data);
}
$data['duration7'] = 0;
echo date("Y-m-d",$startTime).' 苹果 ';
//当日新增设备
$newSql = M('device_record','tab_')->field('min(create_time) as create_time,model,unique_code,id')->where(['version'=>2])->group('unique_code')->select(false);
$data['new_device'] = M('device_record','tab_')->table('(' . $newSql . ') as a')->where(['create_time'=>array('between',[$startTime,$startTime+86399])])->order('create_time')->count();
echo $data['new_device'].' ';
$data['version'] = 2;
$getStatistics = M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>2])->find();
echo "\n";
if (!$getStatistics) {
M('device_statistics','tab_')->add($data);
} else {
M('device_statistics','tab_')->where(['time'=>$startTime,'version'=>2])->save($data);
}
}
}
public function duration($map=array()) {
return M('device_record','tab_')->where($map)->sum('duration');
}
public function active($map=array()) {
$sql = M('device_record','tab_')->field('create_time,model,unique_code')->where($map)->select(false);
$sql = M('device_record','tab_')->table('(' . $sql . ') as a')->group('unique_code')->order('create_time')->select(false);
$sql = "select count(*) as count from ({$sql})t";
$count = M('device_record','tab_')->query($sql);
return $count[0]['count'];
}
public function news_on_time($map=array(),$field='news',$flag=1,$group='time',$order='time',$version = 1) {
switch($flag) {
case 2:{$dateform = '%Y-%m';};break;
case 3:{$dateform = '%Y-%u';};break;
case 4:{$dateform = '%Y';};break;
case 5:{$dateform = '%Y-%m-%d %H';};break;
default:$dateform = '%Y-%m-%d';
}
$sql = M('device_record','tab_')->field('min(create_time) as create_time,version,unique_code,id')->where(['version'=>$version])->group('unique_code')->select(false);
$news = M('device_record','tab_')->table('(' . $sql . ') as a')
->field('FROM_UNIXTIME(a.create_time, "'.$dateform.'") as '.$group.',group_concat(a.id) as id,group_concat(a.unique_code) as unique_code ,COUNT(a.unique_code) AS '.$field)
->where($map)->group($group)->order($order)->select();
// echo M('device_record','tab_')->_sql();die();
return $news;
}
public function active_on_time($map=array(),$field='active',$flag=1,$group='time',$order='time') {
switch($flag) {
case 2:{$dateform = '%Y-%m';};break;
case 3:{$dateform = '%Y-%u';};break;
case 4:{$dateform = '%Y';};break;
case 5:{$dateform = '%Y-%m-%d %H';};break;
default:$dateform = '%Y-%m-%d';
}
$sql = M('device_record','tab_')->field('create_time,version,unique_code,id')->where($map)->select(false);
$mid = M('device_record','tab_')->table('(' . $sql . ') as m')->group('unique_code')->select(false);
$active = M('device_record','tab_')->table('(' . $mid . ') as a')
->field('FROM_UNIXTIME(a.create_time, "'.$dateform.'") as '.$group.',group_concat(a.id) as id,group_concat(a.unique_code) as unique_code ,COUNT(a.unique_code) AS '.$field)
->group($group)->order($order)->select();
// echo $this->_sql();die();
return $active;
}
public function model($map=array()) {
$sql = M('device_record','tab_')->field('create_time,model,version,unique_code')->where($map)->select(false);
$mid = M('device_record','tab_')->table('(' .$sql. ') as m')->group('unique_code')->select(false);
return M('device_record','tab_')->table('(' .$mid. ') as a')->field('a.model,version,count(a.unique_code) as count')->group('model,version')->order('count desc')->select();
}
public function modelDevice($map=array()) {
$sql = M('device_record','tab_')->field('model,version,unique_code')->where($map)->select(false);
$mid = M('device_record','tab_')->table('(' .$sql. ') as m')->group('unique_code')->select();
return $mid;
}
//格式 starttime 20200229 endtime 20200230
public function setAvailableBalance() {
if (strtotime($_GET['endtime'])<strtotime($_GET['starttime']) && $_GET['endtime']) {
echo '错误,结束时间小于开始时间';
}
// $starttime = 1571587200;
$starttime = strtotime(date('Ymd',time()))-86400;
// var_dump($starttime);die();
if ($_GET['starttime']) {
$starttime = strtotime($_GET['starttime']);
}
$endtime = $starttime + 86399;
if ($_GET['endtime']) {
$endtime = strtotime($_GET['endtime']) + 86399;
}
for ($start = $starttime; $start<$endtime; $start=$start + 86400) {
// var_dump($starttime);die();
$data = M('spend','tab_')
->field("sum(`pay_amount`) as pay_amount,promote_id,promote_account,tab_spend.game_id,tab_spend.game_name,tab_spend.server_id,tab_spend.server_name,CASE WHEN old_value_ratio is NULL THEN 0 ELSE sum(`pay_amount`) * old_value_ratio END as available_balance")
->join('left join tab_resource_verify_config as config on tab_spend.game_id = config.game_id and config.status = 1')
->where(['pay_status'=>1,'pay_way'=>['egt',1],'pay_time'=>['between',[$start,$start+86399]]])
->group('promote_id,game_id,server_id')
->select();
// dump($data);die();
foreach ($data as $key => $value) {
$insert['promote_id'] = $value['promote_id'];
$insert['promote_account'] = $value['promote_account'];
$insert['game_id'] = $value['game_id'];
$insert['game_name'] = $value['game_name'];
$insert['available_balance'] = round($value['available_balance'],0);
$insert['server_id'] = $value['server_id'];
$insert['server_name'] = $value['server_name'];
$availableBalanceData = M('promote_game_available_balance','tab_')
->where(['promote_id'=>$value['promote_id'],'game_id'=>$value['game_id'],'server_id'=>$value['server_id']])
->find();
if (round($value['available_balance'],0) > 0 && !$availableBalanceData) {
M('promote_game_available_balance','tab_')->add($insert);
} else if (round($value['available_balance'],0) > 0 && $availableBalanceData) {
M('promote_game_available_balance','tab_')
->where(['promote_id'=>$value['promote_id'],'game_id'=>$value['game_id'],'server_id'=>$value['server_id']])
->setInc('available_balance',round($value['available_balance'],0));
}
// dump(round($value['available_balance'],0));
}
}
echo '执行成功';
}
public function getTogetherUserAccount() {
if (I('user_id','')) {
$user_id = I('user_id','');
} else {
return false;
}
$data = M('user','tab_')->field('account')->where(['id'=>$user_id])->find();
// var_dump($data);
if (!$data) {
return false;
}
$this->ajaxReturn($data);
}
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) {
echo "{$value['my_time']}-{$value['promote_account']}-{$value['game_name']}:{$value['pay_amount']}\n";
$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;
}
public function setPromoteStatistics() {
echo "-------------------------------------------------------------\n";
$startTime = strtotime("20190829");
if (I("start_time")=="yesterday") {
$startTime = strtotime(date("Y-m-d",strtotime("-1 day")));
}
$endTime = time();
if (I("start_time")) {
$startTime = strtotime(I("start_time"));
}
if (I("end_time")) {
$endTime = strtotime(I("end_time"));
}
if ($endTime < $startTime) {
echo "错误,开始时间不得大于结束时间";die();
}
for($start = $startTime;$start<$endTime;$start=$start+86400) {
$map['pay_time'] = ['between', array($start, $start+86399)];
$map['tp1.chain'] = '/';
$map['pay_status'] = 1;
$map['promote_id'] = array('egt', 0);
$data = M('promote', 'tab_')->alias('tp1')
->field('tp1.account as promote_account,tp1.id as promote_id,floor(sum(pay_amount)*100) as count')
->join("tab_promote AS tp2 ON tp2.`chain` LIKE CONCAT('%/', tp1.id, '/%') OR tp2.id = tp1.id", 'left')
->join("tab_spend as s on tp2.id = s.promote_id", 'left')
->where($map)
->group('tp1.id')
->order('count desc')
->select();
foreach($data as $key => $value) {
$insert['promote_id'] = $value['promote_id'];
$insert['promote_account'] = $value['promote_account'];
$insert['count'] = $value['count']?$value['count']:0;
$insert['time'] = $start;
$statisticsData = M("promote_statistics","tab_")
->where(['promote_id'=>$value['promote_id'],'time'=>$start])
->find();
$date = date("Y-m-d",$start);
if ($statisticsData) {
M("promote_statistics","tab_")
->where(['promote_id'=>$value['promote_id'],'time'=>$start])
->save($insert);
echo "更新{$value['promote_account']}{$date}充值:{$value['count']}\n";
} else {
M("promote_statistics","tab_")->add($insert);
echo "新增{$value['promote_account']}{$date}充值:{$value['count']}\n";
}
}
}
echo "-------------------------------------------------------------\n";
}
public function shiftPromoteStatistics() {
$data = M("shift_task")->where(['change_status'=>0,'type'=>2])->select();
$promote_data = [];
$change_id = [];
foreach ($data as $key => $value) {
$promote_data[] = $value['from_promote_id'];
$promote_data[] = $value['to_promote_id'];
$change_id[] = $value['id'];
}
$promote_data=array_unique($promote_data);
sort($promote_data);
if(!$promote_data) {
echo "没有数据";
die();
}
$chain_data = M("promote","tab_")
->field("id")
->where(['chain'=>'/','id'=>['in',$promote_data]])
->select();
$chain_data = array_column($chain_data,'id');
$chain_data_secord = M("promote","tab_")
->field("substring_index(chain, '/', 2) as id")
->where(['id'=>['in',$promote_data]])
->select();
$chain_data_secord = array_column($chain_data_secord,'id');
foreach($chain_data_secord as $k=>$v){
if($v == '/'){
unset($chain_data_secord[$k]);
}
$chain_data_secord[$k] = substr($chain_data_secord[$k],1,strlen($v));
if ($chain_data_secord[$k] == false) {
unset($chain_data_secord[$k]);
}
// var_dump(strlen($chain_data_secord[$k]));
}
$data = [];
$data = array_merge(array_unique($chain_data),array_unique($chain_data_secord));
$this->PromoteStatistics($data);
M("shift_task")->where(['id'=>['in',$change_id]])->save(['change_status'=>1]);
// dump($data);
// dump(array_unique($chain_data_secord));
}
public function PromoteStatistics($promote) {
echo "-------------------------------------------------------------\n";
$startTime = strtotime("20190829");
$endTime = time();
if (I("start_time")) {
$startTime = strtotime(I("start_time"));
}
if ($promote) {
$map['tp2.id'] = ['in',$promote];
}
if (I("end_time")) {
$endTime = strtotime(I("end_time"));
}
if ($endTime < $startTime) {
echo "错误,开始时间不得大于结束时间";die();
}
for($start = $startTime;$start<$endTime;$start=$start+86400) {
$map['pay_time'] = ['between', array($start, $start+86399)];
$map['tp1.chain'] = '/';
$map['pay_status'] = 1;
$map['promote_id'] = array('egt', 0);
$data = M('promote', 'tab_')->alias('tp1')
->field('tp1.account as promote_account,tp1.id as promote_id,floor(sum(pay_amount)*100) as count')
->join("tab_promote AS tp2 ON tp2.`chain` LIKE CONCAT('%/', tp1.id, '/%') OR tp2.id = tp1.id", 'left')
->join("tab_spend as s on tp2.id = s.promote_id", 'left')
->where($map)
->group('tp1.id')
->order('count desc')
->select();
foreach($data as $key => $value) {
$insert['promote_id'] = $value['promote_id'];
$insert['promote_account'] = $value['promote_account'];
$insert['count'] = $value['count']?$value['count']:0;
$insert['time'] = $start;
$statisticsData = M("promote_statistics","tab_")
->where(['promote_id'=>$value['promote_id'],'time'=>$start])
->find();
$date = date("Y-m-d",$start);
if ($statisticsData) {
M("promote_statistics","tab_")
->where(['promote_id'=>$value['promote_id'],'time'=>$start])
->save($insert);
echo "更新{$value['promote_account']}{$date}充值:{$value['count']}\n";
} else {
M("promote_statistics","tab_")->add($insert);
echo "新增{$value['promote_account']}{$date}充值:{$value['count']}\n";
}
}
}
echo "-------------------------------------------------------------\n";
}
function getPayway($name) {
switch ($name){
case 'alipay': //支付宝 1
return 1;
case 'wei_xin': //微信支付 2 3
return 2;
case 'heepay': //汇付宝 6
return 6;
case 'sqpay': //双乾支付9,10,15
return 9;
case 'yeepay': //易宝支付17
return 17;
default:
return '';
}
}
//计算市场专员业绩公式 参数 time 2020-10
public function caculateMarketStream () {
echo date("Y-m-d H:i:s")."----------------------市场结算管理计算----------------------\n";
$map['pay_time'] = date("Y-m",strtotime('-1 month'));
if ($_REQUEST['time']) {
$map['pay_time'] = $_REQUEST['time'];
}
$spend_map = [];
if($_REQUEST['time'] == 'all') {
$map = [];
$spend_map['pay_time'] = ['elt',strtotime(date('Y-m-t 23:59:59', strtotime('-1 month')))];
}
$data = M("auth_group")
->where(['title'=>['like','%市场%']])
->select(false);
$data = M("auth_group_access")
->field("auth.market_percentage,real_name,sys_member.uid,sys_auth_group_access.group_id")
->join("left join ({$data}) auth on auth.id = sys_auth_group_access.group_id")
->join("left join sys_member on sys_member.uid = sys_auth_group_access.uid")
->where("auth.title is not null")
->select(false);
$spend = M("spend","tab_")
->field("sum(pay_amount) as pay_amount,FROM_UNIXTIME(pay_time,'%Y-%m') pay_time,
substring_index(substring_index(tab_spend.game_name,'(',1),'(',-1) game_name,game_id,relation_game_id,tab_spend.sdk_version,
CASE WHEN substring_index(substring_index(`chain`,'/',2),'/',-1) !='' THEN substring_index(substring_index(`chain`,'/',2),'/',-1) ELSE promote_id END promote_id,pay_way,market_admin_id")
->join("left join tab_promote on tab_spend.promote_id = tab_promote.id")
->join("left join tab_game game on game.id=tab_spend.game_id")
->where(['tab_spend.pay_status'=>1,'pay_way'=>['egt',0]])
->where($spend_map)
->group("tab_spend.id")
->select(false);
// var_dump($spend);die();
$payway_spend = M()->table("({$spend}) spend")
->field("sum(pay_amount) as pay_amount,pay_time,promote_id,game_name,game_id,relation_game_id,pay_way,sdk_version")
->where($map)
->group("pay_time,promote_id,game_name,pay_way,market_admin_id")
->select();
$channel_fee = [];
$paywayData = M("tool","tab_")
->field("name,config")
->where(['name'=>['in',"alipay,weixin,wei_xin,wei_xin_app,jubaobar,weixin_gf,jft,jft_wap,goldpig,ptb_pay,bind_pay,sqpay,heepay,yeepay"]])
->select();
//公司税费计算
$tax_radio = [];
$tax_data = M("tax_radio","tab_")
->field("tax_radio,time")
->order("time ASC")
->select();
foreach ($tax_data as $key => $value) {
$tax_radio[$key]['start'] = $value['time'];
$tax_radio[$key]['end'] = $tax_data[$key+1]['time'];
$tax_radio[$key]['tax_radio'] = $value['tax_radio'];
if (!$tax_data[$key+1]['time']) {
$tax_radio[$key]['end'] = 99999999999;
}
}
$pay_rate = [];
foreach ($paywayData as $key => $value) {
$pay_config = json_decode($value['config'],true);
if ($this->getPayway($value['name'])) {
$pay_rate[$this->getPayway($value['name'])] = $pay_config['channel_rate']?$pay_config['channel_rate']:0;
}
}
foreach ($payway_spend as $key => $value) {
switch ($value['pay_way']){
case 3: //微信同步
$value['pay_way'] = '2';
break;
case 10: //双乾同步
$value['pay_way'] = '9';
break;
case 15: //双乾同步
$value['pay_way'] = '9';
break;
}
//将时间、推广员id、游戏名、设备类型作为键值标记渠道费用
if (!$channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name']]) {
if (!$value['pay_way']) {
$channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name']] = 0;
}
$channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name']]
= $value['pay_amount'] * ($pay_rate[$value['pay_way']]/100);
} else {
$channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name']]
= $channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name']]+($value['pay_amount'] * ($pay_rate[$value['pay_way']]/100));
}
}
$spend = M()->table("({$spend}) spend")
->field("sum(pay_amount) as pay_amount,pay_time,promote_id,game_name,game_id,relation_game_id,sdk_version,market_admin_id")
->where($map)
->group("pay_time,promote_id,game_name,market_admin_id")
->select(false);
$promote_data = M()->table("({$spend}) spend")
->field("pay_time,company_id,account as promote_account,company.company_belong,develop_type,spend.promote_id,spend.game_name,game_id,relation_game_id,spend.pay_amount,auth.real_name,market_admin_id as admin_id,market_percentage,sdk_version")
->join("left join tab_promote promote on spend.promote_id = promote.id")
->join("left join tab_promote_company company on promote.company_id = company.id")
->join("left join ({$data}) auth on auth.uid=market_admin_id")
->where(['develop_type'=>['egt',1],'group_id'=>['in',['11','12','21']]])
->select();
// dump($spend);die();
//
// dump($tax_radio);die();
foreach ($promote_data as $key => $value) {
$cp_radio = getGameCpRadio($value['game_id'],$value['pay_amount'],true);
if (!$cp_radio) {
$cp_radio = getGameCpRadio($value['game_id'],$value['pay_amount'],false);
}
//上游流水
$promote_data[$key]['cp_amount'] = $value['pay_amount'] * ($cp_radio * 0.01);
//下游
$promote_radio = getGamePromoteCompanyRadio($value['company_id'],$value['relation_game_id'],time(),$value['pay_amount'],true,$value['company_belong']);
if (!$promote_radio) {
$promote_radio = getGamePromoteCompanyRadio($value['company_id'],$value['relation_game_id'],time(),$value['pay_amount'],false,$value['company_belong']);
}
//下游流水
$promote_data[$key]['promote_amount'] = $value['pay_amount'] * ($promote_radio * 0.01);
//渠道费用
$promote_data[$key]['channel_amount'] = $channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name']];
$tax = 0;
//公司税费
foreach($tax_radio as $k => $v) {
if (strtotime($value['pay_time'])>=$v['start'] && strtotime($value['pay_time'])<$v['end']) {
$tax = $v['tax_radio'];
}
}
$promote_data[$key]['company_tax'] = ($value['pay_amount']-$promote_data[$key]['cp_amount']- $promote_data[$key]['promote_amount'])*($tax/100);
//毛利
$promote_data[$key]['company_profit'] = $value['pay_amount']-$promote_data[$key]['cp_amount']-$promote_data[$key]['promote_amount']-$promote_data[$key]['channel_amount']-$promote_data[$key]['company_tax'];
if ($value['promote_id'] == 0) {
// $promote_data[$key]['promote_account']='官方渠道';
// $promote_data[$key]['company_id']='0';
// $promote_data[$key]['company_belong']='3';
// $promote_data[$key]['develop_type']='0';
continue;
}
unset($promote_data[$key]['market_percentage']);
$marker_data = M("settleup_marketorder","tab_")
->where(['pay_time'=>$value['pay_time'],'promote_id'=>$value['promote_id'],'game_name'=>$value['game_name'],'admin_id'=>$value['admin_id']])
->find();
echo "日期:{$value['pay_time']},游戏:{$value['game_name']},推广员:{$value['promote_account']}\n";
// dump($promote_data[$key]);die();
if (!$marker_data) {
M("settleup_marketorder","tab_")
->add($promote_data[$key]);
} else {
M("settleup_marketorder","tab_")
->where(['pay_time'=>$value['pay_time'],'promote_id'=>$value['promote_id'],'game_name'=>$value['game_name'],'admin_id'=>$value['admin_id']])
->save($promote_data[$key]);
}
}
// var_dump($_REQUEST['time']);
$this->createMarketBonus($_REQUEST['time']);
echo date("Y-m-d H:i:s")."----------------------市场结算管理计算----------------------\n";
}
public function insertAllUserBonus($time) {
// $time = $_REQUEST['time'];
$data = M("member")
->field("access.uid admin_id,real_name,nickname,group_id level")
->join("left join sys_auth_group_access access on sys_member.uid = access.uid")
->join("left join sys_auth_group auth on access.group_id=auth.id")
->where(['group_id'=>['in','11,12,21']])
->select();
foreach ($data as $key => $value) {
$value['pay_time'] = $time;
$value['remark'] = "";
$value['create_time'] = time();
$sign = M("market_altogether","tab_")
->where(['admin_id'=>$value['admin_id'],'pay_time'=>$time])
->find();
if (!$sign) {
M("market_altogether","tab_")->add($value);
} elseif(!$sign['real_name']) {
M("market_altogether","tab_")->where(['admin_id'=>$value['admin_id'],'pay_time'=>$time])->save(['real_name'=>$value['real_name']]);
}
}
}
public function createMarketBonus($pay_time) {
if ($pay_time&&$pay_time!='all') {
$map['pay_time'] = $pay_time;
}
if (!$pay_time) {
$pay_time = date("Y-m",strtotime('-1 month'));
$map['pay_time'] = date("Y-m",strtotime('-1 month'));
}
if ($pay_time == 'all') {
$end = date("Y-m",time());
$start = '2019-08';
$i = 0;
do {
$date = date('Y-m', strtotime($start . ' + ' . $i . ' month'));
$i++;
$this->insertAllUserBonus($date);
} while ($date < date('Y-m', strtotime("{$end} -1 month")));
// $map['pay_time'] = [];
unset($map['pay_time']);
} else{
$this->insertAllUserBonus($pay_time);
}
$all_data = M("settleup_marketorder","tab_")
->field("tab_settleup_marketorder.id,pay_time,promote_account,company_belong,tab_settleup_marketorder.real_name,develop_type,game_name,
sum(pay_amount) pay_amount,sum(company_profit) company_profit,group_id,market_percentage,nickname,admin_id,
sum(if((develop_type = 1 or develop_type = 2), pay_amount, 0)) performance_revenue,sum(if(develop_type = 3, pay_amount, 0)) appraisal_bonuses,
sum(company_tax) company_tax, sum(cp_amount) cp_amount,sum(promote_amount) promote_amount")
->join("left join sys_auth_group_access access on admin_id=access.uid")
->join("left join sys_auth_group auth on access.group_id=auth.id")
->join("left join sys_member member on access.uid=member.uid")
->where(['group_id'=>['in','11,12,21']])
->where($map)
->group("admin_id,pay_time")
->select();
foreach ($all_data as $key => $value) {
$sum_amount = $value['performance_revenue'] + $value['appraisal_bonuses'];
$market_percentage = json_decode($value['market_percentage'],true);//业绩提成
if ($value['performance_revenue']=='0.00') {
$market_percentage['develop_commission_ratio'] = 0;
$market_percentage['appraisal_bonuses_ratio'] = 0;
}
if ($value['appraisal_bonuses']=='0.00') {
$market_percentage['maintain_commission_ratio'] = 0;
$market_percentage['maintain_appraisal_bonuses_ratio'] = 0;
}
// echo "{$value['nickname']}:{$value['pay_time']}\n";
// dump($market_percentage);
$all_data[$key]['performance_commission'] =
number_format($value['company_profit']*($market_percentage['develop_commission_ratio']/100) +
$value['company_profit']*($market_percentage['maintain_commission_ratio']/100), 2, '.', '');
$sum_amount <$market_percentage['performance_revenue'] ? ($all_data[$key]['performance_commission'] = 0):"";
//绩效考核奖金
$all_data[$key]['achievement_bonus'] =
number_format($value['company_profit']*($market_percentage['appraisal_bonuses_ratio']/100) +
$value['company_profit']*($market_percentage['maintain_appraisal_bonuses_ratio']/100), 2, '.', '');
$sum_amount <$market_percentage['appraisal_bonuses'] ? ($all_data[$key]['achievement_bonus'] = 0):"";
//总提成
$all_data[$key]['commission'] = number_format($all_data[$key]['performance_commission']+$all_data[$key]['achievement_bonus'], 2, '.', '');
//应发金额
$all_data[$key]['extend_commission'] =
substr(number_format($all_data[$key]['performance_commission'] * ($market_percentage['performance_monthly_ratio']/100)+
$all_data[$key]['achievement_bonus'] * ($market_percentage['month_bonus_ratio']/100), 4, '.', ''),0,-2);
//待发金额
$all_data[$key]['wait_commission'] = substr(number_format($all_data[$key]['commission'] - ( $all_data[$key]['performance_commission'] * ($market_percentage['performance_monthly_ratio']/100)+
$all_data[$key]['achievement_bonus'] * ($market_percentage['month_bonus_ratio']/100) ), 4, '.', ''),0,-2);
$insert['pay_time'] = $value['pay_time'];
$insert['real_name'] = $value['real_name'];
$insert['admin_id'] = $value['admin_id'];
$insert['nickname'] = $value['nickname'];
$insert['level'] = $value['group_id'];
$insert['pay_amount'] = $value['pay_amount'];
$insert['extend_commission'] = substr(
number_format(($all_data[$key]['performance_commission'] * ($market_percentage['performance_monthly_ratio']/100)
+ $all_data[$key]['achievement_bonus'] * ($market_percentage['month_bonus_ratio']/100)), 4, '.', ''),0,-2);
$insert['wait_commission'] = $all_data[$key]['wait_commission'];
$insert['remark'] = '';
$insert['create_time'] = time();
$insert['company_profit'] = $value['company_profit'];
$insert['company_tax'] = $value['company_tax'];
$insert['cp_amount'] = $value['cp_amount'];
$insert['promote_amount'] = $value['promote_amount'];
$insert['performance_revenue'] = $value['performance_revenue'];
$insert['appraisal_bonuses'] = $value['appraisal_bonuses'];
$insert['performance_commission'] = $all_data[$key]['performance_commission'];
$insert['achievement_bonus'] = $all_data[$key]['achievement_bonus'];
//待发业绩奖金
$insert['wait_performance_commission'] = number_format($all_data[$key]['performance_commission'] * (1-($market_percentage['performance_monthly_ratio']/100)), 2, '.', '');
//待发绩效奖金
$insert['wait_achievement_commission'] = number_format($all_data[$key]['achievement_bonus'] * (1-($market_percentage['month_bonus_ratio']/100)), 2, '.', '');
// $insert['wait_commission'] = ($insert['wait_performance_commission']+$insert['wait_achievement_commission']);
$is_hav = M("market_altogether","tab_")
->where(['admin_id'=>$value['admin_id'],'pay_time'=>$value['pay_time']])
->find();
if (!$is_hav) {
M("market_altogether","tab_")->add($insert);
} else {
M("market_altogether","tab_")
->where(['admin_id'=>$value['admin_id'],'pay_time'=>$value['pay_time']])
->save($insert);
}
}
}
public function sendMarketBonus(){
echo date("Y-m-d H:i:s")."----------------------月初发放市场专员应发份额----------------------\n";
$map['pay_time'] = date("Y-m",strtotime('-1 month'));
if ($_REQUEST['time']) {
$map['pay_time'] = $_REQUEST['time'];
}
if ($_REQUEST['time'] == 'all') {
unset($map['pay_time']);
}
$data = M("market_altogether","tab_")
->where($map)
->select();
// dump($map);die();
foreach ($data as $key => $value) {
$group_config = M("auth_group_access access")
->field("market_percentage")
->join("left join sys_auth_group auth on access.group_id=auth.id")
->where(['uid'=>$value['admin_id']])
->find();
$group_config = json_decode($group_config['market_percentage'],true);
$month = date("m",strtotime($value['pay_time']));
$yes = date("Y",strtotime($value['pay_time']));
$where =[];
if ($group_config['time'] == 1) {
// var_dump($month);die();
if ($month == '07') {
$where = [$yes.'-'.'01',$yes.'-'.'02',$yes.'-'.'03'];
} elseif($month == '10') {
$where = [$yes.'-'.'04',$yes.'-'.'05',$yes.'-'.'06'];
} elseif($month == '04') {
$where = [($yes-1).'-'.'10',($yes-1).'-'.'11',($yes-1).'-'.'12'];
} elseif($month == '01') {
$where = [($yes-1).'-'.'07',($yes-1).'-'.'08',($yes-1).'-'.'09'];
}
} elseif($group_config['time'] == 2) {
if ($month == '07') {
$where = [$yes.'-'.'01',$yes.'-'.'02',$yes.'-'.'03',$yes.'-'.'04',$yes.'-'.'05',$yes.'-'.'06'];
} elseif($month == '01') {
$where = [($yes-1).'-'.'07',($yes-1).'-'.'08',($yes-1).'-'.'09',($yes-1).'-'.'10',($yes-1).'-'.'11',($yes-1).'-'.'12'];
}
} elseif($group_config['time'] == 3) {
if ($month == '01') {
$where = [($yes-1).'-'.'01',($yes-1).'-'.'02',($yes-1).'-'.'03',($yes-1).'-'.'04',($yes-1).'-'.'05',($yes-1).'-'.'06',
($yes-1).'-'.'07',($yes-1).'-'.'08',($yes-1).'-'.'09',($yes-1).'-'.'10',($yes-1).'-'.'11',($yes-1).'-'.'12'];
}
} else {
$where = [date("Y-m",strtotime("last month",strtotime($value['pay_time'])))];
}
echo "{$value['pay_time']}:{$value['real_name']}发放\n";
$save['status'] = 1;
$save['remark'] = '自动发放';
if ($where) {
$map['pay_time'] = ['in',$where];
$map['admin_id'] = $value['admin_id'];
M("market_altogether","tab_")
->where($map)
->save($save);
}
}
echo date("Y-m-d H:i:s")."----------------------市场结算管理计算----------------------\n";
}
}