Files
liche/common/libraries/market/Sylive_entity.php
T
2022-10-29 17:49:05 +08:00

604 lines
24 KiB
PHP

<?php
/**
* 私域直播
*/
class Sylive_entity{
const DEFAULT_HEAD = 'https://qs.haodian.cn/web/images/project/H5-ShiYu/default-head.png'; //默认头像
const DF_IMG = 'https://qs.haodian.cn/web/images/project/H5-ShiYu/goodslogo.jpg'; //东风图标
private $ci;
private $map_kpi_biz = [
'browse' => 'browse', 'subscribe' => 'subscribe', 'order' => 'orderTotal','watch' => 'watch'
];
public function __construct(){
$this->ci = &get_instance();
$this->ci->load->model('market/market_sylive_user_model', 'user_model');
$this->ci->load->model('market/market_sylive_activity_model');
$this->ci->load->model('market/market_sylive_activity_biz_model','mdSytActivityBiz');
$this->ci->load->model('market/market_sylive_activity_team_model','mdSytActivityTeam');
$this->ci->load->model('market/market_sylive_activity_kpidata_model','mdSytActivityKpiData');
$this->ci->load->model('market/market_sylive_organization_model');
$this->ci->load->model('market/market_sylive_activity_orders_model','mdSytActivityOrders');
$this->ci->load->model('live/Live_polyv_viewlog_model', 'mdPolyvViewlog');
$this->ci->load->model('market/market_sylive_team_model');
$this->ci->load->model('market/market_sylive_viewlog_model');
}
/**
* @param $organizationId 机构id
* @param $level 0品牌-管理大区 1 督导-权限范围内所有门店 2 店长-门店所有权限 3 顾问-个人权限 4普通用户-没有权限
* @return int|mixed
*/
public function get_level($organizationId,$level=0){
if(!$organizationId){
return 4;
}
$row = $this->ci->market_sylive_organization_model->get(['organizationId'=>$organizationId],'organizationId,parentId');
if(!$row){
return 4;
}
if($row['parentId']){
$temp = $level+1;
return $this->get_level($row['parentId'],$temp);
}
return $level;
}
/**
* @param $organizationId
* @param $group_id
* @return void
*/
public function get_biz_id($organizationId,$group_id){
if($group_id==3){ //顾问
$org_row = $this->ci->market_sylive_organization_model->get(['organizationId'=>$organizationId],'parentId');
$biz_id = $org_row['parentId'] ? $org_row['parentId'] : 0;
}elseif($group_id==2){ //店长
$biz_id = $organizationId;
}else{
$biz_id = 0;
}
return $biz_id;
}
/**
* @param $organizationId
* @param $group_id
* @return int|mixed
*/
public function get_area_id($organizationId,$group_id){
if($group_id==2){ //店长
$org_row = $this->ci->market_sylive_organization_model->get(['organizationId'=>$organizationId],'parentId');
$area_id = $org_row['parentId'] ? $org_row['parentId'] : 0;
}elseif($group_id==1){
$area_id = $organizationId;
}else{
$area_id = 0;
}
return $area_id;
}
/**
* 判断活动是否有权限
* @param $organizationId
* @param $group_id
* @param $a_id
* @return int
*/
public function act_role($organizationId,$group_id,$a_id){
$where['activityId'] = $a_id;
if($group_id==3){
$where["bizId in (select parentId from lc_market_sylive_organization where organizationId={$organizationId} and status=0)"] = null;
}elseif($group_id==2){
$where['bizId'] = $organizationId;
}elseif ($group_id==1){
$where["areaId"] = $organizationId;
}else{
$where["areaId in (select organizationId from lc_market_sylive_organization where parentId={$organizationId} and status=0)"] = null;
}
$res = false;
$act = $this->ci->mdSytActivityBiz->get($where,'activityId');
if($act){
$row = $this->ci->market_sylive_activity_model->get(['activityId'=>$act['activityId']],'activityId,jsondata');
$jsondata = json_decode($row['jsondata'],true);
$res = true;
if(in_array($group_id,[2,3]) && $jsondata['pay']['way']==2){ //判断是否支付
$where = [
'activityId'=>$a_id,
'status'=>1,
];
if($group_id==3){
$where["bizId in (select parentId from lc_market_sylive_organization where organizationId={$organizationId} and status=0)"] = null;
}else{
$where['bizId'] = $organizationId;
}
$is_pay = $this->ci->mdSytActivityOrders->get($where);
!$is_pay && $res = false;
}
}
return $res;
}
/**
* 获取等级数据
* @param $organizationId
* @param $data
* @return int
*/
public function get_level_lists($organizationId,$data=[]){
$row = $this->ci->market_sylive_organization_model->get(['organizationId'=>$organizationId],'organizationId,parentId,organizationName');
if(!$row){
return $data;
}else{
array_unshift($data,$row);
if($row['parentId']){
return $this->get_level_lists($row['parentId'],$data);
}else{
return $data;
}
}
}
/**
* Notes:添加kpi记录
* Created on: 2021/1/13 11:20
* Created by: dengbw
* @param $params
* @return array
*/
public function kpi_log($params)
{
if(!$params['cf_uid']){
$u_row = $this->user_model->get(['userId'=>$params['uid']]);
if($u_row['teamId']){
$t_level_id = $this->get_team_level($u_row['teamId']);
$params['cf_uid'] = $t_level_id<3 ? $params['uid'] : 0;
}
if($u_row['organizationId']){
$group_id = $this->get_level($u_row['organizationId']);
$params['cf_uid'] = $group_id<4 ? $params['uid'] : 0;
}
}
if ($params['a_id'] && $params['uid'] && $params['cf_uid']) {
$user = $this->user_model->get(['userId'=>$params['cf_uid']]);
if(!$user){
return array('code' => 0, 'msg' => '顾问不存在');
}
$re_a = $this->ci->market_sylive_activity_model->get(array("activityId" => $params['a_id'], "status" => 0));
$now_time = time();
if(!in_array($params['kpi'],['subscribe','browse'])){ //无需活动开始
if ((!$re_a || $now_time < strtotime($re_a['timeStart']) || $now_time > strtotime($re_a['timeEnd']))) {
return array('code' => 0, 'msg' => '无活动或未在活动时间内');
}
}
$type = $user['organizationId'] > 0 ? 0 : 1; //0门店 1团队
$re_kpi = $this->ci->mdSytActivityKpiData->get(array("a_id" => $params['a_id'], 'uid' => $params['uid'], 'kpi' => $params['kpi'],'type'=>$type));
if ($re_kpi) {//已添加过活动kpi(访活动只记录一次用户kpi记录)
return array('code' => 0, 'msg' => '已添加过' . $params['kpi'] . '记录');
}
if($type){
$team_lists = $this->get_team_lists($user['teamId']);
if($team_lists) {
$area_id = $team_lists[0]['teamId']; //大团长id
$biz_id = $team_lists[1]['teamId']; //团长id
}
}else{
$level_lists = $this->get_level_lists($user['organizationId']);
if($level_lists){
//获取大区id
$area_id = $level_lists[1]['organizationId'];
//获取店铺id
$biz_id = $level_lists[2]['organizationId'];
}
}
//增加记录
$addData = [
'a_id' => $params['a_id'],
'uid' => $params['uid'],
'cf_uid' => $params['cf_uid'],
'kpi' => $params['kpi'],
'type' => $type,
'c_time' => time()
];
$area_id && $addData['area_id'] = $area_id;
$biz_id && $addData['biz_id'] = $biz_id;
$jsondata = $params['jsondata'] ? $params['jsondata'] : [];
$jsondata && $addData['jsondata'] = json_encode($jsondata, JSON_UNESCAPED_UNICODE);
$id = $this->ci->mdSytActivityKpiData->add($addData);
if (!$id) {
return array('code' => 0, 'msg' => '添加记录失败');
}
if($this->map_kpi_biz[$params['kpi']] && $biz_id && !$addData['type']){//更新门店统计数据
$up_key = $this->map_kpi_biz[$params['kpi']];
$update[$up_key] = $this->ci->mdSytActivityKpiData->count(['a_id'=>$params['a_id'],'biz_id'=>$biz_id,'kpi'=>$params['kpi'],'type'=>0]);
$this->ci->mdSytActivityBiz->update($update,['activityId'=>$params['a_id'],'bizId'=>$biz_id]);
}
if($this->map_kpi_biz[$params['kpi']] && $biz_id && $addData['type']) {//更新团队统计数据
$up_key = $this->map_kpi_biz[$params['kpi']];
$update[$up_key] = $this->ci->mdSytActivityKpiData->count(['a_id'=>$params['a_id'],'biz_id'=>$biz_id,'kpi'=>$params['kpi'],'type'=>1]);
$this->ci->mdSytActivityTeam->update($update,['activityId'=>$params['a_id'],'teamId2'=>$biz_id]);
}
return array('code' => 1, 'msg' => '添加记录成功');
} else {
return array('code' => 0, 'msg' => '参数错误');
}
}
//排行数据
public function top_lists($aid,$type='browse',$page=1,$size=10,$biz_id=0){
$lists = [];
$where = [
'activityId' => $aid,
'status' => 0
];
$total = $this->ci->mdSytActivityBiz->count($where);
$field = $this->map_kpi_biz[$type];
if($total && $field){
$rows = $this->ci->mdSytActivityBiz->select($where,"{$field} desc,id desc",$page,$size);
$biz_ids = implode(',',array_column($rows,'bizId'));
$org_rows = [];
if($biz_ids){
$org_rows = $this->ci->market_sylive_organization_model->map('organizationId','organizationName',["organizationId in ($biz_ids)"],'','','','organizationId,organizationName');
}
$start = $page>1 ? ($page-1)*$size : 0;
foreach ($rows as $key=>$val) {
$lists[] = [
'ranking' => $start+$key+1,
'name' => $org_rows[$val['bizId']] ? $org_rows[$val['bizId']] : '',
'num' => $val[$this->map_kpi_biz[$type]].'人',
'tip' => $biz_id==$val['bizId'] ? '本店' : '',
];
}
}
$data = [
'total' => $total,
'lists' => $lists
];
return $data;
}
//门店用户排行数据
public function top_biz_user($aid,$biz_id,$uid,$type='browse',$page=1,$size=90){
if(!$biz_id){
return ['total' => 0, 'lists' => []];
}
$where = [
'parentId' => $biz_id
];
$orgs = $this->ci->market_sylive_organization_model->select($where,'',0,0,'organizationId');
$orgs && $org_arr_ids = array_column($orgs,'organizationId');
$org_arr_ids[] = $biz_id;
$org_ids = implode($org_arr_ids,',');
$where = [
"organizationId in ({$org_ids})" => null
];
$total = $this->ci->user_model->count($where);
$lists = [];
if($total){
$rows = $this->ci->user_model->select($where,'userId desc',$page,$size,'userId,uname,nickname,headimg');
$where = [
'a_id' => $aid,
'kpi' => $type,
'biz_id' => $biz_id,
'type' => 0,
];
$t_rows = $this->ci->mdSytActivityKpiData->select_groupby('cf_uid',$where,'total desc',$page,$size,'cf_uid,count(id) as total');
$map = [];
if($t_rows){
foreach($t_rows as $item) {
$map[$item['cf_uid']] = null !== $item['total'] ? $item['total'] : $item;
}
}
foreach ($rows as $item) {
$num = $map[$item['userId']] ? $map[$item['userId']] : '0';
$lists[] = [
'uid' => $item['userId'],
'name' => $item['uname'] ? $item['uname'] : $item['nickname'],
'headimg' => $item['headimg'] ? $item['headimg'] : self::DEFAULT_HEAD,
'total' => $num,
'num' => "{$num}",
'tip' => $uid == $item['userId'] ? '本人' : '',
];
}
$edit = array_column($lists,'total');
array_multisort($edit,SORT_DESC,$lists);
}
$data = [
'total' => $total,
'lists' => $lists
];
return $data;
}
//用户用户观看次数排行
public function top_view_num($channelId,$page=1,$size=10){
$where = [
'channelId' => $channelId
];
$total = $this->ci->mdPolyvViewlog->count($where);
$lists = [];
if($total){
$rows = $this->ci->mdPolyvViewlog->select_groupby('param1',$where,'total desc',$page,$size,'param1,count(id) as total');
$uids = implode("','",array_unique(array_column($rows,'param1')));
$users = [];
if($uids){
$where = [
"unionid in ('$uids')" => null,
];
$users = $this->ci->user_model->map('unionid','',$where,'','','','userId,unionid,uname,nickname,headimg');
}
foreach ($rows as $item) {
$user = $users[$item['param1']] ? $users[$item['param1']][0] : [];
$total = $item['total'];
$lists[] = [
'uid' => $user['userId'],
'name' => $user['nickname'] ? $user['nickname'] : '',
'headimg' => $user['headimg'] ? $user['headimg'] : self::DEFAULT_HEAD,
'total' => $total,
'num' => "{$total}",
];
}
}
$data = [
'total' => $total,
'lists' => $lists
];
return $data;
}
//用户用户观看时长排行
public function top_view_time($channelId,$page=1,$size=10){
$where = [
'channelId' => $channelId
];
$total = $this->ci->mdPolyvViewlog->count($where);
$lists = [];
if($total){
$rows = $this->ci->mdPolyvViewlog->select_groupby('param1',$where,'total desc',$page,$size,'param1,sum(playDuration) as total');
$uids = implode("','",array_unique(array_column($rows,'param1')));
$users = [];
if($uids){
$where = [
"unionid in ('$uids')" => null,
];
$users = $this->ci->user_model->map('unionid','',$where,'','','','userId,unionid,uname,nickname,headimg');
}
foreach ($rows as $item) {
$user = $users[$item['param1']] ? $users[$item['param1']][0] : [];
$total = $item['total'];
$lists[] = [
'uid' => $user['userId'],
'name' => $user['nickname'] ? $user['nickname'] : '',
'headimg' => $user['headimg'] ? $user['headimg'] : self::DEFAULT_HEAD,
'total' => $total,
'num' => "{$total}",
];
}
}
$data = [
'total' => $total,
'lists' => $lists
];
return $data;
}
/**
* @param $teamId 团队id
* @param $level 0大团长 1团长 2团员 3普通用户
* @return int|mixed
*/
public function get_team_level($teamId,$level=0){
if(!$teamId){
return 3;
}
$row = $this->ci->market_sylive_team_model->get(['teamId'=>$teamId],'teamId,parentId');
if(!$row){
return 3;
}
if($row['parentId']){
$temp = $level+1;
return $this->get_team_level($row['parentId'],$temp);
}
return $level;
}
/**
* 获取等级数据
* @param $teamId 团队id
* @param $data
* @return int
*/
public function get_team_lists($teamId,$data=[]){
$row = $this->ci->market_sylive_team_model->get(['teamId'=>$teamId],'teamId,parentId,teamName');
if(!$row){
return $data;
}else{
array_unshift($data,$row);
if($row['parentId']){
return $this->get_team_lists($row['parentId'],$data);
}else{
return $data;
}
}
}
/**
* 判断团长活动是否有权限
* @param $organizationId
* @param $level
* @param $a_id
* @return int
*/
public function team_act_role($organizationId,$level,$a_id){
$where['activityId'] = $a_id;
if($level==2){ //团员
$where["teamId2 in (select parentId from lc_market_sylive_team where teamId={$organizationId} and status=0)"] = null;
}elseif($level==1){ //团长
$where['teamId2'] = $organizationId;
}else{ //大团长
$where["teamId1"] = $organizationId;
}
$act = $this->ci->mdSytActivityTeam->get($where,'activityId');
return $act ? true : false;
}
//团队排行数据
public function top_team_lists($aid,$type='browse',$page=1,$size=10,$teamId=0){
$lists = [];
$where = [
'activityId' => $aid,
'status' => 0
];
$total = $this->ci->mdSytActivityTeam->count($where);
$field = $this->map_kpi_biz[$type];
if($total && $field){
$rows = $this->ci->mdSytActivityTeam->select($where,"{$field} desc,id desc",$page,$size);
$team_ids = implode(',',array_column($rows,'teamId2'));
$org_rows = [];
if($team_ids){
$org_rows = $this->ci->market_sylive_team_model->map('teamId','teamName',["teamId in ($team_ids)"],'','','','teamId,teamName');
}
$start = $page>1 ? ($page-1)*$size : 0;
foreach ($rows as $key=>$val) {
$lists[] = [
'ranking' => $start+$key+1,
'name' => $org_rows[$val['teamId2']] ? $org_rows[$val['teamId2']] : '',
'num' => $val[$this->map_kpi_biz[$type]].'人',
'tip' => $teamId==$val['teamId2'] ? '我的团队' : '',
];
}
}
$data = [
'total' => $total,
'lists' => $lists
];
return $data;
}
//团队用户排行数据
public function top_team_user($aid,$teamId,$uid,$type='browse',$page=1,$size=50){
if(!$teamId){
return ['total' => 0, 'lists' => []];
}
$where = [
'parentId' => $teamId
];
$orgs = $this->ci->market_sylive_team_model->select($where,'',0,0,'teamId');
$orgs && $org_arr_ids = array_column($orgs,'teamId');
$org_arr_ids[] = $teamId;
$org_ids = implode($org_arr_ids,',');
$where = [
"teamId>" => 0,
"teamId in ({$org_ids})" => null
];
$total = $this->ci->user_model->count($where);
$lists = [];
if($total){
$rows = $this->ci->user_model->select($where,'userId desc',$page,$size,'userId,uname,nickname,headimg');
$where = [
'a_id' => $aid,
'kpi' => $type,
'biz_id' => $teamId,
'type' => 1,
];
$t_rows = $this->ci->mdSytActivityKpiData->select_groupby('cf_uid',$where,'total desc',$page,$size,'cf_uid,count(id) as total');
$map = [];
if($t_rows){
foreach($t_rows as $item) {
$map[$item['cf_uid']] = null !== $item['total'] ? $item['total'] : $item;
}
}
foreach ($rows as $item) {
$total = $map[$item['userId']] ? $map[$item['userId']] : '0';
$name = $item['uname'] ? $item['uname'] : $item['nickname'];
$lists[] = [
'uid' => $item['userId'],
'name' => name_asterisk($name),
'headimg' => self::DF_IMG,
'total' => $total,
'num' => "{$total}",
'tip' => $uid == $item['userId'] ? '本人' : '',
];
}
$edit = array_column($lists,'total');
array_multisort($edit,SORT_DESC,$lists);
}
$data = [
'total' => $total,
'lists' => $lists
];
return $data;
}
//用户用户观看次数排行
public function top_team_view_num($where,$page=1,$size=10){
$total = $this->ci->market_sylive_viewlog_model->count($where);
$lists = [];
if($total){
$rows = $this->ci->market_sylive_viewlog_model->select_groupby('userId',$where,'total desc',$page,$size,'userId,count(id) as total');
$uids = implode(",",array_unique(array_column($rows,'userId')));
$users = [];
if($uids){
$where = [
"userId in ('$uids')" => null,
];
$users = $this->ci->user_model->map('userId','',$where,'','','','userId,unionid,uname,nickname,headimg');
}
foreach ($rows as $item) {
$user = $users[$item['userId']] ? $users[$item['userId']][0] : [];
$total = $item['total'];
$lists[] = [
'uid' => $user['userId'],
'name' => $user['nickname'] ? $user['nickname'] : '',
'headimg' => $user['headimg'] ? $user['headimg'] : self::DEFAULT_HEAD,
'total' => $total,
'num' => "{$total}",
];
}
}
$data = [
'total' => $total,
'lists' => $lists
];
return $data;
}
//用户用户观看时长排行
public function top_team_view_time($where,$page=1,$size=10){
$total = $this->ci->market_sylive_viewlog_model->count($where);
$lists = [];
if($total){
$rows = $this->ci->market_sylive_viewlog_model->select_groupby('userId',$where,'total desc',$page,$size,'userId,sum(playDuration) as total');
$uids = implode(",",array_unique(array_column($rows,'userId')));
$users = [];
if($uids){
$where = [
"userId in ('$uids')" => null,
];
$users = $this->ci->user_model->map('userId','',$where,'','','','userId,unionid,uname,nickname,headimg');
}
foreach ($rows as $item) {
$user = $users[$item['userId']] ? $users[$item['userId']][0] : [];
$total = $item['total'];
$lists[] = [
'uid' => $user['userId'],
'name' => $user['nickname'] ? $user['nickname'] : '',
'headimg' => $user['headimg'] ? $user['headimg'] : self::DEFAULT_HEAD,
'total' => $total,
'num' => "{$total}",
];
}
}
$data = [
'total' => $total,
'lists' => $lists
];
return $data;
}
public function __get($name)
{
if ('_model' === substr($name, -6)) {
return $this->ci->$name;
} elseif ('load' == $name) {
return $this->ci->load;
}
return null;
}
}