// +---------------------------------------------------------------------- namespace Admin\Controller; use User\Api\UserApi as UserApi; /** * 后台首页控制器 * @author 麦当苗儿 */ 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'])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); } 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 ''; } } public function caculateMarketStream () { echo date("Y-m-d H:i:s")."----------------------市场结算管理计算----------------------\n"; $data = M("auth_group") ->where(['title'=>['like','%市场%']]) ->select(false); $data = M("auth_group_access") ->field("auth.market_percentage,real_name,sys_member.uid") ->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") ->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',1]]) ->group("tab_spend.id") ->select(false); $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") ->group("pay_time,promote_id,game_name,sdk_version,pay_way") ->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(); $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'].$value['sdk_version']]) { if (!$value['pay_way']) { $channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name'].$value['sdk_version']] = 0; } $channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name'].$value['sdk_version']] = $value['pay_amount'] * ($pay_rate[$value['pay_way']]/100); } else { $channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name'].$value['sdk_version']] = $channel_fee[$value['pay_time'].$value['promote_id'].$value['game_name'].$value['sdk_version']]+($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") ->group("pay_time,promote_id,game_name,sdk_version") ->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,auth.uid 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=admin_id") ->select(); // dump($promote_data);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'].$value['sdk_version']]; //公司税费 $promote_data[$key]['company_tax'] = ($value['pay_amount']-$promote_data[$key]['cp_amount']- $promote_data[$key]['promote_amount'])*(6.72/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'; } 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'],'sdk_version'=>$value['sdk_version']]) ->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']]) ->save($promote_data[$key]); } } // dump($promote_data); echo date("Y-m-d H:i:s")."----------------------市场结算管理计算----------------------\n"; } }