// +---------------------------------------------------------------------- namespace Admin\Controller; use User\Api\UserApi as UserApi; use function GuzzleHttp\Psr7\str; use Base\Factory\BaseFactory; /** * 后台首页控制器 * @author 麦当苗儿 */ class TimingController extends AdminController { private $affairRatioList = []; 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); } 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; case 'ptb_pay': return 0; default: return ''; } } /** * 获取下游公司的特殊补点金额 * @param int $start_time * @param int $end_time * @return array */ public function getSpecialComplement($start_time=0,$end_time =0,$promote_id = 0) { $data = SM("company_statement","tab_") // ->where("statement_begin_time<={$start_time} and statement_end_time<={$end_time} and withdraw_type=3") ->where("(statement_begin_time <={$end_time} AND ( statement_end_time = 0 OR statement_end_time >= {$start_time})) and withdraw_type=3") ->select(); $return = []; foreach ($data as $key => $value) { $info = []; $pool_data = SM("company_statement_pool","tab_") ->field("id") ->where("(statement_begin_time <={$end_time} AND ( statement_end_time = 0 OR statement_end_time >= {$start_time})) and withdraw_type=3 and statement_ids like '%{$value['id']}%'") ->select(); foreach ($pool_data as $pk => $pv) { $info = SM("company_statement_info","tab_") ->field("statement_info") ->where("(statement_begin_time <={$end_time} AND ( statement_end_time = 0 OR statement_end_time >= {$start_time})) and withdraw_type=3 and pool_id = {$pv['id']} and company_id = {$value['company_id']}") ->find()['statement_info']; if ($info) { break; } } if ($info) { $statement_info = json_decode($info,true); } else { $statement_info = json_decode($value['statement_info'],true); } $is_inside = M("PromoteCompany","tab_")->field("is_inside")->where("id='{$value['company_id']}'")->find()['is_inside']; if ($value['company_type'] == 2) { foreach ($statement_info as $skey => $sval) { $info = $sval['game_list']; foreach ($info as $k => $v) { $statement_begin_time = strtotime(str_replace('.','-',$v['statement_begin_time'])); $statement_end_time = strtotime(str_replace('.','-',$v['statement_end_time']))+86399; if (date("m",$statement_begin_time)!=date("m",$start_time) || date("m",$statement_end_time)!=date("m",$end_time)) { continue; } // if (!isset($return[$value['company_id']."-".$sval['promote_id']."-".$v['relation_game_id']])) { $return[$value['company_id']."-".$sval['promote_id']."-".$v['relation_game_id']] = !$is_inside?($v['sum_money']-($v['withhold']?$v['withhold']:0)):0; } else { $return[$value['company_id']."-".$sval['promote_id']."-".$v['relation_game_id']] += !$is_inside?($v['sum_money']-($v['withhold']?$v['withhold']:0)):0; } } } } else { //判断公司比例是不是隶属于这个会长 $compamy_info = M("promote","tab_")->where(['id'=>$promote_id,'company_id'=>$value['company_id']])->find(); if ($compamy_info) { foreach ($statement_info as $k => $v) { $statement_begin_time = strtotime(str_replace('.','-',$v['statement_begin_time'])); $statement_end_time = strtotime(str_replace('.','-',$v['statement_end_time']))+86399; if (date("m",$statement_begin_time)!=date("m",$start_time) || date("m",$statement_end_time)!=date("m",$end_time)) { continue; } $amount_time['is_check'] = ['neq',2]; $amount_time['tab_spend.pay_status'] = 1; $amount_time['pay_way'] = ['egt',0]; $amount_time['_string'] = "payed_time between {$statement_begin_time} and {$statement_end_time} and relation_game_id={$v['relation_game_id']}"; D("Spend")->addSubsiteWhere($amount_time); $dataAmount = M("spend","tab_") ->field("pay_amount,relation_game_id,relation_game_name,payed_time,game_type_name,substring_index(substring_index(promote.`chain`,'/',2),'/',-1) promote_id,chain,admin_id") ->join("left join tab_promote promote on tab_spend.promote_id=promote.id") ->join("left join tab_game game on tab_spend.game_id=game.id") ->where($amount_time) ->select(false); $dataAmount = M()->table("({$dataAmount})a") ->field("a.*,sum(pay_amount) pay_amount,promote.account,member.real_name") ->join("left join tab_promote promote on promote_id=promote.id") ->join("left join sys_member member on promote.admin_id=member.uid ") ->where(['promote_id'=>$promote_id]) ->group("promote_id") ->find(); $return[$value['company_id']."-".$promote_id."-".$v['relation_game_id']] += !$is_inside?(($dataAmount['pay_amount']*$v['increment_ratio'])/100):0; } } } } // dump($return); return $return; } public function test() { $this->getSpecialComplement(strtotime(date("Y-m-1",strtotime("2020-08"))),strtotime(date("Y-m-t",strtotime("2020-08")))+86399,2259); // dump(D("CompanyGameRatio")->getPromoteCompanyGameRatio(18,(string)207,1600099200,1603728000,1)); // $this->createMarketBonus("all"); } // $admin_id = 0,$promote_id = 0,$company_id=0,$relation_game_id=0,$date ='',$pay_amount = 0 public function getCompanyGameRadio($admin_id = 0,$promote_id = 0,$company_id=0,$relation_game_id=0,$begintime=0,$endtime =0,$pay_amount = 0) { // $company_id=239; // $relation_game_id=213; // $pay_amount = 440; // $date = "2020-08"; // $admin_id = 27; // $promote_id = 3235; // $begintime = strtotime(date("Y-m-1",strtotime($date))); // $endtime = strtotime(date("Y-m-t",strtotime($date)))+86399; $is_inside = M("PromoteCompany","tab_")->field("is_inside")->where("id='{$company_id}'")->find()['is_inside']; if ($is_inside == 1) { return 0; } // $company_game_ratio = M("company_game_ratio","tab_") // ->where(['company_id'=>$company_id,'relation_game_id'=>$relation_game_id]) // ->where("(end_time>={$begintime} and (end_time<={$endtime} and end_time!=0)) or (begin_time>={$begintime} and begin_time<={$endtime}) or (begin_time<={$begintime} and (end_time>={$endtime} or end_time=0))") // ->order("begin_time ASC") // ->select(); // $company_data = M('promote_company','tab_') ->field("company_belong") ->where("id={$company_id}") ->find(); // // $game_ratio_mould = M("game_ratio_mould","tab_") // ->where(['relation_game_id'=>$relation_game_id,'company_belong'=>$company_data['company_belong'], // "_string"=>"begin_time <={$endtime} AND ( end_time = 0 OR end_time >= {$begintime})"]) // ->select(); $company_game_ratio = D("CompanyGameRatio")->getPromoteCompanyGameRatio($company_id,(string)$relation_game_id,$begintime,$endtime,$company_data['company_belong']); $company_game_ratio = $company_game_ratio[$relation_game_id]; // $mould = json_decode($company_game_ratio['turnover_ratio'],true); $amount = 0; if (!$company_game_ratio) { // $spend = $this->getSpendData($begintime,$endtime,$company_id,$relation_game_id,$admin_id,$promote_id); // $amount += $this->setSpendData($spend,$mould,$game_ratio_mould['ratio'],$pay_amount); return 0; } foreach ($company_game_ratio as $key => $value) { $turnover_ratio = json_decode($value['turnover_ratio'],true); // dump($value); $spend = $this->getSpendData($value['begintime'],$value['endtime'],$company_id,$relation_game_id,$admin_id,$promote_id); // dump($spend); $amount += $this->setSpendData($spend,$turnover_ratio,$value['ratio'],$pay_amount); } // dump($amount);die(); return $amount?$amount:0; } public function setSpendData($spend=[],$turnover_ratio=[],$radio=0,$pay_amount=0) { $turnover = 0; $amount= 0; foreach ($spend as $k => $v) { //获取的金额 $amount = ($v['pay_amount'] * ($radio?$radio:0))/100; foreach ( $turnover_ratio as $tk => $tv ) { //判断大于的流水是否比前一个要小 if ($turnover >= $tv['turnover']) { continue; } if ($tv['instanceof']==1) { if ($pay_amount >= $tv['turnover']) { $amount = ($v['pay_amount'] * ($tv['ratio']?$tv['ratio']:0))/100; $turnover = $tv['turnover']; } } else { if ($pay_amount > $tv['turnover']) { $amount = ($v['pay_amount'] * ($tv['ratio']?$tv['ratio']:0))/100; $turnover = $tv['turnover']; } } } } return $amount; } public function getSpendData($begintime = 0,$endtime = 0,$company_id=0,$relation_game_id=0,$admin_id=0,$promote_id=0) { $map['tab_spend.pay_status'] = 1; $map['pay_way'] = ['egt',0]; D("Spend")->addSubsiteWhere($map,"tab_spend"); $spend = M("spend","tab_") ->field("sum(pay_amount) pay_amount, CASE WHEN substring_index(substring_index(`chain`,'/',2),'/',-1) !='' THEN substring_index(substring_index(`chain`,'/',2),'/',-1) ELSE promote_id END promote_id,game.relation_game_id,company_id") ->join("left join tab_game game on tab_spend.game_id = game.id") ->join("left join tab_promote promote on tab_spend.promote_id = promote.id") ->where($map) ->where(['payed_time'=>['between',[$begintime,$endtime]],'company_id'=>$company_id,'relation_game_id'=>$relation_game_id,'market_admin_id'=>$admin_id,'is_check'=>['neq',2]]) ->group("tab_spend.id") ->select(false); $spend = M()->table("({$spend})a") ->field("*,sum(pay_amount) pay_amount") ->where(['promote_id'=>$promote_id]) ->group("promote_id") ->select(); return $spend; } //计算市场专员业绩公式 参数 time 2020-10 public function caculateMarketStream () { if ($_REQUEST['time']) { $count_date = $_REQUEST['time']; }else{ $count_date = date('Y-m',strtotime(date('Y',time()).'-'.(date('m',time())-1).'-01')); } A("MarketPerformanceSet")->setFreeMonth($count_date); $this->createMarketBonus($_REQUEST['time']); A("MarketPerformanceSet")->setThisMonthFirstMondayScript(); die(); } public function insertAllUserBonus($time) { // $time = $_REQUEST['time']; $groupIds = A("Market","Event")->getGeneralMarketGroup(); $data = M("member") ->field("access.uid admin_id,real_name,nickname,auth.department_level 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',$groupIds]]) ->select(); foreach ($data as $key => $value) { $value['pay_time'] = $time; $value['remark'] = ""; $value['create_time'] = time(); $sign = SM("market_altogether","tab_") ->where(['admin_id'=>$value['admin_id'],'pay_time'=>$time]) ->find(); if (!$sign) { SM("market_altogether","tab_")->add($value); } elseif(!$sign['real_name']) { SM("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(date('Y',time()).'-'.(date('m',time())-1).'-01')); $map['pay_time'] = date('Y-m',strtotime(date('Y',time()).'-'.(date('m',time())-1).'-01')); } if ($pay_time == 'all') { $end = date("Y-m",time()); $start = date('Y-m',strtotime(date('Y',time()).'-'.(date('m',time())-1).'-01')); $i = 0; do { $date = date('Y-m', strtotime($start . ' + ' . $i . ' month')); $i++; M("market_altogether","tab_")->where(['pay_time'=>$date])->delete(); $this->insertAllUserBonus($date); } while ($date < date('Y-m',strtotime(date('Y',time()).'-'.(date('m',time())-1).'-01'))); $map['pay_time'] = date('Y-m',strtotime(date('Y',time()).'-'.(date('m',time())-1).'-01'));; } else{ SM("market_altogether","tab_")->where(['pay_time'=>$pay_time])->delete(); $this->insertAllUserBonus($pay_time); } $map['is_settlement'] = 1; $groupIds = A("Market","Event")->getGeneralMarketGroup(); //获取admin_id $accessData = M("auth_group_access") ->alias("access") ->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',$groupIds]]) ->getField("access.uid,auth.department_level level,group_id,market_percentage,nickname"); $all_data = SM("settleup_marketorder","tab_") ->field("id,admin_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, 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(if((develop_type = 1 or develop_type = 2), refund_amount, 0)) performance_refund_amount,sum(if(develop_type = 3, refund_amount, 0)) appraisal_refund_amount, sum(company_tax) company_tax, sum(cp_amount) cp_amount,sum(promote_amount) promote_amount") ->where($map) ->where(["admin_id"=>['in',array_keys($accessData)]]) ->group("admin_id,pay_time") ->select(); foreach ($all_data as $key => $value) { $admin_id = $value['admin_id']; if(isset($accessData[$admin_id])){ $value = array_merge($value,$accessData[$admin_id]); } $value['performance_revenue'] -= $value['performance_refund_amount']; $value['appraisal_bonuses'] -= $value['appraisal_refund_amount']; $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; } $settleup_marketorder = SM("settleup_marketorder","tab_") ->where(['admin_id'=>$value['admin_id'],'pay_time'=>$value['pay_time'],'is_settlement'=>1]) ->select(); $all_data[$key]['performance_commission'] = 0; $all_data[$key]['achievement_bonus'] = 0; foreach ($settleup_marketorder as $k => $v) { if($v['develop_type'] == 1 || $v['develop_type'] == 2) { $all_data[$key]['performance_commission'] += $v['company_profit']*($market_percentage['develop_commission_ratio']/100); $all_data[$key]['achievement_profit'] += $v['company_profit']; } if ($v['develop_type'] == 3) { $all_data[$key]['performance_commission'] += $v['company_profit']*($market_percentage['maintain_commission_ratio']/100); $all_data[$key]['performance_profit'] += $v['company_profit']; } //绩效考核奖金 if($v['develop_type'] == 1 || $v['develop_type'] == 2) { $all_data[$key]['achievement_bonus'] +=$v['company_profit']*($market_percentage['appraisal_bonuses_ratio']/100); } if ($v['develop_type'] == 3) { $all_data[$key]['achievement_bonus'] += $v['company_profit']*($market_percentage['maintain_appraisal_bonuses_ratio']/100); } } $all_data[$key]['performance_commission'] = number_format($all_data[$key]['performance_commission'], 2, '.', ''); $all_data[$key]['achievement_bonus'] = number_format($all_data[$key]['achievement_bonus'], 2, '.', ''); $sum_amount <$market_percentage['performance_revenue'] ? ($all_data[$key]['performance_commission'] = 0):""; $sum_amount <$market_percentage['performance_revenue'] ? ($all_data[$key]['achievement_profit'] = 0):""; $sum_amount <$market_percentage['appraisal_bonuses'] ? ($all_data[$key]['achievement_bonus'] = 0):""; $sum_amount <$market_percentage['appraisal_bonuses'] ? ($all_data[$key]['performance_profit'] = 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['level']; $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['achievement_profit'] = number_format($all_data[$key]['achievement_profit'], 2, '.', ''); $insert['performance_profit'] = number_format($all_data[$key]['performance_profit'], 2, '.', ''); $is_hav = SM("market_altogether","tab_") ->where(['admin_id'=>$value['admin_id'],'pay_time'=>$value['pay_time']]) ->find(); if (!$is_hav) { SM("market_altogether","tab_")->add($insert); } else { SM("market_altogether","tab_") ->where(['admin_id'=>$value['admin_id'],'pay_time'=>$value['pay_time']]) ->save($insert); } } //经理计算 A("MarketPerformanceSet")->marketManagerSet($pay_time); } public function sendMarketBonus(){ echo date("Y-m-d H:i:s")."----------------------月初发放市场专员应发份额----------------------\n"; $map['pay_time'] = date('Y-m',strtotime(date('Y',time()).'-'.(date('m',time())-1).'-01')); if ($_REQUEST['time']) { $map['pay_time'] = $_REQUEST['time']; } if ($_REQUEST['time'] == 'all') { unset($map['pay_time']); } $data = SM("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']; SM("market_altogether","tab_") ->where($map) ->save($save); } } echo date("Y-m-d H:i:s")."----------------------市场结算管理计算----------------------\n"; } //离职定时器 public function timingleaveMarketMember() { $instance = BaseFactory::getInstance(); $leavePercentageInstance = $instance->factoryClass("LeavePercentage"); $leavePercentageInstance->leaveTiming(); } public function setbusinessAffairsAward($month = null) { echo date("Y-m-d H:i:s")."----------------------商务奖金计算----------------------\n"; if (is_null($month)) { $month = date('Y-m', strtotime(date('Y-m-01') . ' -1 month')); } $quarterMonthIndex = date('n', strtotime($month . '-01')) % 3; $quarterBeforeMonth = $quarterMonthIndex == 1 ? null : date('Y-m', strtotime($month . '-01' . ' -1 month')); list($becomeTimeList, $becomeTimeCount) = $this->getAffairList($month); /* $time1 = date('Y-m',strtotime(date('Y',$time).'-'.(date('m',$time)-1).'-01')); $time2 = date('Y-m',strtotime(date('Y',$time).'-'.(date('m',$time)-2).'-01')); $time3 = date('Y-m',strtotime(date('Y',$time).'-'.(date('m',$time)-3).'-01')); $lasttime = [$time2,$time3]; */ $firstday = $month . '-01'; $lastday = date('Y-m-d', strtotime($firstday . ' +1 month -1 day')); $timeRange = [strtotime($firstday . ' 00:00:00'), strtotime($lastday . ' 23:59:59')]; $tempoparyData = M("spend","tab_") ->field("tab_spend.*,from_unixtime(payed_time,'%Y-%m') time,from_unixtime(payed_time,'%Y') year,from_unixtime(payed_time,'%m') month,relation_game_id,relation_game_name,game_belong_id,game_belong_name,game_assistant_id,game_assistant_name") ->join("left join tab_game on tab_game.id=tab_spend.game_id") ->where(['tab_spend.pay_status' => 1, 'game_belong_id' => ['neq',0], 'pay_way' => ['egt', 0], 'tab_spend.payed_time' => ['between', $timeRange]]) ->select(false); /* $lastdata = M()->table("({$tempoparyData}) a") ->field("*,sum(pay_amount) pay_amount") ->where(['time'=>['in',$lasttime]]) ->group("time,relation_game_id,game_belong_id") ->select(); $handleData = []; foreach ($lastdata as $key => $value) { $handleData[$value['time'].$value['game_belong_id'].$value['relation_game_id']] = $value; } */ $data = M()->table("({$tempoparyData}) a") ->field("sum(pay_amount) pay_amount,time,year,month,relation_game_id,relation_game_name,game_belong_id,game_belong_name,game_assistant_id,game_assistant_name") ->where(['time' => $month]) ->group("time,relation_game_id,game_belong_id") ->select(); /* $sum_amount = M()->table("({$tempoparyData}) a") ->field("sum(pay_amount) pay_amount") ->where(['time' => $month]) ->find()['pay_amount']; */ //判断是不是初次 // $is_hav = M("business_affairs_award_pool","tab_")->where(['time' => $time2])->find(); $sum_amount = array_sum(array_column($data, 'pay_amount')); $quarter_bonus_pool = 0; /* foreach($data as $key => $value) { $sum_amount += $value['pay_amount']; } */ /* $last_amount = 0; if (!is_null($quarterBeforeMonth)) { $last_amount = M("business_affairs_award_pool", "tab_")->where(['time' => $quarterBeforeMonth])->getField('month_bonus_pool'); $last_amount = $last_amount ?? 0; } */ $month_bonus_pool = $this->businessAffairRatio($sum_amount); foreach ($data as $key => $value) { echo "{$value['time']},{$value['game_belong_name']},{$value['pay_amount']}\n"; $value['month_bonus_pool'] = $month_bonus_pool; unset($value['year']); unset($value['month']); $becomeTimeMonth = $becomeTimeList[$value['game_belong_id']] ? $becomeTimeList[$value['game_belong_id']] : 0; $becomeTimeRate = $becomeTimeMonth == 0 || $becomeTimeCount == 0 ? 0 : $becomeTimeMonth/$becomeTimeCount; /* $is_assistant = M("ucenter_member") ->field("sys_ucenter_member.id admin_id,become_time") ->join("left join sys_auth_group_access access on sys_ucenter_member.id = access.uid") ->join("left join sys_auth_group auth on access.group_id=auth.id") ->where(['title' => ['like', '%助理%'],'sys_ucenter_member.id'=>$value['game_belong_id']]) ->select(); */ $value['total_pay_amount'] = $sum_amount; $value['game_bonus_pool'] = round(($value['pay_amount']/$sum_amount)*$value['month_bonus_pool'], 2); $value['month_award'] = round($value['game_bonus_pool']*0.7, 2); $value['assistant_month_award'] = round($value['game_bonus_pool']*0.05, 2); $value['department_award'] = round($value['game_bonus_pool']*0.25, 2); $value['person_award'] = round($value['game_bonus_pool']*0.25*$becomeTimeRate, 2); if (!$becomeTimeCount) { $value['month_award'] = 0; } $value['person_award'] = $value['quarter_award'] + $value['month_award']; $hav_value = M("business_affairs_award_pool","tab_") ->where([ 'time'=>$value['time'], 'relation_game_id'=>$value['relation_game_id'], 'game_belong_id'=>$value['game_belong_id'] ]) ->find(); if ($hav_value) { M("business_affairs_award_pool","tab_") ->where(['time'=>$value['time'], 'relation_game_id'=>$value['relation_game_id'], 'game_belong_id'=>$value['game_belong_id']]) ->save($value); } else { M("business_affairs_award_pool","tab_")->add($value); } } echo date("Y-m-d H:i:s")."----------------------商务奖金计算----------------------\n"; } //获取转正月数列表 private function getAffairList($now) { $all = 0; $affairList = M("ucenter_member") ->field("sys_ucenter_member.id admin_id,become_time") ->join("left join sys_auth_group_access access on sys_ucenter_member.id = access.uid") ->join("left join sys_auth_group auth on access.group_id=auth.id") ->where(['title' => ['like', '%商务%']]) ->select(); $return = []; foreach($affairList as $key => $value) { if ($value['become_time']) { $become_time = date("Y-m",$value['become_time']); } else { $become_time = $now; } $return[$value['admin_id']] = 0; if ($now > $become_time && $value['become_time']) { $return[$value['admin_id']] = $this->diffDate($become_time,$now); } $all += $return[$value['admin_id']]; } return [$return,$all]; } function diffDate($date1,$date2) { if(strtotime($date1)>strtotime($date2)){ $tmp=$date2; $date2=$date1; $date1=$tmp; } list($Y1,$m1,$d1)=explode('-',$date1); list($Y2,$m2,$d2)=explode('-',$date2); $y=$Y2-$Y1; $m=$m2-$m1; $d=$d2-$d1; if($d<0){ $d+=(int)date('t',strtotime("-1 month $date2")); $m--; } if($m<0){ $m+=12; $y--; } return $m+1; } // private function businessAffairRatio($amount = 0) { if (!$amount) { return 0; } $returnAmount = 0; if (!$this->affairRatioList) { $this->affairRatioList = M("business_affairs_award","tab_")->where("1=1")->group("start_amount")->select(); } foreach ($this->affairRatioList as $key => $value) { $start_amount = $value['start_amount']; $end_amount = $value['end_amount']; $ratio = $value['ratio']; if ($amount > $start_amount&&($amount<=$end_amount||floor($end_amount)=='0')) { $returnAmount += ($amount-$start_amount)*($ratio/100); } elseif($amount > $start_amount &&($amount>$end_amount&&floor($end_amount)!='0')) { $returnAmount += ($end_amount-$start_amount)*($ratio/100); } } return $returnAmount; } }