<?php
namespace Admin\Model;
use Think\Model;
class CompanyGameRatioModel extends Model
{
// 数据表前缀
protected $tablePrefix = 'tab_';
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'];
//设置默认
$game_ratio = [];
$tmp_g = explode(",",$game_ids);
for ($i=0; $i < count ( $ tmp_g ) ; $ i + + ) {
$game_id = $tmp_g[$i];
if ($is_inside){
$game_ratio[$game_id] = [[
"begintime" => $begintime,
"endtime"=>$endtime,
"ratio"=>0,
"turnover_ratio"=>''
]];
}else{
$company_belong_game = M("company_belong_game","tab_")
->field("game_ids,begin_time,end_time")
->where("((begin_time< ={$begintime} and (end_time >={$begintime} or end_time=0)) or (begin_time< ={$endtime} and end_time >={$endtime}) or (end_time>={$begintime} and end_time < ={$endtime})) AND game_ids like '%,{$game_id},%'")
->order("begin_time ASC")
->select();
if ($company_belong == 1 & & !empty($company_belong_game)) {
$company_begin_time = $begintime;
$company_end_time = $endtime;
$game_ratio[$game_id] = [];
foreach($company_belong_game as $key => $value) {
if ($value['begin_time'] > $company_begin_time) {
$game_ratio[$game_id] = array_merge($game_ratio[$game_id],$this->getGameRadio($company_id,$game_id,$company_belong,$company_begin_time,$value['begin_time']-1));
if ($value['end_time'] >= $company_end_time || !$value['end_time']) {
// $game_ratio[$game_id] = array_merge($game_ratio[$game_id],[[
// "begintime" => $value['begin_time'],
// "endtime"=>$company_end_time,
// "ratio"=>0,
// "turnover_ratio"=>''
// ]]);
} else {
// $game_ratio[$game_id] = array_merge($game_ratio[$game_id],[[
// "begintime" => $value['begin_time'],
// "endtime"=>$value['end_time']+86399,
// "ratio"=>0,
// "turnover_ratio"=>''
// ]]);
if (!$company_belong_game[$key+1]['begin_time']) {
// $game_ratio[$game_id] = array_merge($game_ratio[$game_id],[[
// "begintime" => $value['end_time']+86400,
// "endtime"=>$company_end_time,
// "ratio"=>0,
// "turnover_ratio"=>''
// ]]);
$game_ratio[$game_id] = array_merge($game_ratio[$game_id],$this->getGameRadio($company_id,$game_id,$company_belong,$value['end_time']+86400,$company_end_time));
} else {
$company_begin_time = $value['end_time'];
}
}
} else {
if ($value['end_time'] >= $company_end_time || !$value['end_time']) {
// $game_ratio[$game_id] = array_merge($game_ratio[$game_id],[[
// "begintime" => $company_begin_time,
// "endtime"=>$company_end_time,
// "ratio"=>0,
// "turnover_ratio"=>''
// ]]);
} else {
// $game_ratio[$game_id] = array_merge($game_ratio[$game_id],[[
// "begintime" => $company_begin_time,
// "endtime"=>$value['end_time']+86399,
// "ratio"=>0,
// "turnover_ratio"=>''
// ]]);
if (!$company_belong_game[$key+1]['begin_time']) {
$game_ratio[$game_id] = array_merge($game_ratio[$game_id],$this->getGameRadio($company_id,$game_id,$company_belong,$value['end_time']+86400,$company_end_time));
} else {
$company_begin_time = $value['end_time'];
}
}
}
}
} else {
// if ($company_id == 12& & $game_id == 191) {
// dump(2);
// }
$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();
if (empty($res)) {
$map = [
"company_id"=>0,
"relation_game_id"=>$game_id,
"_string"=>"begin_time < ={$endtime} AND ( end_time = 0 OR end_time >= {$begintime})",
"company_belong"=>$company_belong,
];
$res = $this->where($map)->order("begin_time asc")->select();
}
$data = [];
if(empty($res)){
$this->getGameMouldRadio($data,$game_id,$company_belong,$begintime,$endtime);
return $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;
}
}
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){
//全段
$tmp["begintime"] = $begintime;
$tmp["endtime"] = $endtime;
$tmp['ratio'] = $va['ratio'];
$tmp['turnover_ratio'] = $va['turnover_ratio'];
$data[] = $tmp;
return;
}
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($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;
}
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"=>''
];
}
}
}