Files
liche/market/controllers/api/sylive/GroupsStatistics.php
T
xiaoyu 09cb72fb35 0210
2024-02-10 02:30:14 +08:00

1188 lines
68 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once APPPATH . 'controllers/api/BaseController.php';
/**
* Notes:私域直播_分组数据统计
* Created on: 2022/12/05 17:15
* Created by: dengbw
*/
class GroupsStatistics extends BaseController
{
public function __construct()
{
parent::__construct();
$this->load->model('market/Market_sylive_groups_model', 'mdSyliveGroups');
$this->load->model('market/Market_sylive_groups_user_model', 'mdSyliveGroupsUser');
$this->load->model('market/Market_sylive_user_model', 'mdSyliveUser');
$this->load->model('market/Market_sylive_activity_kpidata_model', 'mdSyliveActivityKpidata');
$this->load->model('market/Market_sylive_activity_model', 'mdSyliveActivity');
$this->load->model('market/Market_sylive_order_model', 'mdSyliveOrder');
$this->load->model('market/Market_sylive_items_model', 'mdSyliveItems');
$this->load->model('market/Market_sylive_viewlog_model', 'mdSyliveViewlog');
$this->load->model('live/Live_polyv_session_model', 'mdPolyvSession');
}
/**
* Notes:分组数据统计主页
* Created on: 2022/12/05 14:48
* Created by: dengbw
*/
public function index_get()
{
$activityId = intval($this->input_param('activityId'));
if (!$activityId) {
$this->return_json('参数错误');
}
$title = '';
$activityData1 = $activityData2 = $areaAry = $funnel = $allOrganizationIds = $levelData = $levels = [];
$days[] = ['name' => '全部日期', 'value' => ''];
$goods = [];
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re) {
$res_item = $this->mdSyliveItems->select(['status' => 0, 'activityId' => $activityId, 'price >' => 0], 'sort asc,itemId desc'
, 0, 0, 'itemId,title');
foreach ($res_item as $k => $v) {
$goods[] = ['name' => $v['title'], 'value' => intval($v['itemId'])];
}
$activityStart = strtotime($re['activityStart']) > 0 ? strtotime($re['activityStart']) : strtotime($re['timeStart']) - 7 * 24 * 60 * 60;
$end_time = time() < strtotime($re['timeEnd']) ? time() : strtotime($re['timeEnd']);
$diff_seconds = $end_time - $activityStart;
$diff_days = ceil($diff_seconds / 86400);
$timeEnd = strtotime($re['timeEnd']);//直播结束往前七天
for ($i = 0; $i < $diff_days; $i++) {
if (time() < strtotime($re['timeEnd'])) {
$day = date('Y-m-d', time() - $i * 24 * 60 * 60);
} else {
$day = date('Y-m-d', $timeEnd - $i * 24 * 60 * 60);
}
$days[] = ['name' => $day, 'value' => $day];
if ($i == 14) {
break;
}
}
$title = $re['title'];
$channelId = $re['channelId'];
$browse = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'kpi' => 'browse']);
$subscribe = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'kpi' => 'subscribe']);
$watch = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'kpi' => 'watch']);
//$beforeOrder = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'kpi' => 'beforeOrder']);
$beforeOrder = $this->mdSyliveOrder->count(['activityId' => $activityId, 'itemPrice' => '0', 'status' => 1]);
//$order = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'kpi' => 'order']);
$order = $this->mdSyliveOrder->count(['activityId' => $activityId, 'itemPrice >' => '0', 'type' => 0, '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);
}
}
$consultant = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, "cfUserId in (SELECT userId FROM lc_market_sylive_groups_user WHERE activityId = {$activityId} and bizId > 0 and status = 0)" => null], 'cfUserId');
$allConsultant = $this->mdSyliveGroupsUser->count(['activityId' => $activityId, 'status' => 0, 'bizId >' => 0]);
$consultant > $allConsultant && $allConsultant = $consultant;
$bizs = $this->mdSyliveGroups->count(['activityId' => $activityId, 'ifBiz' => 1, 'status>=' => 0]);
$activityData1 = [['name' => '参与门店数', 'value' => $bizs]
, ['name' => '参与/全部 顾问数', 'value' => $consultant . ' / ' . $allConsultant]];
$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' => $beforeOrder, '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' => '']];
}
$re_gro = $this->mdSyliveGroups->get(['activityId' => $activityId, 'parentId' => 0, 'status>=' => 0]);
if ($re_gro['statisticsType']) {
$this->load->model('market/Market_sys_dictionary_data_model', 'mdSysDictionaryData');
$res_dit = $this->mdSysDictionaryData->select(['status>=' => 0, 'dictId' => $re_gro['statisticsType']], 'sortNumber asc,dictDataId desc'
, 0, 0, 'dictDataCode,dictDataName');
foreach ($res_dit as $k => $v) {
$groupsLevel = intval($v['dictDataCode']);
$re_gro = $this->mdSyliveGroups->get(['activityId' => $activityId, 'groupsLevel' => $groupsLevel, 'status>=' => 0]);
if ($re_gro) {
$levelData[] = ['title' => $v['dictDataName'] . '统计', 'value' => $groupsLevel];
$levels[] = "{$v['dictDataName']},$groupsLevel";
}
}
}
$data = ['activityId' => $activityId, 'title' => $title, 'activityData1' => ['list' => $activityData1, 'style' => 'width: 50%']
, 'activityData2' => ['list' => $activityData2, 'style' => 'width: 20%'], 'levelData' => $levelData
, 'tableData' => ['days' => $days, 'goods' => $goods, 'levels' => $levels]
];
$this->return_response($data);
}
/**
* Notes:分组等级数据统计
* Created on: 2022/12/5 17:50
* Created by: dengbw
*/
public function level_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'));//商品
$groupsLevel = intval($this->input_param('level'));//等级
$levelId = "levelId" . $groupsLevel;
$list = [];
$count = 0;
$page = 1;
$limit = 50;
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re) {
$channelId = intval($re['channelId']);
!$sort && $sort = 'browse';
!$order && $order = 'desc';
if ($sort == 'order' && !$day) {
$sort_order = 'orderTotal ' . $order;
} else {
if($sort == 'beforeOrder'){
$sort_order = 'beforeOrderTotal ' . $order;
}
$sort_order = $sort . ' ' . $order;
}
if ($day) {//按天搜索
$createTimeStart = strtotime($day . ' 00:00:00');
$createTimeEnd = strtotime($day . ' 23:59:59');
$where = $where_sort = ['activityId' => $activityId, 'day' => $day, "$levelId>" => 0, 'kpi' => $sort];
if ($sort == 'order') {
if ($itemId) {
$where['itemId'] = $itemId;
}
} else {
$where = ['activityId' => $activityId, 'day' => $day, "$levelId>" => 0, 'kpi' => $sort];
}
$count = $this->mdSyliveActivityKpidata->count($where, $levelId);
if ($count) {
$res = $this->mdSyliveActivityKpidata->select_groupby($levelId, $where, "total {$order},id desc", $page, $limit, "$levelId,count(id) as total");
foreach ($res as $v) {
$groupsId = intval($v[$levelId]);
$re_gro = $this->mdSyliveGroups->get(['groupsId' => $groupsId, 'activityId' => $activityId, 'status' => 0]);
$groupsName = $re_gro ? $re_gro['groupsName'] : '-';
$where_sort[$levelId] = $groupsId;
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 == 'beforeOrder') {
$beforeOrderTotal = $v['total'];
} else {
$where_sort['kpi'] = 'beforeOrder';
$beforeOrderTotal = $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_sort[$levelId] = $groupsId;
$where_sort['kpi'] = 'order';
if ($itemId) {
$where_sort['itemId'] = $itemId;
}
$orderTotal = $this->mdSyliveActivityKpidata->count($where_sort);
}
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count([$levelId => $groupsId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', [$levelId => $groupsId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
$biz = $this->getBiz($groupsId);
$list[] = [
'groupsName' => $groupsName, 'biz' => $biz, 'browse' => $browse, 'subscribe' => $subscribe
, 'beforeOrder' => $beforeOrderTotal, 'watch' => $watch, 'order' => $orderTotal, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
} else {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$where = ['activityId' => $activityId, 'itemId' => $itemId, "$levelId>" => 0, 'itemId>' => 0];
$count = $this->mdSyliveActivityKpidata->count($where, 'bizId');
} else {
$where = ['activityId' => $activityId, 'status' => 0, 'groupsLevel' => $groupsLevel];
$count = $this->mdSyliveGroups->count($where);
}
if ($count) {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$res = $this->mdSyliveActivityKpidata->select_groupby($levelId, $where, "orderTotal {$order},id desc"
, $page, $limit, "$levelId,count(id) as orderTotal");
} else {
if($sort == 'beforeOrder'){
$sort_order = 'beforeOrderTotal ' . $order;
}
$res = $this->mdSyliveGroups->select($where, "{$sort_order},groupsId desc", $page, $limit,
'groupsId,groupsName,browse,subscribe,watch,beforeOrderTotal,orderTotal');
}
foreach ($res as $v) {
if ($sort == 'order' && $itemId) {//只改变订单数值,其它不变
$groupsId = intval($v[$levelId]);
$re_gro = $this->mdSyliveGroups->get(['groupsId' => $groupsId, 'activityId' => $activityId, 'status>=' => 0]);
$groupsName = $re_gro ? $re_gro['groupsName'] : '-';
$v['browse'] = intval($re_gro['browse']);
$v['subscribe'] = intval($re_gro['subscribe']);
$v['beforeOrderTotal'] = intval($re_gro['beforeOrderTotal']);
$v['watch'] = intval($re_gro['watch']);
} else {
$groupsId = intval($v['groupsId']);
$groupsName = $v['groupsName'] ? $v['groupsName'] : '-';
}
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count([$levelId => $groupsId, 'activityId' => $activityId]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', [$levelId => $groupsId, 'activityId' => $activityId]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
if ($sort != 'order' && $itemId) {//有商品id,区分订单
$order = $this->mdSyliveActivityKpidata->count(['itemId>' => 0, $levelId => $groupsId, 'itemId' => $itemId, 'activityId' => $activityId]);
} else {
$order = $v['orderTotal'];
}
$biz = $this->getBiz($groupsId);
$list[] = [
'groupsName' => $groupsName, 'biz' => $biz, 'browse' => $v['browse'], 'subscribe' => $v['subscribe']
, 'beforeOrder' => $v['beforeOrderTotal'], 'watch' => $v['watch'], 'order' => $order, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
}
}
$date = ['list' => $list, 'count' => $count];
$this->return_response_list($date);
}
/**
* Notes:分组门店统计
* Created on: 2022/12/05 14:48
* Created by: dengbw
*/
public function biz_get()
{
$date = $this->bizList($this->inputs);
$this->return_response_list($date);
}
/**
* Notes:导出分组门店统计
* Created on: 2022/12/12 14:03
* Created by: dengbw
*/
public function biz_export_get()
{
$this->inputs['page'] = 1;
$this->inputs['limit'] = 10000;
$date = $this->bizList($this->inputs);
$this->return_response_list($date);
}
/**
* Notes:分组顾问统计
* Created on: 2022/12/05 14:48
* Created by: dengbw
*/
public function consultant_get()
{
$date = $this->consultantList($this->inputs);
$this->return_response_list($date);
}
/**
* Notes:导出分组顾问统计
* Created on: 2022/12/12 14:03
* Created by: dengbw
*/
public function consultant_export_get()
{
$this->inputs['page'] = 1;
$this->inputs['limit'] = 10000;
$date = $this->consultantList($this->inputs);
$this->return_response_list($date);
}
/**
* Notes:客户列表
* Created on: 2022/12/20 11:34
* Created by: dengbw
*/
public function customer_get()
{
$date = $this->customerList($this->inputs);
$this->return_response_list($date);
}
/**
* Notes:导出客户列表
* Created on: 2022/12/20 11:34
* Created by: dengbw
*/
public function customer_export_get()
{
$this->inputs['page'] = 1;
$this->inputs['limit'] = 10000;
$date = $this->customerList($this->inputs);
$this->return_response_list($date);
}
/**
* Notes:漏头数据
* Created on: 2022/12/05 14:17
* Created by: dengbw
*/
public function funnel_get()
{
$activityId = intval($this->input_param('activityId'));
$itemId = intval($this->input_param('itemId'));//商品
$show = $this->input_param('show');
$funnel_preheating = $funnel_live = [];
$timeStart = 0;
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
$re['timeStart'] && $timeStart = strtotime($re['timeStart']);
if ($show == 'left' || !$show) {//预热阶段
$left_browse_where = "userId in(select userId from lc_market_sylive_activity_kpidata where activityId = {$activityId} AND kpi = 'browse' AND createTime < {$timeStart})";
$left_browse = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'kpi' => 'browse', 'createTime<' => $timeStart]);
$left_subscribe = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'kpi' => 'subscribe', 'createTime<' => $timeStart]);
$left_watch = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'kpi' => 'watch', $left_browse_where => null]);
if ($itemId) {
$left_order = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'itemId' => $itemId, $left_browse_where => null]);
} else {
$left_order = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'itemId>' => 0, $left_browse_where => null]);
}
$left_subscribe_per = number_format_com($left_subscribe / $left_browse * 100, 1, '');
if ($left_watch) {
$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]];
} else {
$left_order_per = number_format_com($left_order / $left_subscribe * 100, 1, '');
$left_expected_data = [['name' => "浏览人数100%", 'value' => 100], ['name' => "预约人数({$left_subscribe_per}%)", 'value' => 66.7]
, ['name' => "订单数({$left_order_per}%)", 'value' => 33.3]];
$left_actual_data = [['name' => '浏览人数', 'value' => $left_browse], ['name' => '预约人数', 'value' => $left_subscribe]
, ['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 kpi = 'browse' AND createTime >= {$timeStart})";
$right_browse = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'kpi' => 'browse', 'createTime>=' => $timeStart]);
$right_watch = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'kpi' => 'watch', $right_browse_where => null]);
if ($itemId) {
$right_order = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'itemId' => $itemId, $right_browse_where => null]);
} else {
$right_order = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'itemId>' => 0, $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);
}
/**
* Notes:获取观看数/订单数区域折线图数据
* Created on: 2022/12/06 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) {
$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']);
$where = ['createTime >=' => $start, 'createTime <=' => $end, 'activityId' => $activityId];
if ($v['value'] == 'order') {
$where['itemId>'] = 0;
} else {
$where['kpi'] = $v['value'];
}
$counts[] = $this->mdSyliveActivityKpidata->count($where);
}
$yAxisSeries[] = ['name' => $v['name'], 'type' => 'line', 'data' => $counts];
}
}
}
$date = ['legendData' => $legendData, 'xAxisData' => $xAxisData, 'yAxisSeries' => $yAxisSeries];
$this->return_response_list($date);
}
private function bizList($params)
{
$activityId = intval($params['activityId']);
$page = $params['page'];
$limit = $params['limit'];
$sort = $params['sort'];
$order = $params['order'];
$day = $params['day'];
$itemId = intval($params['itemId']);//商品
$levels = $params['levels'];
!$page && $page = 1;
!$limit && $limit = 10;
$list = [];
$count = 0;
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($levels && count($levels)) {
foreach ($levels as $k => $v) {
$arr_unit = explode(',', $v);
$levels[$k] = ['label' => $arr_unit[0], 'prop' => "levelName{$arr_unit[1]}", 'levelId' => "levelId{$arr_unit[1]}"];
}
}
if ($re) {
$channelId = intval($re['channelId']);
!$sort && $sort = 'browse';
!$order && $order = 'desc';
if ($sort == 'order' && !$day) {
$sort_order = 'orderTotal ' . $order;
} else {
$sort_order = $sort . ' ' . $order;
}
$groups_ids = $map_groups = [];
if ($day) {//按天搜索
$createTimeStart = strtotime($day . ' 00:00:00');
$createTimeEnd = strtotime($day . ' 23:59:59');
$where_sort = ['activityId' => $activityId, 'day' => $day, "bizId>" => 0, 'kpi' => $sort];
if ($sort == 'order') {
$where = ['activityId' => $activityId, 'day' => $day, "bizId>" => 0, 'kpi' => $sort];
if ($itemId) {
$where['itemId'] = $itemId;
}
} else {
$where = ['activityId' => $activityId, 'day' => $day, "bizId>" => 0, 'kpi' => $sort];
}
$count = $this->mdSyliveActivityKpidata->count($where, 'bizId');
if ($count) {
$res = $this->mdSyliveActivityKpidata->select_groupby('bizId', $where, "total {$order},id desc",
$page, $limit, "levelId1,levelId2,levelId3,bizId,count(id) as total");
foreach ($res as $v) {
if ($v['levelId1'] && !in_array($v['levelId1'], $groups_ids)) {
$groups_ids[] = $v['levelId1'];
}
if ($v['levelId2'] && !in_array($v['levelId2'], $groups_ids)) {
$groups_ids[] = $v['levelId2'];
}
if ($v['levelId3'] && !in_array($v['levelId3'], $groups_ids)) {
$groups_ids[] = $v['levelId3'];
}
if ($v['bizId'] && !in_array($v['bizId'], $groups_ids)) {
$groups_ids[] = $v['bizId'];
}
}
$str_groups = $groups_ids ? implode(',', $groups_ids) : -1;
$map_groups = $this->mdSyliveGroups->map('groupsId', 'groupsName',
["groupsId in({$str_groups})" => null, 'activityId' => $activityId, 'status>=' => 0]);
foreach ($res as $v) {
$bizId = intval($v['bizId']);
$bizName = '-';
$map_groups[$bizId] && $bizName = $map_groups[$bizId];
$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 == 'beforeOrder') {
$beforeOrderTotal = $v['total'];
} else {
$beforeOrderTotal = $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_sort['bizId'] = $bizId;
if ($itemId) {
$where_sort['itemId'] = $itemId;
}
$orderTotal = $this->mdSyliveActivityKpidata->count($where_sort);
}
$consultant = $this->mdSyliveActivityKpidata->count(['bizId' => $bizId, 'activityId' => $activityId, "cfUserId in (SELECT userId FROM lc_market_sylive_groups_user WHERE activityId = {$activityId} and bizId > 0 and status = 0)" => null], 'cfUserId');
$allConsultant = $this->mdSyliveGroupsUser->count(['bizId' => $bizId, 'activityId' => $activityId, 'status' => 0, 'bizId >' => 0]);
$consultant > $allConsultant && $allConsultant = $consultant;
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count(['bizId' => $bizId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['bizId' => $bizId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
$consultantPer = number_format_com($consultant / $allConsultant * 100, 2, '') . '%';
$list1 = [];
foreach ($levels as $k2 => $v2) {
$levelId = $v2['levelId'];
$list1[$v2['prop']] = $map_groups[$v[$levelId]] ? $map_groups[$v[$levelId]] : '';
}
$list2 = ['bizName' => $bizName, 'consultant' => $consultant, 'allConsultant' => $allConsultant, 'consultantPer' => $consultantPer
, 'browse' => $browse, 'subscribe' => $subscribe,'beforeOrder' => $beforeOrderTotal, 'order' => $orderTotal, 'watch' => $watch, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
$list[] = count($list1) ? array_merge($list1, $list2) : $list2;
}
}
} else {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$where = ['activityId' => $activityId, 'itemId' => $itemId, 'bizId>' => 0];
$count = $this->mdSyliveActivityKpidata->count($where, 'bizId');
} else {
$where = ['activityId' => $activityId, 'status' => 0, 'ifBiz' => 1];
$count = $this->mdSyliveGroups->count($where);
}
if ($count) {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$res = $this->mdSyliveActivityKpidata->select_groupby('bizId', $where, "orderTotal {$order},id desc"
, $page, $limit, "levelId1,levelId2,levelId3,bizId,count(id) as orderTotal");
foreach ($res as $v) {
if ($v['levelId1'] && !in_array($v['levelId1'], $groups_ids)) {
$groups_ids[] = $v['levelId1'];
}
if ($v['levelId2'] && !in_array($v['levelId2'], $groups_ids)) {
$groups_ids[] = $v['levelId2'];
}
if ($v['levelId3'] && !in_array($v['levelId3'], $groups_ids)) {
$groups_ids[] = $v['levelId3'];
}
if ($v['bizId'] && !in_array($v['bizId'], $groups_ids)) {
$groups_ids[] = $v['bizId'];
}
}
} else {
if ($sort == 'beforeOrder'){
$sort_order = 'beforeOrderTotal ' . $order;
}
$res = $this->mdSyliveGroups->select($where, "{$sort_order},groupsId desc", $page, $limit,
'groupsId as bizId,groupsName,browse,subscribe,watch,beforeOrderTotal,orderTotal');
foreach ($res as $k => $v) {
$bizId = intval($v['bizId']);
$re_use = $this->mdSyliveGroupsUser->get(["bizId" => $bizId, 'activityId' => $activityId, 'status' => 0]);
if ($re_use) {
$res[$k]['levelId1'] = $re_use['levelId1'];
if ($re_use['levelId1'] && !in_array($re_use['levelId1'], $groups_ids)) {
$groups_ids[] = $re_use['levelId1'];
}
$res[$k]['levelId2'] = $re_use['levelId2'];
if ($re_use['levelId2'] && !in_array($re_use['levelId2'], $groups_ids)) {
$groups_ids[] = $re_use['levelId2'];
}
$res[$k]['levelId3'] = $re_use['levelId3'];
if ($re_use['levelId3'] && !in_array($re_use['levelId3'], $groups_ids)) {
$groups_ids[] = $re_use['levelId3'];
}
}
}
}
$str_groups = $groups_ids ? implode(',', $groups_ids) : -1;
$map_groups = $this->mdSyliveGroups->map('groupsId', 'groupsName',
["groupsId in({$str_groups})" => null, 'activityId' => $activityId, 'status' => 0]);
foreach ($res as $v) {
$bizId = intval($v['bizId']);
$bizName = '-';
if ($v['groupsName']) {
$bizName = $v['groupsName'];
} else {
$map_groups[$bizId] && $bizName = $map_groups[$bizId];
}
$consultant = $this->mdSyliveActivityKpidata->count(['bizId' => $bizId, 'activityId' => $activityId], 'cfUserId');
$allConsultant = $this->mdSyliveGroupsUser->count(['bizId' => $bizId, 'activityId' => $activityId, 'status' => 0]);
$consultant > $allConsultant && $allConsultant = $consultant;
$livePV = $watchDuration = 0;
if ($channelId) {
$livePV = $this->mdSyliveViewlog->count(['bizId' => $bizId, 'activityId' => $activityId]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['bizId' => $bizId, 'activityId' => $activityId]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
if ($sort != 'order' && $itemId) {//有商品id,区分订单
$order = $this->mdSyliveActivityKpidata->count(['itemId>' => 0, 'bizId' => $bizId, 'itemId' => $itemId, 'activityId' => $activityId]);
} else {
$order = $v['orderTotal'];
}
if ($sort == 'order' && $itemId) {//只改变订单数值,其它不变
$re = $this->mdSyliveGroups->get(['groupsId' => $bizId, 'activityId' => $activityId, 'status>=' => 0], 'browse,subscribe,watch');
$v['browse'] = intval($re['browse']);
$v['subscribe'] = intval($re['subscribe']);
$v['watch'] = intval($re['watch']);
$v['beforeOrderTotal'] = intval($re['beforeOrderTotal']);
}
$consultantPer = number_format_com($consultant / $allConsultant * 100, 2, '') . '%';
$list1 = [];
foreach ($levels as $k2 => $v2) {
$levelId = $v2['levelId'];
$list1[$v2['prop']] = $map_groups[$v[$levelId]] ? $map_groups[$v[$levelId]] : '';
}
$list2 = ['bizName' => $bizName, 'consultant' => $consultant, 'allConsultant' => $allConsultant, 'consultantPer' => $consultantPer
, 'browse' => $v['browse'], 'subscribe' => $v['subscribe'], 'beforeOrder' => $v['beforeOrderTotal'], 'order' => $order, 'watch' => $v['watch'], 'livePV' => $livePV, 'watchDuration' => $watchDuration];
$list[] = count($list1) ? array_merge($list1, $list2) : $list2;
}
}
}
}
if ($limit == 10000) {
return $list;
} else {
$columns = '';
if ($page == 1) {
$columns1 = [];
foreach ($levels as $k => $v) {
$columns1[] = ['prop' => $v['prop'], 'label' => $v['label'], 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 80];
}
$columns2 = [
['prop' => 'bizName', 'label' => '门店', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 120],
['prop' => 'consultant', 'label' => '参与顾问数', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 85],
['prop' => 'allConsultant', 'label' => '全部顾问数', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 85],
['prop' => 'consultantPer', 'label' => '开工率', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 75],
['prop' => 'browse', 'label' => '浏览数', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 115, 'sortable' => 'custom'],
['prop' => 'subscribe', 'label' => '预约数', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 115, 'sortable' => 'custom'],
['prop' => 'beforeOrder', 'label' => '留资数', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 115, 'sortable' => 'custom'],
['prop' => 'order', 'label' => '订单数', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 115, 'sortable' => 'custom'],
['prop' => 'watch', 'label' => '观看人数', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 115, 'sortable' => 'custom'],
['prop' => 'livePV', 'label' => '观看人次', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 100, 'sortable' => 'custom'],
['prop' => 'watchDuration', 'label' => '人均观看(分)', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 100, 'sortable' => 'custom'],
];
$columns = count($columns1) ? array_merge($columns1, $columns2) : $columns2;
}
return ['list' => $list, 'count' => $count, 'columns' => $columns];
}
}
private function consultantList($params)
{
$activityId = intval($params['activityId']);
$page = $params['page'];
$limit = $params['limit'];
$sort = $params['sort'];
$order = $params['order'];
$day = $params['day'];
$itemId = intval($params['itemId']);//商品
$levels = $params['levels'];
!$page && $page = 1;
!$limit && $limit = 10;
$list = [];
$count = 0;
if ($levels && count($levels)) {
foreach ($levels as $k => $v) {
$arr_unit = explode(',', $v);
$levels[$k] = ['label' => $arr_unit[0], 'prop' => "levelName{$arr_unit[1]}", 'levelId' => "levelId{$arr_unit[1]}"];
}
}
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re) {
$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_sort = ['activityId' => $activityId, 'day' => $day, "cfUserId>" => 0, 'kpi' => $sort];
if ($sort == 'order') {
$where = ['activityId' => $activityId, 'day' => $day, "cfUserId>" => 0, 'kpi' => $sort];
if ($itemId) {
$where['itemId'] = $itemId;
}
} else {
$where = ['activityId' => $activityId, 'day' => $day, "cfUserId>" => 0, 'kpi' => $sort];
}
$count = $this->mdSyliveActivityKpidata->count($where, 'cfUserId');
if ($count) {
$res = $this->mdSyliveActivityKpidata->select_groupby('cfUserId', $where, "total {$order},id desc", $page, $limit
, "cfUserId,levelId1,levelId2,levelId3,bizId,count(id) as total");
$groups_ids = $userIds = [];
foreach ($res as $v) {
if ($v['levelId1'] && !in_array($v['levelId1'], $groups_ids)) {
$groups_ids[] = $v['levelId1'];
}
if ($v['levelId2'] && !in_array($v['levelId2'], $groups_ids)) {
$groups_ids[] = $v['levelId2'];
}
if ($v['levelId3'] && !in_array($v['levelId3'], $groups_ids)) {
$groups_ids[] = $v['levelId3'];
}
if ($v['bizId'] && !in_array($v['bizId'], $groups_ids)) {
$groups_ids[] = $v['bizId'];
}
$userIds[] = $v['cfUserId'];
}
$str_groups = $groups_ids ? implode(',', $groups_ids) : -1;
$map_groups = $this->mdSyliveGroups->map('groupsId', 'groupsName',
["groupsId in({$str_groups})" => null, 'activityId' => $activityId, 'status>=' => 0]);
$str_user = $userIds ? implode(',', $userIds) : -1;
$map_user = $this->mdSyliveUser->map('userId', 'uname', ["userId in({$str_user})" => null]);
foreach ($res as $v) {
$cfUserId = intval($v['cfUserId']);
$bizName = $consultantName = '-';
$map_groups[$v['bizId']] && $bizName = $map_groups[$v['bizId']];
$map_user[$cfUserId] && $consultantName = $map_user[$cfUserId];
$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 == 'beforeOrder') {
$beforeOrderTotal = $v['total'];
} else {
$beforeOrderTotal = $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_sort['kpi'] = 'order';
if ($itemId) {
$where_sort['itemId'] = $itemId;
}
$order = $this->mdSyliveActivityKpidata->count($where_sort);
}
$livePV = $watchDuration = 0;
if ($limit == 10000 && $channelId) {//导出
$livePV = $this->mdSyliveViewlog->count(['cfUserId' => $cfUserId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['cfUserId' => $cfUserId, 'activityId' => $activityId
, 'createTime>=' => $createTimeStart, 'createTime<=' => $createTimeEnd]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
$list1 = [];
foreach ($levels as $k2 => $v2) {
$levelId = $v2['levelId'];
$list1[$v2['prop']] = $map_groups[$v[$levelId]] ? $map_groups[$v[$levelId]] : '';
}
$list2 = ['bizName' => $bizName, 'consultantName' => $consultantName, 'browse' => $browse,
'subscribe' => $subscribe,'beforeOrder' => $beforeOrderTotal, 'order' => $order, 'watch' => $watch, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
$list[] = count($list1) ? array_merge($list1, $list2) : $list2;
}
}
} else {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$where = ['activityId' => $activityId, 'itemId' => $itemId, 'cfUserId>' => 0, 'kpi' => $sort];
$count = $this->mdSyliveActivityKpidata->count($where, 'cfUserId');
} else {
$where = ['activityId' => $activityId, 'status' => 0];
$count = $this->mdSyliveGroupsUser->count($where);
}
if ($count) {
if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索
$res = $this->mdSyliveActivityKpidata->select_groupby('cfUserId', $where, "orderTotal {$order},id desc"
, $page, $limit, "cfUserId,levelId1,levelId2,levelId3,bizId,count(id) as orderTotal");
} else {
if ($sort == 'beforeOrder'){
$sort_order = 'beforeOrderTotal ' . $order;
}
$res = $this->mdSyliveGroupsUser->select($where, "{$sort_order},groupsUserId desc", $page, $limit,
'userId as cfUserId,levelId1,levelId2,levelId3,bizId,browse,subscribe,beforeOrderTotal,orderTotal,watch');
}
$groups_ids = $userIds = [];
foreach ($res as $v) {
if ($v['levelId1'] && !in_array($v['levelId1'], $groups_ids)) {
$groups_ids[] = $v['levelId1'];
}
if ($v['levelId2'] && !in_array($v['levelId2'], $groups_ids)) {
$groups_ids[] = $v['levelId2'];
}
if ($v['levelId3'] && !in_array($v['levelId3'], $groups_ids)) {
$groups_ids[] = $v['levelId3'];
}
if ($v['bizId'] && !in_array($v['bizId'], $groups_ids)) {
$groups_ids[] = $v['bizId'];
}
$userIds[] = $v['cfUserId'];
}
$str_groups = $groups_ids ? implode(',', $groups_ids) : -1;
$map_groups = $this->mdSyliveGroups->map('groupsId', 'groupsName',
["groupsId in({$str_groups})" => null, 'activityId' => $activityId, 'status>=' => 0]);
$str_user = $userIds ? implode(',', $userIds) : -1;
$map_user = $this->mdSyliveUser->map('userId', 'uname', ["userId in({$str_user})" => null]);
foreach ($res as $v) {
$cfUserId = intval($v['cfUserId']);
$bizName = $consultantName = '-';
$map_groups[$v['bizId']] && $bizName = $map_groups[$v['bizId']];
$map_user[$cfUserId] && $consultantName = $map_user[$cfUserId];
if ($sort != 'order' && $itemId) {//有商品id,区分订单
$order = $this->mdSyliveActivityKpidata->count(['itemId>' => 0, 'cfUserId' => $cfUserId, 'itemId' => $itemId, 'activityId' => $activityId]);
} else {
$order = $v['orderTotal'];
}
if ($sort == 'order' && $itemId) {//只改变订单数值,其它不变
$re = $this->mdSyliveGroupsUser->get(['userId' => $cfUserId, 'activityId' => $activityId], 'browse,subscribe,watch');
$v['browse'] = intval($re['browse']);
$v['subscribe'] = intval($re['subscribe']);
$v['watch'] = intval($re['watch']);
$v['beforeOrderTotal'] = intval($re['beforeOrderTotal']);
}
$browse = intval($v['browse']);
$subscribe = intval($v['subscribe']);
$watch = intval($v['watch']);
$beforeOrderTotal = intval($v['beforeOrderTotal']);
$order = intval($order);
$livePV = $watchDuration = 0;
if ($limit == 10000 && $channelId) {//导出
$livePV = $this->mdSyliveViewlog->count(['cfUserId' => $cfUserId, 'activityId' => $activityId]);
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['cfUserId' => $cfUserId, 'activityId' => $activityId]);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
$list1 = [];
foreach ($levels as $k2 => $v2) {
$levelId = $v2['levelId'];
$list1[$v2['prop']] = $map_groups[$v[$levelId]] ? $map_groups[$v[$levelId]] : '';
}
$list2 = ['bizName' => $bizName, 'consultantName' => $consultantName, 'browse' => $browse,
'subscribe' => $subscribe, 'beforeOrder' => $beforeOrderTotal, 'order' => $order, 'watch' => $watch, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
$list[] = count($list1) ? array_merge($list1, $list2) : $list2;
}
}
}
}
if ($limit == 10000) {
return $list;
} else {
$columns = '';
if ($page == 1) {
$columns1 = [];
foreach ($levels as $k => $v) {
$columns1[] = ['prop' => $v['prop'], 'label' => $v['label'], 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 80];
}
$columns2 = [
['prop' => 'bizName', 'label' => '门店', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 120],
['prop' => 'consultantName', 'label' => '顾问', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 90],
['prop' => 'browse', 'label' => '浏览数', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 120, 'sortable' => 'custom'],
['prop' => 'subscribe', 'label' => '预约数', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 120, 'sortable' => 'custom'],
['prop' => 'beforeOrder', 'label' => '留资数', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 120, 'sortable' => 'custom'],
['prop' => 'order', 'label' => '订单数', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 120, 'sortable' => 'custom'],
['prop' => 'watch', 'label' => '观看人数', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 120, 'sortable' => 'custom'],
['prop' => 'livePV', 'label' => '观看人次', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 100, 'sortable' => 'custom'],
['prop' => 'watchDuration', 'label' => '人均观看(分)', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 100, 'sortable' => 'custom'],
];
$columns = count($columns1) ? array_merge($columns1, $columns2) : $columns2;
}
return ['list' => $list, 'count' => $count, 'columns' => $columns];
}
}
private function customerList($params)
{
$activityId = intval($params['activityId']);
$page = $params['page'];
$limit = $params['limit'];
$sort = $params['sort'];
$order = $params['order'];
$itemId = intval($params['itemId']);//商品
$insiders = intval($params['insiders']);//内部人员
$groupsId = intval($params['groupsId']);//分组id
$levels = $params['levels'];
!$page && $page = 1;
!$limit && $limit = 10;
$list = [];
$count = 0;
if ($levels && count($levels)) {
foreach ($levels as $k => $v) {
$arr_unit = explode(',', $v);
$levels[$k] = ['label' => $arr_unit[0], 'prop' => "levelName{$arr_unit[1]}", 'levelId' => "levelId{$arr_unit[1]}"];
}
}
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re) {
$channelId = intval($re['channelId']);
!$sort && $sort = 'browse';
!$order && $order = 'desc';
$where = ['activityId' => $activityId, 'userId>' => 0];
if ($insiders == 1) {
$where["userId in(select userId from lc_market_sylive_user where organizationId>0)"] = null;
} else if ($insiders == 2) {
$where["userId not in(select userId from lc_market_sylive_user where organizationId>0)"] = null;
}
if ($groupsId) {
$re_gro = $this->mdSyliveGroups->get(['groupsId' => $groupsId], 'groupsId,groupsLevel,parentId');
if ($re_gro && $re_gro['parentId']) {
if ($re_gro['groupsLevel']) {
$where['levelId' . $re_gro['groupsLevel']] = $re_gro['groupsId'];
} else {
$where['bizId'] = $re_gro['groupsId'];
}
}
}
if ($itemId) {
$where['itemId'] = $itemId;
$count = $this->mdSyliveOrder->count($where, 'userId');
} else {
if ($sort == 'order') {
$where['itemId>'] = 0;
} else {
$where['kpi'] = $sort;
}
$count = $this->mdSyliveActivityKpidata->count($where, 'userId');
}
if ($count) {
$fields = 'userId,cfUserId,levelId1,levelId2,levelId3,bizId';
if ($itemId) {
$res = $this->mdSyliveOrder->select_groupby('userId', $where, "createTime {$order},id desc"
, $page, $limit, $fields);
} else {
$res = $this->mdSyliveActivityKpidata->select_groupby('userId', $where, "createTime {$order},id desc"
, $page, $limit, $fields);
}
$groups_ids = $userIds = [];
foreach ($res as $v) {
if ($v['levelId1'] && !in_array($v['levelId1'], $groups_ids)) {
$groups_ids[] = $v['levelId1'];
}
if ($v['levelId2'] && !in_array($v['levelId2'], $groups_ids)) {
$groups_ids[] = $v['levelId2'];
}
if ($v['levelId3'] && !in_array($v['levelId3'], $groups_ids)) {
$groups_ids[] = $v['levelId3'];
}
if ($v['bizId'] && !in_array($v['bizId'], $groups_ids)) {
$groups_ids[] = $v['bizId'];
}
if ($v['cfUserId'] && !in_array($v['cfUserId'], $userIds)) {
$userIds[] = $v['cfUserId'];
}
if ($v['userId'] && !in_array($v['userId'], $userIds)) {
$userIds[] = $v['userId'];
}
}
$str_groups = $groups_ids ? implode(',', $groups_ids) : -1;
$map_groups = $this->mdSyliveGroups->map('groupsId', 'groupsName',
["groupsId in({$str_groups})" => null, 'activityId' => $activityId, 'status>=' => 0]);
$str_user = $userIds ? implode(',', $userIds) : -1;
$map_user = $this->mdSyliveUser->map('userId', 'createTime,uname,nickname,organizationId', ["userId in({$str_user})" => null]);
foreach ($res as $v) {
$userId = intval($v['userId']);
$cfUserId = intval($v['cfUserId']);
$insiders = '否';//内部人员
$createTime = $browseTime = $subscribeTime = $liveTime = '';
$playDuration = $livePV = 0;
$nickname = $consultantName = $bizName = '-';
$map_groups[$v['bizId']] && $bizName = $map_groups[$v['bizId']];
$map_user[$cfUserId] && $consultantName = $map_user[$cfUserId]['uname'];
$user = $map_user[$userId];
if ($user) {
$nickname = $user['nickname'];
$createTime = strtotime($user['createTime']);
$createTime = date('Y-m-d H:i', $createTime);
if ($user['organizationId']) {
$insiders = '是';
}
}
$re_browse = $this->mdSyliveActivityKpidata->get(['userId' => $userId, 'activityId' => $activityId, 'kpi' => 'browse'], 'createTime');
$re_browse && $browseTime = date('Y-m-d H:i', $re_browse['createTime']);
$re_subscribe = $this->mdSyliveActivityKpidata->get(['userId' => $userId, 'activityId' => $activityId, 'kpi' => 'subscribe'], 'createTime');
$re_subscribe && $subscribeTime = date('Y-m-d H:i', $re_subscribe['createTime']);
if ($channelId) {
$pv_count = $this->mdSyliveViewlog->count(['userId' => $userId, 'activityId' => $activityId]);
if ($pv_count) {
$livePV = $pv_count;
$re_vie = $this->mdSyliveViewlog->get(['userId' => $userId, 'activityId' => $activityId, 'createTime']);
$re_vie['createTime'] && $liveTime = date('Y-m-d H:i', $re_vie['createTime']);
}
$re_sum = $this->mdSyliveViewlog->sum('playDuration', ['userId' => $userId, 'activityId' => $activityId]);
$re_sum['playDuration'] && $playDuration = ceil($re_sum['playDuration'] / 60);
}
$list1 = [];
foreach ($levels as $k2 => $v2) {
$levelId = $v2['levelId'];
$list1[$v2['prop']] = $map_groups[$v[$levelId]] ? $map_groups[$v[$levelId]] : '';
}
$list2 = ['bizName' => $bizName, 'nickname' => $nickname, 'createTime' => $createTime, 'browseTime' => $browseTime, 'subscribeTime' => $subscribeTime,
'liveTime' => $liveTime, 'livePV' => $livePV, 'playDuration' => $playDuration, 'insiders' => $insiders, 'consultantName' => $consultantName];
$list[] = count($list1) ? array_merge($list1, $list2) : $list2;
}
}
}
if ($limit == 10000) {
return $list;
} else {
$columns = '';
if ($page == 1) {
$columns1 = [];
foreach ($levels as $k => $v) {
$columns1[] = ['prop' => $v['prop'], 'label' => $v['label'], 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 80];
}
$columns2 = [
['prop' => 'bizName', 'label' => '门店', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 110],
['prop' => 'nickname', 'label' => '昵称', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 80],
['prop' => 'createTime', 'label' => '创建时间', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 90],
['prop' => 'browseTime', 'label' => '首次浏览时间', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 90],
['prop' => 'subscribeTime', 'label' => '预约时间', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 90],
['prop' => 'liveTime', 'label' => '首次观看时间', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 90],
['prop' => 'playDuration', 'label' => '观看时长(分)', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 80],
['prop' => 'livePV', 'label' => '观看次数', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 80],
['prop' => 'insiders', 'label' => '内部人员', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 80],
['prop' => 'consultantName', 'label' => '归属顾问', 'align' => 'center', 'showOverflowTooltip' => true, 'minWidth' => 80],
];
$columns = count($columns1) ? array_merge($columns1, $columns2) : $columns2;
}
return ['list' => $list, 'count' => $count, 'columns' => $columns];
}
}
/**
* Notes:获取门店数
* Created on: 2022/12/6 14:48
* Created by: dengbw
* @param $groupsId
* @return int
*/
private function getBiz($groupsId)
{
$groupsIds = '';
$res1 = $this->mdSyliveGroups->select(['parentId' => $groupsId, 'status>=' => 0], "groupsId desc", 0, 0, 'groupsId');
if ($res1) {
$groupsIds = implode(',', array_column($res1, 'groupsId'));
$res2 = $this->mdSyliveGroups->select(["parentId in({$groupsIds})" => null, 'status>=' => 0]
, "groupsId desc", 0, 0, 'groupsId');
if ($res2) {
$groupsIds = implode(',', array_column($res2, 'groupsId'));
$res3 = $this->mdSyliveGroups->select(["parentId in({$groupsIds})" => null, 'status>=' => 0]
, "groupsId desc", 0, 0, 'groupsId');
if ($res3) {
$groupsIds = implode(',', array_column($res3, 'groupsId'));
}
}
}
$biz = 0;
if ($groupsIds) {
$biz = $this->mdSyliveGroups->count(["groupsId in({$groupsIds})" => null, 'status>=' => 0]);
}
return $biz;
}
/**
* Notes:根据等级找名字
* Created on: 2022/12/10 13:56
* Created by: dengbw
* @param $groupsId
* @param $groupsLevel
* @param string $groupsName
* @return string
*/
private function getLevelName($groupsId, $groupsLevel, $groupsName = '')
{
$re = $this->mdSyliveGroups->get(['groupsId' => $groupsId], 'groupsName,parentId,groupsLevel');
if (!$re) {
return $groupsName;
} else {
if ($re['groupsLevel'] == $groupsLevel) {
$groupsName = $re['groupsName'];
}
if (!$groupsName && $re['parentId']) {
return $this->getLevelName($re['parentId'], $groupsLevel, $groupsName);
} else {
return $groupsName;
}
}
}
}