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.

185 lines
6.9 KiB
PHTML

<?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];
$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();
$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"=>''
];
}
}
}