From 73172f2bb1a5b9965ada47ca9d22585311a7fab5 Mon Sep 17 00:00:00 2001 From: dengbw Date: Thu, 5 Jan 2023 17:01:56 +0800 Subject: [PATCH] market_live_105 --- api/controllers/plan/Plan.php | 6 +- market/controllers/api/sylive/Activity.php | 2 +- market/controllers/api/sylive/Live.php | 342 +++++++++++++++++++++ 3 files changed, 346 insertions(+), 4 deletions(-) create mode 100644 market/controllers/api/sylive/Live.php diff --git a/api/controllers/plan/Plan.php b/api/controllers/plan/Plan.php index 9707a7bc..c994a9c3 100644 --- a/api/controllers/plan/Plan.php +++ b/api/controllers/plan/Plan.php @@ -53,10 +53,10 @@ class Plan extends CI_Controller $plan[] = array('url' => base_url(array('plan', 'order', 'send_aggs_biz_ding_piao_rpt')), 'interval' => 1); //向飞书发送小狸播报 // $plan[] = array('url' => base_url(array('plan', 'syt', 'subscribemsg')), 'interval' => 1); //私域通模板消息 - $plan[] = array('url' => base_url(array('plan', 'syt', 'syn_polyv')), 'interval' => 60); //私域通直播间数据同步 - $plan[] = array('url' => base_url(array('plan', 'syt', 'api_viewlog')), 'interval' => 1); //私域通直播间播放日志数据同步 +// $plan[] = array('url' => base_url(array('plan', 'syt', 'syn_polyv')), 'interval' => 60); //私域通直播间数据同步 +// $plan[] = array('url' => base_url(array('plan', 'syt', 'api_viewlog')), 'interval' => 1); //私域通直播间播放日志数据同步 +// $plan[] = array('url' => base_url(array('plan', 'syt', 'union_user')), 'interval' => 1); //直播数据关联用户 $plan[] = array('url' => base_url(array('plan', 'syt', 'out_time')), 'interval' => 1); //订单过期 - $plan[] = array('url' => base_url(array('plan', 'syt', 'union_user')), 'interval' => 1); //直播数据关联用户 $this->plan = $plan; } diff --git a/market/controllers/api/sylive/Activity.php b/market/controllers/api/sylive/Activity.php index e82245b8..2da83cf3 100644 --- a/market/controllers/api/sylive/Activity.php +++ b/market/controllers/api/sylive/Activity.php @@ -425,7 +425,7 @@ class Activity extends BaseController if (!$activityId) { $this->return_json('参数错误'); } - $re = $this->mdSyliveActivity->get(['activityId' => $activityId], 'activityId,title'); + $re = $this->mdSyliveActivity->get(['activityId' => $activityId], 'activityId,title,channelId'); if (!$re) { $this->return_json('活动不存在'); } diff --git a/market/controllers/api/sylive/Live.php b/market/controllers/api/sylive/Live.php new file mode 100644 index 00000000..96ca77a2 --- /dev/null +++ b/market/controllers/api/sylive/Live.php @@ -0,0 +1,342 @@ +load->model('market/Market_sylive_activity_model', 'mdSyliveActivity'); + $this->load->model('market/Market_sylive_viewlog_model', 'mdSyliveViewlog'); + $this->load->model('live/Live_polyv_session_model', 'mdPolyvSession'); + $this->load->model('live/Live_polyv_viewlog_model', 'mdPolyvViewlog'); + } + + /** + * Notes:直播管理列表 + * Created on: 2022/9/20 14:48 + * Created by: dengbw + */ + public function index_get() + { + $activityId = intval($this->input_param('activityId')); + $re = $this->mdSyliveActivity->get(['activityId' => $activityId]); + if (!$re) { + $this->return_json('活动不存在'); + } + $channelId = $re['channelId'] ? $re['channelId'] : ''; + $where = ['channelId' => $channelId]; + $count = $this->mdPolyvSession->count($where); + $list = []; + if ($count) { + $res = $this->mdPolyvSession->select($where, "startTime desc"); + foreach ($res as $key => $value) { + $setValue = $value; + $setValue['duration'] = $value['duration'] ? ceil($value['duration'] / 60) : '0'; + $setValue['totalPlayDuration'] = $value['totalPlayDuration'] ? ceil($value['totalPlayDuration'] / 60) : '0'; + $setValue['startTime'] = $value['startTime'] ? date('Y-m-d H:i:s', $value['startTime'] / 1000) : ''; + $setValue['endTime'] = $value['endTime'] ? date('Y-m-d H:i:s', $value['endTime'] / 1000) : ''; + $setValue['viewlogTime'] = $value['viewlog_time'] != '0000-00-00 00:00:00' + ? date('Y-m-d H:i', strtotime($value['viewlog_time'])) : ''; + $setValue['sessionTime'] = $value['session_time'] != '0000-00-00 00:00:00' ? + date('Y-m-d H:i', strtotime($value['session_time'])) : ''; + $list[] = $setValue; + } + } + $date = ['list' => $list, 'count' => $count]; + $this->return_response_list($date); + } + + /** + * Notes:同步频道多场次数据 + * Created on: 2023/1/3 14:28 + * Created by: dengbw + */ + public function session_api_get() + { + $activityId = intval($this->input_param('activityId')); + $channelId = $this->input_param('channelId'); + $sessionId = $this->input_param('sessionId'); + if (!$channelId) { + $this->return_json('参数错误'); + } + $this->load->library('PolyvApi'); + $polyv = new PolyvApi(['channel_id' => $channelId]); + if ($sessionId) { + $where['sessionIds'] = $sessionId; + } else { + $re = $this->mdSyliveActivity->get(['activityId' => $activityId]); + if (!$re) { + $this->return_json('活动不存在'); + } + $where['startTime'] = strtotime($re['timeStart']) * 1000; + $where['endTime'] = strtotime($re['timeEnd']) * 1000; + } + $results = $polyv->getSessionStats($where); + if ($results['status'] == 'success') { + foreach ($results['data']['list'] as $key => $value) { + $re = $this->mdPolyvSession->get(['channelId' => $channelId, 'sessionId' => $value['sessionId']]); + $data = [ + 'channelId' => $value['channelId'], 'sessionId' => $value['sessionId'], 'name' => $value['name'], + 'startTime' => $value['startTime'], 'endTime' => $value['endTime'], 'duration' => $value['duration'], + 'liveUV' => $value['liveUV'], 'livePV' => $value['livePV'], 'playbackUV' => $value['playbackUV'], + 'playbackPV' => $value['playbackPV'], 'totalPlayDuration' => $value['totalPlayDuration'], + 'session_time' => date('Y-m-d H:i:s'), + ]; + if ($re) {//同步更新数据 + $this->mdPolyvSession->update($data, ['id' => $re['id']]); + } else {//新增 + $data['c_time'] = time(); + $this->mdPolyvSession->add($data); + } + } + $this->return_response($results); + } else { + $this->return_json($results['message']); + } + } + + /** + * Notes:同步直播观看详情数据 + * Created on: 2022/12/26 16:49 + * Created by: dengbw + */ + public function view_api_get() + { + $this->load->model('market/market_sylive_user_model', 'mdSyliveUser'); + $this->load->model('market/market_sylive_activity_user_model', 'mdSyliveActivityUser'); + $this->load->model('market/Market_sylive_groups_user_model', 'mdSyliveGroupsUser'); + $activityId = intval($this->input_param('activityId')); + $channelId = $this->input_param('channelId'); + $sessionId = $this->input_param('sessionId'); + $page = $this->input_param('page'); + if (!$activityId) { + $this->return_json('活动id不能为空'); + } + if (!$channelId) { + $this->return_json('频道id不能为空'); + } + if (!$channelId) { + $this->return_json('场次id不能为空'); + } + !$page && $page = 1; + $re_session = $this->mdPolyvSession->get(['channelId' => $channelId, 'sessionId' => $sessionId]); + if (!$re_session) { + $this->return_json('无此场次数据'); + } + $params['sessionIds'] = $sessionId; + $params['startTime'] = $re_session['startTime']; + $params['endTime'] = $re_session['endTime']; + $params['page'] = $page; + $params['pageSize'] = 100; + $this->load->library('PolyvApi'); + $polyv = new PolyvApi(['channel_id' => $channelId]); + $results = $polyv->getViewLog($params); + $percent = $nextPageNumber = 0; + if ($results['status'] == 'success') { + $percent = ceil(($results['data']['pageNumber'] / $results['data']['totalPages']) * 100); + if (!$results['data']['lastPage'] && ($results['data']['pageNumber'] != $results['data']['nextPageNumber'])) { + $nextPageNumber = $results['data']['nextPageNumber']; + } + foreach ($results['data']['contents'] as $key => $value) { + $re = $this->mdPolyvViewlog->get(['playId' => $value['playId']]); + $data = [ + 'channelId' => $value['channelId'], 'sessionId' => $value['sessionId'], 'playId' => $value['playId'], + 'userId' => $value['userId'], 'playDuration' => $value['playDuration'], 'stayDuration' => $value['stayDuration'], + 'flowSize' => $value['flowSize'], 'param1' => $value['param1'], 'param2' => $value['param2'], + 'param3' => $value['param3'], 'param4' => $value['param4'], 'param5' => $value['param5'], 'ptype' => $value['ptype'], + 'ipAddress' => $value['ipAddress'], 'country' => $value['country'], 'province' => $value['province'], + 'city' => $value['city'], 'isp' => $value['isp'], 'referer' => $value['referer'], 'userAgent' => $value['userAgent'], + 'operatingSystem' => $value['operatingSystem'], 'browser' => $value['browser'], 'isMobile' => $value['isMobile'], + 'currentDay' => $value['currentDay'], 'createdTime' => $value['createdTime'], 'lastModified' => $value['lastModified'], + ]; + if ($re) {//同步更新数据 + $this->mdPolyvViewlog->update($data, ['id' => $re['id']]); + $vlogId = $re['id']; + } else {//新增 + $data['c_time'] = time(); + //新增数据 + $vlogId = $this->mdPolyvViewlog->add($data); + } + if ($vlogId) {//私域直播_观看日志 + $re_user = $this->mdSyliveUser->get(['unionid' => $data['param1']]); + if ($re_user) { + $ac_user = $this->mdSyliveActivityUser->get(['activityId' => $activityId, 'userId' => $re_user['userId']]); + if ($ac_user) { + $addData = [ + 'userId' => $re_user['userId'], + 'vlogId' => $vlogId, + 'activityId' => $activityId, + 'playId' => $data['playId'], + 'playDuration' => $data['playDuration'], + 'stayDuration' => $data['stayDuration'] + ]; + $p_user = $ac_user; + if ($ac_user['channelId']) {//顾问 + $ch_user = $this->mdSyliveGroupsUser->get(['activityId' => $activityId, 'userId' => $ac_user['channelId']]); + if ($ch_user) { + $p_user = $ch_user; + $addData['cfUserId'] = $p_user['userId']; + } + } + $p_user['bizId'] && $addData['bizId'] = $p_user['bizId']; + $p_user['levelId1'] && $addData['levelId1'] = $p_user['levelId1']; + $p_user['levelId2'] && $addData['levelId2'] = $p_user['levelId2']; + $p_user['levelId3'] && $addData['levelId3'] = $p_user['levelId3']; + $re_log = $this->mdSyliveViewlog->get(['vlogId' => $vlogId]); + if ($re_log) { + $this->mdSyliveViewlog->update($addData, ['id' => $re_log['id']]); + } else { + $addData['createTime'] = time(); + $this->mdSyliveViewlog->add($addData); + } + } + } + } + } + $this->mdPolyvSession->update(['viewlog_time' => date('Y-m-d H:i:s')], + ['channelId' => $channelId, 'sessionId' => $sessionId]); + } + $date = ['percent' => $percent, 'nextPageNumber' => $nextPageNumber]; + $this->return_response_list($date); + } + + /** + * Notes:订单管理列表 + * Created on: 2022/12/08 14:48 + * Created by: dengbw + */ + public function viewlog_get() + { + $date = $this->viewlogList($this->inputs); + $this->return_response_list($date); + } + + /** + * Notes:导出订单数据 + * Created on: 2022/12/08 15:26 + * Created by: dengbw + */ + public function export_viewlog_get() + { + $this->inputs['page'] = 1; + $this->inputs['limit'] = 10000; + $date = $this->viewlogList($this->inputs); + $this->return_response_list($date); + } + + private function viewlogList($params) + { + $this->load->model('market/Market_sylive_groups_model', 'mdSyliveGroups'); + $this->load->model('market/Market_sylive_user_model', 'mdSyliveUser'); + $activityId = intval($params['activityId']); + $bizId = intval($params['bizId']); + $sessionId = $params['sessionId']; + $page = $params['page']; + $limit = $params['limit']; + !$page && $page = 1; + !$limit && $limit = 10; + $where['a.activityId'] = $activityId; + $sessionId && $where['b.sessionId'] = $sessionId; + if ($bizId) { + $res_org = $this->mdSyliveGroups->get(["groupsId" => $bizId]); + if ($res_org) { + if ($res_org['groupsLevel']) { + $levelId = 'a.levelId' . $res_org['groupsLevel']; + $where[$levelId] = $bizId; + } else if ($res_org['parentId']) {//门店 + $where['a.bizId'] = $bizId; + } + } + } + if ($limit == 10000) { + $count = $limit; + } else { + $count = $this->db->select('a.id') + ->from('lc_market_sylive_viewlog as a') + ->join('lc_live_polyv_viewlog as b', "b.id=a.vlogId", 'left') + ->where($where) + ->count_all_results(); + } + $list = []; + if ($count) { + $offset = ($page - 1) * $limit; + $res = $this->db->select('a.*,b.createdTime as time') + ->from('lc_market_sylive_viewlog as a') + ->join('lc_live_polyv_viewlog as b', "b.id=a.vlogId", 'left') + ->where($where) + ->order_by('b.id Desc') + ->limit($limit, $offset) + ->get()->result_array(); + $user_ids = implode(",", array_column($res, 'userId')); + $map_user = $this->mdSyliveUser->map('userId', 'uname', ["userId in({$user_ids})" => null]); + foreach ($res as $v) { + $uname = ''; + $consultant = $this->consultantGet(['userId' => $v['userId'] + , 'levelId1' => $v['levelId1'], 'levelId2' => $v['levelId2'], 'levelId3' => $v['levelId3'] + , 'bizId' => $v['bizId'], 'cfUserId' => $v['cfUserId']]); + $playDuration = '0'; + if ($v['playDuration']) { + $format = $v['playDuration'] >= 3600 ? 'H:i:s' : 'i:s';//大1小时 + $playDuration = gmdate($format, $v['playDuration']); + } + $map_user[$v['userId']] && $uname = $map_user[$v['userId']]; + $item = [ + 'userId' => $v['userId'], 'uname' => $uname, 'playDuration' => $playDuration, + 'createTime' => date('Y-m-d H:i:s', $v['time'] / 1000) + , 'levelName1' => $consultant['levelName1'], 'levelName2' => $consultant['levelName2'], 'levelName3' => $consultant['levelName3'] + , 'stores' => $consultant['stores'], 'consultant' => $consultant['consultant'] + ]; + $list[] = $item; + } + } + if ($limit == 10000) { + return $list; + } else { + return ['list' => $list, 'count' => $count]; + } + } + + /** + * Notes:获取顾问信息 + * Created on: 2022/12/08 11:29 + * Created by: dengbw + * @param $params + * @return string + */ + private function consultantGet($params) + { + $stores = $consultant = $levelName1 = $levelName2 = $levelName3 = ''; + $levelId1 = intval($params['levelId1']); + $levelId2 = intval($params['levelId2']); + $levelId3 = intval($params['levelId3']); + $bizId = intval($params['bizId']); + $cfUserId = intval($params['cfUserId']); + $res_org = $this->mdSyliveGroups->get(["groupsId" => $bizId]); + $res_org['groupsName'] && $stores = $res_org['groupsName']; + if ($levelId1) { + $res_org = $this->mdSyliveGroups->get(["groupsId" => $levelId1]); + $res_org['groupsName'] && $levelName1 = $res_org['groupsName']; + } + if ($levelId2) { + $res_org = $this->mdSyliveGroups->get(["groupsId" => $levelId2]); + $res_org['groupsName'] && $levelName2 = $res_org['groupsName']; + } + if ($levelId3) { + $res_org = $this->mdSyliveGroups->get(["groupsId" => $levelId3]); + $res_org['groupsName'] && $levelName3 = $res_org['groupsName']; + } + $re_user = $this->mdSyliveUser->get(['userId' => $cfUserId]); + $re_user['uname'] && $consultant = $re_user['uname']; + return ['stores' => $stores, 'consultant' => $consultant, 'levelName1' => $levelName1, + 'levelName2' => $levelName2, 'levelName3' => $levelName3]; + } + +} \ No newline at end of file