Files
liche/market/controllers/api/sylive/Statistics.php
T
2022-11-23 13:41:21 +08:00

1345 lines
81 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once APPPATH . 'controllers/api/BaseController.php';
/**
* Notes:私域直播_统计
* Created on: 2022/10/27 17:15
* Created by: dengbw
*/
class Statistics extends BaseController
{
public function __construct()
{
parent::__construct();
$this->load->model('market/Market_sylive_activity_model', 'mdSyliveActivity');
$this->load->model('market/market_sylive_activity_user_model', 'mdSyliveActivityUser');
$this->load->model('market/Market_sylive_activity_kpidata_model', 'mdSyliveActivityKpidata');
$this->load->model('market/Market_sylive_activity_biz_model', 'mdSyliveActivityBiz');
$this->load->model('market/Market_sylive_order_model', 'mdSyliveOrder');
$this->load->model('market/Market_sylive_team_model', 'mdSyliveTeam');
$this->load->model('market/Market_sylive_activity_team_model', 'mdSyliveActivityTeam');
$this->load->model('market/Market_sylive_organization_model', 'mdSyliveOrganization');
$this->load->model('market/Market_sylive_user_model', 'mdSyliveUser');
$this->load->model('market/Market_sylive_viewlog_model', 'mdSyliveViewlog');
$this->load->model('market/Market_sylive_items_model', 'mdSyliveItems');
$this->load->model('live/Live_polyv_session_model', 'mdPolyvSession');
$this->load->model('live/Live_polyv_viewlog_model', 'mdPolyvViewlog');
}
/**
* Notes:机构数据统计主页
* Created on: 2022/10/27 16:39
* Created by: dengbw
* @param null $activityId
*/
public function biz_get($activityId = null)
{
$activityId = intval($activityId);
if (!$activityId) {
$this->return_json('参数错误');
}
$type = 0;
$title = '';
$timeStart = '0000-00-00 00:00:00';
$activityData1 = $activityData2 = $areaAry = $funnel = $allOrganizationIds = [];
$days[] = ['name' => '全部日期', 'value' => ''];
$goods[] = ['name' => '全部订单', 'value' => ''];
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re) {
$res_item = $this->mdSyliveItems->select(['status>=' => 0, 'activityId' => $activityId], 'sort asc,itemId desc'
, 0, 0, 'itemId,title');
foreach ($res_item as $k => $v) {
$goods[] = ['name' => $v['title'], 'value' => intval($v['itemId'])];
}
if ($re['timeEnd'] != '0000-00-00 00:00:00') {
$timeEnd = date("Y-m-d", strtotime($re['timeEnd']));
for ($i = 6; $i >= 0; $i--) {
$day = date("Y-m-d", strtotime("$timeEnd -{$i} day"));
$days[] = ['name' => $day, 'value' => $day];
}
}
$title = $re['title'];
$timeStart = $re['timeStart'];
$channelId = $re['channelId'];
$res_biz = $this->mdSyliveActivityBiz->select(['activityId' => $activityId, 'status>=' => 0], 'browse desc');
foreach ($res_biz as $k => $v) {
$areaId = intval($v['areaId']);
$bizId = intval($v['bizId']);
if ($areaId) {
if (!in_array($areaId, $areaAry)) {
$areaAry[] = $areaId;
}
if (!in_array($areaId, $allOrganizationIds)) {
$allOrganizationIds[] = $areaId;
}
}
if ($bizId) {
$allOrganizationIds[] = $bizId;
$res_org = $this->mdSyliveOrganization->select(['parentId' => $bizId, 'status>=' => 0], 'organizationId desc', 0, 0, 'organizationId');
foreach ($res_org as $k2 => $v2) {
$allOrganizationIds[] = $v2['organizationId'];
}
}
}
$browse = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => $type, 'kpi' => 'browse']);
$subscribe = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => $type, 'kpi' => 'subscribe']);
$watch = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => $type, 'kpi' => 'watch']);
$order = $this->mdSyliveOrder->count(['activityId' => $activityId, 'cfrom' => $type, 'status' => 1]);
$duration_min = $livePV = $liveUV = $watchDuration = $watchDuration = $pvDuration = 0;
if ($channelId) {
$re_duration = $this->mdPolyvSession->sum('duration', ['channelId' => $channelId]);
if ($re_duration['duration']) {
$re_livePV = $this->mdPolyvSession->sum('livePV', ['channelId' => $channelId]);
$re_liveUV = $this->mdPolyvSession->sum('liveUV', ['channelId' => $channelId]);
$livePV = $re_livePV['livePV'];
$liveUV = $re_liveUV['liveUV'];
$re_totalPlayDuration = $this->mdPolyvSession->sum('totalPlayDuration', ['channelId' => $channelId]);
$duration_min = round($re_duration['duration'] / 60);
$totalPlayDuration_min = round($re_totalPlayDuration['totalPlayDuration'] / 60);
$watchDuration = round($totalPlayDuration_min / $liveUV);
$pvDuration = round($livePV / $liveUV, 1);
}
}
$allConsultant = 0;
$consultant = $this->mdSyliveActivityUser->count(['activityId' => $activityId, 'organizationId>' => 0, 'userId>' => 0]);
if ($allOrganizationIds) {
$str_organizationIds = implode(',', array_unique($allOrganizationIds));
$allConsultant = $this->mdSyliveUser->count(["organizationId in({$str_organizationIds})" => null, 'status>=' => 0, 'organizationId>' => 0]);
}
$consultant > $allConsultant && $allConsultant = $consultant;
$biz = $this->mdSyliveActivityBiz->count(['activityId' => $activityId, 'status>=' => 0, 'bizId>' => 0]);
$activityData1 = [['name' => '参与区域数', 'value' => count($areaAry)], ['name' => '参与门店数', 'value' => $biz]
, ['name' => '参与/全部顾问数', 'value' => $consultant . '/' . $allConsultant]];
$url = "/sylive/activity/statistics/piechart?activityId={$activityId}&";
$watch_un = 0;
if ($liveUV) {
$watch_un = $watch - $liveUV;
$watch_un < 0 && $watch_un = 0;
}
$activityData2 = [['name' => '浏览数(人)', 'value' => $browse, 'url' => ''], ['name' => '预约数(人)', 'value' => $subscribe, 'url' => '']
, ['name' => '订单数(单)', 'value' => $order, 'url' => ''], ['name' => '有效观看(人)', 'value' => $liveUV, 'url' => ''], ['name' => '无效观看(人)', 'value' => $watch_un, 'url' => '']
, ['name' => '直播时长(分)', 'value' => $duration_min, 'url' => ''], ['name' => '观看数(人次)', 'value' => $livePV, 'url' => '']
, ['name' => '人均观看(分)', 'value' => $watchDuration, 'url' => ''], ['name' => '人均观看(次)', 'value' => $pvDuration, 'url' => '']];
// $activityData2 = [['name' => '浏览数(人)', 'value' => $browse, 'url' => "{$url}kpi=browse"], ['name' => '预约数(人)', 'value' => $subscribe, 'url' => "{$url}kpi=subscribe"]
// , ['name' => '观看数(人)', 'value' => $watch, 'url' => "{$url}kpi=watch"], ['name' => '订单数(单)', 'value' => $order, 'url' => "{$url}kpi=order"]
// , ['name' => '直播时长(分)', 'value' => $duration, 'url' => ''], ['name' => '观看数(人次)', 'value' => $livePV, 'url' => '']
// , ['name' => '人均观看(分)', 'value' => $watchDuration, 'url' => ''], ['name' => '人均观看(次)', 'value' => $pvDuration, 'url' => '']];
}
$data = ['activityId' => $activityId, 'title' => $title, 'timeStart' => strtotime($timeStart), 'activityData1' => ['list' => $activityData1, 'style' => 'width: 33.3%']
, 'activityData2' => ['list' => $activityData2, 'style' => 'width: 11.1%'], 'tableData' => ['days' => $days, 'goods' => $goods]];
$this->return_response($data);
}
/**
* Notes:区域统计列表
* Created on: 2022/11/7 16:30
* Created by: dengbw
*/
public function page_area_get()
{
$activityId = intval($this->input_param('activityId'));
$sort = $this->input_param('sort');
$order = $this->input_param('order');
$day = $this->input_param('day');//日期
$itemId = intval($this->input_param('itemId'));//商品
$list = [];
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re) {
$type = 0;
$areaAry = [];
$res_biz = $this->mdSyliveActivityBiz->select(['activityId' => $activityId, 'status>=' => 0, 'bizId>=' => 0], 'browse desc');
foreach ($res_biz as $k => $v) {
$areaId = intval($v['areaId']);
$bizId = intval($v['bizId']);
$areaAry[$areaId]['biz'][] = $bizId;
$res_org = $this->mdSyliveOrganization->select(['parentId' => $bizId, 'status>=' => 0], 'organizationId desc', 0, 0, 'organizationId');
foreach ($res_org as $k2 => $v2) {
$areaAry[$areaId]['sales'][] = $v2['organizationId'];
}
}
$channelId = intval($re['channelId']);
!$sort && $sort = 'browse';
!$order && $order = 'desc';
if ($day) {//按天搜索
$createTimeStart = strtotime($day . ' 00:00:00');
$createTimeEnd = strtotime($day . ' 23:59:59');
foreach ($areaAry as $k => $v) {
$areaId = intval($k);
$re_area = $this->mdSyliveOrganization->get(['organizationId' => $areaId]);
if ($re_area) {
$consultant = $this->mdSyliveActivityKpidata->count(['areaId' => $areaId, 'type' => $type, 'day' => $day, 'activityId' => $activityId], 'distinct(cfUserId)');
$browse = $this->mdSyliveActivityKpidata->count(['kpi' => 'browse', 'areaId' => $areaId, 'type' => $type, 'day' => $day, 'activityId' => $activityId]);
$subscribe = $this->mdSyliveActivityKpidata->count(['kpi' => 'subscribe', 'areaId' => $areaId, 'type' => $type, 'day' => $day, 'activityId' => $activityId]);
$watch = $this->mdSyliveActivityKpidata->count(['kpi' => 'watch', 'areaId' => $areaId, 'type' => $type, 'day' => $day, 'activityId' => $activityId]);
$where_order = ['kpi' => 'order', 'areaId' => $areaId, 'type' => $type, 'day' => $day, 'activityId' => $activityId];
$itemId && $where_order['itemId'] = $itemId;
$orderTotal = $this->mdSyliveActivityKpidata->count($where_order);
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count(['areaId' => $areaId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd, 'type' => $type]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['areaId' => $areaId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd, 'type' => $type]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
$allConsultant = 0;
if ($v['biz']) {
$organizationIds = $v['sales'] ? array_merge($v['biz'], $v['sales']) : $v['biz'];
$str_organizationIds = implode(',', array_unique($organizationIds));
$allConsultant = $this->mdSyliveUser->count(["organizationId in({$str_organizationIds})" => null, 'status>=' => 0, 'organizationId>' => 0]);
}
$consultant > $allConsultant && $allConsultant = $consultant;
$biz = count($v['biz']);
$organizationName = $re_area['organizationName'] ? $re_area['organizationName'] : '-';
$list[] = ['areaName' => $organizationName, 'biz' => $biz, 'consultant' => $consultant, 'allConsultant' => $allConsultant
, 'browse' => $browse, 'subscribe' => $subscribe, 'watch' => $watch, 'order' => $orderTotal
, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
} else {
foreach ($areaAry as $k => $v) {
$areaId = intval($k);
$re_area = $this->mdSyliveOrganization->get(['organizationId' => $areaId]);
if ($re_area) {
//$consultant = $this->mdSyliveActivityKpidata->count(['areaId' => $areaId, 'type' => 0, 'activityId' => $activityId], 'distinct(cfUserId)');
//$browse = $this->mdSyliveActivityKpidata->count(['kpi' => 'browse', 'areaId' => $areaId, 'type' => 0, 'activityId' => $activityId]);
$consultant = $this->mdSyliveActivityUser->count(['areaId' => $areaId, 'activityId' => $activityId, 'organizationId>' => 0, 'userId>' => 0]);
$browse = $this->mdSyliveActivityBiz->sum('browse', ['areaId' => $areaId, 'activityId' => $activityId]);
$subscribe = $this->mdSyliveActivityBiz->sum('subscribe', ['areaId' => $areaId, 'activityId' => $activityId]);
$watch = $this->mdSyliveActivityBiz->sum('watch', ['areaId' => $areaId, 'activityId' => $activityId]);
if ($itemId) {
$orderTotal = $this->mdSyliveActivityKpidata->count(['kpi' => 'order', 'areaId' => $areaId, 'type' => $type, 'itemId' => $itemId, 'activityId' => $activityId]);
} else {
$sumOrder = $this->mdSyliveActivityBiz->sum('orderTotal', ['areaId' => $areaId, 'activityId' => $activityId]);
$orderTotal = intval($sumOrder['orderTotal']);
}
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count(['areaId' => $areaId, 'activityId' => $activityId, 'type' => $type]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['areaId' => $areaId, 'activityId' => $activityId, 'type' => $type]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
$allConsultant = 0;
if ($v['biz']) {
$organizationIds = $v['sales'] ? array_merge($v['biz'], $v['sales']) : $v['biz'];
$str_organizationIds = implode(',', array_unique($organizationIds));
$allConsultant = $this->mdSyliveUser->count(["organizationId in({$str_organizationIds})" => null, 'status>=' => 0, 'organizationId>' => 0]);
}
$consultant > $allConsultant && $allConsultant = $consultant;
$biz = count($v['biz']);
$browse = intval($browse['browse']);
$subscribe = intval($subscribe['subscribe']);
$watch = intval($watch['watch']);
$organizationName = $re_area['organizationName'] ? $re_area['organizationName'] : '-';
$list[] = ['areaName' => $organizationName, 'biz' => $biz, 'consultant' => $consultant, 'allConsultant' => $allConsultant
, 'browse' => $browse, 'subscribe' => $subscribe, 'watch' => $watch, 'order' => $orderTotal
, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
}
}
$order = $order == 'desc' ? SORT_DESC : SORT_ASC;
$sortKey = array_column($list, $sort);
array_multisort($sortKey, $order, $list);
$count = count($list);
$date = ['list' => $list, 'count' => $count];
$this->return_response_list($date);
}
/**
* Notes:机构门店统计列表
* Created on: 2022/10/27 14:48
* Created by: dengbw
*/
public function page_biz_get()
{
$activityId = intval($this->input_param('activityId'));
$page = $this->input_param('page');
$limit = $this->input_param('limit');
$sort = $this->input_param('sort');
$order = $this->input_param('order');
$day = $this->input_param('day');
$itemId = intval($this->input_param('itemId'));//商品
!$page && $page = 1;
!$limit && $limit = 10;
$list = [];
$count = 0;
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re) {
$type = 0;
$channelId = intval($re['channelId']);
!$sort && $sort = 'browse';
!$order && $order = 'desc';
if ($sort == 'order' && !$day) {
$sort_order = 'orderTotal ' . $order;
} else {
$sort_order = $sort . ' ' . $order;
}
if ($day) {//按天搜索
$createTimeStart = strtotime($day . ' 00:00:00');
$createTimeEnd = strtotime($day . ' 23:59:59');
$where = $where_order = $where_sort = ['activityId' => $activityId, 'day' => $day, 'type' => $type, 'bizId>' => 0, 'kpi' => $sort];
if ($sort == 'order') {
$itemId && $where['itemId'] = $itemId;
}
$count = $this->mdSyliveActivityKpidata->count($where, 'bizId');
if ($count) {
$res = $this->mdSyliveActivityKpidata->select_groupby('bizId', $where, "total {$order},id desc", $page, $limit, "bizId,count(id) as total");
foreach ($res as $v) {
$bizId = intval($v['bizId']);
$re_biz = $this->mdSyliveOrganization->get(['organizationId' => $bizId, 'status>=' => 0]);
$bizName = $re_biz ? $re_biz['organizationName'] : '-';
$where_sort['bizId'] = $bizId;
if ($sort == 'browse') {
$browse = $v['total'];
} else {
$where_sort['kpi'] = 'browse';
$browse = $this->mdSyliveActivityKpidata->count($where_sort);
}
if ($sort == 'subscribe') {
$subscribe = $v['total'];
} else {
$where_sort['kpi'] = 'subscribe';
$subscribe = $this->mdSyliveActivityKpidata->count($where_sort);
}
if ($sort == 'watch') {
$watch = $v['total'];
} else {
$where_sort['kpi'] = 'watch';
$watch = $this->mdSyliveActivityKpidata->count($where_sort);
}
if ($sort == 'order') {
$orderTotal = $v['total'];
} else {
$where_order['kpi'] = 'order';
$where_order['bizId'] = $bizId;
$itemId && $where_order['itemId'] = $itemId;
$orderTotal = $this->mdSyliveActivityKpidata->count($where_order);
}
$consultant = $this->mdSyliveActivityUser->count(['bizId' => $bizId, 'activityId' => $activityId, 'organizationId>' => 0, 'userId>' => 0]);
$consultants = [];
$consultants[] = $bizId;
$res_org = $this->mdSyliveOrganization->select(['parentId' => $bizId, 'status>=' => 0], 'organizationId desc', 0, 0, 'organizationId');
foreach ($res_org as $k2 => $v2) {
$consultants[] = $v2['organizationId'];
}
$str_organizationIds = implode(',', array_unique($consultants));
$allConsultant = $this->mdSyliveUser->count(["organizationId in({$str_organizationIds})" => null, 'status>=' => 0, 'organizationId>' => 0]);
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count(['bizId' => $bizId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd, 'type' => $type]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['bizId' => $bizId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd, 'type' => $type]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
$list[] = [
'bizName' => $bizName, 'consultant' => $consultant, 'allConsultant' => $allConsultant, 'browse' => $browse, 'subscribe' => $subscribe
, 'watch' => $watch, 'order' => $orderTotal, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
} else {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$where = ['activityId' => $activityId, 'type' => $type, 'itemId' => $itemId, 'bizId>' => 0, 'kpi' => $sort];
$count = $this->mdSyliveActivityKpidata->count($where, 'bizId');
} else {
$where = ['activityId' => $activityId, 'status>=' => 0, 'bizId>' => 0];
$count = $this->mdSyliveActivityBiz->count($where);
}
if ($count) {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$res = $this->mdSyliveActivityKpidata->select_groupby('bizId', $where, "orderTotal {$order},id desc", $page, $limit, "bizId,count(id) as orderTotal");
} else {
$res = $this->mdSyliveActivityBiz->select($where, "{$sort_order},id desc", $page, $limit);
}
foreach ($res as $v) {
$bizId = intval($v['bizId']);
$re_biz = $this->mdSyliveOrganization->get(['organizationId' => $bizId, 'status>=' => 0]);
$bizName = $re_biz ? $re_biz['organizationName'] : '-';
$consultant = $this->mdSyliveActivityUser->count(['bizId' => $bizId, 'activityId' => $activityId, 'organizationId>' => 0, 'userId>' => 0]);
//统计顾问数
$consultants = [];
$consultants[] = $bizId;
$res_org = $this->mdSyliveOrganization->select(['parentId' => $bizId, 'status>=' => 0], 'organizationId desc', 0, 0, 'organizationId');
foreach ($res_org as $k2 => $v2) {
$consultants[] = $v2['organizationId'];
}
$str_organizationIds = implode(',', array_unique($consultants));
$allConsultant = $this->mdSyliveUser->count(["organizationId in({$str_organizationIds})" => null, 'status>=' => 0, 'organizationId>' => 0]);
$consultant > $allConsultant && $allConsultant = $consultant;
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count(['bizId' => $bizId, 'activityId' => $activityId, 'type' => $type]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['bizId' => $bizId, 'activityId' => $activityId, 'type' => $type]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
if ($sort != 'order' && $itemId) {//有商品id,区分订单
$order = $this->mdSyliveActivityKpidata->count(['kpi' => 'order', 'bizId' => $bizId, 'type' => $type, 'itemId' => $itemId, 'activityId' => $activityId]);
} else {
$order = $v['orderTotal'];
}
if ($sort == 'order' && $itemId) {//只改变订单数值,其它不变
$re = $this->mdSyliveActivityBiz->get(['bizId' => $bizId, 'activityId' => $activityId, 'status>=' => 0], 'browse,subscribe,watch');
$v['browse'] = intval($re['browse']);
$v['subscribe'] = intval($re['subscribe']);
$v['watch'] = intval($re['watch']);
}
$list[] = [
'bizName' => $bizName, 'consultant' => $consultant, 'allConsultant' => $allConsultant, 'browse' => $v['browse'], 'subscribe' => $v['subscribe']
, 'watch' => $v['watch'], 'order' => $order, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
}
}
$date = ['list' => $list, 'count' => $count];
$this->return_response_list($date);
}
/**
* Notes:机构顾问统计列表
* Created on: 2022/10/27 14:48
* Created by: dengbw
*/
public function page_consultant_get()
{
$activityId = intval($this->input_param('activityId'));
$page = $this->input_param('page');
$limit = $this->input_param('limit');
$sort = $this->input_param('sort');
$order = $this->input_param('order');
$day = $this->input_param('day');
$itemId = intval($this->input_param('itemId'));//商品
!$page && $page = 1;
!$limit && $limit = 10;
$list = [];
$count = 0;
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re) {
$type = 0;
$channelId = intval($re['channelId']);
!$sort && $sort = 'browse';
!$order && $order = 'desc';
if ($sort == 'order' && !$day) {
$sort_order = 'orderTotal ' . $order;
} else {
$sort_order = $sort . ' ' . $order;
}
if ($day) {
$createTimeStart = strtotime($day . ' 00:00:00');
$createTimeEnd = strtotime($day . ' 23:59:59');
$where = $where_order = $where_sort = ['activityId' => $activityId, 'day' => $day, 'type' => $type, 'cfUserId>' => 0, 'kpi' => $sort];
if ($sort == 'order') {
$itemId && $where['itemId'] = $itemId;
}
$count = $this->mdSyliveActivityKpidata->count($where, 'cfUserId');
if ($count) {
$res = $this->mdSyliveActivityKpidata->select_groupby('cfUserId', $where, "total {$order},id desc", $page, $limit, "bizId,cfUserId,count(id) as total");
foreach ($res as $v) {
$cfUserId = intval($v['cfUserId']);
$re_org = $this->mdSyliveOrganization->get(['organizationId' => $v['bizId'], 'status>=' => 0]);
$organizationName = $re_org['organizationName'] ? $re_org['organizationName'] : '-';
$re_user = $this->mdSyliveUser->get(['userId' => $cfUserId, 'status>=' => 0]);
$uname = $re_user['uname'] ? $re_user['uname'] : $re_user['nickname'];
$consultantName = $organizationName . ' ' . $uname;
$where_sort['cfUserId'] = $cfUserId;
if ($sort == 'browse') {
$browse = $v['total'];
} else {
$where_sort['kpi'] = 'browse';
$browse = $this->mdSyliveActivityKpidata->count($where_sort);
}
if ($sort == 'subscribe') {
$subscribe = $v['total'];
} else {
$where_sort['kpi'] = 'subscribe';
$subscribe = $this->mdSyliveActivityKpidata->count($where_sort);
}
if ($sort == 'watch') {
$watch = $v['total'];
} else {
$where_sort['kpi'] = 'watch';
$watch = $this->mdSyliveActivityKpidata->count($where_sort);
}
if ($sort == 'order') {
$order = $v['total'];
} else {
$where_order['kpi'] = 'order';
$where_order['cfUserId'] = $cfUserId;
$itemId && $where_order['itemId'] = $itemId;
$order = $this->mdSyliveActivityKpidata->count($where_order);
}
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count(['cfUserId' => $cfUserId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd, 'type' => $type]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['cfUserId' => $cfUserId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd, 'type' => $type]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
$list[] = ['consultantName' => $consultantName, 'browse' => $browse, 'subscribe' => $subscribe
, 'watch' => $watch, 'order' => $order, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
} else {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$where = ['activityId' => $activityId, 'type' => $type, 'itemId' => $itemId, 'cfUserId>' => 0, 'kpi' => $sort];
$count = $this->mdSyliveActivityKpidata->count($where, 'cfUserId');
} else {
$where = ['activityId' => $activityId, 'organizationId>' => 0];
$count = $this->mdSyliveActivityUser->count($where, 'userId');
}
if ($count) {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$res = $this->mdSyliveActivityKpidata->select_groupby('cfUserId', $where, "orderTotal {$order},id desc", $page, $limit, "cfUserId as userId,bizId,count(id) as orderTotal");
} else {
$res = $this->mdSyliveActivityUser->select_groupby('userId', $where, "{$sort_order},id desc", $page
, $limit, 'userId,bizId,browse,subscribe,orderTotal,watch');
}
foreach ($res as $v) {
$userId = intval($v['userId']);
$re_org = $this->mdSyliveOrganization->get(['organizationId' => $v['bizId'], 'status>=' => 0]);
$organizationName = $re_org['organizationName'] ? $re_org['organizationName'] : '-';
$re_user = $this->mdSyliveUser->get(['userId' => $userId, 'status>=' => 0]);
$uname = $re_user['uname'] ? $re_user['uname'] : $re_user['nickname'];
$consultantName = $organizationName . ' ' . $uname;
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count(['cfUserId' => $userId, 'activityId' => $activityId, 'type' => $type]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['cfUserId' => $userId, 'activityId' => $activityId, 'type' => $type]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
if ($sort != 'order' && $itemId) {//有商品id,区分订单
$order = $this->mdSyliveActivityKpidata->count(['kpi' => 'order', 'cfUserId' => $userId, 'type' => $type, 'itemId' => $itemId, 'activityId' => $activityId]);
} else {
$order = $v['orderTotal'];
}
if ($sort == 'order' && $itemId) {//只改变订单数值,其它不变
$re = $this->mdSyliveActivityUser->get(['userId' => $userId, 'activityId' => $activityId, 'organizationId>' => 0], 'browse,subscribe,watch');
$v['browse'] = intval($re['browse']);
$v['subscribe'] = intval($re['subscribe']);
$v['watch'] = intval($re['watch']);
}
$list[] = ['consultantName' => $consultantName, 'browse' => $v['browse'], 'subscribe' => $v['subscribe'], 'watch' => $v['watch']
, 'order' => $order, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
}
}
$date = ['list' => $list, 'count' => $count];
$this->return_response_list($date);
}
/**
* Notes:团队数据统计主页
* Created on: 2022/10/27 10:02
* Created by: dengbw
* @param null $activityId
*/
public function team_get($activityId = null)
{
$activityId = intval($activityId);
if (!$activityId) {
$this->return_json('参数错误');
}
$type = 1;
$title = '';
$timeStart = '0000-00-00 00:00:00';
$activityData1 = $activityData2 = $areaAry = $funnel = $allTeamIds = [];
$days[] = ['name' => '全部日期', 'value' => ''];
$goods[] = ['name' => '全部订单', 'value' => ''];
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re) {
$res_item = $this->mdSyliveItems->select(['status>=' => 0, 'activityId' => $activityId], 'sort asc,itemId desc'
, 0, 0, 'itemId,title');
foreach ($res_item as $k => $v) {
$goods[] = ['name' => $v['title'], 'value' => intval($v['itemId'])];
}
if ($re['timeEnd'] != '0000-00-00 00:00:00') {
$timeEnd = date("Y-m-d", strtotime($re['timeEnd']));
for ($i = 6; $i >= 0; $i--) {
$day = date("Y-m-d", strtotime("$timeEnd -{$i} day"));
$days[] = ['name' => $day, 'value' => $day];
}
}
$title = $re['title'];
$timeStart = $re['timeStart'];
$channelId = $re['channelId'];
$res_biz = $this->mdSyliveActivityTeam->select(['activityId' => $activityId, 'status>=' => 0], 'browse desc');
foreach ($res_biz as $k => $v) {
$areaId = intval($v['teamId1']);
$bizId = intval($v['teamId2']);
if ($areaId) {
if (!in_array($areaId, $areaAry)) {
$areaAry[] = $areaId;
}
if (!in_array($areaId, $allTeamIds)) {
$allTeamIds[] = $areaId;
}
}
if ($bizId) {
$allOrganizationIds[] = $bizId;
$res_org = $this->mdSyliveTeam->select(['parentId' => $bizId, 'status>=' => 0], 'teamId desc', 0, 0, 'teamId');
foreach ($res_org as $k2 => $v2) {
$allTeamIds[] = $v2['teamId'];
}
}
}
$browse = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => $type, 'kpi' => 'browse']);
$subscribe = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => $type, 'kpi' => 'subscribe']);
$watch = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => $type, 'kpi' => 'watch']);
$order = $this->mdSyliveOrder->count(['activityId' => $activityId, 'cfrom' => $type, 'status' => 1]);
$duration_min = $livePV = $liveUV = $watchDuration = $watchDuration = $pvDuration = 0;
if ($channelId) {
$re_duration = $this->mdPolyvSession->sum('duration', ['channelId' => $channelId]);
if ($re_duration['duration']) {
$re_livePV = $this->mdPolyvSession->sum('livePV', ['channelId' => $channelId]);
$re_liveUV = $this->mdPolyvSession->sum('liveUV', ['channelId' => $channelId]);
$livePV = $re_livePV['livePV'];
$liveUV = $re_liveUV['liveUV'];
$re_totalPlayDuration = $this->mdPolyvSession->sum('totalPlayDuration', ['channelId' => $channelId]);
$duration_min = round($re_duration['duration'] / 60);
$totalPlayDuration_min = round($re_totalPlayDuration['totalPlayDuration'] / 60);
$watchDuration = round($totalPlayDuration_min / $liveUV);
$pvDuration = round($livePV / $liveUV, 1);
}
}
$allConsultant = 0;
$consultant = $this->mdSyliveActivityUser->count(['activityId' => $activityId, 'teamId>' => 0, 'userId>' => 0]);
if ($allTeamIds) {
$str_teamIds = implode(',', array_unique($allTeamIds));
$allConsultant = $this->mdSyliveUser->count(["teamId in({$str_teamIds})" => null, 'status>=' => 0, 'teamId>' => 0]);
}
$consultant > $allConsultant && $allConsultant = $consultant;
$activityData1 = [['name' => '参与团队数', 'value' => count($areaAry)]
, ['name' => '参与/全部团员数', 'value' => $consultant . '/' . $allConsultant]];
$url = "/sylive/activity/statistics/piechart?activityId={$activityId}&type={$type}&";
$watch_un = 0;
if ($liveUV) {
$watch_un = $watch - $liveUV;
$watch_un < 0 && $watch_un = 0;
}
$activityData2 = [['name' => '浏览数(人)', 'value' => $browse, 'url' => ''], ['name' => '预约数(人)', 'value' => $subscribe, 'url' => '']
, ['name' => '订单数(单)', 'value' => $order, 'url' => ''], ['name' => '有效观看(人)', 'value' => $liveUV, 'url' => ''], ['name' => '无效观看(人)', 'value' => $watch_un, 'url' => '']
, ['name' => '直播时长(分)', 'value' => $duration_min, 'url' => ''], ['name' => '观看数(人次)', 'value' => $livePV, 'url' => '']
, ['name' => '人均观看(分)', 'value' => $watchDuration, 'url' => ''], ['name' => '人均观看(次)', 'value' => $pvDuration, 'url' => '']];
// $activityData2 = [['name' => '浏览数(人)', 'value' => $browse, 'url' => "{$url}kpi=browse"], ['name' => '预约数(人)', 'value' => $subscribe, 'url' => "{$url}kpi=subscribe"]
// , ['name' => '观看数(人)', 'value' => $watch, 'url' => "{$url}kpi=watch"], ['name' => '订单数(单)', 'value' => $order, 'url' => "{$url}kpi=order"]
// , ['name' => '直播时长(分)', 'value' => $duration, 'url' => ''], ['name' => '观看数(人次)', 'value' => $livePV, 'url' => '']
// , ['name' => '人均观看(分)', 'value' => $watchDuration, 'url' => ''], ['name' => '人均观看(次)', 'value' => $pvDuration, 'url' => '']];
}
$data = ['activityId' => $activityId, 'title' => $title, 'timeStart' => strtotime($timeStart), 'activityData1' => ['list' => $activityData1, 'style' => 'width: 50%']
, 'activityData2' => ['list' => $activityData2, 'style' => 'width: 11.1%'], 'tableData' => ['days' => $days, 'goods' => $goods]];
$this->return_response($data);
}
/**
* Notes:团队统计列表
* Created on: 2022/10/27 14:48
* Created by: dengbw
*/
public function page_team_biz_get()
{
$activityId = intval($this->input_param('activityId'));
$page = $this->input_param('page');
$limit = $this->input_param('limit');
$sort = $this->input_param('sort');
$order = $this->input_param('order');
$day = $this->input_param('day');
$itemId = intval($this->input_param('itemId'));//商品
!$page && $page = 1;
!$limit && $limit = 10;
$list = [];
$count = 0;
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re) {
$type = 1;
$channelId = intval($re['channelId']);
!$sort && $sort = 'browse';
!$order && $order = 'desc';
if ($sort == 'order' && !$day) {
$sort_order = 'orderTotal ' . $order;
} else {
$sort_order = $sort . ' ' . $order;
}
if ($day) {//按天搜索
$createTimeStart = strtotime($day . ' 00:00:00');
$createTimeEnd = strtotime($day . ' 23:59:59');
$where = $where_order = $where_sort = ['activityId' => $activityId, 'day' => $day, 'type' => $type, 'bizId>' => 0, 'kpi' => $sort];
if ($sort == 'order') {
$itemId && $where['itemId'] = $itemId;
}
$count = $this->mdSyliveActivityKpidata->count($where, 'bizId');
if ($count) {
$res = $this->mdSyliveActivityKpidata->select_groupby('bizId', $where, "total {$order},id desc", $page, $limit, "bizId,count(id) as total");
foreach ($res as $v) {
$bizId = intval($v['bizId']);
$re_biz = $this->mdSyliveTeam->get(['teamId' => $bizId, 'status>=' => 0]);
$bizName = $re_biz ? $re_biz['teamName'] : '-';
$where_sort['bizId'] = $bizId;
if ($sort == 'browse') {
$browse = $v['total'];
} else {
$where_sort['kpi'] = 'browse';
$browse = $this->mdSyliveActivityKpidata->count($where_sort);
}
if ($sort == 'subscribe') {
$subscribe = $v['total'];
} else {
$where_sort['kpi'] = 'subscribe';
$subscribe = $this->mdSyliveActivityKpidata->count($where_sort);
}
if ($sort == 'watch') {
$watch = $v['total'];
} else {
$where_sort['kpi'] = 'watch';
$watch = $this->mdSyliveActivityKpidata->count($where_sort);
}
if ($sort == 'order') {
$orderTotal = $v['total'];
} else {
$where_order['kpi'] = 'order';
$where_order['bizId'] = $bizId;
$itemId && $where_order['itemId'] = $itemId;
$orderTotal = $this->mdSyliveActivityKpidata->count($where_order);
}
$consultant = $this->mdSyliveActivityUser->count(['bizId' => $bizId, 'activityId' => $activityId, 'teamId>' => 0, 'userId>' => 0]);
$consultants = [];
$consultants[] = $bizId;
$res_org = $this->mdSyliveTeam->select(['parentId' => $bizId, 'status>=' => 0], 'teamId desc', 0, 0, 'teamId');
foreach ($res_org as $k2 => $v2) {
$consultants[] = $v2['teamId'];
}
$str_organizationIds = implode(',', array_unique($consultants));
$allConsultant = $this->mdSyliveUser->count(["teamId in({$str_organizationIds})" => null, 'status>=' => 0, 'teamId>' => 0]);
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count(['bizId' => $bizId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd, 'type' => $type]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['bizId' => $bizId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd, 'type' => $type]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
$list[] = [
'bizName' => $bizName, 'consultant' => $consultant, 'allConsultant' => $allConsultant, 'browse' => $browse, 'subscribe' => $subscribe
, 'watch' => $watch, 'order' => $orderTotal, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
} else {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$where = ['activityId' => $activityId, 'type' => $type, 'itemId' => $itemId, 'bizId>' => 0, 'kpi' => $sort];
$count = $this->mdSyliveActivityKpidata->count($where, 'bizId');
} else {
$where = ['activityId' => $activityId, 'status>=' => 0, 'teamId2>' => 0];
$count = $this->mdSyliveActivityTeam->count($where);
}
if ($count) {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$res = $this->mdSyliveActivityKpidata->select_groupby('bizId', $where, "orderTotal {$order},id desc", $page, $limit, "bizId,count(id) as orderTotal");
} else {
$res = $this->mdSyliveActivityTeam->select($where, "{$sort_order},id desc", $page, $limit
, 'teamId2 as bizId,browse,subscribe,watch,orderTotal');
}
foreach ($res as $v) {
$bizId = intval($v['bizId']);
$re_biz = $this->mdSyliveTeam->get(['teamId' => $bizId, 'status>=' => 0]);
$bizName = $re_biz ? $re_biz['teamName'] : '-';
$consultant = $this->mdSyliveActivityUser->count(['bizId' => $bizId, 'activityId' => $activityId, 'teamId>' => 0, 'userId>' => 0]);
//统计顾问数
$consultants = [];
$consultants[] = $bizId;
$res_org = $this->mdSyliveTeam->select(['parentId' => $bizId, 'status>=' => 0], 'teamId desc', 0, 0, 'teamId');
foreach ($res_org as $k2 => $v2) {
$consultants[] = $v2['teamId'];
}
$str_organizationIds = implode(',', array_unique($consultants));
$allConsultant = $this->mdSyliveUser->count(["teamId in({$str_organizationIds})" => null, 'status>=' => 0, 'teamId>' => 0]);
$consultant > $allConsultant && $allConsultant = $consultant;
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count(['bizId' => $bizId, 'activityId' => $activityId, 'type' => $type]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['bizId' => $bizId, 'activityId' => $activityId, 'type' => $type]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
if ($sort != 'order' && $itemId) {//有商品id,区分订单
$order = $this->mdSyliveActivityKpidata->count(['kpi' => 'order', 'bizId' => $bizId, 'type' => $type, 'itemId' => $itemId, 'activityId' => $activityId]);
} else {
$order = $v['orderTotal'];
}
if ($sort == 'order' && $itemId) {//只改变订单数值,其它不变
$re = $this->mdSyliveActivityTeam->get(['teamId2' => $bizId, 'activityId' => $activityId, 'status>=' => 0], 'browse,subscribe,watch');
$v['browse'] = intval($re['browse']);
$v['subscribe'] = intval($re['subscribe']);
$v['watch'] = intval($re['watch']);
}
$list[] = [
'bizName' => $bizName, 'consultant' => $consultant, 'allConsultant' => $allConsultant, 'browse' => $v['browse'], 'subscribe' => $v['subscribe']
, 'watch' => $v['watch'], 'order' => $order, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
}
}
$date = ['list' => $list, 'count' => $count];
$this->return_response_list($date);
}
/**
* Notes:团员统计列表
* Created on: 2022/11/11 14:48
* Created by: dengbw
*/
public function page_team_consultant_get()
{
$activityId = intval($this->input_param('activityId'));
$page = $this->input_param('page');
$limit = $this->input_param('limit');
$sort = $this->input_param('sort');
$order = $this->input_param('order');
$day = $this->input_param('day');
$itemId = intval($this->input_param('itemId'));//商品
!$page && $page = 1;
!$limit && $limit = 10;
$list = [];
$count = 0;
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re) {
$type = 1;
$channelId = intval($re['channelId']);
!$sort && $sort = 'browse';
!$order && $order = 'desc';
if ($sort == 'order' && !$day) {
$sort_order = 'orderTotal ' . $order;
} else {
$sort_order = $sort . ' ' . $order;
}
if ($day) {
$createTimeStart = strtotime($day . ' 00:00:00');
$createTimeEnd = strtotime($day . ' 23:59:59');
$where = $where_order = $where_sort = ['activityId' => $activityId, 'day' => $day, 'type' => $type, 'cfUserId>' => 0, 'kpi' => $sort];
if ($sort == 'order') {
$itemId && $where['itemId'] = $itemId;
}
$count = $this->mdSyliveActivityKpidata->count($where, 'cfUserId');
if ($count) {
$res = $this->mdSyliveActivityKpidata->select_groupby('cfUserId', $where, "total {$order},id desc", $page, $limit, "bizId,cfUserId,count(id) as total");
foreach ($res as $v) {
$cfUserId = intval($v['cfUserId']);
$re_org = $this->mdSyliveTeam->get(['teamId' => $v['bizId'], 'status>=' => 0]);
$organizationName = $re_org['teamName'] ? $re_org['teamName'] : '-';
$re_user = $this->mdSyliveUser->get(['userId' => $cfUserId, 'status>=' => 0]);
$uname = $re_user['uname'] ? $re_user['uname'] : $re_user['nickname'];
$consultantName = $organizationName . ' ' . $uname;
$where_sort['cfUserId'] = $cfUserId;
if ($sort == 'browse') {
$browse = $v['total'];
} else {
$where_sort['kpi'] = 'browse';
$browse = $this->mdSyliveActivityKpidata->count($where_sort);
}
if ($sort == 'subscribe') {
$subscribe = $v['total'];
} else {
$where_sort['kpi'] = 'subscribe';
$subscribe = $this->mdSyliveActivityKpidata->count($where_sort);
}
if ($sort == 'watch') {
$watch = $v['total'];
} else {
$where_sort['kpi'] = 'watch';
$watch = $this->mdSyliveActivityKpidata->count($where_sort);
}
if ($sort == 'order') {
$order = $v['total'];
} else {
$where_order['kpi'] = 'order';
$where_order['cfUserId'] = $cfUserId;
$itemId && $where_order['itemId'] = $itemId;
$order = $this->mdSyliveActivityKpidata->count($where_order);
}
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count(['cfUserId' => $cfUserId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd, 'type' => $type]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['cfUserId' => $cfUserId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd, 'type' => $type]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
$list[] = ['consultantName' => $consultantName, 'browse' => $browse, 'subscribe' => $subscribe
, 'watch' => $watch, 'order' => $order, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
} else {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$where = ['activityId' => $activityId, 'type' => $type, 'itemId' => $itemId, 'cfUserId>' => 0, 'kpi' => $sort];
$count = $this->mdSyliveActivityKpidata->count($where, 'cfUserId');
} else {
$where = ['activityId' => $activityId, 'teamId>' => 0];
$count = $this->mdSyliveActivityUser->count($where, 'userId');
}
if ($count) {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$res = $this->mdSyliveActivityKpidata->select_groupby('cfUserId', $where, "orderTotal {$order},id desc"
, $page, $limit, "cfUserId as userId,bizId,count(id) as orderTotal");
} else {
$res = $this->mdSyliveActivityUser->select_groupby('userId', $where, "{$sort_order},id desc", $page
, $limit, 'userId,bizId,browse,subscribe,orderTotal,watch');
}
foreach ($res as $v) {
$userId = intval($v['userId']);
$re_org = $this->mdSyliveTeam->get(['teamId' => $v['bizId'], 'status>=' => 0]);
$organizationName = $re_org['teamName'] ? $re_org['teamName'] : '-';
$re_user = $this->mdSyliveUser->get(['userId' => $userId, 'status>=' => 0]);
$uname = $re_user['uname'] ? $re_user['uname'] : $re_user['nickname'];
$consultantName = $organizationName . ' ' . $uname;
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count(['cfUserId' => $userId, 'activityId' => $activityId, 'type' => $type]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['cfUserId' => $userId, 'activityId' => $activityId, 'type' => $type]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
if ($sort != 'order' && $itemId) {//有商品id,区分订单
$order = $this->mdSyliveActivityKpidata->count(['kpi' => 'order', 'cfUserId' => $userId, 'type' => $type, 'itemId' => $itemId, 'activityId' => $activityId]);
} else {
$order = $v['orderTotal'];
}
if ($sort == 'order' && $itemId) {//只改变订单数值,其它不变
$re = $this->mdSyliveActivityUser->get(['userId' => $userId, 'activityId' => $activityId, 'teamId>' => 0], 'browse,subscribe,watch');
$v['browse'] = intval($re['browse']);
$v['subscribe'] = intval($re['subscribe']);
$v['watch'] = intval($re['watch']);
}
$list[] = ['consultantName' => $consultantName, 'browse' => $v['browse'], 'subscribe' => $v['subscribe'], 'watch' => $v['watch']
, 'order' => $order, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
}
}
$date = ['list' => $list, 'count' => $count];
$this->return_response_list($date);
}
/**
* Notes:获取区域饼状数据
* Created on: 2022/10/9 17:21
* Created by: dengbw
*/
public function piechart_get()
{
$activityId = intval($this->input_param('activityId'));
$type = intval($this->input_param('type'));
$kpi = $this->input_param('kpi');
if (!$activityId || !$kpi) {
$this->return_json('参数错误');
}
$data = [];
if ($type == 1) {
$res = $this->mdSyliveActivityTeam->select(['activityId' => $activityId, 'status>=' => 0], 'browse desc', 0, 0, 'distinct(teamId1) as areaId');
foreach ($res as $k => $v) {
$areaId = intval($v['areaId']);
$re = $this->mdSyliveTeam->get(['teamId' => $areaId]);
$name = $re['teamName'] ? $re['teamName'] : '-';
$re_kpi = $this->mdSyliveActivityTeam->sum($kpi, ['teamId1' => $areaId, 'status>=' => 0, 'activityId' => $activityId]);
$data[] = ['name' => $name, 'value' => $re_kpi[$kpi] ? $re_kpi[$kpi] : '0'
, 'url' => "/sylive/activity/statistics/barchart?activityId={$activityId}&areaId={$areaId}&kpi={$kpi}&type={$type}"];
}
} else {
$res = $this->mdSyliveActivityBiz->select(['activityId' => $activityId, 'status>=' => 0], 'browse desc', 0, 0, 'distinct(areaId)');
foreach ($res as $k => $v) {
$areaId = intval($v['areaId']);
$re = $this->mdSyliveOrganization->get(['organizationId' => $areaId]);
$name = $re['organizationName'] ? $re['organizationName'] : '-';
$re_kpi = $this->mdSyliveActivityBiz->sum($kpi, ['areaId' => $areaId, 'status>=' => 0, 'activityId' => $activityId]);
$data[] = ['name' => $name, 'value' => $re_kpi[$kpi] ? $re_kpi[$kpi] : '0'
, 'url' => "/sylive/activity/statistics/barchart?activityId={$activityId}&areaId={$areaId}&kpi={$kpi}&type={$type}"];
}
}
$this->return_response($data);
}
/**
* Notes:获取门店柱状数据
* Created on: 2022/10/9 17:30
* Created by: dengbw
*/
public function barchart_get()
{
$activityId = intval($this->input_param('activityId'));
$areaId = intval($this->input_param('areaId'));
$type = intval($this->input_param('type'));
$kpi = $this->input_param('kpi');
if (!$activityId || !$areaId || !$kpi) {
$this->return_json('参数错误');
}
$data = $dataTitle = $dataValue = $dataUrl = [];
if ($type == 1) {
$res = $this->mdSyliveActivityTeam->select(['activityId' => $activityId, 'teamId1' => $areaId, 'status>=' => 0]);
foreach ($res as $k => $v) {
$bizId = intval($v['teamId2']);
$re = $this->mdSyliveTeam->get(['teamId' => $bizId]);
$name = $re['teamName'] ? $re['teamName'] : '-';
$value = $v[$kpi] ? $v[$kpi] : '0';
$url = "/sylive/activity/statistics/storebarchart?activityId={$activityId}&bizId={$bizId}&kpi={$kpi}&type={$type}";
$data[] = ['name' => $name, 'value' => $value, 'url' => $url];
}
} else {
$res = $this->mdSyliveActivityBiz->select(['activityId' => $activityId, 'areaId' => $areaId, 'status>=' => 0]);
foreach ($res as $k => $v) {
$bizId = intval($v['bizId']);
$re = $this->mdSyliveOrganization->get(['organizationId' => $bizId]);
$name = $re['organizationName'] ? $re['organizationName'] : '-';
$value = $v[$kpi] ? $v[$kpi] : '0';
$url = "/sylive/activity/statistics/storebarchart?activityId={$activityId}&bizId={$bizId}&kpi={$kpi}&type={$type}";
$data[] = ['name' => $name, 'value' => $value, 'url' => $url];
}
}
if ($data) {
$edit = array_column($data, 'value');
array_multisort($edit, SORT_ASC, $data);
foreach ($data as $k => $v) {
$dataTitle[] = $v['name'];
$dataValue[] = $v['value'];
$dataUrl[] = $v['url'];
}
}
$data['dataTitle'] = $dataTitle;
$data['dataValue'] = $dataValue;
$data['dataUrl'] = $dataUrl;
$this->return_response($data);
}
/**
* Notes:获取顾问柱状数据
* Created on: 2022/10/10 10:08
* Created by: dengbw
*/
public function storebarchart_get()
{
$activityId = intval($this->input_param('activityId'));
$bizId = intval($this->input_param('bizId'));
$type = intval($this->input_param('type'));
$kpi = $this->input_param('kpi');
if (!$activityId || !$bizId || !$kpi) {
$this->return_json('参数错误');
}
$data = $dataTitle = $dataValue = [];
$res_user = $this->mdSyliveActivityKpidata->select(['activityId' => $activityId, 'bizId' => $bizId, 'type' => $type]
, 'id desc', 0, 0, 'distinct(cfUserId)');
foreach ($res_user as $k => $v) {
$cfUserId = intval($v['cfUserId']);
$re_user = $this->mdSyliveUser->get(['userId' => $cfUserId]);
if ($re_user) {
$name = $re_user['uname'] ? $re_user['uname'] : $re_user['nickname'];
$value = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'cfUserId' => $cfUserId, 'type' => $type, 'kpi' => $kpi]);
$data[] = ['name' => $name ? $name : '-', 'value' => $value];
}
}
if ($data) {
$edit = array_column($data, 'value');
array_multisort($edit, SORT_DESC, $data);
foreach ($data as $k => $v) {
$dataTitle[] = $v['name'];
$dataValue[] = $v['value'];
}
}
$data['dataTitle'] = $dataTitle;
$data['dataValue'] = $dataValue;
$this->return_response($data);
}
/**
* Notes:获取区域折线图数据
* Created on: 2022/11/22 9:39
* Created by: dengbw
*/
public function stacked_area_get()
{
$activityId = intval($this->input_param('activityId'));
$kpi = $this->input_param('kpi');
$legendData = $yAxisSeries = $xAxisData = [];
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
!$kpi && $kpi = 'subscribe';
if ($re && $re['timeEnd'] != '0000-00-00 00:00:00') {
$type = 0;
$timeEnd = date("Y-m-d", strtotime($re['timeEnd']));
for ($i = 6; $i >= 0; $i--) {
$day = date("Y-m-d", strtotime("$timeEnd -{$i} day"));
$xAxisData[] = $day;
}
$res = $this->mdSyliveActivityBiz->select_groupby('areaId', ['activityId' => $activityId, 'status>=' => 0
, 'areaId>' => 0], "$kpi desc", 0, 0, 'areaId');
foreach ($res as $v) {
$areaId = intval($v['areaId']);
$re_area = $this->mdSyliveOrganization->get(['organizationId' => $areaId]);
$organizationName = $re_area['organizationName'] ? $re_area['organizationName'] : '-';
$counts = [];
foreach ($xAxisData as $v2) {
$counts[] = $this->mdSyliveActivityKpidata->count(['kpi' => $kpi, 'areaId' => $areaId, 'type' => $type
, 'day' => $v2, 'activityId' => $activityId]);
}
$legendData[] = $organizationName;
$yAxisSeries[] = ['name' => $organizationName, 'type' => 'line', 'data' => $counts];
}
}
//折线图数据
$date = ['legendData' => $legendData, 'xAxisData' => $xAxisData, 'yAxisSeries' => $yAxisSeries];
$this->return_response_list($date);
}
/**
* Notes:获取观看数-订单数区域折线图数据
* Created on: 2022/11/23 10:54
* Created by: dengbw
*/
public function stacked_watchOrder_get()
{
$activityId = intval($this->input_param('activityId'));
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
$times = $legendData = $yAxisSeries = $xAxisData = [];
if ($re) {
$type = 0;
$timeStart = $re['timeStart'];
$timeEnd = $re['timeEnd'];
if ($timeStart != '0000-00-00 00:00:00' && (date('Y-m-d H:i:s') > $timeStart)) {
$start_Time = strtotime($timeStart);
$end_Time = strtotime($timeEnd);
$legend_data = [['name' => date('Y-m-d', $start_Time) . '的观看数', 'value' => 'watch'],
['name' => date('Y-m-d', $start_Time) . '的订单数', 'value' => 'order']];
$add_time = '+1 hour';
$diff = abs($start_Time - $end_Time); // 获取时间差
$hour = ceil($diff / 3600); // 获取相差小时数
$hour <= 6 && $add_time = '+30 minute';//转分钟显示
$ii = 0;
while ($start_Time < $end_Time) {
$start = date('Y-m-d H:i:s', $start_Time);
$ii > 0 && $start = date('Y-m-d H:i:s', $start_Time + 60);
$name_start = date('H:i', $start_Time);
$start_Time = strtotime($add_time, $start_Time);
$name = $name_start . '-' . date('H:i', $start_Time);
$end = date('Y-m-d H:i:s', $start_Time + 59);
$times[] = ['name' => $name, 'start' => $start, 'end' => $end];
$xAxisData[] = $name;
$ii++;
}
foreach ($legend_data as $v) {
$legendData[] = $v['name'];
$counts = [];
foreach ($times as $v2) {
$start = strtotime($v2['start']);
$end = strtotime($v2['end']);
$counts[] = $this->mdSyliveActivityKpidata->count(['kpi' => $v['value'], 'type' => $type
, 'createTime >=' => $start, 'createTime <=' => $end, 'activityId' => $activityId]);
}
$yAxisSeries[] = ['name' => $v['name'], 'type' => 'line', 'data' => $counts];
}
}
}
$date = ['legendData' => $legendData, 'xAxisData' => $xAxisData, 'yAxisSeries' => $yAxisSeries];
$this->return_response_list($date);
}
/**
* Notes:查询排名
* Created on: 2022/11/3 19:45
* Created by: dengbw
*/
public function ranking_get()
{
$activityId = intval($this->input_param('activityId'));
$type = intval($this->input_param('type'));
$kpi = $this->input_param('kpi');
$itemId = intval($this->input_param('itemId'));//商品
$area = $biz = $consultant = [];
$kpi == 'order' && $kpi = 'orderTotal';
if ($type == 1) {
//团队排名
if ($kpi == 'orderTotal' && $itemId) {
$where_order = ['activityId' => $activityId, 'type' => $type, 'itemId' => $itemId, 'bizId>' => 0, 'kpi' => 'order'];
$res = $this->mdSyliveActivityKpidata->select_groupby('bizId', $where_order, "orderTotal desc,id desc", 1, 10, "bizId,count(id) as orderTotal");
} else {
$res = $this->mdSyliveActivityTeam->select(['activityId' => $activityId, 'status>=' => 0]
, "{$kpi} desc,id desc", 1, 10, "{$kpi},teamId2 as bizId");
}
if ($res) {
$bizMax = $res[0][$kpi];
foreach ($res as $k => $v) {
$bizId = intval($v['bizId']);
$re = $this->mdSyliveTeam->get(['teamId' => $bizId]);
$name = $re['teamName'] ? $re['teamName'] : '-';
$percent = ceil(($v[$kpi] / $bizMax) * 100);
$biz[] = ['name' => $name, 'value' => $v[$kpi], 'percent' => $percent];
}
}
//顾问排名
if ($kpi == 'orderTotal' && $itemId) {
$where_order = ['activityId' => $activityId, 'type' => $type, 'itemId' => $itemId, 'cfUserId>' => 0, 'kpi' => 'order'];
$res = $this->mdSyliveActivityKpidata->select_groupby('cfUserId', $where_order, "orderTotal desc,id desc", 1, 10, "bizId,cfUserId as userId,count(id) as orderTotal");
} else {
$res = $this->mdSyliveActivityUser->select_groupby('userId', ['activityId' => $activityId, 'teamId>' => 0]
, "{$kpi} desc,id desc", 1, 10, "{$kpi},bizId,userId");
}
if ($res) {
$consultantMax = $res[0][$kpi];
foreach ($res as $k => $v) {
$userId = intval($v['userId']);
$re_user = $this->mdSyliveUser->get(['userId' => $userId, 'status>=' => 0]);
$name = $re_user['uname'] ? $re_user['uname'] : $re_user['nickname'];
$re_org = $this->mdSyliveTeam->get(['teamId' => $v['bizId']]);
$re_org && $name .= "({$re_org['teamName']})";
$percent = ceil(($v[$kpi] / $consultantMax) * 100);
$consultant[] = ['name' => $name, 'value' => $v[$kpi], 'percent' => $percent];
}
}
} else {
//大区排名
$res = $this->mdSyliveActivityBiz->select(['activityId' => $activityId, 'status>=' => 0, 'bizId>=' => 0], "id desc", 0, 0, 'distinct(areaId)');
foreach ($res as $k => $v) {
$areaId = intval($v['areaId']);
$re = $this->mdSyliveOrganization->get(['organizationId' => $areaId]);
$name = $re['organizationName'] ? $re['organizationName'] : '-';
if ($kpi == 'orderTotal' && $itemId) {
$value[$kpi] = $this->mdSyliveActivityKpidata->count(['kpi' => 'order', 'areaId' => $areaId, 'type' => $type, 'itemId' => $itemId, 'activityId' => $activityId]);
} else {
$value = $this->mdSyliveActivityBiz->sum($kpi, ['areaId' => $areaId, 'activityId' => $activityId]);
}
$area[] = ['name' => $name, 'value' => $value[$kpi], 'percent' => 0];
}
if ($res) {
$edit = array_column($area, 'value');
array_multisort($edit, SORT_DESC, $area);
$areaMax = $area[0]['value'];
foreach ($area as $k => $v) {
$percent = ceil(($v['value'] / $areaMax) * 100);
$area[$k]['percent'] = $percent;
}
$area = array_splice($area, 0, 10);
}
//门店排名
if ($kpi == 'orderTotal' && $itemId) {
$where_order = ['activityId' => $activityId, 'type' => $type, 'itemId' => $itemId, 'bizId>' => 0, 'kpi' => 'order'];
$res = $this->mdSyliveActivityKpidata->select_groupby('bizId', $where_order, "orderTotal desc,id desc", 1, 10, "bizId,count(id) as orderTotal");
} else {
$res = $this->mdSyliveActivityBiz->select(['activityId' => $activityId, 'status>=' => 0]
, "{$kpi} desc,id desc", 1, 10, "{$kpi},bizId");
}
if ($res) {
$bizMax = $res[0][$kpi];
foreach ($res as $k => $v) {
$bizId = intval($v['bizId']);
$re = $this->mdSyliveOrganization->get(['organizationId' => $bizId]);
$name = $re['organizationName'] ? $re['organizationName'] : '-';
$percent = ceil(($v[$kpi] / $bizMax) * 100);
$biz[] = ['name' => $name, 'value' => $v[$kpi], 'percent' => $percent];
}
}
//顾问排名
if ($kpi == 'orderTotal' && $itemId) {
$where_order = ['activityId' => $activityId, 'type' => $type, 'itemId' => $itemId, 'cfUserId>' => 0, 'kpi' => 'order'];
$res = $this->mdSyliveActivityKpidata->select_groupby('cfUserId', $where_order, "orderTotal desc,id desc", 1, 10, "bizId,cfUserId as userId,count(id) as orderTotal");
} else {
$res = $this->mdSyliveActivityUser->select_groupby('userId', ['activityId' => $activityId, 'organizationId>' => 0]
, "{$kpi} desc,id desc", 1, 10, "{$kpi},bizId,userId");
}
if ($res) {
$consultantMax = $res[0][$kpi];
foreach ($res as $k => $v) {
$userId = intval($v['userId']);
$re_user = $this->mdSyliveUser->get(['userId' => $userId, 'status>=' => 0]);
$name = $re_user['uname'] ? $re_user['uname'] : $re_user['nickname'];
$re_org = $this->mdSyliveOrganization->get(['organizationId' => $v['bizId'], 'status>=' => 0]);
$re_org && $name .= "({$re_org['organizationName']})";
$percent = ceil(($v[$kpi] / $consultantMax) * 100);
$consultant[] = ['name' => $name, 'value' => $v[$kpi], 'percent' => $percent];
}
}
}
$data['area'] = $area;
$data['biz'] = $biz;
$data['consultant'] = $consultant;
$this->return_response($data);
}
/**
* Notes:漏头数据
* Created on: 2022/11/17 14:17
* Created by: dengbw
*/
public function funnel_get()
{
$activityId = intval($this->input_param('activityId'));
$type = intval($this->input_param('type'));
$itemId = intval($this->input_param('itemId'));//商品
$timeStart = $this->input_param('timeStart');//直播开始时间
$show = $this->input_param('show');
$funnel_preheating = $funnel_live = [];
if ($show == 'left' || !$show) {
$left_browse_where = "userId in(select userId from lc_market_sylive_activity_kpidata where activityId = {$activityId} AND type = $type AND kpi = 'browse' AND createTime < {$timeStart})";
$left_browse = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => $type, 'kpi' => 'browse', 'createTime<' => $timeStart]);
$left_subscribe = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => $type, 'kpi' => 'subscribe', 'createTime<' => $timeStart]);
$left_watch = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => $type, 'kpi' => 'watch', $left_browse_where => null]);
if ($itemId) {
$left_order = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'itemId' => $itemId, 'type' => $type, 'kpi' => 'order', $left_browse_where => null]);
} else {
$left_order = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => $type, 'kpi' => 'order', $left_browse_where => null]);
}
$left_subscribe_per = number_format_com($left_subscribe / $left_browse * 100, 1, '');
$left_watch_per = number_format_com($left_watch / $left_subscribe * 100, 1, '');
$left_order_per = number_format_com($left_order / $left_watch * 100, 1, '');
$left_expected_data = [['name' => "浏览人数100%", 'value' => 100], ['name' => "预约人数({$left_subscribe_per}%)", 'value' => 75]
, ['name' => "观看人数({$left_watch_per}%)", 'value' => 50], ['name' => "订单数({$left_order_per}%)", 'value' => 25]];
$left_actual_data = [['name' => '浏览人数', 'value' => $left_browse], ['name' => '预约人数', 'value' => $left_subscribe]
, ['name' => '观看人数', 'value' => $left_watch], ['name' => '订单数', 'value' => $left_order]];
$funnel_preheating = ['title' => '浏览转化漏斗((预热阶段)', 'expectedData' => $left_expected_data, 'actualData' => $left_actual_data];
}
if ($show == 'right' || !$show) {
$right_browse_where = "userId in(select userId from lc_market_sylive_activity_kpidata where activityId = {$activityId} AND type = {$type} AND kpi = 'browse' AND createTime >= {$timeStart})";
$right_browse = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => $type, 'kpi' => 'browse', 'createTime>=' => $timeStart]);
$right_watch = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => $type, 'kpi' => 'watch', $right_browse_where => null]);
if ($itemId) {
$right_order = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'itemId' => $itemId, 'type' => $type, 'kpi' => 'order', $right_browse_where => null]);
} else {
$right_order = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => $type, 'kpi' => 'order', $right_browse_where => null]);
}
$right_watch_per = number_format_com($right_watch / $right_browse * 100, 1, '');
$right_order_per = number_format_com($right_order / $right_watch * 100, 1, '');
$right_expected_data = [['name' => "浏览人数100%", 'value' => 100], ['name' => "观看人数({$right_watch_per}%)", 'value' => 66.7], ['name' => "订单数({$right_order_per}%)", 'value' => 33.3]];
$right_actual_data = [['name' => '浏览人数_' . $right_browse, 'value' => $right_browse], ['name' => '观看人数', 'value' => $right_watch], ['name' => '订单数', 'value' => $right_order]];
$funnel_live = ['title' => '浏览转化漏斗(直播阶段)', 'expectedData' => $right_expected_data, 'actualData' => $right_actual_data];
}
$data['funnelPreheating'] = $funnel_preheating;
$data['funnelLive'] = $funnel_live;
$this->return_response($data);
}
}