*/
class PartnerController extends ThinkController
{
private $modelName = 'Partner';
public $OpAuthList=[];
public function _initialize()
{
$this->admininfo = $_SESSION['onethink_admin']['user_auth'];
parent::_initialize();
}
//列表
public function lists()
{
$model = M($this->modelName, 'tab_');
$map = [];
$id = intval(I('id', 0));
if (!empty($id)) {
$map['tab_partner.id'] = $id;
$parameter['id'] = $id;
}
if (isset($_REQUEST['status']) && $_REQUEST['status'] !== '') {
$status = intval($_REQUEST['status']);
$map['tab_partner.status'] = $status;
$parameter['status'] = $status;
}
if (!empty(I('company_type'))) {
$map['company_type'] = I('company_type');
}
if (!empty(I('partner'))) {
$map['partner'] = ['like', '%' . I('partner') . '%'];
}
if (isset($_REQUEST['timestart']) && isset($_REQUEST['timeend'])) {
$time_start = strtotime($_REQUEST['timestart']);
$time_end = strtotime($_REQUEST['timeend'])+ 86399;
$map["_string"] = "(contract_start_time BETWEEN {$time_start} AND {$time_end}) OR (contract_end_time BETWEEN {$time_start} AND {$time_end}) OR (contract_start_time <= {$time_end} AND contract_end_time >= {$time_end}) OR (contract_start_time >0 AND contract_end_time = 0)";
} elseif (isset($_REQUEST['timestart'])) {
$time_start = strtotime($_REQUEST['timestart']);
$map["_string"] = "(contract_start_time >= {$time_start} ) OR (contract_end_time >= {$time_start}) OR ( contract_start_time >0 AND contract_end_time = 0)";
} elseif (isset($_REQUEST['timeend'])) {
$time_end = strtotime($_REQUEST['timeend'])+ 86399;
$map["_string"] = "(contract_start_time <= {$time_end} ) OR (contract_end_time <= {$time_end})";
}
$page = intval(I('get.p', 0));
$page = $page ? $page : 1; //默认显示第一页数据
$row = intval(I('row', 0));
$row = empty($row) ? 10 : $row;//每页条数
$is_export= false;
if (isset($_REQUEST['export']) && $_REQUEST['export']==1){
$is_export = true;
}
//获取分页数据
$query = $model->alias('p')
->where($map)
->order("id desc");
if($is_export){
$partners = $query
->field("p.*,IFNULL(d.advance_amount,'--') advance_amount")
->join("left join tab_partner_divide_warn d ON d.partner_id = p.id AND d.status = 1")
->select();
}else{
$partners = $query
->field("p.*")
->page($page,$row)
->select();
}
//获取公司id
$partner_ids = array_column($partners, 'id');
$partner_ids = $partner_ids ? $partner_ids : [0];
$game_map['partner_id'] = ["in",$partner_ids];
$games = M("game","tab_")->alias('g')
->field("g.game_type_name,g.relation_game_name,g.original_package_name,g.partner_id,g.relation_game_id as id")
->where($game_map)
->group('relation_game_id')
->select();
$game_ids = array_column($games, 'id');
$game_ids = $game_ids ? $game_ids : [0];
$cp_game_ratio_map['game_id'] = ["in", $game_ids];
$cp_game_ratio_map['is_del'] = '0';
//获取游戏比例
$gameradiores = M("cp_game_ratio","tab_")
->alias("cg")
->join("tab_game ga ON cg.game_id = ga.id")
->field("cg.ratio,cg.instanceof,cg.game_id,cg.begin_total_amount")
->where($cp_game_ratio_map)
->group('relation_game_id,begin_total_amount')
->select();
// dd($cp_game_ratio_map);
$parseGameRadiores = array();//合作方分成比例
foreach ($gameradiores as $key =>$val) {
$val['row'] = 1;
$val['my_ratio'] = 100 - $val['ratio'];
if ($val['begin_total_amount'] == 0) {
$val['name'] = '默认比例';
if (isset($parseGameRadiores[$val['game_id']])) {
$parseGameRadiores[$val['game_id']] = [];
}
} else {
if($val['instanceof']=='1'){
$val['name']="月流水≥".$val['begin_total_amount'];
}else{
$val['name']="月流水>".$val['begin_total_amount'];
}
}
$parseGameRadiores[$val['game_id']][] = $val;
}
$myGameRadiores = $parseGameRadiores;//我方分成比例
foreach ($myGameRadiores as $key => &$val) {
foreach ($val as $k => &$v) {
$v['ratio'] = 100 - $v['ratio'];
}
}
$parseGames = array();
foreach ($games as $key =>$val) {
$val['game_ratio'] = $parseGameRadiores[$val['id']];
$val['my_game_ratio'] = $myGameRadiores[$val['id']];
if(array_key_exists($val['id'],$parseGameRadiores)){//游戏有比例
$row2 = array_sum(array_column($val['game_ratio'], 'row')) ;
$val['row'] = empty($row2) ? 1: $row2;
} else {
$val["row"] = 1;
}
$val['original_package_name'] = $val['original_package_name'] ?: '-';
$parseGames[$val['partner_id']][] = $val;
}
$parseData = array();
foreach ($partners as $key =>$value) {
$tmpr = D("CompanyRelation")->getCompanyRelation(1,$value['id']);
$value['is_collection'] = $tmpr['collection'];
$value['settlement_type'] = $tmpr['settlement_type'];
$value['invoice_type'] = $tmpr['invoice_type'];
$value['invoice_content'] = $tmpr['invoice_content'];
$value['game'] = $parseGames[$value['id']];
$value['has_advance_charge'] = $value['has_advance_charge'] ? '是' : '否';
$value['is_sign_contract'] = $value['is_sign_contract'] ? '是' : '否';
if(empty($value['has_server_fee'])){
$value['server_fee'] = 0;
}
$value['has_server_fee'] = $value['has_server_fee'] ? '是' : '否';
$value['cooperation_status'] = $value['cooperation_status'] ? '合作中' : '暂停合作';
$value['company_type'] = $value['company_type'] == 2 ? '个人' : '公司';
$contract_start_time = $value['contract_start_time'] ? time_format($value['contract_start_time'], 'Y/m/d') : '-';
if($value['contract_start_time']){
$contract_end_time = $value['contract_end_time'] ? date('Y/m/d', $value['contract_end_time']) : '永久';
}else{
$contract_end_time = $value['contract_end_time'] ? time_format($value['contract_end_time'], 'Y/m/d') : '-';
}
$value['contract_time'] = $contract_start_time . '-' . $contract_end_time;
$validity_start_time = $value['validity_start_time'] ? time_format($value['validity_start_time'], 'Y/m/d') : '-';
if($value['validity_start_time']){
$validity_end_time = $value['validity_end_time'] ? date('Y/m/d', $value['validity_end_time']) : '永久';
}else{
$validity_end_time = $value['validity_end_time'] ? time_format($value['validity_end_time'], 'Y/m/d') : '-';
}
$value['validity_time'] = $validity_start_time . '-' . $validity_end_time;
$row2 = array_sum(array_column($value['game'], 'row')) ;
$value['row'] = empty($row2) ? 1: $row2;
$parseData[] = $value;
}
if($is_export){
$this->assign('listData', $parseData);
$GetData = $_GET;
unset($GetData['export']);
addOperationLog(['op_type'=>3,'key'=>getNowDate(),'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData),'menu'=>"游戏-游戏管理-合作方管理-导出"]);
$this->display("export");
die();
}
/* 查询记录总数 */
$count = $model
->where($map)
->count();
//分页
$parameter['p'] = $page;
$parameter['row'] = $row;
$page = set_pagination($count, $row, $parameter);
if ($page) {
$this->assign('_page', $page);
}
$this->checkListOrCountAuthRestMap($map,[]);
$this->assign('listData', $parseData);
$this->assign('count', $count);
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Partner/lists'])->find());
$this->meta_title = '上游CP公司列表';
$this->display();
}
//审核列表
public function index()
{
$page = intval(I('get.p', 0));
$page = $page ? $page : 1; //默认显示第一页数据
$row = intval(I('row', 0));
$row = empty($row) ? 10 : $row;//每页条数
$p = $_REQUEST;
if(!IS_ROOT){
$this->OpAuthList= getModuleControllerAuth();
}
$map = [];
if (!empty(I('partner_name'))) {
$map['partner_name'] = ['like', '%' . I('partner_name') . '%'];
}
if (!empty($p['verify_type'])) {
if($p['verify_type'] == 1){
$map['partner_id'] = 0;
}else{
$map['partner_id'] = ["neq",0];
}
}
if (isset($p['verify_status'])) {
$map['verify_status'] = $p['verify_status'];
}
if (isset($_REQUEST['timestart']) && isset($_REQUEST['timeend'])) {
$time_start = strtotime($_REQUEST['timestart']);
$time_end = strtotime($_REQUEST['timeend'])+ 86399;
$map["create_time"] =['BETWEEN',[$time_start, $time_end]];
} elseif (isset($_REQUEST['timestart'])) {
$time_start = strtotime($_REQUEST['timestart']);
$map["create_time"] =['EGT',$time_start];
} elseif (isset($_REQUEST['timeend'])) {
$time_end = strtotime($_REQUEST['timeend'])+ 86399;
$map["create_time"] = ['ELT',$time_end];
}
$model = M("partner_verify", 'tab_');
$data = $model->where($map)->page($page,$row)->order("FIELD(verify_status,0,1,-1),create_time desc")->select();
foreach ($data as $k => &$v) {
$v['partner_info'] = json_decode($v['partner_info'],true);
if($v['partner_id'] == 0){
$v['verify_type'] = "新增";
}else{
$v['verify_type'] = "修改";
}
$v['partner_info']['company_type'] = $v['partner_info']['company_type'] == 2 ? '个人' : '公司';
$contract_start_time = $v['partner_info']['contract_start_time'] ? time_format($v['partner_info']['contract_start_time'], 'Y/m/d') : '-';
if($v['partner_info']['contract_start_time']){
$contract_end_time = $v['partner_info']['contract_end_time'] ? date('Y/m/d', $v['partner_info']['contract_end_time']) : '永久';
}else{
$contract_end_time = $v['partner_info']['contract_end_time'] ? time_format($v['partner_info']['contract_end_time'], 'Y/m/d') : '-';
}
$v['contract_time'] = $contract_start_time . '-' . $contract_end_time;
$validity_start_time = $v['partner_info']['validity_start_time'] ?
time_format($v['partner_info']['validity_start_time'], 'Y/m/d') : '-';
if($v['partner_info']['validity_start_time']){
$validity_end_time = $v['partner_info']['validity_end_time'] ? date('Y/m/d', $v['partner_info']['validity_end_time']) : '永久';
}else{
$validity_end_time = $v['partner_info']['validity_end_time'] ? time_format($v['partner_info']['validity_end_time'], 'Y/m/d') : '-';
}
$v['validity_time'] = $validity_start_time . '-' . $validity_end_time;
$create_time = date("Y-m-d H:i:s",$v["create_time"]);
$v['verify_log'] = json_decode($v['verify_log'], true);
$v["create"]= "{$v['verify_log']['create_user']}
{$v['verify_log']['create_time']}";
if(isset($v['verify_log']['verify_user'])){
if($v['verify_status'] == -1){
$ts = "审核拒绝";
}else{
$ts = "审核通过";
}
$v["verify"]= "{$ts}({$v['verify_log']['verify_user']})
{$v['verify_log']['verify_time']}";
}else{
$v["verify"] = '--';
}
}
$count = $model->where($map)->count();
$page = set_pagination($count, $row);
if ($page) {
$this->assign('_page', $page);
}
$this->assign('menubtn',$this->menuAuth());
$this->assign('listData', $data);
$this->assign('count', $count);
$this->assign('VerifyStatus', D("PartnerVerify")->VerifyStatus);
$this->meta_title = '上游CP审核列表';
$this->display();
}
//添加
public function add()
{
$model = M("partner_verify", 'tab_');
if ($_POST) {
$save = I("post.");
$this->checkData($save);
$save['uid'] = UID;
$save['create_time'] = time();
$has_check = M("partner_verify","tab_")->where(["partner_name"=>$partner_info['partner'],"verify_status"=>0])->field("id")->find();
if ($has_check) {
$this->error('已存在同名未审核的cp,请勿重复添加');
}
$has_check = M("partner","tab_")->where(["partner_name"=>$partner_info['partner']])->field("id")->find();
if ($has_check) {
$this->error('已存在同名cp,请勿重复添加');
}
$res =D("PartnerVerify")->add_db($save);
if ($res) {
//操作日志
addOperationLog(array(
"op_type"=>0,
"key"=>$save['partner'],
"url"=>U("Partner/lists",array("partner"=>$save['partner']))
));
$this->success('保存成功,请联系管理尽快审核', U('index'));
} else {
$this->error('保存失败');
}
} else {
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Partner/add'])->find());
$this->meta_title = '新增上游CP公司';
$this->display();
}
}
//编辑
public function edit()
{
$model = M($this->modelName, 'tab_');
if ($_POST) {
$save = I("post.");
$this->checkData($save);
$res =D("PartnerVerify")->edit_db($save);
if ($res === false) {
$this->error('保存失败');
} else {
\Think\Log::actionLog('Partner/edit', 'partner', $id);
addOperationLog(array(
"op_type"=>1,
"key"=>$save['partner'],
"url"=>U("Partner/lists",array("partner"=>$save['partner']))
));
$this->success('保存成功,请联系管理尽快审核', U('index'));
}
} else {
$id = intval(I('get.id', 0));
$map['id'] = $id;
$data = $model
->find($id);
if (empty($data)) {
$this->error('数据异常', U('lists'));
}
if ($data['contract_start_time']) {
$data['contract_start_time'] = date('Y-m-d', $data['contract_start_time']);
}else{
$data['contract_start_time']='';
}
if ($data['contract_end_time']) {
$data['contract_end_time'] = date('Y-m-d', $data['contract_end_time']);
}else{
$data['contract_end_time']='';
}
if ($data['validity_start_time']) {
$data['validity_start_time'] = date('Y-m-d', $data['validity_start_time']);
}else{
$data['validity_start_time']='';
}
if ($data['validity_end_time']) {
$data['validity_end_time'] = date('Y-m-d', $data['validity_end_time']);
}else{
$data['validity_end_time']='';
}
$this->assign('data', $data);
$action = I('action');
$this->assign('action', $action);
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Partner/edit'])->find());
$this->meta_title = $action ? '查看合作方' : '编辑合作方';
$this->display($action);
}
}
//删除
public function del()
{
if (!empty($_POST['ids'])) {
if (!is_array($_POST['ids'])) {
$this->error('参数异常');
}
$id = implode(',', $_POST['ids']);
} else {
$id = intval(I('get.id', 0));
if ($id == 0) {
$this->error('参数异常');
}
}
@$yres = M($this->modelName, 'tab_')->field("partner")->find($id)['partner'];
$res = M($this->modelName, 'tab_')->delete($id);
if ($res === false) {
$this->error('删除失败');
}
addOperationLog(array(
"op_type"=>2,
"key"=>$yres,
"url"=>U("Partner/lists")
));
$this->success('删除成功', U('lists'));
}
public function verifyView()
{
if(!isset($_REQUEST['id'])) $this->error('数据异常', U('index'));
$id = $_REQUEST['id'];
$map['id'] = $id;
$dbres = M("partner_verify", 'tab_')->find($id);
$data = json_decode($dbres['partner_info'],true);
if ($data['contract_start_time']) {
$data['contract_start_time'] = date('Y-m-d', $data['contract_start_time']);
}else{
$data['contract_start_time']='';
}
if ($data['contract_end_time']) {
$data['contract_end_time'] = date('Y-m-d', $data['contract_end_time']);
}else{
$data['contract_end_time']='';
}
if ($data['validity_start_time']) {
$data['validity_start_time'] = date('Y-m-d', $data['validity_start_time']);
}else{
$data['validity_start_time']='';
}
if ($data['validity_end_time']) {
$data['validity_end_time'] = date('Y-m-d', $data['validity_end_time']);
}else{
$data['validity_end_time']='';
}
$this->assign('data', $data);
$this->assign('action', $action);
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Partner/edit'])->find());
$this->meta_title ='审核信息查看';
$this->display("show");
}
public function adminRefuse(){
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$dbres = M("partner_verify", 'tab_')->field("id,verify_status,verify_log")->where("id in ({$ids})")->select();
foreach($dbres as $k=>&$v){
if($v['verify_status'] != 0) continue;
$v['verify_log'] = json_decode($v['verify_log'],true);
$v['verify_log']['verify_user']=$this->admininfo["username"];
$v['verify_log']['verify_time']=date("Y-m-d H:i:s");
$v['verify_log'] = json_encode($v['verify_log']);
$v['verify_status']=-1;
$v["create_time"]=time();
M("partner_verify", 'tab_')->save($v);
addOperationLog(['op_type'=>1,'key'=>$v['id'],"op_name"=>"管理员审核",'url'=>U('index')]);
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"管理员审核拒绝成功"
));
}
//管理员成功
public function adminAgree(){
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$Partner = M("partner", 'tab_');
$PartnerVerify = M("partner_verify", 'tab_');
$dbres =$PartnerVerify->field("*")->where("id in ({$ids})")->select();
foreach($dbres as $k=>&$v){
if($v['verify_status'] != 0) continue;
$v['verify_log'] = json_decode($v['verify_log'],true);
$v['verify_log']['verify_user']=$this->admininfo["username"];
$v['verify_log']['verify_time']=date("Y-m-d H:i:s");
$v['verify_log'] = json_encode($v['verify_log']);
$v['verify_status']=1;
$v["create_time"]=time();
$partner_info = json_decode($v['partner_info'],true);
if($v['partner_id'] == 0){
//新增
$v['partner_id'] = $Partner->add($partner_info);
}else{
//编辑
$Partner->save($partner_info);
}
//修改自身
$PartnerVerify->save($v);
M("partner_verify", 'tab_')->save($v);
addOperationLog(['op_type'=>1,'key'=>$v['partner_id'],"op_name"=>"管理员审核",'url'=>U('index')]);
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"管理员审核通过成功"
));
}
protected function checkData(&$save)
{
$partner = trim($save['partner']);
$status = intval($save['status']);
if (empty($partner)) {
$this->error('请输入合作方名称');
}
if (!strlen(I('is_sign_contract'))) {
$this->error('请选择是否签署合同');
}
if (empty(I('company_type'))) {
$this->error('请输入公司性质');
}
if (!strlen(I('has_game_evidence'))) {
$this->error('请选择是否有游戏资质');
}
$model = M($this->modelName, 'tab_');
$map['partner'] = $partner;
$res = $model->where($map)->field("id")->find();
if ($res) {
if(isset($save['id'])){
if($save['id'] !=$res['id']){
$this->error('合作方已存在');
}
}else{
$this->error('合作方已存在');
}
}
// if(!empty(I('link_phone')) && !checkPhone(I('link_phone'))) {
// $this->error('联系电话格式不正确');
// }
// if(!empty(I('reg_phone')) && !checkPhone(I('reg_phone'))) {
// $this->error('注册电话格式不正确');
// }
if(!empty(I('taxation_rate')) && I('taxation_rate') > 0) {
if(!checkNumber(I('taxation_rate'))){
$this->error('税费费率格式错误');
}else{
$save['taxation_rate'] = ($save['taxation_rate'] ?: 0);
}
}else{
$save['taxation_rate']=0;
}
if(!empty(I('channel_rate')) && I('channel_rate') > 0) {
if(!checkNumber(I('channel_rate'))){
$this->error('渠道费率格式不对');
}else{
$save['channel_rate'] = ($save['channel_rate'] ?: 0);
}
}else{
$save['channel_rate']=0;
}
if(!empty(I('invoice_rate')) && I('invoice_rate') > 0) {
if(!checkNumber(I('invoice_rate'))){
$this->error('开票税点格式不对');
}else{
$save['invoice_rate'] = ($save['invoice_rate'] ?: 0);
}
}else{
$save['invoice_rate']=0;
}
if(!empty(I('server_fee')) && !checkNumber(I('server_fee')) && I("has_server_fee") == 1) {
$this->error('服务器费用格式不对');
}else{
if(empty(I('server_fee'))){
$save['server_fee']=0;
}else{
$save['server_fee']=I('server_fee');
}
}
$time = time();
$save['contract_start_time'] = strtotime($save['contract_start_time']);
$save['contract_end_time'] = strtotime($save['contract_end_time']);
$save['validity_start_time'] = strtotime($save['validity_start_time']);
$save['validity_end_time'] = strtotime($save['validity_end_time']);
$save['last_up_time'] = $time;
}
public function menuAuth()
{
$addurl = U("addRelation");
$mentBtn = [
"adminAgree"=>"管理员审核通过",
"adminRefuse"=>"管理员审核拒绝"
];
$resarr = [];
foreach ($mentBtn as $k => $v) {
if(IS_ROOT){
$resarr[] = $v;
}else{
if(in_array($k,$this->OpAuthList)){
$resarr[] = $v;
}
}
}
return $resarr;
}
}