diff --git a/market/controllers/api/sylive/GroupsStatistics.php b/market/controllers/api/sylive/GroupsStatistics.php index 983b2188..558cb468 100644 --- a/market/controllers/api/sylive/GroupsStatistics.php +++ b/market/controllers/api/sylive/GroupsStatistics.php @@ -46,11 +46,17 @@ class GroupsStatistics extends BaseController 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]; + if ($re['activityStart'] != '0000-00-00 00:00:00' && $re['timeEnd'] != '0000-00-00 00:00:00') { + $activityStart = date("Y-m-d", strtotime($re['activityStart'])); + $times = 0; + while ($times >= 0) { + $day = date("Y-m-d", strtotime("$activityStart +{$times} day")); + if ($day <= $re['timeEnd'] & $times <= 15) { + $days[] = ['name' => $day, 'value' => $day]; + $times++; + } else { + $times = -1; + } } } $title = $re['title']; @@ -263,177 +269,20 @@ class GroupsStatistics extends BaseController */ public function 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) { - $channelId = intval($re['channelId']); - !$sort && $sort = 'browse'; - !$order && $order = 'desc'; - if ($sort == 'order' && !$day) { - $sort_order = 'orderTotal ' . $order; - } else { - $sort_order = $sort . ' ' . $order; - } - //一级分组 - $map_level1 = $this->mdSyliveGroups->map('groupsId', 'groupsName', ['groupsLevel' => 1, 'status>=' => 0]); - $map_biz = []; - if ($day) {//按天搜索 - $createTimeStart = strtotime($day . ' 00:00:00'); - $createTimeEnd = strtotime($day . ' 23:59:59'); - $where = $where_order = $where_sort = ['activityId' => $activityId, 'day' => $day, '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,levelId1,count(id) as total"); - $str_biz = implode(',', array_column($res, 'bizId')); - $map_biz = $this->mdSyliveGroups->map('groupsId', 'groupsName', - ["groupsId in({$str_biz})" => null, 'activityId' => $activityId, 'status>=' => 0]); - foreach ($res as $v) { - $bizId = intval($v['bizId']); - $levelId1 = intval($v['levelId1']); - $bizName = ''; - if ($map_level1[$levelId1]) { - $bizName = $map_level1[$levelId1]; - } - if ($map_biz[$bizId]) { - $bizName = $bizName ? $bizName . ' ' . $map_biz[$bizId] : $map_biz[$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 == '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->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 - , '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); - } - } - $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, 'itemId' => $itemId, 'bizId>' => 0, 'kpi' => $sort]; - $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, "bizId,levelId1,count(id) as orderTotal"); - $str_biz = implode(',', array_column($res, 'bizId')); - $map_biz = $this->mdSyliveGroups->map('groupsId', 'groupsName', - ["groupsId in({$str_biz})" => null, 'activityId' => $activityId, 'status>=' => 0]); - } else { - $res = $this->mdSyliveGroups->select($where, "{$sort_order},groupsId desc", $page, $limit, - 'groupsId,groupsName,browse,subscribe,watch,orderTotal'); - } - foreach ($res as $v) { - $bizName = ''; - if ($sort == 'order' && $itemId) { - $bizId = intval($v['bizId']); - $levelId1 = intval($v['levelId1']); - if ($map_level1[$levelId1]) { - $bizName = $map_level1[$levelId1]; - } - if ($map_biz[$bizId]) { - $bizName = $bizName ? $bizName . ' ' . $map_biz[$bizId] : $map_biz[$bizId]; - } - } else { - $bizId = intval($v['groupsId']); - $re_use = $this->mdSyliveGroupsUser->get(["bizId" => $bizId, 'activityId' => $activityId, 'status>=' => 0]); - if ($re_use['levelId1']) {//根据用户找1级 - $levelId1 = intval($re_use['levelId1']); - if ($map_level1[$levelId1]) { - $bizName = $map_level1[$levelId1]; - } - } - if (!$bizName && $bizId) {//根据门店向上找1级 - $bizName = $this->getLevelName($bizId, 1, ''); - } - if ($v['groupsName']) { - $bizName = $bizName ? $bizName . ' ' . $v['groupsName'] : $v['groupsName']; - } - } - $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(['kpi' => 'order', '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']); - } - $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]; + $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); } @@ -444,171 +293,20 @@ class GroupsStatistics extends BaseController */ public function 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) { - $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, '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 - , "levelId1,levelId2,levelId3,bizId as groupsId,cfUserId,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['groupsId'] && !in_array($v['groupsId'], $groups_ids)) { - $groups_ids[] = $v['groupsId']; - } - $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 = $uname = ''; - $map_groups[$v['levelId1']] && $bizName = $map_groups[$v['levelId1']] . ' '; - $map_groups[$v['levelId2']] && $bizName .= $map_groups[$v['levelId2']] . ' '; - $map_groups[$v['levelId3']] && $bizName .= $map_groups[$v['levelId3']] . ' '; - if ($map_groups[$v['groupsId']]) { - $groupsName = $map_groups[$v['groupsId']] . ' '; - if ($bizName != $groupsName) { - $bizName .= $groupsName; - } - } - $map_user[$cfUserId] && $uname = $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 == '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); - } - $list[] = ['consultantName' => $bizName . $uname, 'browse' => $browse, 'subscribe' => $subscribe - , 'watch' => $watch, 'order' => $order]; - } - } - } 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 as userId,levelId1,levelId2,levelId3,bizId as groupsId,count(id) as orderTotal"); - } else { - $res = $this->mdSyliveGroupsUser->select($where, "{$sort_order},groupsUserId desc", $page, $limit, - 'userId,levelId1,levelId2,levelId3,groupsId,browse,subscribe,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['groupsId'] && !in_array($v['groupsId'], $groups_ids)) { - $groups_ids[] = $v['groupsId']; - } - $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', 'uname', ["userId in({$str_user})" => null]); - foreach ($res as $v) { - $userId = intval($v['userId']); - $bizName = $uname = ''; - $map_groups[$v['levelId1']] && $bizName = $map_groups[$v['levelId1']] . ' '; - $map_groups[$v['levelId2']] && $bizName .= $map_groups[$v['levelId2']] . ' '; - $map_groups[$v['levelId3']] && $bizName .= $map_groups[$v['levelId3']] . ' '; - if ($map_groups[$v['groupsId']]) { - $groupsName = $map_groups[$v['groupsId']] . ' '; - if ($bizName != $groupsName) { - $bizName .= $groupsName; - } - } - $map_user[$userId] && $uname = $map_user[$userId]; - if ($sort != 'order' && $itemId) {//有商品id,区分订单 - $order = $this->mdSyliveActivityKpidata->count(['kpi' => 'order', 'cfUserId' => $userId, 'itemId' => $itemId, 'activityId' => $activityId]); - } else { - $order = $v['orderTotal']; - } - if ($sort == 'order' && $itemId) {//只改变订单数值,其它不变 - $re = $this->mdSyliveGroupsUser->get(['userId' => $userId, 'activityId' => $activityId], 'browse,subscribe,watch'); - $v['browse'] = intval($re['browse']); - $v['subscribe'] = intval($re['subscribe']); - $v['watch'] = intval($re['watch']); - } - $list[] = ['consultantName' => $bizName . $uname, 'browse' => $v['browse'], 'subscribe' => $v['subscribe'] - , 'watch' => $v['watch'], 'order' => $order]; - } - } - } - } - $date = ['list' => $list, 'count' => $count]; + $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); } @@ -724,6 +422,374 @@ class GroupsStatistics extends BaseController $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']);//商品 + !$page && $page = 1; + !$limit && $limit = 10; + $list = []; + $count = 0; + $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; + } + $groups_ids = $map_groups = []; + if ($day) {//按天搜索 + $createTimeStart = strtotime($day . ' 00:00:00'); + $createTimeEnd = strtotime($day . ' 23:59:59'); + $where = $where_order = $where_sort = ['activityId' => $activityId, 'day' => $day, '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, "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']); + $levelName1 = $levelName2 = $levelName3 = $bizName = '-'; + $map_groups[$v['levelId1']] && $levelName1 = $map_groups[$v['levelId1']]; + $map_groups[$v['levelId2']] && $levelName2 = $map_groups[$v['levelId2']]; + $map_groups[$v['levelId3']] && $levelName3 = $map_groups[$v['levelId3']]; + $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 == '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->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 + , '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, '') . '%'; + $list[] = ['levelName1' => $levelName1, 'levelName2' => $levelName2, 'levelName3' => $levelName3, 'bizName' => $bizName + , 'consultant' => $consultant, 'allConsultant' => $allConsultant, 'consultantPer' => $consultantPer, 'browse' => $browse, 'subscribe' => $subscribe + , 'watch' => $watch, 'order' => $orderTotal, 'livePV' => $livePV, 'watchDuration' => $watchDuration]; + } + } + } else { + if ($sort == 'order' && $itemId) {//有订单排序与商品id,区分订单搜索 + $where = ['activityId' => $activityId, 'itemId' => $itemId, 'bizId>' => 0, 'kpi' => $sort]; + $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 { + $res = $this->mdSyliveGroups->select($where, "{$sort_order},groupsId desc", $page, $limit, + 'groupsId as bizId,groupsName,browse,subscribe,watch,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']); + $levelName1 = $levelName2 = $levelName3 = $bizName = '-'; + $map_groups[$v['levelId1']] && $levelName1 = $map_groups[$v['levelId1']]; + $map_groups[$v['levelId2']] && $levelName2 = $map_groups[$v['levelId2']]; + $map_groups[$v['levelId3']] && $levelName3 = $map_groups[$v['levelId3']]; + 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(['kpi' => 'order', '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']); + } + $consultantPer = number_format_com($consultant / $allConsultant * 100, 2, '') . '%'; + $list[] = ['levelName1' => $levelName1, 'levelName2' => $levelName2, 'levelName3' => $levelName3, 'bizName' => $bizName + , 'consultant' => $consultant, 'allConsultant' => $allConsultant, 'consultantPer' => $consultantPer, 'browse' => $v['browse'], 'subscribe' => $v['subscribe'] + , 'watch' => $v['watch'], 'order' => $order, 'livePV' => $livePV, 'watchDuration' => $watchDuration]; + } + } + } + } + if ($limit == 10000) { + return $list; + } else { + return ['list' => $list, 'count' => $count]; + } + } + + 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']);//商品 + !$page && $page = 1; + !$limit && $limit = 10; + $list = []; + !$sort && $sort = 'browse'; + !$order && $order = 'desc'; + if ($sort == 'order' && !$day) { + $sort_order = 'orderTotal ' . $order; + } else { + $sort_order = $sort . ' ' . $order; + } + if ($day) { + $where = $where_order = $where_sort = ['activityId' => $activityId, 'day' => $day, '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 + , "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']); + $levelName1 = $levelName2 = $levelName3 = $bizName = $consultantName = '-'; + $map_groups[$v['levelId1']] && $levelName1 = $map_groups[$v['levelId1']]; + $map_groups[$v['levelId2']] && $levelName2 = $map_groups[$v['levelId2']]; + $map_groups[$v['levelId3']] && $levelName3 = $map_groups[$v['levelId3']]; + $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 == '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); + } + $list[] = ['levelName1' => $levelName1, 'levelName2' => $levelName2, 'levelName3' => $levelName3, + 'bizName' => $bizName, 'consultantName' => $consultantName, 'browse' => $browse, + 'subscribe' => $subscribe, 'watch' => $watch, 'order' => $order]; + } + } + } 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 { + $res = $this->mdSyliveGroupsUser->select($where, "{$sort_order},groupsUserId desc", $page, $limit, + 'userId as cfUserId,levelId1,levelId2,levelId3,bizId,browse,subscribe,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']); + $levelName1 = $levelName2 = $levelName3 = $bizName = $consultantName = '-'; + $map_groups[$v['levelId1']] && $levelName1 = $map_groups[$v['levelId1']]; + $map_groups[$v['levelId2']] && $levelName2 = $map_groups[$v['levelId2']]; + $map_groups[$v['levelId3']] && $levelName3 = $map_groups[$v['levelId3']]; + $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(['kpi' => 'order', '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']); + } + $browse = intval($v['browse']); + $subscribe = intval($v['subscribe']); + $watch = intval($v['watch']); + $order = intval($order); + $list[] = ['levelName1' => $levelName1, 'levelName2' => $levelName2, 'levelName3' => $levelName3, + 'bizName' => $bizName, 'consultantName' => $consultantName, 'browse' => $browse, + 'subscribe' => $subscribe, 'watch' => $watch, 'order' => $order]; + } + } + } + if ($limit == 10000) { + return $list; + } else { + return ['list' => $list, 'count' => $count]; + } + } + /** * Notes:获取门店数 * Created on: 2022/12/6 14:48