<?php
namespace Admin\Controller;
use mysql_xdevapi\Exception;
use User\Api\UserApi as UserApi;
use OSS\OssClient;
use OSS\Core\OSsException;
use Think\Controller;
/**
* 后台首页控制器
* @author 麦当苗儿 < zuojiazi @ vip . qq . com >
*/
class PromoteCompanyController extends ThinkController
{
private $modelName = 'PromoteCompany';
//列表
public function lists()
{
$model = M($this->modelName, 'tab_');
$is_export= false;
if (isset($_REQUEST['export']) & & $_REQUEST['export']==1){
$is_export = true;
}
$map = [];
$promotemap=[];
$company_name = trim(I('company_name'));
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;
$parameter['status'] = $status;
}
if (isset($_REQUEST['promote_id'])) {
$map['id'] = M("Promote","tab_")->field("company_id")->where("id={$_REQUEST['promote_id']}")->find()['company_id'];
$promotemap['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'];
}
$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();
}
if(1 == 0){
//获取公司id
$companyids = implode(',', array_column($companyres, 'id'));
//获取所属会长及其游戏分成
$promotemap['company_id']=["in",$companyids];
$promotemap['level'] = 1;
$promoteres = M("Promote","tab_")
->field("id,company_id,account,settlement_type")
->where($promotemap)
->select();
//获取会长id
$promoteids = implode(',', array_column($promoteres, 'id'));
//获取游戏比例
$gameradiores = M("PromoteGameRatio","tab_")
->alias("pg")
->field("pg.ratio,pg.relation_game_id,pg.turnover_ratio,pg.game_id,pg.promote_id,ga.relation_game_name,ga.game_type_name")
->join("tab_game ga ON pg.game_id = ga.id")
->where(array(
"status"=>1,
"promote_id"=>["in",$promoteids]
))
->group("relation_game_id,promote_id")
->select();
//比例数据处理
$gameradiotemp = array();
foreach($gameradiores as $k=>$v){
$turnover_ratio = json_decode($v['turnover_ratio'],true);
$v['turnover_ratio'] = array(
array("ratio"=>$v['ratio']."%","name"=>"默认比例")
);
foreach($turnover_ratio as $ke=>$va){
$t=array("ratio"=>$va["ratio"]."%");
if(array_key_exists("instanceof",$va)){
//存在
if($va['instanceof']=='1'){
$t['name']="月流水≥".$va['turnover'];
}else{
$t['name']="月流水>".$va['turnover'];
}
}else{
$t['name']="月流水≥".$va['turnover'];
}
$v['turnover_ratio'][]=$t;
}
$v['row'] = count($v['turnover_ratio']);
unset($v["ratio"]);
unset($v["relation_game_id"]);
unset($v["game_id"]);
$gameradiotemp[$v['promote_id']][]=$v;
}
unset($gameradiores);
//会长数据处理
$promoterestemp=[];
foreach($promoteres as $k=>$v){
if(array_key_exists($v['id'],$gameradiotemp)){
//存在
$v['list'] = $gameradiotemp[$v['id']];
foreach($gameradiotemp[$v['id']] as $ke=>$va){
$v['row'] += $va['row'];
}
}else{
$v['list']=[
["relation_game_name"=>"--","game_type_name"=>"--",
"turnover_ratio"=>[
["name"=>"--","ratio"=>"--"]
],
"row"=>1
]
];
$v["row"]=1;
}
if($v['settlement_type'] == 1){
$v['settlement_type']="周结";
}else{
$v['settlement_type']="月结";
}
$promoterestemp[$v['company_id']]["list"][]=$v;
}
unset($promoteres);
}
//处理公司数据
foreach($companyres as $k=>& $v){
// if(array_key_exists($v['id'],$promoterestemp)){
// //存在
// $v['list'] = $promoterestemp[$v['id']]['list'];
// foreach($promoterestemp[$v['id']] as $ke=>$va){
// foreach($va as $key=>$val){
// $v['row'] += $val['row'];
// }
// }
// $v['can_del'] = 0;
// }else{
// //空值
// $v['can_del'] = 1;
// $v['list']=[
// [
// "account"=>"--",
// "settlement_type"=>"--",
// 'list'=>[
// [
// "relation_game_name"=>"--",
// "game_type_name"=>"--",
// "turnover_ratio"=>[
// ["name"=>"--","ratio"=>"--"]
// ],
// "row"=>1
// ]
// ]
// ]
// ];
// $v['row'] += 1;
// }
if( $v["contact_begin"] >0 ){
$v["contact_begin"] = date('Y/m/d', $v["contact_begin"]);
}else{
$v["contact_begin"] = "--";
}
if( $v["contact_end"] > 0){
$v["contact_end"] = date('Y/m/d', $v["contact_end"]);
}else{
$v["contact_end"] = "--";
}
if($v['platform_name'] == '' || $v['platform_name'] == "0"){
$v['platform_name'] = "--";
}
$v["is_sign_contact"] = ($v["is_sign_contact"]=="1" ? "是" :'否');
$v["company_type"] = ($v["company_type"]=="1" ? "公司" :'个人');
$v["company_belong"] = ($v["company_belong"]=="0" ? "内团" : ($v["company_belong"]=="1" ? "外团" : ($v["company_belong"]=="2" ? "外团分发" : '无')));
}
// dd($companyres);
//公司及会长数量统计
$companycount = $model->field("id")->where($map)->select();
$count = count($companycount);
$companyallids = implode(',', array_column($companycount, 'id'));
$promotemap['company_id']=["in",$companyallids];
$promotecount = M("Promote","tab_")->where($promotemap)->count();
$this->assign('listData', $companyres);
$this->assign('count', $count);
$this->assign('promotecount', $promotecount);
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, $parameter);
if ($page) {
$this->assign('_page', $page);
}
//分配其他页面需要参数
$this->assign('promoteList', getPromoteByLevel(1));
$this->assign('commonset', M('Kuaijieicon')->where(['url' => 'Partner/lists'])->find());
$this->meta_title = '推广公司';
$this->display();
}
//添加
public function add()
{
if ($_POST) {
$company_name = I('post.company_name', '');
$model = M($this->modelName, 'tab_');
$map['company_name'] = $company_name;
$res = $model->where($map)->getField('id');
if ($res) {
$this->ajaxReturn(array(
'status' => 0,
'info' => "推广公司已经存在"
));
}
$time = time();
$save = I('post.');//前端已经验证
$save['create_time'] = $time;
$save['last_up_time'] = $time;
$save['contact_begin']=strtotime($save['contact_begin']);
$save['contact_end']=strtotime($save['contact_end']);
if(isset($save['resources'])){
$save['resources'] = json_encode($save['resources'],JSON_UNESCAPED_UNICODE);
}
if(empty($save['contact_phone'])){
$save['contact_phone'] = 0;
}
$res = $model->add($save);
if ($res) {
addOperationLog(['op_type'=>0,'key'=>$company_name,'op_name'=>'新增推广公司','url'=>U('PromoteCompany/lists'),'menu'=>'推广员-推广员管理-推广公司管理-新增推广公司']);
\Think\Log::actionLog('PromoteCompany/add', 'partner', $res);
$array=array(
"info"=>"添加成功",
"status"=>1
);
$this->ajaxReturn($array);
} else {
$this->ajaxReturn(array(
'status' => 0,
'info' => "保存失败"
));
}
} else {
$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'] ="[]";
}
$this->assign('data', $data);
$this->display();
}
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()
{
$model = M($this->modelName, 'tab_');
if ($_POST) {
$time = time();
$save = I('post.');//前端已经验证
$save['create_time'] = $time;
$save['last_up_time'] = $time;
$save['contact_begin']=strtotime($save['contact_begin']);
$save['contact_end']=strtotime($save['contact_end']);
if(isset($save['resources'])){
$save['resources'] = json_encode($save['resources'],JSON_UNESCAPED_UNICODE);
}
//判断内外团是否修改
$is_change_belong = false;
$company_id = $save['id'];
$y_belong = $model->field("company_belong")->where("id = {$save['id']}")->find()['company_belong'];
if(isset($save['company_belong']) & & ($y_belong != $save['company_belong'])){
$is_change_belong = true;
}
if(empty($save['contact_phone'])){
$save['contact_phone'] = 0;
}
$res = $model->save($save);
if ($res) {
\Think\Log::actionLog('PromoteCompany/edit', 'partner', $res);
//处理底下所有会长的公会属性
if($is_change_belong){
$savedata = array(
"company_belong"=>$save['company_belong']
);
// 权限跟随
$authGroup = M('sys_auth_group')->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('sys_auth_group')->where(['title'=>'市场总监'])->save(['data_president'=>implode(',', $dataPresident)]);
M("Promote","tab_")->where("company_id = '{$company_id}'")->save($savedata);
}
addOperationLog(['op_type'=>1,'key'=>$_POST['company_name'],'op_name'=>'编辑推广公司','url'=>U('PromoteCompany/lists'),'menu'=>'推广员-推广员管理-推广公司管理-编辑推广公司']);
$array=array(
"info"=>"修改成功",
"status"=>1
);
$this->ajaxReturn($array);
} else {
$this->ajaxReturn(array(
'status' => 0,
'info' => "修改失败"
));
}
} else {
$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['resources'] == ''){
$data['resources'] ="[]";
}
$this->assign('serviceAgreements', M('service_agreement', 'tab_')->select());
$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('删除失败');
}
// echo M()->_sql();die();
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('设置完成');
}
}