|
|
<?php
|
|
|
namespace Admin\Event;
|
|
|
|
|
|
use Think\Controller;
|
|
|
|
|
|
class MarketEvent extends Controller
|
|
|
{
|
|
|
private $MarketDepartmentId = "1,2,3";
|
|
|
private $ManagerLevel = "4";
|
|
|
private $DirectorLevel = "5";
|
|
|
private $LevelConfig = [
|
|
|
'1'=>'初级',
|
|
|
'2'=>'中级',
|
|
|
'3'=>'高级',
|
|
|
'4'=>'经理'
|
|
|
];
|
|
|
private $uid;
|
|
|
private $gid;
|
|
|
private $isMarket = false;
|
|
|
private $isInit = false;
|
|
|
private $marketDepartment=[];
|
|
|
|
|
|
public function defaultInit()
|
|
|
{
|
|
|
if($isInit) return $this;
|
|
|
$this->uid = $_SESSION['onethink_admin']['user_auth']['uid'];
|
|
|
$this->gid = session('user_group_id');
|
|
|
$info = $this->isMarketAdminGroup($this->gid);
|
|
|
if($info){
|
|
|
$this->isMarket = true;
|
|
|
}
|
|
|
return $this;
|
|
|
}
|
|
|
/**
|
|
|
* 获取所有市场部组id
|
|
|
*/
|
|
|
public function getMarketGroupId($departmentId = null)
|
|
|
{
|
|
|
return [];
|
|
|
$departmentIds = $departmentId ? $departmentId : $this->MarketDepartmentId;
|
|
|
$res = M('department','sys_')->where("id in ({$departmentIds})")->getField("group_ids",true);
|
|
|
$sendData = '';
|
|
|
foreach ($res as $value) {
|
|
|
$sendData .= $value.",";
|
|
|
}
|
|
|
return rtrim($sendData,",");
|
|
|
}
|
|
|
/**
|
|
|
* 获取市场专员gid(除了总监)
|
|
|
*/
|
|
|
public function getNoLeaderMarketGroupId($departmentId = null)
|
|
|
{
|
|
|
$departmentIds = $departmentId ? [$departmentId] : $this->MarketDepartmentId;
|
|
|
$where = [
|
|
|
"department_id"=>['in', $departmentIds],
|
|
|
"department_level"=>['neq', $this->DirectorLevel]
|
|
|
];
|
|
|
$dbres = M('auth_group','sys_')->where($where)->getField("id",true);
|
|
|
return implode(",",$dbres);
|
|
|
}
|
|
|
/**
|
|
|
* 获取普通的市场专员(初中高)的gid
|
|
|
*/
|
|
|
public function getGeneralMarketGroup()
|
|
|
{
|
|
|
$where = [
|
|
|
"department_id"=>['in',$this->MarketDepartmentId],
|
|
|
"department_level"=>['LT',4]
|
|
|
];
|
|
|
$dbres = M('auth_group','sys_')->where($where)->getField("id",true);
|
|
|
return implode(",",$dbres);
|
|
|
}
|
|
|
/**
|
|
|
* 获取经理信息
|
|
|
*/
|
|
|
public function getManagerPerformanInfo()
|
|
|
{
|
|
|
$where = [
|
|
|
"department_id"=>['in',$this->MarketDepartmentId],
|
|
|
"department_level"=>['eq',$this->ManagerLevel]
|
|
|
];
|
|
|
$dbres = M('auth_group','sys_')->where($where)->getField("id",true);
|
|
|
$market = M("auth_group_access")
|
|
|
->alias("acc")
|
|
|
->where(["acc.group_id"=>['in',$dbres]])
|
|
|
->join("sys_member as mem on acc.uid = mem.uid")
|
|
|
->join("sys_auth_group as gro on acc.group_id = gro.id")
|
|
|
->getField("acc.uid,mem.nickname,mem.real_name,gro.department_id,gro.market_percentage",true);
|
|
|
return $market;
|
|
|
}
|
|
|
/**
|
|
|
* 获取配置信息
|
|
|
* @param [string] $key
|
|
|
* @return void
|
|
|
*/
|
|
|
public function getConfig($key)
|
|
|
{
|
|
|
return $this->defaultInit()->$key;
|
|
|
}
|
|
|
/**
|
|
|
* 获取等级名称
|
|
|
*/
|
|
|
public function getLevelName($level)
|
|
|
{
|
|
|
return $this->LevelConfig[$level];
|
|
|
}
|
|
|
/**
|
|
|
* 是否是市场专员
|
|
|
* @param [int] $gid 管理组id
|
|
|
* @return boolean
|
|
|
*/
|
|
|
public function isMarketAdminGroup($gid)
|
|
|
{
|
|
|
$where = [
|
|
|
"department_id"=>['in',$this->MarketDepartmentId],
|
|
|
"id"=>$gid
|
|
|
];
|
|
|
return M('auth_group','sys_')->field("id,department_id,department_level")->where($where)->find();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加部门总监的会长权限
|
|
|
* $uid,$promote_id
|
|
|
*/
|
|
|
public function addPresident($uid,$promote_id)
|
|
|
{
|
|
|
$leader_group = M("auth_group_access")
|
|
|
->alias("acc")
|
|
|
->where(['acc.uid'=>$uid])
|
|
|
->join("sys_auth_group as gro on acc.group_id = gro.id")
|
|
|
->join("sys_department as dep on gro.department_id = dep.id")
|
|
|
->getField("dep.leader_group",1);
|
|
|
if(!$leader_group){
|
|
|
return true;
|
|
|
}
|
|
|
$info = M('auth_group','sys_')->where(['id'=>$leader_group])->find();
|
|
|
$dataPresident = explode(',', $info['data_president']);
|
|
|
$dataPresident = array_unique(array_merge($dataPresident, array($promote_id)));
|
|
|
return $this->saveLeaderPresident($leader_group,$dataPresident);;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 移除原有权限
|
|
|
* @param [array] $promoteIds
|
|
|
* @return void
|
|
|
*/
|
|
|
public function removePresident($promoteIds)
|
|
|
{
|
|
|
if(!is_array($promoteIds)){
|
|
|
$promoteIds = explode(",",$promoteIds);
|
|
|
};
|
|
|
$leaderPresident = $this->getLeaderPresident();
|
|
|
foreach ($leaderPresident as $k => $v) {
|
|
|
$dataPresident = explode(',', $v);
|
|
|
$dataPresident = array_diff($dataPresident, $promoteIds);
|
|
|
$this->saveLeaderPresident($k,$dataPresident);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 添加原有权限,设计重复问题
|
|
|
* @param [array] $promoteIds
|
|
|
* @return void
|
|
|
*/
|
|
|
public function pushPresident($promoteIds)
|
|
|
{
|
|
|
if(!is_array($promoteIds)){
|
|
|
$promoteIds = explode(",",$promoteIds);
|
|
|
};
|
|
|
foreach ($promoteIds as $k => $v) {
|
|
|
$admin_id = M("Promote","tab_")->where("id = {$v}")->getField("admin_id");
|
|
|
$this->addPresident($admin_id,$v);
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
private function getLeaderPresident()
|
|
|
{
|
|
|
$where = [
|
|
|
"department_id"=>['in',$this->MarketDepartmentId],
|
|
|
"department_level"=>['eq',$this->DirectorLevel]
|
|
|
];
|
|
|
return M('auth_group','sys_')->where($where)->getField("id,data_president",true);
|
|
|
}
|
|
|
private function saveLeaderPresident($gid,$data_president)
|
|
|
{
|
|
|
if(is_array($data_president)){
|
|
|
$data_president = implode(",",$data_president);
|
|
|
};
|
|
|
$savedata = [
|
|
|
"id"=>$gid,
|
|
|
"data_president"=>$data_president
|
|
|
];
|
|
|
return M('auth_group','sys_')->save($savedata);
|
|
|
}
|
|
|
|
|
|
public function getDepartmentUserId($department_id,$type = "uid")
|
|
|
{
|
|
|
$field = "mem.".$type;
|
|
|
$res = M("auth_group_access")
|
|
|
->alias("acc")
|
|
|
->field($field)
|
|
|
->where(['gro.department_id'=>$department_id])
|
|
|
->join("sys_member as mem on acc.uid = mem.uid")
|
|
|
->join("sys_auth_group as gro on acc.group_id = gro.id")
|
|
|
->select();
|
|
|
return array_column($res,$type);
|
|
|
}
|
|
|
|
|
|
public function getAdminCompanyId()
|
|
|
{
|
|
|
if (!empty(session('user_auth_company_ids'))) {
|
|
|
return session('user_auth_company_ids');
|
|
|
}
|
|
|
$userAuth = session('user_auth');
|
|
|
$companyIds = 'all';
|
|
|
$userAuth['data_president']= trim($userAuth['data_president'], ",");
|
|
|
if (!empty($userAuth['data_empower_type'])) {//数据权限 1 全部 2 部分数据 3 自己底下的会长
|
|
|
//等于1默认全部数据,不进行筛选
|
|
|
if (in_array($userAuth['data_empower_type'], [2, 3])) {
|
|
|
$companyIds = '';
|
|
|
//自己创建的会长
|
|
|
$myPromote_ids = M('promote', 'tab_')
|
|
|
->field('GROUP_CONCAT(id) as promote_ids')
|
|
|
->where(['admin_id' => $userAuth['uid']])
|
|
|
->find();
|
|
|
$myPromote_ids = $myPromote_ids['promote_ids'];
|
|
|
if ($myPromote_ids) {
|
|
|
if ($userAuth['data_empower_type'] == 2) {//部分会长加上自己创建的会长
|
|
|
|
|
|
// $userAuth['data_president'] .= "," . $myPromote_ids;
|
|
|
} elseif ($userAuth['data_empower_type'] == 3) {//自己创建的会长和底下推广员
|
|
|
$userAuth['data_president'] = $myPromote_ids;
|
|
|
}
|
|
|
}
|
|
|
$dbres = M('promote', 'tab_')
|
|
|
->field('company_id')
|
|
|
->where(['id' =>['in',$userAuth['data_president']],'level'=>1])
|
|
|
->group('company_id')
|
|
|
->getField('company_id',true);
|
|
|
if($dbres){
|
|
|
$companyIds = implode(",",$dbres);
|
|
|
}else{
|
|
|
$companyIds = "-1";
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
// dd($companyIds);
|
|
|
session('user_auth_company_ids', $companyIds);
|
|
|
return $companyIds;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取部门领导管辖的所有用户,非领导返回自己
|
|
|
*
|
|
|
* @param int $value uid/null时候为自己
|
|
|
* @param string $type uid/group_id
|
|
|
* @return array
|
|
|
*/
|
|
|
public function getAdminidByLeaderid($uid=null)
|
|
|
{
|
|
|
$this->defaultInit();
|
|
|
if($uid){
|
|
|
$gid = M("auth_group_access")->where("uid = '{$uid}'")->getField('group_id');
|
|
|
}else{
|
|
|
$uid = $this->uid;
|
|
|
$gid = $this->gid;
|
|
|
}
|
|
|
$group_ids = M('department','sys_')->where("leader_group = '{$gid}'")->getField("group_concat(group_ids) as group_ids");
|
|
|
if(!$group_ids) return $uid;
|
|
|
|
|
|
$adminIds = M("auth_group_access")->where("group_id in ({$group_ids})")->getField('uid',true);
|
|
|
return $adminIds ?? $uid;
|
|
|
}
|
|
|
/**
|
|
|
* 获取部门名称通过市场专员类型
|
|
|
*
|
|
|
* @param string $type id/group_id
|
|
|
* @return array
|
|
|
*/
|
|
|
public function getDepartmentNameByMarketType($type = 'uid')
|
|
|
{
|
|
|
if(isset($this->marketDepartment[$type])){
|
|
|
return $this->marketDepartment[$type];
|
|
|
}
|
|
|
//获取所有公司
|
|
|
$field = "mem.".$type;
|
|
|
$this->marketDepartment[$type] = M("auth_group_access")
|
|
|
->alias("acc")
|
|
|
->where(['gro.department_id'=>['in',$this->MarketDepartmentId]])
|
|
|
->join("sys_member as mem on acc.uid = mem.uid")
|
|
|
->join("sys_auth_group as gro on acc.group_id = gro.id")
|
|
|
->getField("{$field},gro.department_name",true);
|
|
|
|
|
|
return $this->marketDepartment[$type];
|
|
|
}
|
|
|
/**获取市场部部门 */
|
|
|
public function getDepartments()
|
|
|
{
|
|
|
return M('department', 'sys_')->where(['id'=>['in',$this->MarketDepartmentId]])->field("id,name")->select();
|
|
|
}
|
|
|
|
|
|
} |