market_statistics_1104

This commit is contained in:
dengbw
2022-11-04 10:12:41 +08:00
parent f3ae9b45f9
commit 04a4eae82f
+96 -114
View File
@@ -14,6 +14,7 @@ class Statistics extends BaseController
{
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');
@@ -38,7 +39,7 @@ class Statistics extends BaseController
$this->return_json('参数错误');
}
$title = '';
$activityData1 = $activityData2 = $areaData = $bizData = $consultantData = $areaAry = $funnel = $allOrganizationIds = [];
$activityData1 = $activityData2 = $areaData = $areaAry = $funnel = $allOrganizationIds = [];
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re) {
$title = $re['title'];
@@ -47,65 +48,32 @@ class Statistics extends BaseController
foreach ($res_biz as $k => $v) {
$areaId = intval($v['areaId']);
$bizId = intval($v['bizId']);
$re_biz = $this->mdSyliveOrganization->get(['organizationId' => $bizId]);
if ($re_biz) {
$consultant = $this->mdSyliveActivityKpidata->count(['bizId' => $bizId, 'type' => 0, 'activityId' => $activityId], 'distinct(cfUserId)');
$livePV = $watchDuration = 0;
if ($channelId) {
$res_watch = $this->mdSyliveActivityKpidata->db->select('b.unionid')
->from('lc_market_sylive_activity_kpidata as a')
->join('lc_market_sylive_user as b', "b.userId=a.userId", 'left')
->where(['a.bizId' => $bizId, 'a.type' => 0, 'a.activityId' => $activityId, 'a.kpi' => 'watch', "unionid!=''" => null])
->limit(10000, 0)
->get()->result_array();
if ($res_watch) {
$str_unionids = implode("','", array_column($res_watch, 'unionid'));
$where_pv = ['channelId' => $channelId, 'param3' => 'live'];
$where_pv["param1 in ('" . $str_unionids . "')"] = null;
$livePV = $this->mdPolyvViewlog->count($where_pv);
$re_sum = $this->mdPolyvViewlog->sum('playDuration', $where_pv);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
if ($areaId) {
if (!in_array($areaId, $allOrganizationIds)) {
$allOrganizationIds[] = $areaId;
}
//统计顾问
$allConsultant = 0;
if ($areaId) {
if (!in_array($areaId, $allOrganizationIds)) {
$allOrganizationIds[] = $areaId;
}
}
if ($bizId) {
$allOrganizationIds[] = $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) {
$allOrganizationIds[] = $v2['organizationId'];
$areaAry[$areaId]['sales'][] = $v2['organizationId'];
}
if ($bizId) {
$allOrganizationIds[] = $bizId;
$areaAry[$areaId]['biz'][] = $bizId;
$consultants = [];
$consultants[] = $bizId;
$res_org = $this->mdSyliveOrganization->select(['parentId' => $bizId, 'status>=' => 0], 'organizationId desc', 0, 0, 'organizationId');
foreach ($res_org as $k2 => $v2) {
$allOrganizationIds[] = $v2['organizationId'];
$areaAry[$areaId]['sales'][] = $v2['organizationId'];
$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;
$bizData[] = ['areaId' => $areaId, 'bizId' => $bizId,
'bizName' => $re_biz['organizationName'], 'consultant' => $consultant . '/' . $allConsultant, 'browse' => $v['browse'], 'subscribe' => $v['subscribe']
, 'watch' => $v['watch'], 'order' => $v['orderTotal'], 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
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->mdSyliveActivityBiz->sum('browse', ['areaId' => $areaId, 'status>=' => 0, 'activityId' => $activityId]);
$subscribe = $this->mdSyliveActivityBiz->sum('subscribe', ['areaId' => $areaId, 'status>=' => 0, 'activityId' => $activityId]);
$watch = $this->mdSyliveActivityBiz->sum('watch', ['areaId' => $areaId, 'status>=' => 0, 'activityId' => $activityId]);
$orderTotal = $this->mdSyliveActivityBiz->sum('orderTotal', ['areaId' => $areaId, 'status>=' => 0, 'activityId' => $activityId]);
//$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], 'distinct(userId)');
$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]);
$orderTotal = $this->mdSyliveActivityBiz->sum('orderTotal', ['areaId' => $areaId, 'activityId' => $activityId]);
$livePV = $watchDuration = 0;
if ($channelId) {
$res_watch = $this->mdSyliveActivityKpidata->db->select('b.unionid')
@@ -138,42 +106,9 @@ class Statistics extends BaseController
, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
}
$res_user = $this->mdSyliveActivityKpidata->select_groupby('cfUserId', ['activityId' => $activityId, 'type' => 0, 'cfUserId>' => 0], 'id desc', 0, 0, 'cfUserId,bizId');
foreach ($res_user as $k => $v) {
$cfUserId = intval($v['cfUserId']);
$re_user = $this->mdSyliveUser->get(['userId' => $cfUserId, 'status>=' => 0]);
if ($re_user) {
$cfUserIds[] = $cfUserId;
$consultantName = $re_user['uname'] ? $re_user['uname'] : $re_user['nickname'];
$re_org = $this->mdSyliveOrganization->get(['organizationId' => $v['bizId'], 'status>=' => 0]);
$re_org && $consultantName .= "({$re_org['organizationName']})";
$browse = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => 0, 'cfUserId' => $cfUserId, 'kpi' => 'browse']);
$subscribe = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => 0, 'cfUserId' => $cfUserId, 'kpi' => 'subscribe']);
$watch = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => 0, 'cfUserId' => $cfUserId, 'kpi' => 'watch']);
$order = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => 0, 'cfUserId' => $cfUserId, 'kpi' => 'order']);
$livePV = $watchDuration = 0;
if ($channelId) {
$res_watch = $this->mdSyliveActivityKpidata->db->select('b.unionid')
->from('lc_market_sylive_activity_kpidata as a')
->join('lc_market_sylive_user as b', "b.userId=a.userId", 'left')
->where(['a.cfUserId' => $cfUserId, 'a.activityId' => $activityId, 'a.type' => 0, 'a.kpi' => 'watch', "unionid!=''" => null])
->limit(10000, 0)
->get()->result_array();
if ($res_watch) {
$str_unionids = implode("','", array_column($res_watch, 'unionid'));
$where_pv = ['channelId' => $channelId, 'param3' => 'live'];
$where_pv["param1 in ('" . $str_unionids . "')"] = null;
$livePV = $this->mdPolyvViewlog->count($where_pv);
$re_sum = $this->mdPolyvViewlog->sum('playDuration', $where_pv);
if ($re_sum['playDuration']) {
$playDuration = ceil($re_sum['playDuration'] / 60);
$watchDuration = round($playDuration / $livePV);
}
}
}
$consultantData[] = ['consultantName' => $consultantName, 'browse' => $browse, 'subscribe' => $subscribe, 'watch' => $watch
, 'order' => $order, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
if ($areaData) {
$edit = array_column($areaData, 'browse');
array_multisort($edit, SORT_DESC, $areaData);
}
$browse = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => 0, 'kpi' => 'browse']);
$subscribe = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => 0, 'kpi' => 'subscribe']);
@@ -195,13 +130,14 @@ class Statistics extends BaseController
}
}
$allConsultant = 0;
$consultant = count($res_user);
$consultant = $this->mdSyliveActivityUser->count(['activityId' => $activityId, 'organizationId>' => 0], 'userId');
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;
$activityData1 = [['name' => '参与区域数', 'value' => count($areaData)], ['name' => '参与门店数', 'value' => count($bizData)]
$biz = $this->mdSyliveActivityBiz->count(['activityId' => $activityId, 'status>=' => 0, 'bizId>' => 0]);
$activityData1 = [['name' => '参与区域数', 'value' => count($areaData)], ['name' => '参与门店数', 'value' => $biz]
, ['name' => '参与/全部顾问数', 'value' => $consultant . '/' . $allConsultant]];
$url = "/sylive/activity/statistics/piechart?activityId={$activityId}&";
$watch_un = $watch - $liveUV;
@@ -241,7 +177,7 @@ class Statistics extends BaseController
$funnel_subscribe = ['title' => '浏览转化漏斗(直播阶段)', 'expected_data' => $right_expected_data, 'actual_data' => $right_actual_data];
$data = ['activityId' => $activityId, 'title' => $title, 'activityData1' => ['list' => $activityData1, 'style' => 'width: 33.3%'], 'activityData2' => ['list' => $activityData2, 'style' => 'width: 11.1%']
, 'areaData' => $areaData, 'bizData' => $bizData, 'consultantData' => $consultantData, 'funnelBrowse' => $funnel_browse, 'funnelSubscribe' => $funnel_subscribe];
, 'areaData' => $areaData, 'funnelBrowse' => $funnel_browse, 'funnelSubscribe' => $funnel_subscribe];
$this->return_response($data);
}
@@ -281,7 +217,8 @@ class Statistics extends BaseController
$bizId = intval($v['bizId']);
$re_biz = $this->mdSyliveOrganization->get(['organizationId' => $bizId, 'status>=' => 0]);
$bizName = $re_biz ? $re_biz['organizationName'] : '-';
$consultant = $this->mdSyliveActivityKpidata->count(['bizId' => $bizId, 'type' => 0, 'activityId' => $activityId], 'distinct(cfUserId)');
//$consultant = $this->mdSyliveActivityKpidata->count(['bizId' => $bizId, 'type' => 0, 'activityId' => $activityId], 'distinct(cfUserId)');
$consultant = $this->mdSyliveActivityUser->count(['bizId' => $bizId, 'activityId' => $activityId, 'organizationId>' => 0], 'distinct(userId)');
if ($bizId) {//统计顾问数
$consultants = [];
$consultants[] = $bizId;
@@ -342,30 +279,31 @@ class Statistics extends BaseController
$re = $this->mdSyliveActivity->get(['activityId' => $activityId]);
if ($re) {
$channelId = intval($re['channelId']);
$sort_order = 'id desc';
$sort_order = 'browse desc,id desc';
if ($sort && $order) {
$sort_order = $sort . ' ' . $order;
if ($sort == 'order') {
$sort_order = 'orderTotal ' . $order;
} else {
$sort_order = $sort . ' ' . $order;
}
}
$where = ['activityId' => $activityId, 'type' => 0, 'cfUserId>' => 0];
$count = $this->mdSyliveActivityKpidata->count($where, 'cfUserId');
$where = ['activityId' => $activityId, 'organizationId>' => 0];
$count = $this->mdSyliveActivityUser->count($where, 'userId');
if ($count) {
$res = $this->mdSyliveActivityKpidata->select_groupby('cfUserId', $where, $sort_order, $page, $limit, 'cfUserId,bizId');
$filed = 'userId,bizId,browse,subscribe,orderTotal,watch';
$res = $this->mdSyliveActivityUser->select_groupby('userId', $where, $sort_order, $page, $limit, $filed);
foreach ($res as $v) {
$cfUserId = intval($v['cfUserId']);
$re_user = $this->mdSyliveUser->get(['userId' => $cfUserId, 'status>=' => 0]);
$userId = intval($v['userId']);
$re_user = $this->mdSyliveUser->get(['userId' => $userId, 'status>=' => 0]);
$consultantName = $re_user['uname'] ? $re_user['uname'] : $re_user['nickname'];
$re_org = $this->mdSyliveOrganization->get(['organizationId' => $v['bizId'], 'status>=' => 0]);
$re_org && $consultantName .= "({$re_org['organizationName']})";
$browse = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => 0, 'cfUserId' => $cfUserId, 'kpi' => 'browse']);
$subscribe = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => 0, 'cfUserId' => $cfUserId, 'kpi' => 'subscribe']);
$watch = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => 0, 'cfUserId' => $cfUserId, 'kpi' => 'watch']);
$order = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'type' => 0, 'cfUserId' => $cfUserId, 'kpi' => 'order']);
$livePV = $watchDuration = 0;
if ($channelId) {
$res_watch = $this->mdSyliveActivityKpidata->db->select('b.unionid')
->from('lc_market_sylive_activity_kpidata as a')
->join('lc_market_sylive_user as b', "b.userId=a.userId", 'left')
->where(['a.cfUserId' => $cfUserId, 'a.activityId' => $activityId, 'a.type' => 0, 'a.kpi' => 'watch', "unionid!=''" => null])
->where(['a.cfUserId' => $userId, 'a.activityId' => $activityId, 'a.type' => 0, 'a.kpi' => 'watch', "unionid!=''" => null])
->limit(10000, 0)
->get()->result_array();
if ($res_watch) {
@@ -380,11 +318,9 @@ class Statistics extends BaseController
}
}
}
$list[] = ['consultantName' => $consultantName, 'browse' => $browse, 'subscribe' => $subscribe, 'watch' => $watch
, 'order' => $order, 'livePV' => $livePV, 'watchDuration' => $watchDuration];
$list[] = ['consultantName' => $consultantName, 'browse' => $v['browse'], 'subscribe' => $v['subscribe'], 'watch' => $v['watch']
, 'order' => $v['orderTotal'], 'livePV' => $livePV, 'watchDuration' => $watchDuration];
}
$edit = array_column($list, 'browse');
array_multisort($edit, SORT_DESC, $list);
}
}
$date = ['list' => $list, 'count' => $count];
@@ -682,26 +618,72 @@ class Statistics extends BaseController
$this->return_response($data);
}
/**
* 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');
$areaRank = $bizRank = $consultantRank = [];
$area = $biz = $consultant = [];
if ($type == 1) {
} else {
$res = $this->mdSyliveActivityBiz->select(['activityId' => $activityId, 'status>=' => 0], 'browse desc', 0, 0, 'distinct(areaId) as areaId');
//大区排名
$res = $this->mdSyliveActivityBiz->select(['activityId' => $activityId, 'status>=' => 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'] : '-';
$re_kpi = $this->mdSyliveActivityBiz->sum($kpi, ['areaId' => $areaId, 'status>=' => 0, 'activityId' => $activityId]);
$areaRank[] = ['name' => $name, 'value' => $re_kpi[$kpi] ? $re_kpi[$kpi] : 0];
$value = $this->mdSyliveActivityKpidata->count(['activityId' => $activityId, 'areaId' => $areaId, 'type' => $type, 'kpi' => $kpi]);
$area[] = ['name' => $name, 'value' => $value, '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 = number_format_com(($v['value'] / $areaMax) * 100, 1, '');
$area[$k]['percent'] = $percent;
}
}
//门店排名
$kpi == 'order' && $kpi = 'orderTotal';
$res = $this->mdSyliveActivityBiz->select(['activityId' => $activityId, 'status>=' => 0]
, "{$kpi} desc,id desc", 0, 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 = number_format_com(($v[$kpi] / $bizMax) * 100, 1, '');
$biz[] = ['name' => $name, 'value' => $v[$kpi], 'percent' => $percent];
}
}
//顾问排名
$res = $this->mdSyliveActivityUser->select_groupby('userId', ['activityId' => $activityId, 'organizationId>' => 0]
, "{$kpi} desc,id desc", 0, 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 = number_format_com(($v[$kpi] / $consultantMax) * 100, 1, '');
$consultant[] = ['name' => $name, 'value' => $v[$kpi], 'percent' => $percent];
}
}
}
$data['area'] = $area;
$data['biz'] = $biz;
$data['consultant'] = $consultant;
$this->return_response($data);
}
}