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.

409 lines
19 KiB
PHTML

<?php
// 游戏默认分成比例模板
namespace Admin\Controller;
class GameRatioMouldController extends AdminController
{
public $admininfo;
public $DBModel;
public function _initialize()
{
$this->admininfo = $_SESSION['onethink_admin']['user_auth'];
$this->DBModel = M("game_ratio_mould","tab_");
parent::_initialize();
}
public function index()
{
$params = I('get.');
$page = $params['p'] ? intval($params['p']) : 1;
$row = $params['row'] ? intval($params['row']) : 10;
//查询数据
//获取游戏id
$gamewhere['_string'] = '1 = 1';
$where['_string'] = '1 = 1';
if(isset($params['relation_game_id'])){
$gamewhere['relation_game_id'] = $params['relation_game_id'];
$where['m.relation_game_id'] = $params['relation_game_id'];
}
if(isset($params['game_type_id'])){
$gamewhere['game_type_id'] = $params['game_type_id'];
}
if(isset($params['company_belong'])){
$where['m.company_belong'] = $params['company_belong'];
}
$this->checkListOrCountAuthRestMap($where);
$gameres = M("game","tab_")->field("tab_game.relation_game_id,tab_game.relation_game_name,IF(tab_game.original_package_name='','未配置',tab_game.original_package_name) original_package_name,tab_game_type.type_name game_type_name")->where($gamewhere)->join("tab_game_type on tab_game.game_type_id = tab_game_type.id")->group("tab_game.relation_game_id")->select(false);
$dbres = $this->DBModel->alias('m')
->field("*,group_concat(id ORDER BY begin_time ASC SEPARATOR '|') id,group_concat(turnover_type ORDER BY begin_time ASC SEPARATOR '|') turnover_type,group_concat(ratio ORDER BY begin_time ASC SEPARATOR '|') ratio,group_concat(begin_time ORDER BY begin_time ASC SEPARATOR '|') begin_time,group_concat(end_time ORDER BY begin_time ASC SEPARATOR '|') end_time,group_concat(m.turnover_ratio ORDER BY begin_time ASC SEPARATOR '|') turnover_ratio")
->join("INNER JOIN ({$gameres}) g ON m.relation_game_id = g.relation_game_id")->where($where)->order('create_time desc, id desc');
if(isset($params['export'])){
$dbres = $dbres->group("m.relation_game_id,company_belong")->select();
}else{
$dbres = $dbres->page($page, $row)->group("m.relation_game_id,company_belong")->select();
}
foreach($dbres as $k=>&$v){
$begin_time = explode('|',$v['begin_time']);
$end_time = explode('|',$v['end_time']);
$id_data = explode('|',$v['id']);
$turnover_type = explode('|',$v['turnover_type']);
foreach ($begin_time as $tk => $tv) {
$begintime = date("Y-m-d",$tv);
$endtime = $end_time[$tk] ? date('Y-m-d', $end_time[$tk]) : '永久';
$v["valid"][$tk] = "{$begintime} ~ {$endtime}";
$v["ids"][$tk] = $id_data[$tk];
if($turnover_type[$tk] == 0) {
$v["turnover_types"][$tk] = "无";
} elseif($turnover_type[$tk] == 1) {
$v["turnover_types"][$tk] = "月流水";
} elseif($turnover_type[$tk] == 2) {
$v["turnover_types"][$tk] = "历史流水";
} elseif($turnover_type[$tk] == 3) {
$v["turnover_types"][$tk] = "cp月流水";
} elseif($turnover_type[$tk] == 4) {
$v["turnover_types"][$tk] = "cp历史流水";
}
}
$v['create_time'] = date("Y-m-d H:i:s",$v['create_time']);
$v['company_belong'] ="下游".getCompanyBlong($v['company_belong']);
$this->readTurnoverRatio($v);
}
$this->assign('data', $dbres);
//判断导出
if(isset($_REQUEST['export'])){
$GetData = $_GET;
unset($GetData['export']);
addOperationLog(['op_type'=>3,'key'=>getNowDate(),'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData),'menu'=>"推广员-推广员管理-游戏分成比例模板-导出"]);
$this->display("export");
exit();
}
// $count = $this->DBModel->alias('m')->field("count(id) count")->join("INNER JOIN ({$gameres}) g ON m.relation_game_id = g.relation_game_id")->where($where)->find()['count'];
$count = $this->DBModel->alias('m')
->field("id")
->join("INNER JOIN ({$gameres}) g ON m.relation_game_id = g.relation_game_id")->where($where)->group("m.relation_game_id,company_belong")->select(false);
$count = M()->table("({$count})a")->count();
5 years ago
$page = set_pagination($count, $row,$params);
if($page) {
$this->assign('_page', $page);
}
$this->assign('gameList', getAllGameList(true));
$this->assign('gameTypeList', getGameTypes());
$this->assign("company_belong",getCompanyBlong());
$this->display();
}
public function companyRatioTimeChange($company_belong = 0,$relation_game_id = 0, $begin_time = 0, $end_time = 0,$ratiov=[]) {
// dump($ratiov);die();
if (!$company_belong&&$company_belong!="0" || !$relation_game_id) {
$this->error("参数错误.");
}
// dump($end_time);dump($begin_time);die();
$check_data = M("game_ratio_mould","tab_")
->where(['company_belong'=>$company_belong,'relation_game_id'=>$relation_game_id,'begin_time'=>$begin_time,'end_time'=>$end_time])
->find();
if ($check_data) {
M("game_ratio_mould","tab_")
->where(['company_belong'=>$company_belong,'relation_game_id'=>$relation_game_id,'begin_time'=>$begin_time,'end_time'=>$end_time])
->save($ratiov);
return;
}
M("game_ratio_mould","tab_")
->where("company_belong={$company_belong} and relation_game_id={$relation_game_id} and begin_time>={$begin_time} and end_time <= {$end_time} and end_time!=0")
->delete();
$data = M("game_ratio_mould","tab_")
->where(['company_belong'=>$company_belong,'relation_game_id'=>$relation_game_id])
->order("begin_time DESC")
->select();
foreach($data as $key => $value) {
if ($value['begin_time'] > $begin_time) {
if (!$end_time) {
M("game_ratio_mould","tab_")
->where(['company_belong'=>$company_belong,'relation_game_id'=>$relation_game_id,'begin_time'=>$value['begin_time'],'end_time'=>$value['end_time']])
->delete();
continue;
}
if ($value['begin_time'] >= $end_time && $value['end_time']!=0) {
continue;
}
if ($end_time > $value['begin_time'] && ($end_time < $value['end_time']||$value['end_time']==0)) {
M("game_ratio_mould","tab_")
->where(['company_belong'=>$company_belong,'relation_game_id'=>$relation_game_id,'begin_time'=>$value['begin_time'],'end_time'=>$value['end_time']])
->save(['begin_time'=>$end_time+1]);
} else if ($end_time > $value['begin_time'] && ($end_time >= $value['end_time']&&$value['end_time']!=0)) {
M("game_ratio_mould","tab_")
->where(['company_belong'=>$company_belong,'relation_game_id'=>$relation_game_id,'begin_time'=>$value['begin_time'],'end_time'=>$value['end_time']])
->save(['begin_time'=>$begin_time,'end_time'=>$end_time]);
}
} else if ($value['begin_time'] <= $begin_time) {
if (!$end_time&&($value['end_time']>$begin_time||$value['end_time']==0)) {
M("game_ratio_mould","tab_")
->where(['company_belong'=>$company_belong,'relation_game_id'=>$relation_game_id,'begin_time'=>$value['begin_time'],'end_time'=>$value['end_time']])
->save(['end_time'=>$begin_time-1]);
continue;
}else if (!$end_time) {
continue;
}
if ($begin_time >= $value['end_time']&& $value['end_time']!=0) {
continue;
}
if ($begin_time > $value['begin_time'] && ($end_time >= $value['end_time'] && $value['end_time']!=0)) {
M("game_ratio_mould","tab_")
->where(['company_belong'=>$company_belong,'relation_game_id'=>$relation_game_id,'begin_time'=>$value['begin_time'],'end_time'=>$value['end_time']])
->save(['end_time'=>$begin_time-1]);
} else if ($begin_time > $value['begin_time'] && ($end_time < $value['end_time']||$value['end_time']==0)) {
M("game_ratio_mould","tab_")
->where(['company_belong'=>$company_belong,'relation_game_id'=>$relation_game_id,'begin_time'=>$value['begin_time'],'end_time'=>$value['end_time']])
->save(['end_time'=>$begin_time-1]);
$dataValue = $value;
$dataValue['begin_time'] = $end_time+1;
unset($dataValue['id']);
M("game_ratio_mould","tab_")->add($dataValue);
} else if ($begin_time == $value['begin_time'] && ($end_time < $value['end_time']||$value['end_time']==0)) {
M("game_ratio_mould","tab_")
->where(['company_belong'=>$company_belong,'relation_game_id'=>$relation_game_id,'begin_time'=>$value['begin_time'],'end_time'=>$value['end_time']])
->save(['begin_time'=>$end_time+1]);
} else if ($end_time == $value['end_time']&&$begin_time>$value['begin_time']) {
M("game_ratio_mould","tab_")
->where(['company_belong'=>$company_belong,'relation_game_id'=>$relation_game_id,'begin_time'=>$value['begin_time'],'end_time'=>$value['end_time']])
->save(['end_time'=>$begin_time-1]);
} else if ($end_time == $value['end_time']&&$begin_time == $value['begin_time']) {
M("game_ratio_mould","tab_")
->where(['company_belong'=>$company_belong,'relation_game_id'=>$relation_game_id,'begin_time'=>$value['begin_time'],'end_time'=>$value['end_time']])
->save(['begin_time'=>$begin_time,'end_time'=>$end_time]);
}
}
}
M("game_ratio_mould","tab_")->add($ratiov);
}
public function add()
{
if ($_POST) {
$params = I('post.');
if (!isset($params['ratio']) || $params['ratio'] === '') {
$this->error('默认分成比例不能为空');
}
$save['turnover_ratio'] = $this->setTurnoverRatio($params);
$save['ratio'] = $params['ratio'] ?? 0;
$save['relation_game_id'] = $params['relation_game_id'] ?? 0;
$save['company_belong'] = $params['company_belong'] ?? 3;
$save["admin_name"]=$this->admininfo["username"];
$save["admin_id"]=$this->admininfo["uid"];
$save["create_time"]=time();
$save["begin_time"] = strtotime($params["begin_time"]) ?? 0;
$save["end_time"] = $params["end_time"] ?strtotime($params["end_time"])+86399: 0;
$save["turnover_type"] = $params["turnover_type"] ?? 0;
//判断是否存在
// $where = [
// "relation_game_id"=>$save['relation_game_id'],
// "company_belong"=>$save['company_belong']
// ];
// $hasdb = $this->DBModel->field("count(id) count")->where($where)->find()['count'];
// if($hasdb > 0){
// $this->error('该游戏与该公司类型的模板已经存在,请搜索后编辑');
// }
$this->companyRatioTimeChange($save['company_belong'],$save['relation_game_id'],$save["begin_time"],$save["end_time"],$save);
//保存
// $id = $this->DBModel->add($save);
addOperationLog(['op_type'=>0,'key'=>$save['relation_game_id'],'op_name'=>'新增游戏分成比例模板','url'=>U('GameRatioMould/index')]);
$this->ajaxReturn(["msg"=>"添加成功","code"=>1,"url"=>U("index")]);
} else {
$game_type = M("game_type","tab_")->field("id,type_name")->where("status=1")->select();
$this->assign("game_type",$game_type);
//渠道类型
$this->assign("company_belong",getCompanyBlong());
$this->display();
}
}
public function edit()
{
if ($_POST) {
$params = I('post.');
if(!isset($params['id'])){
$this->error('参数错误');
}
$save['ratio'] = $params['ratio'] ?? 0;
$save['id'] = $params['id'];
$save["create_time"]=time();
$save['turnover_ratio'] = $this->setTurnoverRatio($params);
$save["turnover_type"] = $params["turnover_type"] ?? 0;
$this->DBModel->save($save);
addOperationLog(['op_type'=>1,'key'=>$save['id'],'op_name'=>'修改游戏分成比例模板','url'=>U('PromoteGameRatio/index')]);
$this->ajaxReturn(["msg"=>"修改成功","code"=>1,"url"=>U("index")]);
} else {
$params = I('get.');
$id = $params['id'] ?? 0;
$id = intval($id);
$map['id'] = $id;
$gameres = M("game","tab_")->field("tab_game.relation_game_id,tab_game.relation_game_name,IF(tab_game.original_package_name='','未配置',tab_game.original_package_name) original_package_name,tab_game_type.type_name game_type_name")->join("tab_game_type on tab_game.game_type_id = tab_game_type.id")->group("tab_game.relation_game_id")->select(false);
$dbres = $this->DBModel->alias('m')->join("INNER JOIN ({$gameres}) g ON m.relation_game_id = g.relation_game_id")->where($map)->find();
$dbres['turnover_ratio'] = $dbres['turnover_ratio'] ? json_decode($dbres['turnover_ratio'], true) : $dbres['turnover_ratio'];
$dbres['company_belong'] = getCompanyBlong($dbres['company_belong']);
$dbres['begin_time'] = date("Y-m-d",$dbres['begin_time']);
$dbres['end_time'] = $dbres['end_time']==0 ? '永久' :date("Y-m-d",$dbres['end_time']);
$this->assign('data', $dbres);
$this->display();
}
}
public function del()
{
if(!isset($_REQUEST['id'])){
$this->error('参数错误');
}
$id = $_REQUEST['id'];
$res = $this->DBModel->where("id='{$id}'")->delete();
if($res !== false){
addOperationLog(['op_type'=>2,'key'=>$id,'op_name'=>'删除游戏分成模板','url'=>U('PromoteGameRatio/index')]);
$this->ajaxReturn(["msg"=>"删除成功","code"=>1,"url"=>U("index")]);
}else{
$this->error('删除错误');
}
}
//基础信息获取
public function getGames()
{
$gameTypeId = intval(I('game_type_id', 0));
$games = M("game","tab_")->field("relation_game_id,relation_game_name,original_package_name")->where("game_type_id = '{$gameTypeId}'")->group("relation_game_name")->select();
$this->ajaxReturn($games);
}
public function error($data)
{
header('Content-Type:application/json; charset=utf-8');
$data =json_encode(['msg'=>$data,"code"=>4000],JSON_UNESCAPED_UNICODE);
exit($data);
}
public function setTurnoverRatio($params)
{
$save['turnover_ratio'] = [];
if (!empty(array_filter($params['turnover'])) || !empty(array_filter($params['turnover_ratio'])) ) {
if (is_array($params['turnover']) && is_array($params['turnover_ratio']) && is_array($params['instanceof'])) {
foreach ($params['turnover'] as $turnover) {
if (empty($turnover)) {
$this->error('月流水不能为空');
}
}
foreach ($params['turnover_ratio'] as $turnoverRatio) {
if (empty($turnoverRatio)) {
$this->error('月流水分成比例不能为空');
}
}
foreach ($params['instanceof'] as $intervalClosedStatus) {
if (!in_array($intervalClosedStatus, [1, 2])) {
$this->error('月流水分符号不能为空');
}
}
$turnoverCount = count($params['turnover']);
$sortTurnover = $params['turnover'];
sort($sortTurnover);
if ($params['turnover'] != $sortTurnover || $turnoverCount != count(array_unique($params['turnover']))) {
$this->error('月流水必须以正序的方式填写,且必须大于上一个月流水');
}
$ratio = $params['ratio'] ?? 0;
// if ($params['turnover_ratio'][0] <= $ratio) {
// $this->error('月流水分成比例必须大于默认分成比例');
// }
$turnoverRatioCount = count($params['turnover_ratio']);
$sortTurnoverRatio = $params['turnover_ratio'];
sort($sortTurnoverRatio);
// if ($params['turnover_ratio'] != $sortTurnoverRatio || $turnoverRatioCount != count(array_unique($params['turnover_ratio']))) {
// $this->error('月流水分成比例必须以正序的方式填写,且必须大于上一个月流水分成比例');
// }
foreach ($params['turnover'] as $key => $turnover) {
$save['turnover_ratio'][] = [
'turnover' => bcdiv($turnover, 1, 2),
'ratio' => bcdiv($params['turnover_ratio'][$key], 1, 2),
'instanceof' => (isset($params['instanceof'][$key]) ? $params['instanceof'][$key] : 1),
];
}
$save['turnover_ratio'] = json_encode($save['turnover_ratio']);
}
}
return $save['turnover_ratio'];
}
//设置比例阶梯
protected function readTurnoverRatio(&$v){
$radio = explode('|',$v['ratio']);
$turnover_ratio = explode('|',$v['turnover_ratio']);
$turnover_type = $v['turnover_types'];
$sec_row = 1;
foreach ($radio as $key => $value) {
$turnoverratio = json_decode($turnover_ratio[$key],true);
$v['turnover_ratios'][$key] = array(
array("ratio"=>floatval($value)."%","name"=>"默认比例")
);
foreach($turnoverratio as $ke=>$va){
$t=array("ratio"=>floatval($va["ratio"])."%");
if(array_key_exists("instanceof",$va)){
//存在
if($va['instanceof']=='1'){
$t['name']="{$turnover_type[$key]}≥".$va['turnover'];
}else{
$t['name']="{$turnover_type[$key]}".$va['turnover'];
}
}else{
$t['name']="{$turnover_type[$key]}≥".$va['turnover'];
}
$sec_row++;
$v['turnover_ratios'][$key][]=$t;
}
$v['sec_row'][$key] = $sec_row;
$sec_row = 1;
}
$v['row'] = array_sum($v['sec_row']);
}
}