Files
liche/common/libraries/market/Sylive_entity.php
T
2022-10-21 21:23:29 +08:00

334 lines
13 KiB
PHP

<?php
/**
* 私域直播
*/
class Sylive_entity{
const DEFAULT_HEAD = 'https://qs.haodian.cn/web/images/project/H5-ShiYu/default-head.png'; //默认头像
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_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');
}
/**
* @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
* @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=[]){
$this->ci->load->model('market/market_sylive_organization_model');
$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']){
$group_id = $this->get_level($params['uid']);
if($group_id<4){
$params['cf_uid'] = $params['uid'];
}
}
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' => '无活动或未在活动时间内');
}
}
$re_kpi = $this->ci->mdSytActivityKpiData->get(array("a_id" => $params['a_id'], 'uid' => $params['uid'], 'kpi' => $params['kpi']));
if ($re_kpi) {//已添加过活动kpi(访活动只记录一次用户kpi记录)
return array('code' => 0, 'msg' => '已添加过' . $params['kpi'] . '记录');
}
$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'],
'c_time' => time()
];
$area_id && $addData['area_id'] = $area_id;
$biz_id && $addData['biz_id'] = $biz_id;
$jsondata = array();
$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){//更新门店统计数据
$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']]);
$this->ci->mdSytActivityBiz->update($update,['activityId'=>$params['a_id'],'bizId'=>$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",$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');
}
foreach ($rows as $key=>$val) {
$lists[] = [
'ranking' => $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=50){
$where = [
'parentId' => $biz_id
];
$orgs = $this->ci->market_sylive_organization_model->select($where,'',0,0,'organizationId');
$org_ids = 0;
if($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
];
$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';
$lists[] = [
'uid' => $item['userId'],
'name' => $item['uname'] ? $item['uname'] : $item['nickname'],
'headimg' => $item['headimg'] ? $item['headimg'] : self::DEFAULT_HEAD,
'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_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;
}
public function __get($name)
{
if ('_model' === substr($name, -6)) {
return $this->ci->$name;
} elseif ('load' == $name) {
return $this->ci->load;
}
return null;
}
}