优化比例获取

master
chenzhi 4 years ago
parent 97c33c8e1b
commit 7ade60fb02

@ -8,222 +8,178 @@ class CompanyGameRatioModel extends Model
{
// 数据表前缀
protected $tablePrefix = 'tab_';
/**
* 按公司获取游戏分成比例
*
* @param [type] $company_id 公司id
* @param [type] $game_ids 游戏关联id
* @param [type] $begintime 开始时间
* @param [type] $endtime 结束时间
* @param boolean $company_belong 公司内外团
* @return void
*/
public function getPromoteCompanyGameRatio($company_id,$game_ids,$begintime,$endtime,$company_belong=false){
public function getPromoteCompanyGameRatio($company_id,$game_ids,$begintime,$endtime,$company_belong=false){
//获取默认比例
if($company_belong === false){
$company_belong = M("PromoteCompany","tab_")->field("company_belong")->where("id='{$company_id}'")->find()['company_belong'];
}
$is_inside = M("PromoteCompany","tab_")->field("is_inside")->where("id='{$company_id}'")->find()['is_inside'];
$mwhere = [
"company_belong"=>$company_belong,
"relation_game_id"=>["in",$game_ids],
"_string"=>"begin_time <={$endtime} AND ( end_time = 0 OR end_time >= {$begintime})"
];
$m_res = M("GameRatioMould","tab_")->where($mwhere)->select();
$modul_ratio = [];
for ($i=0; $i < count($m_res); $i++) {
$modul_ratio[$m_res[$i]['relation_game_id']] = $m_res[$i];
}
unset($m_res);
//设置默认
$game_ratio = [];
$tmp_g = explode(",",$game_ids);
for ($i=0; $i < count($tmp_g); $i++) {
$game_ratio[$tmp_g[$i]] = [
["begintime"=>$begintime,"endtime"=>$endtime]
];
}
$map = [
"company_id"=>$company_id,
"relation_game_id"=>['in',$game_ids],
"_string"=>"begin_time <={$endtime} AND ( end_time = 0 OR end_time >= {$begintime})"
$game_id = $tmp_g[$i];
$game_ratio[$game_id] = $this->getGameRadio($company_id,$game_id,$company_belong,$begintime,$endtime);
}
return $game_ratio;
}
protected function getGameRadio($company_id,$game_id,$company_belong,$begintime,$endtime)
{
$map = [
"company_id"=>$company_id,
"relation_game_id"=>$game_id,
"_string"=>"begin_time <={$endtime} AND ( end_time = 0 OR end_time >= {$begintime})"
];
$res = $this->where($map)->order("begin_time asc")->select();
foreach($res as $k=>$v){
//获取最后一个数据
$tgr = &$game_ratio[$v['relation_game_id']];
$game_radio_data = $game_ratio[$v['relation_game_id']];
$game_key = [];
$last_time_ratio = [];
foreach ($game_radio_data as $key => $value) {
if (!isset($value['ratio'])) {
$game_key[] = $key;
$last_time_ratio[] = $value;
$res = $this->where($map)->order("begin_time asc")->select();
$data = [];
foreach ($res as $ke => $va) {
if($va['begin_time'] <= $begintime){
//
if($va['end_time'] == 0 || $va['end_time'] >= $endtime){
//全段
$tmp["begintime"] = $begintime;
$tmp["endtime"] = $endtime;
$tmp['ratio'] = $va['ratio'];
$tmp['turnover_ratio'] = $va['turnover_ratio'];
$data[] = $tmp;
break;
}
if($va['end_time'] > 0 && $va['end_time'] < $endtime){
//上分
$tmp["begintime"] = $begintime;
$tmp["endtime"] = $va['end_time'];
$tmp['ratio'] = $va['ratio'];
$tmp['turnover_ratio'] = $va['turnover_ratio'];
//插入空段
$begintime = $va['end_time']-0+1;
$data[] = $tmp;
continue;
}
}
foreach($last_time_ratio as $key => $value) {
if($v['end_time'] == 0 || ($v['end_time'] >= $value['endtime'])){
if($va['begin_time'] > $begintime){
//空段 模板比例
$this->getGameMouldRadio($data,$game_id,$company_belong,$begintime,$va['begin_time']-1);
//补充内容
if($va['end_time'] == 0 || $va['end_time'] >= $endtime){
$tmp["begintime"] = $va['begin_time'];
$tmp["endtime"] = $endtime;
$tmp['ratio'] = $va['ratio'];
$tmp['turnover_ratio'] = $va['turnover_ratio'];
$data[] = $tmp;
break;
}
if($va['end_time'] > 0 && $va['end_time'] < $endtime){
//上分
$tmp["begintime"] = $va['begin_time'];
$tmp["endtime"] = $va['end_time'];
$tmp['ratio'] = $va['ratio'];
$tmp['turnover_ratio'] = $va['turnover_ratio'];
//插入空段
$begintime = $va['end_time']-0+1;
$data[] = $tmp;
continue;
}
}
}
//获取最后一段
$end = end($data)['endtime'];
if($end !== $endtime){
//空段 模板比例
$this->getGameMouldRadio($data,$game_id,$company_belong,$end+1,$endtime);
}
foreach ($data as $ke => &$va) {
$va['begintime'] = \date("Y-m-d H:i:s", $va['begintime']);
$va['endtime'] = \date("Y-m-d H:i:s", $va['endtime']);
}
return $data;
}
//获取模板比例
protected function getGameMouldRadio(&$data,$game_id,$company_belong,$begintime,$endtime){
$where = [
"company_belong"=>$company_belong,
"relation_game_id"=>$game_id,
"_string"=>"begin_time <={$endtime} AND ( end_time = 0 OR end_time >= {$begintime})"
];
$res = M("GameRatioMould","tab_")->where($where)->order("begin_time asc")->select();
if(empty($res)){
$data[] = [
"begintime"=>$begintime,
"endtime"=>$endtime,
"ratio"=>0,
"turnover_ratio"=>''
];
return ;
}
foreach ($res as $ke => $va) {
if($va['begin_time'] <= $begintime){
//
if($va['end_time'] == 0 || $va['end_time'] >= $endtime){
//全段
if($v['begin_time'] <= $value['begintime']){
$tgr[$game_key[$key]]['ratio'] = !$is_inside?$v['ratio']:0;
$tgr[$game_key[$key]]['turnover_ratio'] = !$is_inside?$v['turnover_ratio']:[];
continue;
}elseif($v['end_time'] >= $value['begintime']){
//上分段
$tgr[$game_key[$key]]['endtime'] = $v['begin_time']-1;
$tgr[] = ['begintime'=>$v['begin_time'],"endtime"=>$value['endtime'],"ratio"=>!$is_inside?$v['ratio']:0,"turnover_ratio"=>!$is_inside?$v['turnover_ratio']:[]];
continue;
}
$tmp["begintime"] = $begintime;
$tmp["endtime"] = $endtime;
$tmp['ratio'] = $va['ratio'];
$tmp['turnover_ratio'] = $va['turnover_ratio'];
$data[] = $tmp;
return;
}
if($v['end_time'] < $value['endtime'] && $v['end_time'] != 0){
if($value['begintime'] < $v['begin_time']){
//中段
$tgr[$game_key[$key]]['endtime'] = $v['begin_time']-1;
$tgr[] = ['begintime'=>$v['begin_time'],"endtime"=>$v['end_time'],"ratio"=>!$is_inside?$v['ratio']:0,"turnover_ratio"=>!$is_inside?$v['turnover_ratio']:[]];
$tgr[] = ['begintime'=>$v['end_time']-0+1,"endtime"=>$value['endtime']];
continue;
}
if($value['begintime'] > $v['begin_time']){
//下分段
$tgr[$game_key[$key]]['begintime'] = $v['end_time']-0+1;
// $tgr[] = ['begintime'=>$v['end_time']-0+1,"endtime"=>$last_time_ratio['endtime'],"ratio"=>!$is_inside?$v['ratio']:0,"turnover_ratio"=>!$is_inside?$v['turnover_ratio']:[]];
$tgr[] = ['begintime'=>$value['begintime'],"endtime"=>$v['end_time']-0,"ratio"=>!$is_inside?$v['ratio']:0,"turnover_ratio"=>!$is_inside?$v['turnover_ratio']:[]];
continue;
}
if($value['begintime'] = $v['begin_time']){
$tgr[$game_key[$key]]['endtime'] = $v['end_time'];
$tgr[$game_key[$key]]['ratio'] = !$is_inside?$v['ratio']:0;
$tgr[$game_key[$key]]['turnover_ratio'] = !$is_inside?$v['turnover_ratio']:[];
$tgr[] = ['begintime'=>$v['end_time']-0+1,"endtime"=>$value['endtime']];
}
if($va['end_time'] > 0 && $va['end_time'] < $endtime){
//上分
$tmp["begintime"] = $begintime;
$tmp["endtime"] = $va['end_time'];
$tmp['ratio'] = $va['ratio'];
$tmp['turnover_ratio'] = $va['turnover_ratio'];
//插入空段
$begintime = $va['end_time']-0+1;
$data[] = $tmp;
continue;
}
}
// if($v['end_time'] == 0 || ($v['end_time'] >= $last_time_ratio['endtime'])){
// //全段
// if($v['begin_time'] <= $last_time_ratio['begintime']){
//
// $tgr[$game_key]['ratio'] = !$is_inside?$v['ratio']:0;
// $tgr[$game_key]['turnover_ratio'] = !$is_inside?$v['turnover_ratio']:[];
//
// continue;
// }else{
// //上分段
//
// $tgr[$game_key]['endtime'] = $v['begin_time']-1;
// $tgr[] = ['begintime'=>$v['begin_time'],"endtime"=>$last_time_ratio['endtime'],"ratio"=>!$is_inside?$v['ratio']:0,"turnover_ratio"=>!$is_inside?$v['turnover_ratio']:[]];
//
// continue;
// }
// }
// if($v['end_time'] < $last_time_ratio['endtime']){
// if($last_time_ratio['begintime'] < $v['begin_time']){
//
// //中段
// $tgr[$game_key]['endtime'] = $v['begin_time']-1;
//
// $tgr[] = ['begintime'=>$v['begin_time'],"endtime"=>$v['end_time'],"ratio"=>!$is_inside?$v['ratio']:0,"turnover_ratio"=>!$is_inside?$v['turnover_ratio']:[]];
//
// $tgr[] = ['begintime'=>$v['end_time']-0+1,"endtime"=>$last_time_ratio['endtime']];
//
// continue;
// }
//
// if($last_time_ratio['begintime'] > $v['begin_time']){
// //下分段
//
// $tgr[$game_key]['begintime'] = $v['end_time']-0+1;
//// $tgr[] = ['begintime'=>$v['end_time']-0+1,"endtime"=>$last_time_ratio['endtime'],"ratio"=>!$is_inside?$v['ratio']:0,"turnover_ratio"=>!$is_inside?$v['turnover_ratio']:[]];
// $tgr[] = ['begintime'=>$last_time_ratio['begintime'],"endtime"=>$v['end_time']-0,"ratio"=>!$is_inside?$v['ratio']:0,"turnover_ratio"=>!$is_inside?$v['turnover_ratio']:[]];
// continue;
// }
//
// if($last_time_ratio['begintime'] = $v['begin_time']){
//
// $tgr[$game_key]['endtime'] = $v['end_time'];
// $tgr[$game_key]['ratio'] = !$is_inside?$v['ratio']:0;
// $tgr[$game_key]['turnover_ratio'] = !$is_inside?$v['turnover_ratio']:[];
// $tgr[] = ['begintime'=>$v['end_time']-0+1,"endtime"=>$last_time_ratio['endtime']];
//
// }
//
// }
}
$return_data = [];
//模板填充
foreach($game_ratio as $k=>&$list){
foreach($list as $i => &$ratio){
if(!isset($ratio['ratio'])){
$mouldwhere = [
"company_belong"=>$company_belong,
"relation_game_id"=>["in",(string)$k],
"_string"=>"begin_time <={$ratio['endtime']} AND ( end_time = 0 OR end_time >= {$ratio['begintime']})"
];
$moulddata = M("GameRatioMould","tab_")->field("begin_time begintime,end_time endtime,ratio,turnover_ratio")->where($mouldwhere)->order("begin_time asc")->select();
if ($moulddata) {
foreach ($moulddata as $key => $value) {
if ($key == 0) {
$value['begintime'] = $ratio['begintime'];
}
if (!$moulddata[$key+1]) {
$value['endtime'] = $ratio['endtime'];
}
$return_data[$k][] = $value;
}
} else {
$ratio['ratio'] = 0;
$ratio['turnover_ratio'] =null;
$return_data[$k][] = $ratio;
}
// if(isset($modul_ratio[$k]['ratio'])){
// $ratio['ratio'] = !$is_inside?$modul_ratio[$k]['ratio']:0;
// $ratio['turnover_ratio'] = !$is_inside?$modul_ratio[$k]['turnover_ratio']:null;
// }else{
// $ratio['ratio'] = 0;
// $ratio['turnover_ratio'] =null;
// }
} else {
$return_data[$k][] = $ratio;
if($va['begin_time'] > $begintime){
//补充空段
$bank["begintime"] = $begintime;
$bank["endtime"] = $va['begin_time']-1;
$bank["ratio"] = 0;
$bank["turnover_ratio"] = '';
$data[] = $bank;
//补充内容
if($va['end_time'] == 0 || $va['end_time'] >= $endtime){
$tmp["begintime"] = $va['begin_time'];
$tmp["endtime"] = $endtime;
$tmp['ratio'] = $va['ratio'];
$tmp['turnover_ratio'] = $va['turnover_ratio'];
$data[] = $tmp;
break;
}
}
}
return $return_data;
}
if($va['end_time'] > 0 && $va['end_time'] < $endtime){
//上分
$tmp["begintime"] = $va['begin_time'];
$tmp["endtime"] = $va['end_time'];
$tmp['ratio'] = $va['ratio'];
$tmp['turnover_ratio'] = $va['turnover_ratio'];
//插入空段
$begintime = $va['end_time']-0+1;
$data[] = $tmp;
continue;
}
}
}
$end = end($data)['endtime'];
if($end !== $endtime){
$data[] = [
"begintime"=>$end+1,
"endtime"=>$endtime,
"ratio"=>0,
"turnover_ratio"=>''
];
}
}
}
Loading…
Cancel
Save