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.

1832 lines
77 KiB
PHP

<?php
namespace Admin\Controller;
use Base\Tool\AggregateClient;
use mysql_xdevapi\Exception;
use User\Api\UserApi as UserApi;
use OSS\OssClient;
use OSS\Core\OSsException;
use Base\Service\PresidentDepositService;
use Base\Service\PromoteCompanyService;
use Think\Controller;
/**
* 后台首页控制器
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
class PromoteCompanyController extends ThinkController
{
private $modelName = 'PromoteCompany';
public $ratio = [];
public $aggregatePackageName = [];
public function _initialize()
{
$this->admininfo = $_SESSION['onethink_admin']['user_auth'];
parent::_initialize();
}
//列表
public function lists()
{
//设定默认时间
if(!array_key_exists("begintime",$_REQUEST) && I('type') != 2){
$this->redirect(ACTION_NAME, array('begintime' => date('Y-m-d',strtotime('-30 day')),"endtime"=>date('Y-m-d')));
}
$model = M($this->modelName, 'tab_');
$is_export= false;
if (isset($_REQUEST['export']) && $_REQUEST['export']==1){
$is_export = true;
$this->getAggregatePackage();
}
$map = [
"_string" => "1 = 1"
];
$promotemap=[
"_string" => "1 = 1"
];
$company_name = trim(I('company_name'));
//市场部权限begin
$pro_id = getMarketAuthPromoteId();
if($pro_id){
$promotemap["_string"] .=" AND p.id in ({$pro_id})";
$c_id = throwPromoteIdToCompanyId($pro_id);
$map["_string"] .=" AND id in ({$c_id})";
}
//市场部权限end
if($company_name) $map['tab_promote_company.company_name'] = array('like',"%{$company_name}%");
if (isset($_REQUEST['status']) && $_REQUEST['status'] !== '') {
$status = intval($_REQUEST['status']);
$map['tab_promote_company.status'] = $status;
}
if (isset($_REQUEST['promote_id'])) {
$map['id'] = M("Promote","tab_")->field("company_id")->where("id={$_REQUEST['promote_id']}")->find()['company_id'];
$promotemap['p.id']=$_REQUEST['promote_id'];
}
if (isset($_REQUEST['company_type'])) {
$map['company_type'] = $_REQUEST['company_type'];
}
if (isset($_REQUEST['company_belong'])) {
$map['company_belong'] = $_REQUEST['company_belong'];
}
if (isset($_REQUEST['game_name'])) {
//换成id
$g_name = $_REQUEST['game_name'];
$g_id = M("Game","tab_")->where("relation_game_name = '{$g_name}'")->field("relation_game_id")->find()['relation_game_id'];
// $reg_str = "([^0-9]|^){$g_id },";
$map["_string"] .= ' AND concat(",", game_ids, ",") like "%,' . $g_id . ',%"';
// $map["game_ids"]=["EXP","REGEXP '{$reg_str}'"];
}
if (isset($_REQUEST['is_sign_contact'])) {
$map["is_sign_contact"]=$_REQUEST['is_sign_contact'];
}
if (isset($_REQUEST['pay_type'])) {
$pay_type = $_REQUEST['pay_type'];
$c_id = M("president_deposit","tab_")->where("pay_type = '$pay_type'")->group("company_id")->getField("company_id",true);
if(empty($c_id)){
$c_id = -1;
}
$c_id = implode(",", $c_id);
$map["_string"] .= " AND id in ($c_id)";
}
// if (isset($_REQUEST['time_start']) && isset($_REQUEST['time_end'])) {
// $time_start = strtotime($_REQUEST['time_start']);
// $time_end = strtotime($_REQUEST['time_end'])+ 86399;
// $where["_string"] = "(begin_time BETWEEN {$time_start} AND {$time_end}) OR (end_time BETWEEN {$time_start} AND {$time_end}) OR (begin_time <= {$time_end} AND end_time >= {$time_end}) OR (begin_time >0 AND end_time = 0)";
// } elseif (isset($_REQUEST['time_start'])) {
// $time_start = strtotime($_REQUEST['time_start']);
// $where["_string"] = "end_time >= {$time_start} OR end_time = 0";
// } elseif (isset($_REQUEST['time_end'])) {
// $time_end = strtotime($_REQUEST['time_end'])+ 86399;
// $where["_string"] = "begin_time <= {$time_end}";
// }
// dd($map);
if (I('ali_user', 0)) {
$map['ali_user'] = I('ali_user');
}
if (I('ali_account', 0)) {
$map['ali_account'] = I('ali_account');
}
if (I('bank_address', 0)) {
$map['bank_address'] = I('bank_address');
}
if (I('settlement_contact', 0)) {
$map['settlement_contact'] = I('settlement_contact');
}
if (I('contact_phone', 0)) {
$map['contact_phone'] = I('contact_phone');
}
$page = intval(I('get.p', 0));
$page = $page ? $page : 1; //默认显示第一页数据
$row = intval(I('row', 0));
$row = empty($row) ? 10 : $row;//每页条数
//获取分页公司数据
$companyres = $model->field("*")
->where($map)
->order("last_up_time desc");
if($is_export){
$companyres = $companyres->select();
}else{
$companyres = $companyres->page($page,$row)->select();
}
$payWays = PresidentDepositService::$payWays;
$payTypes = PresidentDepositService::$payTypes;
$statusList = PresidentDepositService::$statusList;
//获取公司信息
if(!empty($companyres)){
$companyIds = array_column($companyres, 'id') ;
$depositList = M('president_deposit', 'tab_')->field(['company_id', 'pay_type', 'amount', 'status'])->where(['company_id' => ['in', $companyIds]])->select();
$depositList = index_by_column('company_id', $depositList);
foreach($companyres as $k=>&$v){
$deposit = $depositList[$v['id']] ?? null;
$tmpr = D("CompanyRelation")->getCompanyRelation(2,$v['id']);
$v["settlement_type"] = $tmpr['settlement_type'];
$v["deposit_pay_type"] = $deposit ? $payTypes[$deposit['pay_type']] : '--';
$v["deposit_status"] = !$deposit || $deposit['pay_type'] == PresidentDepositService::PAY_TYPE_NONE ? '--' : $statusList[$deposit['status']];
$v["deposit_amount"] = $deposit ? $deposit['amount'] : 0;
$v["collection"] = $tmpr['collection'];
$v["invoice_type"] = $tmpr['invoice_type'];
$v["invoice_content"] = $tmpr['invoice_content'];
if ($tmpr['is_payment']==1) {
$v['bank_address'] = $v['ali_user'];
$v['bank_card'] = $v['ali_account'];
$v['bank_name'] = '--';
}
$v['is_payment'] = ($tmpr['is_payment']==1) ? '是' : '否';
$v["company_belong"] = getCompanyBlong($v["company_belong"]);
$v["develop_type"] = getCompanyRelation($v["develop_type"]);
$v['company_type'] = $v['company_type'] == 2 ? '个人' : '公司';
$v['is_sign_contact'] = $v['is_sign_contact'] ? '是' : '否';
$contract_start_time = $v['contact_begin'] ? time_format($v['contact_begin'], 'Y/m/d') : '-';
if($v['contact_begin']){
$contract_end_time = $v['contact_end'] ? date('Y/m/d', $v['contact_end']) : '永久';
}else{
$contract_end_time = $v['contact_end'] ? time_format($v['contact_end'], 'Y/m/d') : '-';
}
$v['contract_time'] = $contract_start_time . '-' . $contract_end_time;
$validity_start_time = $v['validity_start_time'] ? time_format($v['validity_start_time'], 'Y/m/d') : '-';
if($v['validity_start_time']){
$validity_end_time = $v['validity_end_time'] ? date('Y/m/d', $v['validity_end_time']) : '永久';
}else{
$validity_end_time = $v['validity_end_time'] ? time_format($v['validity_end_time'], 'Y/m/d') : '-';
}
$v['validity_time'] = $validity_start_time . '-' . $validity_end_time;
if(!$is_export){
$v['can_del'] = $this->promoteCompanyIsCanDel($v["id"]) ? 1 : 0;
}
//遍历获取游戏信息
if(empty($v["game_ids"])){
$senddata=false;
}else{
if(isset($_REQUEST['game_name'])) {
$v["game_ids"] = $g_id;
}
$senddata = $this->getGamePromoteCompanyRadio($v["id"],$v["game_ids"],$is_export);
foreach($senddata as $key => $value) {
$senddata[$key]['ratio_data'] = array_values($senddata[$key]['ratio_data']);
}
// dump($senddata);
}
$game = $this->getPromoteGameRadio($v["id"],$senddata,$promotemap);
$v += $game;
// dump($game);
}
}
// die();
//公司及会长数量统计
$companycount = $model->field("id")->where($map)->select();
$count = count($companycount);
$companyallids = implode(',', array_column($companycount, 'id'));
$promotemap['p.company_id']=["in",$companyallids];
$promotemap['p.level']=1;
$promotecount = M("Promote","tab_")->alias('p')->where($promotemap)->count();
// dd($companyres);
$this->assign('listData', $companyres);
$this->assign('count', $count);
$this->assign('promotecount', $promotecount);
// dump($companyres);die();
if($is_export){
$getData = $_GET;
unset($getData['export']);
addOperationLog(['op_type'=>3,'key'=>getNowDate(),'op_name'=>'导出推广公司','url'=>U('PromoteCompany/lists'),'menu'=>'推广员-推广员管理-推广公司管理-导出推广公司']);
//导出
$this->display("export");
die();
}
//分页
$parameter['p'] = $page;
$parameter['row'] = $row;
$page = set_pagination($count, $row);
if ($page) {
$this->assign('_page', $page);
}
$games = M('game', 'tab_')->field('relation_game_id id,relation_game_name game_name,short')->where(['game_status'=>1,'down_port'=>1])->group("relation_game_id")->select();
$this->assign('games', $games);
//分配其他页面需要参数
$this->assign('promoteList', getPromoteByLevel(1));
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Partner/lists'])->find());
$payTypes = PresidentDepositService::$payTypes;
$this->assign('payTypes', $payTypes);
$this->meta_title = '下游推广公司';
$this->display();
}
// 批量上下架
public function displayOnShelves() {
if (IS_AJAX) {
$post = I("post.");
$game = M('game', 'tab_')->where(['id' => $post['batch_game']])->find();
$relationGames = M('game', 'tab_')->where(['relation_game_id' => $game['relation_game_id']])->select();
foreach ($relationGames as $relationGame) {
$gameId = $relationGame['id'];
$this->doDisplayOnShelves($gameId, $post['batch_company_type'], $post['batch_type']);
}
echo $this->ajaxReturn([
'code' => 1,
'msg' => '成功',
'data' => []
]);
}
}
private function doDisplayOnShelves($gameId, $batchCompanyType, $batchType) {
$where = [];
$exp = '';
if ($batchCompanyType == 1) {
$where['company_belong'] = array('exp', " IN (1,2)");
} else {
$where['company_belong'] = $batchCompanyType;
}
if ($batchType == 1) { // 批量增加
$where['_string'] = "concat(',', game_ids, ',') not like \"%,{$gameId},%\"";
$exp = "trim(BOTH ',' FROM concat(game_ids, ',', {$gameId}))";
$op_name = "批量上架";
}
if ($batchType == 2) { // 批量删除
$where['_string'] = "concat(',', game_ids, ',') like \"%,{$gameId},%\"";
$exp = "trim(BOTH ',' FROM replace(concat(',', game_ids, ','), \",{$gameId},\", ','))";
$op_name = "批量下架";
}
M('promote', 'tab_')->where($where)->save([
'game_ids' => ['exp', $exp]
]);
//加入日志
$companys = M('promote_company', 'tab_')->field("id")->where($where)->select();
if(!empty($companys)){
$company_ids = implode(",",array_column($companys,"id"));
addOperationLog(['op_type'=>1,'key'=>"","op_name"=>$op_name,"content"=>json_encode(["company_id"=>$company_ids,"batchCompanyType"=>$batchCompanyType,"game_id"=>$gameId,'batch_type'=>$batchType])]);
}
M('promote_company', 'tab_')->where($where)->save([
'game_ids' => ['exp', $exp]
]);
}
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('company_name'))) {
$map['company_name'] = ['like', '%' . I('company_name') . '%'];
}
if (!empty($p['verify_type'])) {
if($p['verify_type'] == 1){
$map['company_id'] = 0;
}else{
$map['company_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("promote_company_verify", 'tab_');
$data = $model->where($map)->page($page,$row)->order("create_time desc")->select();
foreach ($data as $k => &$v) {
$v['company_info'] = json_decode($v['company_info'],true);
if($v['company_id'] == 0){
$v['verify_type'] = "新增";
}else{
$v['verify_type'] = "修改";
}
$v['company_info']['company_type'] = $v['company_info']['company_type'] == 2 ? '个人' : '公司';
$contract_start_time = $v['company_info']['contact_begin'] ?
time_format($v['company_info']['contact_begin'], 'Y/m/d') : '-';
if($v['company_info']['contact_begin']){
$contract_end_time = $v['company_info']['contact_end'] ? date('Y/m/d', $v['company_info']['contact_end']) : '永久';
}else{
$contract_end_time = $v['company_info']['contact_end'] ? time_format($v['company_info']['contact_end'], 'Y/m/d') : '-';
}
$v['contract_time'] = $contract_start_time . '-' . $contract_end_time;
$validity_start_time = $v['company_info']['validity_start_time'] ?
time_format($v['company_info']['validity_start_time'], 'Y/m/d') : '-';
if($v['company_info']['validity_start_time']){
$validity_end_time = $v['company_info']['validity_end_time'] ? date('Y/m/d', $v['company_info']['validity_end_time']) : '永久';
}else{
$validity_end_time = $v['company_info']['validity_end_time'] ? time_format($v['company_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']} <br> {$v['verify_log']['create_time']}";
if(isset($v['verify_log']['market_user'])){
if($v['verify_status'] == -1){
$ts = "审核拒绝";
}else{
$ts = "审核通过";
}
$v["market"]= "{$ts}({$v['verify_log']['market_user']}) <br> {$v['verify_log']['market_time']}";
}else{
$v["market"] = '--';
}
if(isset($v['verify_log']['admin_user'])){
if($v['verify_status'] == -2){
$ts = "审核拒绝";
}else{
$ts = "审核通过";
}
$v["admin"]= "{$ts}({$v['verify_log']['admin_user']}) <br> {$v['verify_log']['admin_time']}";
}else{
$v["admin"] = '--';
}
}
$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("PromoteCompanyVerify")->VerifyStatus);
$this->meta_title = '下游推广公司审核列表';
$this->display();
}
public function marketAgree(){
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$records = M("PromoteCompanyVerify", 'tab_')->where("id in ({$ids})")->select();
$service = new PromoteCompanyService();
foreach($records as $record){
if($record['verify_status'] != 0) {
continue;
}
$record = $service->marketAgree($record);
$company = json_decode($record['company_info'], true);
if (in_array($company['company_belong'], [1, 2])) {
$service->adminAgree($record, true);
}
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"市场部审核通过成功"
));
}
public function marketRefuse(){
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$dbres = M("PromoteCompanyVerify", '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']['market_user']=$this->admininfo["username"];
$v['verify_log']['market_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("PromoteCompanyVerify", 'tab_')->save($v);
addOperationLog(['op_type'=>1,'key'=>$v['id'],"op_name"=>"市场部审核",'url'=>U('index')]);
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"市场部审核拒绝成功"
));
}
public function adminRefuse(){
if(!isset($_REQUEST['ids'])) $this->error("参数错误");
$ids = $_REQUEST['ids'];
$dbres = M("PromoteCompanyVerify", 'tab_')->field("id,verify_status,verify_log")->where("id in ({$ids})")->select();
foreach($dbres as $k=>&$v){
if($v['verify_status'] != 1) continue;
$v['verify_log'] = json_decode($v['verify_log'],true);
$v['verify_log']['admin_user']=$this->admininfo["username"];
$v['verify_log']['admin_time']=date("Y-m-d H:i:s");
$v['verify_log'] = json_encode($v['verify_log']);
$v['verify_status']=-2;
$v["create_time"] = time();
M("PromoteCompanyVerify", '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'];
$records = M("PromoteCompanyVerify", 'tab_')->field("*")->where("id in ({$ids})")->select();
$service = new PromoteCompanyService();
foreach($records as $record) {
$service->adminAgree($record);
}
$this->ajaxReturn(array(
'status' => 1,
"info"=>"管理员审核通过成功"
));
}
//添加
public function add()
{
if ($_POST) {
$time = time();
$save = I('post.');//前端已经验证
$save['promotes'] = json_decode($save['promotes'], true);
$save['relation']['collaborate_way'] = $save['part_a_type'];
if ($save['part_a_type'] == 1) {
$save['relation']['self_company_id'] = $save['part_b'];
}
if ($save['part_a_type'] == 2) {
$save['relation']['self_company_id'] = $save['part_a'];
}
$this->checkData($save);
$presidentDepositService = new PresidentDepositService();
$promoteCompanyService = new PromoteCompanyService();
try {
if (in_array($save['company_belong'], [1, 2])) {
$presidentDepositService->checkBeforeSave($save['deposit'] ?? []);
}
$promoteCompanyService->checkPromotes($save['promotes'] ?? []);
$promoteCompanyService->checkCompanyRelation($save['relation'] ?? []);
} catch (\Exception $e) {
$this->ajaxReturn(array(
'status' => 0,
'info' => $e->getMessage()
));
}
$save['create_time'] = $time;
$save['turnover_ratio'] = $this->setTurnoverRatio($save);
$result = D("PromoteCompanyVerify")->add_db($save);
if ($result) {
addOperationLog(['op_type'=>0,'key'=>$save['company_name'],'op_name'=>'新增推广公司','url'=>U('PromoteCompany/lists')]);
\Think\Log::actionLog('PromoteCompany/add', 'partner', $result);
if (!in_array($save['company_belong'], [1, 2])) {
$promoteCompanyService->marketAgree($result, true);
}
$array=array(
"info"=>"添加成功,请联系管理员尽快审核",
"status"=>1
);
$this->ajaxReturn($array);
} else {
$this->ajaxReturn(array(
'status' => 0,
'info' => "保存失败"
));
}
} else {
$selfCompanies = M('company_info', 'tab_')->field('id,partner company_name,link_man,link_phone')->where(['status' => 1])->select();
$this->assign('selfCompanies', $selfCompanies);
$this->assign('payWays', PresidentDepositService::$payWays);
$this->assign('payTypes', PresidentDepositService::$payTypes);
$this->assign('serviceAgreements', M('service_agreement', 'tab_')->select());
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'PromoteCompany/add'])->find());
$this->meta_title = '新增下游推广公司';
$this->display();
}
}
//查看
public function view()
{
$model = M($this->modelName, 'tab_');
$id = $_REQUEST['id'] ?: 0;
$map['id'] = $id;
$data = $model->field('*')->find($id);
if (empty($data)) {
$this->error('数据异常', U('lists'));
}
if($data['contact_begin'] >0){
$data['contact_begin']=date("Y-m-d",$data['contact_begin']);
}else{
$data['contact_begin']='';
}
if($data['contact_end'] >0){
$data['contact_end']=date("Y-m-d",$data['contact_end']);
}else{
$data['contact_end']='';
}
if($data['resources'] == ''){
$data['resources'] ="[]";
}
if($data['validity_start_time'] >0){
$data['validity_start_time']=date("Y-m-d",$data['validity_start_time']);
}else{
$data['validity_start_time']='';
}
if($data['validity_end_time'] >0){
$data['validity_end_time']=date("Y-m-d",$data['validity_end_time']);
}else{
$data['validity_end_time']='';
}
if($data['resources'] == ''){
$data['resources'] ="[]";
}
$data['turnover_ratio'] = $data['turnover_ratio'] ? json_decode($data['turnover_ratio'], true) : $data['turnover_ratio'];
$selfCompanies = M('company_info', 'tab_')->field('id,partner company_name,link_man,link_phone')->where(['status' => 1])->select();
$promotes = M('promote', 'tab_')
->field(['id', 'account', 'real_name', 'email', 'mobile_phone', 'can_view_recharge'])
->where(['company_id' => $data['id'], 'level' => 1])
->select();
$relationWhere = '(first_company_type=2 and first_company_id=' . $data['id'] .
') or (second_company_type=2 and second_company_id=' . $data['id'] . ')';
$relation = M('company_relation', 'tab_')->where($relationWhere)->find();
if ($relation) {
if ($relation['first_company_type'] == 2 && $relation['first_company_id'] == $data['id']) {
$relation['collaborate_way'] = 1;
$relation['self_company_id'] = $relation['second_company_id'];
} else {
$relation['collaborate_way'] = 2;
$relation['self_company_id'] = $relation['first_company_id'];
}
}
$deposit = M('president_deposit', 'tab_')->where(['company_id' => $data['id']])->find();
$siteConfig = $data['site_config'] == '' ? null : json_decode($data['site_config'], true);
if ($siteConfig) {
$siteConfig['logo'] = get_cover($siteConfig['logo'], 'path');
$siteConfig['icon'] = get_cover($siteConfig['icon'], 'path');
}
$this->assign('selfCompanies', $selfCompanies);
$this->assign('payWays', PresidentDepositService::$payWays);
$this->assign('payTypes', PresidentDepositService::$payTypes);
$this->assign('promotes', $promotes);
if (in_array($data['company_belong'], [1, 2])) {
$this->assign('deposit', $deposit);
}
$this->assign('relation', $relation);
$this->assign('siteConfig', $siteConfig);
$this->assign('data', $data);
$this->meta_title ='推广公司信息查看';
$this->display();
}
public function verifyView()
{
if(!isset($_REQUEST['id'])) $this->error('数据异常', U('index'));
$id = $_REQUEST['id'];
$map['id'] = $id;
$dbres = M("PromoteCompanyVerify", 'tab_')->find($id);
$data = json_decode($dbres['company_info'],true);
if($data['contact_begin'] >0){
$data['contact_begin']=date("Y-m-d",$data['contact_begin']);
}else{
$data['contact_begin']='';
}
if($data['contact_end'] >0){
$data['contact_end']=date("Y-m-d",$data['contact_end']);
}else{
$data['contact_end']='';
}
if($data['resources'] == ''){
$data['resources'] ="[]";
}
if($data['validity_start_time'] >0){
$data['validity_start_time']=date("Y-m-d",$data['validity_start_time']);
}else{
$data['validity_start_time']='';
}
if($data['validity_end_time'] >0){
$data['validity_end_time']=date("Y-m-d",$data['validity_end_time']);
}else{
$data['validity_end_time']='';
}
if($data['resources'] == ''){
$data['resources'] ="[]";
}
$data['turnover_ratio'] = $data['turnover_ratio'] ? json_decode($data['turnover_ratio'], true) : $data['turnover_ratio'];
$selfCompanies = M('company_info', 'tab_')->field('id,partner company_name,link_man,link_phone')->where(['status' => 1])->select();
$siteConfig = $data['site_config'] ?? null;
if ($siteConfig) {
$siteConfig['logo'] = get_cover($siteConfig['logo'], 'path');
$siteConfig['icon'] = get_cover($siteConfig['icon'], 'path');
}
$this->assign('selfCompanies', $selfCompanies);
$this->assign('payWays', PresidentDepositService::$payWays);
$this->assign('payTypes', PresidentDepositService::$payTypes);
$this->assign('promotes', $data['promotes'] ?? []);
if (in_array($data['company_belong'], [1, 2])) {
$this->assign('deposit', $data['deposit'] ?? null);
}
$this->assign('relation', $data['relation'] ?? null);
$this->assign('data', $data);
$this->assign('siteConfig', $siteConfig);
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Partner/edit'])->find());
$this->meta_title ='审核信息查看';
$this->display("view");
}
public function verifyDel(){
if(!isset($_REQUEST['id'])) $this->error('数据异常', U('index'));
$id = $_REQUEST['id'];
M("PromoteCompanyVerify", 'tab_')->where("id = '{$id}'")->delete();
addOperationLog(['op_type'=>2,'key'=>$id,'op_name'=>'删除审核','url'=>U('PromoteCompany/index')]);
$this->success('删除成功', U('index'));
}
public function marketingDirectorShow($ids=[], $isShow=0)
{
$upt = M('promote_company','tab_')->where(['id'=>['in', $ids]])->save(['marketing_director_show'=>$isShow]);
return $upt ? $this->success('更新成功'): $this->error('更新失败');
}
//编辑
public function edit()
{
if ($_POST) {
$save = I('post.');//前端已经验证
if ($save['part_a_type'] == 1) { // 1己方公司的时候 合作类型是2
$save['relation']['collaborate_way'] = 2;
}
if ($save['part_a_type'] == 2) { // 2下游推广公司的时候 合作类型是1
$save['relation']['collaborate_way'] = 1;
}
if ($save['part_a_type'] == 1) { // 1己方公司的时候
$save['relation']['self_company_id'] = $save['part_a'];
}
if ($save['part_a_type'] == 2) { // 2下游推广公司的时候
$save['relation']['self_company_id'] = $save['part_b'];
}
$this->checkData($save);
$promoteCompanyService = new PromoteCompanyService();
$oldData = M('promote_company', 'tab_')->field(['id', 'company_belong'])->where(['id' => $save['id']])->find();
$isNewDeposit = false;
if (!in_array($oldData['company_belong'], [1, 2]) && in_array($save['company_belong'], [1, 2])) {
$isNewDeposit = true;
}
try {
$presidentDepositService = new PresidentDepositService();
if ($isNewDeposit && in_array($save['company_belong'], [1, 2])) {
$presidentDepositService->checkBeforeSave($save['deposit'] ?? []);
}
$promoteCompanyService->checkPromotes($save['promotes'] ?? []);
$promoteCompanyService->checkCompanyRelation($save['relation'] ?? []);
} catch (\Exception $e) {
$this->ajaxReturn(array(
'status' => 0,
'info' => $e->getMessage()
));
}
$save['turnover_ratio'] = $this->setTurnoverRatio($save);
$res = D("PromoteCompanyVerify")->edit_db($save);
if ($res) {
addOperationLog(['op_type'=>1,'key'=>$_POST['company_name'],'op_name'=>'编辑推广公司','url'=>U('PromoteCompany/lists')]);
if (!in_array($save['company_belong'], [1, 2])) {
$promoteCompanyService->marketAgree($res, true);
}
$array=array(
"info"=>"修改成功,请联系管理员尽快审核",
"status"=>1
);
$this->ajaxReturn($array);
} else {
$this->ajaxReturn(array(
'status' => 0,
'info' => "修改失败"
));
}
} else {
$model = M($this->modelName, 'tab_');
$id = $_REQUEST['id'] ?: 0;
$map['id'] = $id;
$data = $model->field('*')->find($id);
if (empty($data)) {
$this->error('数据异常', U('lists'));
}
if($data['contact_begin'] >0){
$data['contact_begin']=date("Y-m-d",$data['contact_begin']);
}else{
$data['contact_begin']='';
}
if(empty($data['contact_phone'])){
$data['contact_phone'] = '';
}
if($data['contact_end'] >0){
$data['contact_end']=date("Y-m-d",$data['contact_end']);
}else{
$data['contact_end']='';
}
if($data['validity_start_time'] >0){
$data['validity_start_time']=date("Y-m-d",$data['validity_start_time']);
}else{
$data['validity_start_time']='';
}
if($data['validity_end_time'] >0){
$data['validity_end_time']=date("Y-m-d",$data['validity_end_time']);
}else{
$data['validity_end_time']='';
}
if($data['resources'] == ''){
$data['resources'] ="[]";
}
$siteConfig = null;
if ($data['site_config']) {
$siteConfig = json_decode($data['site_config'], true);
}
$data['turnover_ratio'] = $data['turnover_ratio'] ? json_decode($data['turnover_ratio'], true) : $data['turnover_ratio'];
// $data['game_ids'] = explode(',', $data['game_ids']);
$selfCompanies = M('company_info', 'tab_')->field('id,partner company_name,link_man,link_phone')->where(['status' => 1])->select();
$promotes = M('promote', 'tab_')
->field(['id', 'account', 'real_name', 'email', 'mobile_phone', 'can_view_recharge'])
->where(['company_id' => $data['id'], 'level' => 1])
->select();
$relationWhere = '(first_company_type=2 and first_company_id=' . $data['id'] .
') or (second_company_type=2 and second_company_id=' . $data['id'] . ')';
$relation = M('company_relation', 'tab_')->where($relationWhere)->find();
if ($relation) {
if ($relation['first_company_type'] == 2 && $relation['first_company_id'] == $data['id']) {
$relation['collaborate_way'] = 1;
$relation['self_company_id'] = $relation['second_company_id'];
} else {
$relation['collaborate_way'] = 2;
$relation['self_company_id'] = $relation['first_company_id'];
}
}
$deposit = M('president_deposit', 'tab_')->where(['company_id' => $data['id']])->find();
$this->assign('selfCompanies', $selfCompanies);
$this->assign('payWays', PresidentDepositService::$payWays);
$this->assign('payTypes', PresidentDepositService::$payTypes);
$this->assign('promotes', $promotes);
if (in_array($data['company_belong'], [1, 2])) {
$this->assign('deposit', $deposit);
$this->assign('needDeposit', true);
} else {
$this->assign('needDeposit', false);
}
$this->assign('relation', $relation);
$this->assign('serviceAgreements', M('service_agreement', 'tab_')->select());
$this->assign('siteConfig', $siteConfig);
$this->assign('data', $data);
$this->display();
}
}
//删除
public function del()
{
$model = M($this->modelName, 'tab_');
if (!empty($_POST['id'])) {
if (!is_array($_POST['id'])) {
$this->error('参数异常');
}
$id = $_POST['id'];
} else {
$id = intval(I('get.id', 0));
if ($id == 0) {
$this->error('参数异常');
}
}
//删除所有资源
$data = $model->field('resources')->find($id)['resources'];
$data = json_encode($data,true);
if(is_array($data) && count($data) >0){
foreach($data as $k=>$v){
unlink("./".$v['file_path']);
}
}
$companyData = M('promote_company','tab_')->field('company_name')->where(['id'=>$_REQUEST['id']])->find();
$res = $model->delete($id);
//删除对应的推广员
if ($res === false) {
$this->error('删除失败');
}
addOperationLog(['op_type'=>2,'key'=>$companyData['company_name'],'op_name'=>'删除推广公司','url'=>U('PromoteCompany/lists'),'menu'=>'推广员-推广员管理-推广公司管理-删除推广公司']);
$this->success('删除成功', U('lists'));
}
//处理上传图片
public function saveFile()
{
$path = '/Uploads/';
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 0 ;// 设置附件上传大小
$upload->exts = '';// 设置附件上传类型
$upload->rootPath = '.'.$path; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
// dump($info);
if(!$info) {// 上传错误提示错误信息
$msg = $upload->getError();
$array= array('status' => 0, 'info' => $msg);
}else{// 上传成功
$array=array(
"info"=>"上传成功",
"status"=>1,
"file_path"=>$path.$info['file']['savepath'].$info['file']['savename'],
"file_name"=>$_POST['file_name'],
"file_type"=>$_FILES['file']['type'],
"file_size"=>$_FILES['file']['size'],
"upload_time"=>date("Y-m-d H:i:s",time())
);
}
$this->ajaxReturn($array);
}
//删除图片
public function delFile()
{
$id = $_REQUEST['id'];
$file_path = $_REQUEST['file_path'] ?: false;
unlink("./".$file_path);
if($id == 0){
$this->ajaxReturn(array(
'status' => 1,
'info' => "删除成功"
));
}else{
//需要修改
$array = array(
"resources"=>json_encode($_REQUEST['resources'],JSON_UNESCAPED_UNICODE)
);
$res = M($this->modelName, 'tab_')->where("id='{$id}'")->save($array);
if($res === false){
$this->ajaxReturn(array(
'status' => 0,
'info' => "删除保存失败"
));
}else{
$this->ajaxReturn(array(
'status' => 1,
'info' => "删除成功"
));
}
}
# code...
}
public function promoteDataConfigIndex() {
if (IS_POST) {
$changeData = I('post.','');
$promoteData = M("promote","tab_")
->field("id")
->where(['company_belong'=>2])
->select();
$promoteData = implode(',',array_column($promoteData,'id'));
if ($changeData['admin_type'] == 1) {
$num_pend = 0;
$num_deposit = 0;
if ($promoteData) {
$sql_spend = "UPDATE `tab_spend` SET `old_change_promote_id`=`promote_id`,`old_change_promote_account`=`promote_account`,`promote_id`=0,`promote_account`='无' WHERE promote_id in({$promoteData})";
$sql_deposit = "UPDATE `tab_deposit` SET `old_change_promote_id`=`promote_id`,`old_change_promote_account`=`promote_account`,`promote_id`=0,`promote_account`='无' WHERE promote_id in({$promoteData})";
$model = new \Think\Model();
$num_pend = $model->execute($sql_spend,true);
$num_deposit = $model->execute($sql_deposit,true);
$this->ajaxReturn(['info'=>"处理订单数据".($num_pend+$num_deposit).'条']);
}
} else if ($changeData['admin_type'] == 2){
$num_pend = 0;
$num_deposit = 0;
if ($promoteData) {
$sql_spend = "UPDATE `tab_spend` SET `promote_id`=`old_change_promote_id`,`promote_account`=`old_change_promote_account`,`old_change_promote_id`=0,`old_change_promote_account`=''
WHERE old_change_promote_id in ({$promoteData})";
$sql_deposit = "UPDATE `tab_deposit` SET `promote_id`=`old_change_promote_id`,`promote_account`=`old_change_promote_account`,`old_change_promote_id`=0,`old_change_promote_account`=''
WHERE old_change_promote_id in ({$promoteData})";
$model = new \Think\Model();
$num_pend = $model->execute($sql_spend,true);
$num_deposit = $model->execute($sql_deposit,true);
$this->ajaxReturn(['info'=>"将数据还原,处理订单数据".($num_pend+$num_deposit).'条']);
}
}
} else {
$promoteData = M("promote","tab_")
->field("id")
->where(['company_belong'=>2])
->select();
$promoteData = implode(',',array_column($promoteData,'id'));
$spendData = M('spend','tab_')->field("old_change_promote_account")->where(['old_change_promote_id'=>['in',$promoteData]])->find();
// var_dump($spendData);die();
if ($spendData['old_change_promote_account']) {
$this->assign('old_change_promote_account',1);
}
$this->display('promotedataconfigindex');
}
}
public function serviceAgreementList($row = 10, $p = 1)
{
$map = [];
$map['_string'] = " 1=1 ";
if (isset($_GET['time-start'])) {
$map['_string'] .= " and created_time >= " . strtotime($_GET['time-start']);
}
if (isset($_GET['time-end'])) {
$map['_string'] .= " and created_time < " . (strtotime($_GET['time-end']) + 86400);
}
if (isset($_GET['type'])) {
$map['type'] = $_GET['type'];
}
if (!empty($_GET['title'])) {
$map['title'] = ['like', "%{$_GET['title']}%"];
}
$list = M('service_agreement', 'tab_')
->field("tab_service_agreement.*, group_concat(distinct(tab_promote_company.company_name)) as company_name")
->join('tab_promote_company on tab_promote_company.service_agreement_show = tab_service_agreement.id', 'LEFT')->where($map)->group('tab_service_agreement.id asc')->page($p, $row)->order("status desc")->select();
$count = M('service_agreement', 'tab_')->where($map)->count();
$page = set_pagination($count, $row);
if ($page) {
$this->assign('_page', $page);
}
$this->assign('list_data', $list);
$this->display('service_agreement_list');
}
public function serviceAgreementSave($id=0, $delete=0)
{
if (IS_POST) {
if (!empty(I('ids'))) {
$ids = is_array(I('ids')) ? I('ids') : [I('ids')];
if ($delete) {
// 获取默认合同id
$defaultService = M('service_agreement', 'tab_')->where(['status'=>2])->find();
if(in_array($defaultService['id'], $ids)) {
return $this->error('无法删除默认模板');
}
$usingService = M('promote_company', 'tab_')->where(['service_agreement_show'=>['in', $ids]])->find();
if (!empty($usingService)) return $this->error('有正在使用该服务合同的公司,无法删除');
$result = M('service_agreement', 'tab_')->where(['id'=>['in', $ids]])->delete();
return $result ? $this->success('删除完成') : $this->error('删除失败');
}
}
if (empty($_POST['name']) || empty($_POST['content'])) {
return $this->error('请填写完整内容');
}
if ($id) {
$result = M('service_agreement', 'tab_')->where(['id'=>$id])->save($_POST);
return $result ? $this->success('更新完成') : $this->error('更新失败');
} else {
$data = $_POST;
$data['creator'] = is_login();
$data['created_time'] = time();
$result = M('service_agreement', 'tab_')->add($data);
return $result ? $this->success('生成协议完成') : $this->error('生成协议失败');
}
} else {
if (!empty($id)) {
if ($delete) {
$ids = [$id];
// 获取默认合同id
$defaultService = M('service_agreement', 'tab_')->where(['status'=>2])->find();
if(in_array($defaultService['id'], $ids)) {
return $this->error('无法删除默认模板');
}
$usingService = M('promote_company', 'tab_')->where(['service_agreement_show'=>['in', $ids]])->find();
if (!empty($usingService)) return $this->error('有正在使用该服务合同的公司,无法删除');
$result = M('service_agreement', 'tab_')->where(['id'=>['in', $ids]])->delete();
return $result ? $this->success('删除完成') : $this->error('删除失败');
}
$info = M('service_agreement', 'tab_')->where(['id'=>$id])->find();
if (empty($info)) return $this->error('未找到协议');
$this->assign('info', $info);
}
$this->display('service_agreement_save');
}
}
public function serviceAgreementSetDefault($id)
{
M('service_agreement', 'tab_')->where('id > 0')->save(['status'=>1]);
M('service_agreement', 'tab_')->where(['id'=>$id])->save(['status'=>2]);
return $this->success('设置完成');
}
//
public function getCompanyGame()
{
if (IS_AJAX) {
//获取所有游戏
$game_list = M('game', 'tab_')->field('relation_game_id id,relation_game_name game_name,short')->where(['game_status'=>1,'down_port'=>1])->group("relation_game_id")->select();
$data['data']['game_list'] = empty($game_list) ? '' : $game_list;
//获取推广员信息
$promote_info = M('promote_company', 'tab_')->field('id,company_name,game_ids')->where(['id'=>I('id', 0, 'intval')])->find();
if (!empty($promote_info['game_ids'])) {
$promote_info['game_ids'] = explode(',', $promote_info['game_ids']);
}
$data['data']['promote_info'] = empty($promote_info) ? '' : $promote_info;
$data['msg'] = '请求成功';
$data['code'] = 1;
$this->ajaxReturn($data);
exit;
}
}
//更新推广员可推广游戏
public function saveCompanyGame()
{
if (IS_AJAX) {
$id = I('promote_id', 0, 'intval');
if (empty($id)) {
$this->ajaxReturn(['code'=>0,'msg'=>'请选择推广公司']);
}
$promoter = M('promote_company', 'tab_')->where(['id'=>$id])->find();
if (empty($promoter)) {
$this->ajaxReturn(['code'=>0,'msg'=>'未找到推广公司']);
}
$game_ids = I('game_ids', '');
if (!empty($game_ids)) {
$game_ids = implode(',', $game_ids);
} else {
$game_ids = '';
}
$this->changePromoteGameids($id,$game_ids);
$res = M('promote_company', 'tab_')->where(['id'=>$id])->save(['game_ids'=>$game_ids]);
if ($res) {
$this->ajaxReturn(['code'=>1,'msg'=>'更新成功']);
} else {
$this->ajaxReturn(['code'=>0,'msg'=>'数据未发生变化']);
}
}
}
//
protected 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),
];
}
}
}
return json_encode($save['turnover_ratio']);
}
protected function checkData(&$save)
{
$company_name = trim($save["company_name"]);
$status = intval($save['status']);
if (empty($company_name)) {
$this->error('请输入推广公司名称');
}
$model = M("promote_company", 'tab_');
$map['company_name'] = $company_name;
$res = $model->where($map)->field("id")->find();
if ($res) {
if(isset($save['id'])){
if($save['id'] !=$res['id']){
$this->error('推广公司已存在');
}
}else{
$this->error('推广公司已存在');
}
}
if(!isset($save['id'])){
$map['company_id'] = 0;
$res = M("PromoteCompanyVerify","tab_")->where($map)->field("id")->find();
if($res){
$this->error('已添加过此推广公司,请联系管理尽快审核');
}
}
if (isset($save['is_site_custom']) && $save['is_site_custom'] == 1) {
if ($save['site_domain_prefix'] == '') {
$this->error('请输入推广后台域名前缀');
}
if (strlen($save['site_domain_prefix']) > 6) {
$this->error('域名前缀不能超过6个字符');
}
if (!preg_match('/^[a-z0-9]+$/', $save['site_domain_prefix'])) {
$this->error('域名前缀只能由小写字母和数字组成');
}
if ($save['site_config']['icon'] == '') {
$this->error('请上传推广后台ICON');
}
if ($save['site_config']['logo'] == '') {
$this->error('请上传推广后台logo');
}
/**
* @todo 暂未检查待审核的推广公司是否含有重复域名前缀
*/
$domainExistMap = ['site_domain_prefix' => $save['site_domain_prefix']];
if (!empty($save['id'])) {
$domainExistMap['id'] = ['neq', $save['id']];
}
$domainCompany = M('promote_company', 'tab_')->field(['id'])->where($domainExistMap)->find();
if ($domainCompany) {
$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('fax_ratio')) && I('fax_ratio') > 0) {
if(!checkNumber(I('fax_ratio'))){
$this->error('开票税点格式不对');
}else{
$save['fax_ratio'] = ($save['fax_ratio'] ?: 0);
}
}else{
$save['fax_ratio']=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['contact_begin'] = strtotime($save['contact_begin']);
$save['contact_end'] = strtotime($save['contact_end']);
$save['validity_start_time'] = strtotime($save['validity_start_time']);
$save['validity_end_time'] = strtotime($save['validity_end_time']);
$save['last_up_time'] = $time;
}
protected function changePromoteGameids($company_id,$game_ids){
$old_info = M("PromoteCompany","tab_")->field("company_belong,develop_type,game_ids")->where("id = {$company_id}")->find();
$is_change_game = false;
$Promote = M("Promote", "tab_");
if($old_info['game_ids'] != $game_ids){
//操作日志
$this->addCompanyGameChangeLog($company_id,$old_info['game_ids'],$game_ids);
if($old_info['company_belong'] == 1 || $old_info['company_belong'] == 2){
//外团跟随公司
$new_game_ids = D("Game")->changeRelationGameidToGameid($game_ids,true);
$new_game_ids = implode(',',$new_game_ids);
$save = [
"game_ids"=>$new_game_ids
];
$dbres = $Promote->where("company_id = '{$company_id}'")->save($save);
}else{
$oids = explode(',', $old_info['game_ids']);
$nids = explode(',',$game_ids);
$diff_ids = [];
if(empty($old_info['game_ids'])){
$is_change_game = false;
}elseif(empty($game_ids)){
$is_change_game = true;
$diff_ids = $oids;
}else{
$is_change_game = true;
foreach ($oids as $k => $v) {
if(!in_array($v,$nids)){
$diff_ids[] = $v;
}
}
}
if(count($diff_ids) > 0){
$diff_ids = D("Game")->changeRelationGameidToGameid(implode(',',$diff_ids),true);
}
if($is_change_game){
$dbres = $Promote->field("id,game_ids")->where("company_id = '{$company_id}'")->select();
foreach ($dbres as $k=>&$v) {
$temp_ids = explode(',', $v['game_ids']);
$v['game_ids'] = implode(',', array_diff($temp_ids, $diff_ids));
$Promote->save($v);
}
}
}
}
}
protected function addCompanyGameChangeLog($company_id,$old_game_ids,$new_game_ids){
$old_game_ids = explode(',',$old_game_ids);
$new_game_ids = explode(',',$new_game_ids);
$addGameid = array_diff($new_game_ids,$old_game_ids);
$removeGameid = array_diff($old_game_ids,$new_game_ids);
if(!empty($addGameid)){
$gameid = implode(',',$addGameid);
addOperationLog(['op_type'=>1,'key'=>"addCompanyGame","op_name"=>"新增推广游戏","content"=>json_encode(["company_id"=>$company_id,"relation_game_id"=>$gameid])]);
}
if(!empty($removeGameid)){
$gameid = implode(',',$removeGameid);
addOperationLog(['op_type'=>1,'key'=>"removeCompanyGame","op_name"=>"移除推广游戏","content"=>json_encode(["company_id"=>$company_id,"relation_game_id"=>$gameid])]);
}
}
/**
* 由于修改公司信息引起的会长信息修改
* @param [type] $save
* @return void
*/
protected function changePromote(&$save){
$is_change_belong = false;
$is_change_relation =false;
if(empty($save['id'])){
return false;
}
$company_id = $save['id'];
$old_info = M("PromoteCompany","tab_")->field("company_belong,develop_type,game_ids")->where("id = {$save['id']}")->find();
if(isset($save['company_belong']) && ($old_info['company_belong'] != $save['company_belong'])){
$is_change_belong = true;
}
if(isset($save['develop_type']) && ($old_info['develop_type'] != $save['develop_type'])){
$is_change_relation = true;
}
//处理底下所有会长的公会属性
if($is_change_belong || $is_change_relation ){
$savedata = array(
"company_belong"=>$save['company_belong'],
"company_relation"=>$save['develop_type']
);
M("Promote","tab_")->where("company_id = '{$company_id}'")->save($savedata);
// 权限跟随
if($is_change_belong){
$authGroup = M('auth_group',"sys_")->where(['title'=>'市场总监'])->find();
$subPromoteIds = M('promote', 'tab_')->where(['level' => 1, 'company_id' => $company_id])->getField('id', true);
$dataPresident = explode(',', $authGroup['data_president']);
if ($save['company_belong'] == 1 || $save['company_belong'] == 2) {
$dataPresident = array_unique(array_merge($dataPresident, $subPromoteIds));
} else {
$dataPresident = array_diff($dataPresident, $subPromoteIds);
}
M('auth_group',"sys_")->where(['title'=>'市场总监'])->save(['data_president'=>implode(',', $dataPresident)]);
resetUserAuth();
}
}
}
protected function menuAuth()
{
$addurl = U("addRelation");
$mentBtn = [
"marketAgree"=>"<a class='butn' id='marketAgree'>市场部审批通过</a>",
"marketRefuse"=>"<a class='butn' id='marketRefuse' style='background-color: red;'>市场部审核拒绝</a>",
"adminAgree"=>"<a class='butn' id='adminAgree'>管理员审核通过</a>",
"adminRefuse"=>"<a class='butn' id='adminRefuse' style='background-color: red;'>管理员审核拒绝</a>"
];
$resarr = [];
foreach ($mentBtn as $k => $v) {
if(IS_ROOT){
$resarr[] = $v;
}else{
if(in_array($k,$this->OpAuthList)){
$resarr[] = $v;
}
}
}
return $resarr;
}
/**
* 返回公司的游戏分成比例
*
* @param [type] $company_id
* @param [type] $game_ids
* @param boolean $is_export
* @return void
*/
protected function getGamePromoteCompanyRadio($company_id,$game_ids,$is_export = false){
$is_inside = M("PromoteCompany","tab_")->field("is_inside")->where("id='{$company_id}'")->find()['is_inside'];
//判断是否有生效
$senddata = [];
$game = $this->getGameInfo($game_ids);
//获取cp分成比例
if($is_export){
$CpRadio = $this->getPromoteCompanyCpRadio($game_ids);
}
// dd($CpRadio);
if (isset($_REQUEST['begintime']) && isset($_REQUEST['endtime'])) {
$time_start = strtotime($_REQUEST['begintime']);
$time_end = strtotime($_REQUEST['endtime'])+ 86399;
$rwhere["_string"] = "(begin_time BETWEEN {$time_start} AND {$time_end}) OR (end_time BETWEEN {$time_start} AND {$time_end}) OR (begin_time <= {$time_end} AND end_time >= {$time_end}) OR (begin_time <{$time_start} AND (end_time >{$time_end} or end_time = 0))";
$mwhere['_string'] = "(begin_time BETWEEN {$time_start} AND {$time_end}) OR (end_time BETWEEN {$time_start} AND {$time_end}) OR (begin_time <= {$time_end} AND end_time >= {$time_end}) OR (begin_time <{$time_start} AND (end_time >{$time_end} or end_time = 0))";
} elseif (isset($_REQUEST['begintime'])) {
$time_start = strtotime($_REQUEST['begintime']);
$rwhere["_string"] = "end_time >= {$time_start} OR end_time = 0";
$mwhere["_string"] = "end_time >= {$time_start} OR end_time = 0";
} elseif (isset($_REQUEST['time_end'])) {
$time_end = strtotime($_REQUEST['endtime'])+ 86399;
$rwhere["_string"] = "begin_time <= {$time_end}";
$mwhere["_string"] = "begin_time <= {$time_end}";
}
$company_belong = M("PromoteCompany","tab_")->field("company_belong")->where("id='{$company_id}'")->find()['company_belong'];
$rwhere['company_id'] = $company_id;
$rwhere['relation_game_id'] = ['in',$game_ids];
// $rwhere['company_belong'] = $company_belong;
// dump($rwhere);
// $endtime = time();
// $rwhere = "(end_time = 0 OR end_time >= '{$endtime}') and company_id='{$company_id}' and relation_game_id in ({$game_ids})";
$cgr_res = M("CompanyGameRatio","tab_")->where($rwhere)->select();
//判断哪些游戏为空
$game_arr = array_flip(explode(",",$game_ids));
if(!empty($cgr_res)){
foreach ($cgr_res as $k => $v) {
$t_gid = $v['relation_game_id'];
unset($game_arr[$t_gid]);
$senddata[$t_gid]['ratio_data'][$v['begin_time']] = ["turnover_ratio"=>$v['turnover_ratio'],"ratio"=>!$is_inside?$v['ratio']:0,"relation_game_id"=>$v['relation_game_id'],'time'=>(($v['begin_time']<strtotime($_REQUEST['begintime'])) ? date("Y.m.d",strtotime($_REQUEST['begintime'])) : date("Y.m.d",$v['begin_time']))."-".(($v['end_time']&&$v['end_time']>strtotime($_REQUEST['endtime']))?date("Y.m.d",$v['end_time']):date("Y.m.d",strtotime($_REQUEST['endtime'])))];
$senddata[$t_gid] += $game[$t_gid];
$this->setPromoteRadio($senddata[$t_gid]['ratio_data'][$v['begin_time']],$is_inside);
$senddata[$t_gid]['row'] += $senddata[$t_gid]['ratio_data'][$v['begin_time']]['row'];
if($is_export){
if(isset($CpRadio[$t_gid])){
$senddata[$t_gid]["cp_ratio"] = $CpRadio[$t_gid];
if( count($CpRadio[$t_gid]) >= $senddata[$t_gid]['row'] ){
$c = count($CpRadio[$t_gid]) - $senddata[$t_gid]['row'];
for ($x=0; $x<$c; $x++) {
$senddata[$t_gid]['ratio_data'][$v['begin_time']]['turnover_ratio'][]=["name"=>"--","ratio"=>"--"];
$senddata[$t_gid]['ratio_data'][$v['begin_time']]['row'] += 1;
}
$senddata[$t_gid]['row'] = count($CpRadio[$t_gid]);
}else{
$c = $senddata[$t_gid]['row'] - count($CpRadio[$t_gid]);
for ($x=0; $x<$c; $x++) {
$senddata[$t_gid]['cp_ratio'][]=["name"=>"--","ratio"=>"--"];
}
}
}
}
}
}
if(empty($game_arr)){
return $senddata;
}
$diff_ids = implode(",",array_keys($game_arr));
// $mwhere = "(end_time = 0 OR end_time >= '{$endtime}') and company_belong='{$company_belong}' and relation_game_id in ({$diff_ids})";
$mwhere['company_belong'] = $company_belong;
$mwhere['relation_game_id'] = ['in',$diff_ids];
$m_res = M("GameRatioMould","tab_")->where($mwhere)->select();
// dump($m_res);die();
if(!empty($m_res)){
foreach ($m_res as $k => $v) {
$t_gid = $v['relation_game_id'];
unset($game_arr[$t_gid]);
$senddata[$t_gid]['ratio_data'][$v['begin_time']] = ["turnover_ratio"=>$v['turnover_ratio'],"ratio"=>!$is_inside?$v['ratio']:0,"relation_game_id"=>$v['relation_game_id'],'time'=>(($v['begin_time']<strtotime($_REQUEST['begintime'])) ? date("Y.m.d",strtotime($_REQUEST['begintime'])) : date("Y.m.d",$v['begin_time']))."-".(($v['end_time']&&$v['end_time']>strtotime($_REQUEST['endtime']))?date("Y.m.d",$v['end_time']):date("Y.m.d",strtotime($_REQUEST['endtime'])))];
$senddata[$t_gid] += $game[$t_gid];
$this->setPromoteRadio($senddata[$t_gid]['ratio_data'][$v['begin_time']],$is_inside);
$senddata[$t_gid]['row'] += $senddata[$t_gid]['ratio_data'][$v['begin_time']]['row'];
if($is_export){
if(isset($CpRadio[$t_gid])){
$senddata[$t_gid]["cp_ratio"] = $CpRadio[$t_gid];
if( count($CpRadio[$t_gid]) >= $senddata[$t_gid]['row'] ){
$c = count($CpRadio[$t_gid]) - $senddata[$t_gid]['row'];
for ($x=0; $x<$c; $x++) {
$senddata[$t_gid]['ratio_data'][$v['begin_time']]['turnover_ratio'][]=["name"=>"--","ratio"=>"--"];
$senddata[$t_gid]['ratio_data'][$v['begin_time']]['row'] += 1;
}
$senddata[$t_gid]['row'] = count($CpRadio[$t_gid]);
}else{
$c = $senddata[$t_gid]['row'] - count($CpRadio[$t_gid]);
for ($x=0; $x<$c; $x++) {
$senddata[$t_gid]['cp_ratio'][]=["name"=>"--","ratio"=>"--"];
}
}
}
}
}
}
if(empty($game_arr)){
return $senddata;
}
foreach($game_arr as $k=>$v){
if(empty($game[$k])) continue;
$senddata[$k]['ratio_data']['1567267200']= ["turnover_ratio"=>[["name"=>"--","ratio"=>"--"]],"ratio"=>0,"relation_game_id"=>$k,"cp_ratio"=>[["name"=>"--","ratio"=>"--"]],'time'=>date("Y.m.d",strtotime($_REQUEST['begintime']))."-".date("Y.m.d",strtotime($_REQUEST['endtime']))];
$senddata[$k]["row"] = 1;
$senddata[$k] += $game[$k];
}
return $senddata;
}
protected function getPromoteGameRadio($company_id,$ratio,$p_map = false){
//获取会长信息
$rdata = [];
$promotemap['p.company_id']=$company_id;
$promotemap['p.level'] = 1;
if($p_map){
$promotemap += $p_map;//外界条件
}
$company_row = 0;
$promoteres = M("Promote","tab_")
->alias('p')
->field("p.id,p.company_id,p.account,p.game_ids")
->where($promotemap)
->select();
// dd($promoteres);
$adminList = getMarketAdminsByPromoteIds(array_column($promoteres, 'id'));
if(empty($promoteres)){
//无会长
return ["list"=>["account" => "--","list" => ["turnover_ratio"=>[["name"=>"--","ratio"=>"--"]],"ratio"=>0,"relation_game_id"=>$k,"cp_ratio"=>[["name"=>"--","ratio"=>"--"]],"row"=>1,"original_package_name"=>"--","relation_game_name"=>"--","game_type_name"=>"--"]],"row"=>1];
}
foreach ($promoteres as $k => &$v) {
$admin = isset($adminList[$v['id']]) && $adminList[$v['id']]['admin'] ? $adminList[$v['id']]['admin'] : null;
$v['market_admin_username'] = $admin ? ($admin['real_name'] ? $admin['real_name'] : $admin['username']) : '无';
if($ratio == false || empty($v['game_ids'])){
$v['row']=1;
$v['list'] = ["turnover_ratio"=>[["name"=>"--","ratio"=>"--"]],"ratio"=>0,"relation_game_id"=>$k,"cp_ratio"=>[["name"=>"--","ratio"=>"--"]],"row"=>1,"original_package_name"=>"--","relation_game_name"=>"--","game_type_name"=>"--"];
$rdata[] = $v;
$company_row += $v['row'];
}else{
$v['list'] = [];
$v['row']=0;
$v['game_ids'] = D("Game")->changeGameidToRelationGameid($v['game_ids']);
$game_arr = array_flip(explode(",",$v['game_ids']));
foreach($game_arr as $ke=>$va){
if(isset($ratio[$ke])){
$v['list'][] = $ratio[$ke];
$v['row'] += $ratio[$ke]["row"];
}
}
if($v['row'] == 0) $v['row']=1;
$rdata[] = $v;
$company_row += $v['row'];
}
// dd($v);
}
return ["list"=>$rdata,"row"=>$company_row];
}
//获取cp分成比例
protected function getPromoteCompanyCpRadio($game_ids)
{
$cp_game_ratio_map['game_id'] = ["in", $game_ids];
$cp_game_ratio_map['is_del'] = '0';
$gameradiores = M("cp_game_ratio","tab_")
->alias("cg")
->field("cg.ratio,cg.instanceof,cg.game_id,cg.begin_total_amount")
->where($cp_game_ratio_map)
->group('game_id,begin_total_amount')
->order("begin_total_amount asc")
->select();
$parseGameRadiores = array();//合作方分成比例
foreach ($gameradiores as $key =>$val) {
$val['row'] = 1;
if ($val['begin_total_amount'] == 0) {
$val['name'] = "0.00≤月流水≤";
// $val['name'] = '默认比例';
if (isset($parseGameRadiores[$val['game_id']])) {
$parseGameRadiores[$val['game_id']] = [];
}
// if ($val['game_id']==197) {
// dump($gameradiores[$key+1]['begin_total_amount']);
// }
} else {
// if ($val['game_id']==197) {
// dump($gameradiores[$key+1]['begin_total_amount']);
// }
// if($val['instanceof']=='1'){
// $val['name']="月流水≥".$val['begin_total_amount'];
// }else{
// $val['name']="月流水>".$val['begin_total_amount'];
// }
if($val['instanceof']=='1'){
if ($gameradiores[$key+1]['begin_total_amount']) {
$val['name']=$val['begin_total_amount']."≤月流水<".$gameradiores[$key+1]['begin_total_amount'];
} else {
$val['name']=$val['begin_total_amount']."≤月流水≤∞";
}
// $val['name']="月流水≥".$val['begin_total_amount'];
}else{
if($gameradiores[$key+1]['begin_total_amount']!=0) {
if ($gameradiores[$key+1]['instanceof'] == '1') {
$val['name']=$val['begin_total_amount']."<月流水<".$gameradiores[$key+1]['begin_total_amount'];
} else {
$val['name']=$val['begin_total_amount']."≤月流水<".$gameradiores[$key+1]['begin_total_amount'];
}
} else {
if ($gameradiores[$key+1]['instanceof'] == '1') {
$val['name']=$val['begin_total_amount']."<月流水≤∞";
} else {
$val['name']=$val['begin_total_amount']."≤月流水≤∞";
}
}
// $val['name']="月流水>".$val['begin_total_amount'];
}
}
$parseGameRadiores[$val['game_id']][] =['name'=>$val['name'],"ratio"=>floatval($val['ratio'])];
}
foreach ($parseGameRadiores as $key => $value) {
foreach ($value as $k => $v) {
if ($value[$k+1] && $k==0) {
$parseGameRadiores[$key][$k]['name'] .= number_format(substr($value[$k+1]['name'],0,strpos($value[$k+1]['name'], '.')),'2','.','');
} elseif($k==0) {
$parseGameRadiores[$key][$k]['name'] .= '∞';
}
}
}
return $parseGameRadiores;
}
//设置比例阶梯
protected function setPromoteRadio(&$v,$is_inside=0){
$turnover_ratio = json_decode($v['turnover_ratio'],true);
if (!$turnover_ratio) {
$rate_detail = "0≤月流水≤∞";
} else {
$rate_detail = "0≤月流水<{$turnover_ratio[0]['turnover']}";
}
$v['turnover_ratio'] = array(
array("ratio"=>floatval(!$is_inside?$v['ratio']:0)."%","name"=>$rate_detail)
);
foreach($turnover_ratio as $ke=>$va){
$t=array("ratio"=>floatval(!$is_inside?$va["ratio"]:0)."%");
if(array_key_exists("instanceof",$va)){
//存在
if($va['instanceof']=='1'){
if ($turnover_ratio[$ke+1]['turnover']) {
$t['name']=$va['turnover']."≤月流水<".$turnover_ratio[$ke+1]['turnover'];
} else {
$t['name']=$va['turnover']."≤月流水≤∞";
}
}else{
if($turnover_ratio[$ke+1]['turnover']) {
$t['name']=$va['turnover']."<月流水<".$turnover_ratio[$ke+1]['turnover'];
} else {
$t['name']=$va['turnover']."<月流水≤∞";
}
}
}else{
// $t['name']="月流水≥".$va['turnover'];
if ($turnover_ratio[$ke+1]['turnover']) {
$t['name']=$va['turnover']."≤月流水<".$turnover_ratio[$ke+1]['turnover'];
} else {
$t['name']=$va['turnover']."≤月流水<∞";
}
}
$v['turnover_ratio'][]=$t;
}
$v['row'] = count($v['turnover_ratio']);
}
//获取游戏基础信息
protected function getGameInfo($game_ids){
$gameinfo = [];
$dbres = M("Game","tab_")->where("relation_game_id in ($game_ids)")->field("relation_game_id,relation_game_name,game_type_name,original_package_name,unique_code")->group("relation_game_id")->select();
foreach($dbres as $k=>$v){
$v['aggregate_gamename'] = $this->aggregatePackageName[$v['unique_code']];
$gameinfo[$v['relation_game_id']] = $v;
}
return $gameinfo;
}
//判断公司是否能被删除
protected function promoteCompanyIsCanDel($company_id){
$r = M("Promote","tab_")->where("company_id={$company_id}")->count();
if($r > 0){
return false;
}else{
return true;
}
}
protected function getAggregatePackage() {
$client = new AggregateClient();
$result = $client->api('aggregate-gamename', []);
$this->aggregatePackageName = $result['data'];
}
}