$params, 'message' => '雷达组名称已使用', ],'interface'); return ['雷达组名称已使用', 3400]; } // 获取雷达组最大排序值 $maxSortOrder = RadarCustomerGroup::getMaxSortOrder($params['corpid'], $params['sys_group_id']); $res = RadarCustomerGroup::query() ->insert([ 'sys_group_id' => $params['sys_group_id'], 'corpid' => $params['corpid'], 'group_name' => $params['group_name'], 'creator_id' => \Auth::id(), 'sort_order' => $maxSortOrder + 1, ]); if($res) { return ['添加成功', 0]; } else { Log::logError('radarGroupCreate', [ 'params' => $params, 'message' => '添加失败', 'res' => $res, ],'interface'); return ['添加失败', 400]; } } catch (\Exception $exception) { Log::logError('radarGroupCreate', [ 'params' => $params, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return ['添加失败', 400]; } } // 雷达组列表 public static function radarGroupList($params) { try{ $query = RadarCustomerGroup::query() ->where('corpid', $params['corpid']) ->where('enable', 1); if(!empty($params['sys_group_id'])) { $query->where('sys_group_id', $params['sys_group_id']); } if(!empty($params['keyword'])) { $query->where('group_name', 'like', '%'.$params['keyword'].'%'); } $count = $query->count(); $list = $query->select(['group_name', 'sort_order']) ->selectRaw('id as group_id') ->orderBy('sort_order', 'desc') ->offset(($params['page']-1) * $params['page_size']) ->limit($params['page_size']) ->get(); return [$list, $count]; } catch (\Exception $exception) { Log::logError('radarGroupList', [ 'params' => $params, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return [[], 0]; } } public static function radarGroupUpdate($params) { try{ // 验证雷达组名称是否重复 $info = RadarCustomerGroup::getGroupInfoByName($params['corpid'], $params['sys_group_id'], $params['group_name']); if(isset($info->id) && $info->id != $params['group_id']) { Log::logError('radarGroupUpdate', [ 'params' => $params, 'message' => '雷达组名称已使用', ],'interface'); return ['雷达组名称已使用', 3400]; } $radarGroupInfo = RadarCustomerGroup::query() ->where('id', $params['group_id']) ->first(); if($params['group_name'] == $radarGroupInfo->group_name) { return ['修改成功', 0]; } $res = RadarCustomerGroup::query() ->where('id', $params['group_id']) ->update([ 'group_name' => $params['group_name'] ]); if($res) { return ['修改成功', 0]; } else { Log::logError('radarGroupUpdate', [ 'params' => $params, 'message' => '修改失败', 'res' => $res, ],'interface'); return ['修改失败', 400]; } } catch (\Exception $exception) { Log::logError('radarGroupUpdate', [ 'params' => $params, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return ['修改失败', 400]; } } public static function radarGroupDelete($params) { try{ \DB::begintransaction(); $groupRes = RadarCustomerGroup::query() ->where('id', $params['group_id']) ->update([ 'enable' => 0 ]); // 防止出现本来雷达组下就没有雷达,更新不到数据导致整体删除失败 $detailCount = RadarCustomerDetail::query() ->where('group_id', $params['group_id']) ->where('enable', 1) ->count(); if($detailCount > 0) { if(1 == $params['type']) { $detailRes = RadarCustomerDetail::query() ->where('group_id', $params['group_id']) ->where('enable', 1) ->update(['group_id' => 0]); } else { $detailRes = RadarCustomerDetail::query() ->where('group_id', $params['group_id']) ->where('enable', 1) ->update(['enable' => 0]); } } else { $detailRes = true; } if($groupRes && $detailRes) { \DB::commit(); return ['删除成功', 0]; } else { \DB::rollBack(); Log::logError('radarGroupDelete', [ 'params' => $params, 'message' => '修改失败', 'group_res' => $groupRes, 'detail_res' => $detailRes, ],'interface'); return ['删除失败', 400]; } } catch (\Exception $exception) { Log::logError('radarGroupDelete', [ 'params' => $params, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return ['删除失败', 400]; } } public static function radarGroupSortUpdate($params) { try{ // 查询被操作分组原始排序值 $operatorGroup = RadarCustomerGroup::query() ->where('id', $params['operate_group_id']) ->first(); $operatorGroupOriginalSortOrder = $operatorGroup->sort_order; if(!empty($params['behind_group_id']) && empty($params['front_group_id'])) { // 上移到第一位,判断是否有调整后的排在调整分组后面的分组id $behindGroup = RadarCustomerGroup::query() ->where('id', $params['behind_group_id']) ->first(); $behindGroupOriginalSortOrder = $behindGroup->sort_order; list($data, $code) = self::radarGroupMoveUp($params, $behindGroupOriginalSortOrder, $operatorGroupOriginalSortOrder); return [$data, $code]; } else if(!empty($params['front_group_id']) && empty($params['behind_group_id'])) { // 下移到最后一位,判断是否有调整后的排在调整分组前面的分组id $frontGroup = RadarCustomerGroup::query() ->where('id', $params['front_group_id']) ->first(); $frontGroupOriginalSortOrder = $frontGroup->sort_order; list($data, $code) = self::radarGroupMoveDown($params, $frontGroupOriginalSortOrder, $operatorGroupOriginalSortOrder); return [$data, $code]; } else if(!empty($params['front_group_id']) && !empty($params['behind_group_id'])) { $frontGroup = RadarCustomerGroup::query() ->where('id', $params['front_group_id']) ->first(); $frontGroupOriginalSortOrder = $frontGroup->sort_order; $behindGroup = RadarCustomerGroup::query() ->where('id', $params['behind_group_id']) ->first(); $behindGroupOriginalSortOrder = $behindGroup->sort_order; if($operatorGroupOriginalSortOrder > $frontGroupOriginalSortOrder) { // 下移 list($data, $code) = self::radarGroupMoveDown($params, $frontGroupOriginalSortOrder, $operatorGroupOriginalSortOrder); } else { // 上移 list($data, $code) = self::radarGroupMoveUp($params, $behindGroupOriginalSortOrder, $operatorGroupOriginalSortOrder); } return [$data, $code]; } else { Log::logError('radarGroupSortUpdate', [ 'params' => $params, 'message' => '被操作雷达组前后分组id不可同时为空', ],'interface'); return ['被操作雷达组前后分组id不可同时为空', 3405]; } } catch (\Exception $exception) { Log::logError('radarGroupSortUpdate', [ 'params' => $params, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return ['移动失败', 400]; } } // 上移 public static function radarGroupMoveUp($params, $behindGroupOriginalSortOrder, $operatorGroupOriginalSortOrder) { \DB::begintransaction(); $moveRes = RadarCustomerGroup::query() ->where('corpid', $params['corpid']) ->where('sys_group_id', $params['sys_group_id']) ->where('enable', 1) ->where('sort_order', '<=', $behindGroupOriginalSortOrder) ->where('sort_order', '>', $operatorGroupOriginalSortOrder) ->decrement('sort_order');// 自减1 $operatorRes = RadarCustomerGroup::query() ->where('id', $params['operate_group_id']) ->update(['sort_order' => $behindGroupOriginalSortOrder]); if($moveRes && $operatorRes) { \DB::commit(); return ['移动成功', 0]; } else { \DB::rollBack(); Log::logError('radarGroupSortUpdate', [ 'params' => $params, 'message' => '移动失败', 'move_res' => $moveRes, 'operator_res' => $operatorRes, 'operator_sort_order' => $operatorGroupOriginalSortOrder, 'behind_sort_order' => $behindGroupOriginalSortOrder, ],'interface'); return ['移动失败', 400]; } } // 下移 public static function radarGroupMoveDown($params, $frontGroupOriginalSortOrder, $operatorGroupOriginalSortOrder) { \DB::begintransaction(); $moveRes = RadarCustomerGroup::query() ->where('corpid', $params['corpid']) ->where('sys_group_id', $params['sys_group_id']) ->where('enable', 1) ->where('sort_order', '>=', $frontGroupOriginalSortOrder) ->where('sort_order', '<', $operatorGroupOriginalSortOrder) ->increment('sort_order');// 自增1 $operatorRes = RadarCustomerGroup::query() ->where('id', $params['operate_group_id']) ->update(['sort_order' => $frontGroupOriginalSortOrder]); if($moveRes && $operatorRes) { \DB::commit(); return ['移动成功', 0]; } else { \DB::rollBack(); Log::logError('radarGroupSortUpdate', [ 'params' => $params, 'message' => '移动失败', 'move_res' => $moveRes, 'operator_res' => $operatorRes, 'operator_sort_order' => $operatorGroupOriginalSortOrder, 'front_sort_order' => $frontGroupOriginalSortOrder, ],'interface'); return ['移动失败', 400]; } } public static function radarCreate($params) { try{ // 查询目前该企业排序值最大的排序值 $maxSortOrder = RadarCustomerDetail::getMaxSortOrder($params['corpid'], $params['sys_group_id']); $params['creator_id'] = \Auth::id(); $params['sort_order'] = $maxSortOrder+1; $params['url_list'] = json_encode($params['url_list']); if(1 == $params['customer_label']) { $params['tag_list'] = json_encode($params['tag_list']); } else { $params['tag_list'] = json_encode([]); } $params['group_id'] = empty($params['group_id']) ? '' : $params['group_id']; $res = RadarCustomerDetail::query() ->insert($params); if($res) { return ['创建成功', 0]; } else { Log::logError('radarCreate', [ 'params' => $params, 'message' => '创建失败', 'res' => $res, ],'interface'); return ['创建失败', 400]; } } catch (\Exception $exception) { Log::logError('radarCreate', [ 'params' => $params, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return ['创建失败', 400]; } } public static function radarDetail($params) { try{ $detail = RadarCustomerDetail::query() ->select(['group_id', 'name', 'type', 'url_list', 'title', 'description', 'cover_id', 'behavior_notification', 'dynamic_notification', 'customer_label', 'tag_list', 'corpid']) ->selectRaw('id as radar_id') ->where('id', $params['radar_id']) ->where('enable', 1) ->first(); if(empty($detail)) { return []; } else { // 通过封面素材id查询对应的文件链接 $materialInfo = Material::query() ->where('id', $detail->cover_id) ->first(); $detail->cover_url = !empty($materialInfo->oss_url) ? $materialInfo->oss_url : null; $detail->tag_list = is_array(json_decode($detail->tag_list, 1)) ? json_decode($detail->tag_list, 1) : []; $detail->url_list = json_decode($detail->url_list, 1); $detail->group_id = empty($detail->group_id) ? null : $detail->group_id; return $detail->toArray(); } } catch (\Exception $exception) { Log::logError('radarDetail', [ 'params' => $params, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return []; } } public static function radarUpdate($params) { try{ $info = RadarCustomerDetail::query() ->where('id', $params['radar_id']) ->where('enable', 1) ->first(); if(empty($info)) { return ['雷达信息查询异常', ]; } $updateData = []; if($params['group_id'] && strval($params['group_id']) != $info->group_id) { $updateData['group_id'] = empty($params['group_id']) ? '' : $params['group_id']; } if($params['name'] && $params['name'] != $info->name) { $updateData['name'] = $params['name']; } if($params['url_list'] && json_encode($params['url_list']) != $info->url_list) { $updateData['url_list'] = json_encode($params['url_list']); } if($params['title'] && $params['title'] != $info->title) { $updateData['title'] = $params['title']; } if($params['description'] && $params['description'] != $info->description) { $updateData['description'] = $params['description']; } if($params['cover_id'] && $params['cover_id'] != $info->cover_id) { $updateData['cover_id'] = $params['cover_id']; } if(isset($params['behavior_notification']) && $params['behavior_notification'] != $info->behavior_notification) { $updateData['behavior_notification'] = $params['behavior_notification']; } if(isset($params['dynamic_notification']) && $params['dynamic_notification'] != $info->dynamic_notification) { $updateData['dynamic_notification'] = $params['dynamic_notification']; } if(isset($params['customer_label']) && $params['customer_label'] != $info->customer_label) { $updateData['customer_label'] = $params['customer_label']; } if(1 == $params['customer_label']) { if(isset($params['tag_list']) && json_encode($params['tag_list']) != $info->tag_list) { $updateData['tag_list'] = json_encode($params['tag_list']); } } else { if(json_encode([]) != $info->tag_list) { $updateData['tag_list'] = json_encode([]); } } if(empty($updateData)) { return ['编辑成功', 0]; } $res = RadarCustomerDetail::query() ->where('id', $params['radar_id']) ->update($updateData); if($res) { return ['编辑成功', 0]; } else { Log::logError('radarUpdate', [ 'params' => $params, 'message' => '编辑失败', 'res' => $res, ],'interface'); return ['编辑失败', 400]; } } catch (\Exception $exception) { Log::logError('radarUpdate', [ 'params' => $params, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return ['编辑失败', 400]; } } public static function radarDelete($params) { try{ $res = RadarCustomerDetail::query() ->where('id', $params['radar_id']) ->update(['enable' => 0]); if($res) { return ['删除成功', 0]; } else { Log::logError('radarDelete', [ 'params' => $params, 'message' => '删除失败', 'res' => $res, ],'interface'); return ['删除失败', 400]; } } catch (\Exception $exception) { Log::logError('radarDelete', [ 'params' => $params, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return ['删除失败', 400]; } } // 雷达列表 public static function radarList($params) { try{ $query = RadarCustomerDetail::query() ->where('corpid', $params['corpid']) ->where('enable', 1); if(!empty($params['group_id'])) { $query->where('group_id', $params['group_id']); } if(!empty($params['sys_group_id'])) { $query->where('sys_group_id', $params['sys_group_id']); } if(!empty($params['name'])) { $query->where('name', 'like', '%'.$params['name'].'%'); } if(isset($params['group_id']) && 0 == $params['group_id']) { $query->whereRaw('group_id is null'); } $count = $query->count(); $data = $query->select(['name', 'type', 'tag_list', 'create_time', 'group_id', 'customer_label', 'description', 'cover_id', 'url_list', 'title']) ->selectRaw('id as radar_id') ->orderBy('sort_order', 'desc') ->offset(($params['page'] - 1) * $params['page_size']) ->limit($params['page_size']) ->get(); // 提取雷达id,雷达分组id,雷达客户标签 $radarIdList = $radarGroupIdList = $tagList = []; foreach($data as $val) { if(!empty($val->group_id)) { $radarGroupIdList[] = $val->group_id; } $radarIdList[] = $val->radar_id; if(!empty($val->tag_list)) { $radarTagList = json_decode($val->tag_list, 1); $tagList = array_merge($tagList, $radarTagList); } # 处理缩略图 $val->pic_url = Material::where('id', $val->cover_id)->value('oss_url'); } $tagList = array_unique($tagList); $radarGroupIdList = array_unique($radarGroupIdList); // 批量查询分组 if(!empty($radarGroupIdList)) { $radarGroupList = RadarCustomerGroup::query() ->whereIn('id', $radarGroupIdList) ->where('enable', 1) ->get(); } else { $radarGroupList = null; } // 批量查询客户标签 if(!empty($tagList)) { $tagDataList = Tag::query() ->where('corpid', $params['corpid']) ->whereIn('tag_md5', $tagList) ->where('enable', 1) ->get(); } else { $tagDataList = null; } // 批量查询雷达统计信息 $radarStatistics = RadarCustomerBehavior::radarStatistics($radarIdList); foreach($data as &$value) { if(1 == $value->customer_label && !empty($value->tag_list)) { $radarTagList = json_decode($value->tag_list, 1); if(is_array($radarTagList) && !empty($radarTagList)) { $value->tag_list = $tagDataList->whereIn('tag_md5', $radarTagList)->pluck('tag_name')->toArray(); } else { $value->tag_list = []; } } else { $value->tag_list = []; } if(!empty($value->group_id)) { $group_name = $radarGroupList->where('id', $value->group_id)->first(); $value->group_name = !empty($group_name->group_name) ? $group_name->group_name : null; } else { $value->group_name = null; } $statistics = $radarStatistics->where('radar_id', $value->radar_id)->first(); $value->uv_count = !empty($statistics->uv_count) ? $statistics->uv_count : 0; $value->send_count = !empty($statistics->send_count) ? $statistics->send_count : 0; } return [$data, $count]; } catch (\Exception $exception) { Log::logError('radarList', [ 'params' => $params, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return [[], 0]; } } public static function radarSortUpdate($params) { try{ // 查询被操作雷达原始排序值 $operatorRadar = RadarCustomerDetail::query() ->where('id', $params['operate_radar_id']) ->first(); $operatorRadarOriginalSortOrder = $operatorRadar->sort_order; if(!empty($params['behind_radar_id']) && empty($params['front_radar_id'])) { // 上移到第一个,判断是否有调整后的排在调整雷达后面的分组id $behindRadar = RadarCustomerDetail::query() ->where('id', $params['behind_radar_id']) ->first(); $behindRadarOriginalSortOrder = $behindRadar->sort_order; list($data, $code) = self::radarMoveUp($params, $behindRadarOriginalSortOrder, $operatorRadarOriginalSortOrder); return [$data, $code]; } else if(!empty($params['front_radar_id']) && empty($params['behind_radar_id'])) { // 下移到最后一个,判断是否有调整后的排在调整分组前面的分组id $frontRadar = RadarCustomerDetail::query() ->where('id', $params['front_radar_id']) ->first(); $frontRadarOriginalSortOrder = $frontRadar->sort_order; list($data, $code) = self::radarMoveDown($params, $frontRadarOriginalSortOrder, $operatorRadarOriginalSortOrder); return [$data, $code]; } else if (!empty($params['behind_radar_id']) && !empty($params['front_radar_id'])) { // 中间移动,首先判断上移还是下移 $frontRadar = RadarCustomerDetail::query() ->where('id', $params['front_radar_id']) ->first(); $frontRadarOriginalSortOrder = $frontRadar->sort_order; if($operatorRadarOriginalSortOrder > $frontRadarOriginalSortOrder) { // 下移 list($data, $code) = self::radarMoveDown($params, $frontRadarOriginalSortOrder, $operatorRadarOriginalSortOrder); } else { // 上移 $behindRadar = RadarCustomerDetail::query() ->where('id', $params['behind_radar_id']) ->first(); $behindRadarOriginalSortOrder = $behindRadar->sort_order; list($data, $code) = self::radarMoveUp($params, $behindRadarOriginalSortOrder, $operatorRadarOriginalSortOrder); } return [$data, $code]; } else { Log::logError('radarSortUpdate', [ 'params' => $params, 'message' => '被操作雷达前后雷达id不可同时为空', ],'interface'); return ['被操作雷达前后雷达id不可同时为空', 3405]; } } catch (\Exception $exception) { Log::logError('radarSortUpdate', [ 'params' => $params, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return ['移动失败', 400]; } } // 上移 public static function radarMoveUp($params, $behindRadarOriginalSortOrder, $operatorRadarOriginalSortOrder) { \DB::begintransaction(); $moveRes = RadarCustomerDetail::query() ->where('corpid', $params['corpid']) ->where('sys_group_id', $params['sys_group_id']) ->where('enable', 1) ->where('sort_order', '<=', $behindRadarOriginalSortOrder) ->where('sort_order', '>', $operatorRadarOriginalSortOrder) ->decrement('sort_order');// 自减1 $operatorRes = RadarCustomerDetail::query() ->where('id', $params['operate_radar_id']) ->update(['sort_order' => $behindRadarOriginalSortOrder]); if($moveRes && $operatorRes) { \DB::commit(); return ['移动成功', 0]; } else { \DB::rollBack(); Log::logError('radarSortUpdate', [ 'params' => $params, 'message' => '移动失败', 'move_res' => $moveRes, 'operator_res' => $operatorRes, 'operator_sort_order' => $operatorRadarOriginalSortOrder, 'behind_sort_order' => $behindRadarOriginalSortOrder, ],'interface'); return ['移动失败', 400]; } } // 下移 public static function radarMoveDown($params, $frontRadarOriginalSortOrder, $operatorRadarOriginalSortOrder) { \DB::begintransaction(); $moveRes = RadarCustomerDetail::query() ->where('corpid', $params['corpid']) ->where('sys_group_id', $params['sys_group_id']) ->where('enable', 1) ->where('sort_order', '>=', $frontRadarOriginalSortOrder) ->where('sort_order', '<', $operatorRadarOriginalSortOrder) ->increment('sort_order');// 自增1 $operatorRes = RadarCustomerDetail::query() ->where('id', $params['operate_radar_id']) ->update(['sort_order' => $frontRadarOriginalSortOrder]); if($moveRes && $operatorRes) { \DB::commit(); return ['移动成功', 0]; } else { \DB::rollBack(); Log::logError('radarSortUpdate', [ 'params' => $params, 'message' => '修改失败', 'move_res' => $moveRes, 'operator_res' => $operatorRes, 'operator_sort_order' => $operatorRadarOriginalSortOrder, 'front_sort_order' => $frontRadarOriginalSortOrder, ],'interface'); return ['移动失败', 400]; } } public static function dataStatisticsTotal($params) { try{ // 查询总访问人数,总访问次数 $totalData = RadarCustomerBehavior::radarTotal($params['radar_id'], null, null); // 查询今日访问人数,今日访问次数 $todayData = RadarCustomerBehavior::radarTotal($params['radar_id'], date('Y-m-d'), null); $data['total_pv_count'] = !empty($totalData->pv_count) ? $totalData->pv_count : 0; $data['total_uv_count'] = !empty($totalData->uv_count) ? $totalData->uv_count : 0; $data['today_pv_count'] = !empty($todayData->pv_count) ? $todayData->pv_count : 0; $data['today_uv_count'] = !empty($todayData->uv_count) ? $todayData->uv_count : 0; return $data; } catch (\Exception $exception) { Log::logError('dataStatisticsTotal', [ 'params' => $params, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return [ 'total_pv_count' => 0, 'total_uv_count' => 0, 'today_pv_count' => 0, 'today_uv_count' => 0, ]; } } // 数据分析 public static function dataStatisticsList($params) { try { if (!empty($params['customer_name'])) { $externalUseridList = Customer::suffix($params['corpid']) ->where('corpid', $params['corpid']) ->where('name', 'like', '%' . $params['customer_name'] . '%') ->pluck('external_userid') ->toArray(); if (empty($externalUseridList)) { Log::logError('dataStatisticsList', [ 'params' => $params, 'message' => '根据客户名称查询结果为空', ], 'interface'); return [['list' => [], 'customer_count' => 0], 0]; } $params['external_userid_list'] = $externalUseridList; } $statisticsQuery = RadarCustomerBehavior::radarStatisticsList($params); $statisticsCountQuery = clone($statisticsQuery); $statisticsCustomerCountQuery = clone($statisticsQuery); $count = $statisticsCountQuery->select('con_user_cus')->distinct()->count(); $customerCount = $statisticsCustomerCountQuery->selectRaw('count(distinct(`external_userid`)) as count') ->first()->count; $data = $statisticsQuery->selectRaw('max(id) as id, user_id, external_userid, con_user_cus') ->groupBy('con_user_cus') ->offset(($params['page'] - 1) * $params['page_size']) ->limit($params['page_size']) ->get(); $data = $data->toArray(); // 提取id,用来统计最近一次点击的时间以及渠道 // 提取客户external_userid ,用来批量查询客户名称备注等信息 // 提取客服user_id,用来查询客服信息 // 提取con_user_cus,用来批量计算客户访问总次数 $behaviorIdList = $externalUserList = $userList = $conusercusList = []; foreach ($data as $val) { $behaviorIdList[] = $val['id']; $externalUserList[] = $val['external_userid']; $userList[] = $val['user_id']; $conusercusList[] = $val['con_user_cus']; } $externalUserList = array_unique($externalUserList); $userList = array_unique($userList); $behaviorStatistics = RadarCustomerBehavior::query() ->whereIn('id', $behaviorIdList) ->get(); $customerInfoList = Customer::suffix($params['corpid']) ->where('corpid', $params['corpid']) ->whereIn('external_userid', $externalUserList) ->get(); $userInfoList = DjUser::query() ->where('corpid', $params['corpid']) ->whereIn('user_id', $userList) ->get(); $conusercusStatisticsQuery = RadarCustomerBehavior::radarStatisticsList(array_merge($params, ['con_user_cus_list' => $conusercusList])); $conusercusStatistics = $conusercusStatisticsQuery->selectRaw('con_user_cus, count(1) as pv_count') ->groupBy('con_user_cus') ->get(); $channelList = array_column(RadarCustomerBehavior::CHANNEL, null, 'key'); foreach ($data as &$value) { $behaviorInfo = $behaviorStatistics->where('id', $value['id'])->first(); $value['last_click_time'] = !empty($behaviorInfo->create_time) ? $behaviorInfo->create_time : null; $value['last_click_channel'] = !empty($channelList[$behaviorInfo->channel]['value']) ? $channelList[$behaviorInfo->channel]['value'] : null; $customerInfo = $customerInfoList->where('external_userid', $value['external_userid'])->first(); $value['customer_name'] = !empty($customerInfo->name) ? $customerInfo->name : null; $value['type'] = !empty($customerInfo->type) ? $customerInfo->type : null; $value['customer_id'] = !empty($customerInfo->id) ? $customerInfo->id : null; $userInfo = $userInfoList->where('user_id', $value['user_id'])->first(); $value['user_name'] = !empty($userInfo->name) ? $userInfo->name : null; $conusercusStatisticsInfo = $conusercusStatistics->where('con_user_cus', $value['con_user_cus'])->first(); $value['pv_count'] = !empty($conusercusStatisticsInfo->pv_count) ? $conusercusStatisticsInfo->pv_count : 0; } return [['list' => $data, 'customer_count' => $customerCount], $count]; } catch (\Exception $exception) { Log::logError('dataStatisticsList', [ 'params' => $params, 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return [['list' => [], 'customer_count' => 0], 0]; } } /** * 使用雷达id获取链接类型信息体 * */ public static function getLinkMsgOfRadar($radarId, $corpid, $userId, $channel, $ruleId, $state='STATE') { try { $params['rule_id'] = $ruleId; $params['corpid'] = $corpid; $params['user_id'] = $userId; $params['channel'] = $channel; $params['state'] = $state; $linkMsg = RadarService::getTraceLink($radarId, $params, $errno); if($errno) { EmailQueue::rPush('雷达信息转图文链接信息失败', Error::getError($errno), ['xiaohua.hou@kuxuan-inc.com', 'song.shen@kuxuan-inc.com'], '雷达信息转图文链接信息失败'); return []; } } catch (\Exception $e) { EmailQueue::rPush('雷达信息处理成链接类型消息体过程发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com', 'song.shen@kuxuan-inc.com'], '雷达信息处理成链接类型消息体过程发生异常'); Log::logError('雷达信息处理成链接类型消息体过程发生异常', [ 'line' => $e->getLine(), 'msg' => $e->getMessage() ], 'RadarAttachmentDealTrace'); return []; } return $linkMsg; } /** * 雷达信息转图文链接信息 * */ public static function getTraceLink($radarId, $extra, &$errno) { # 校验参数信息 if(!isset($extra['corpid']) || !isset($extra['user_id'])) { $errno = 2803; return []; } # 获取雷达对应的链接消息四要素(标题、封面图、描述和链接) $radarInfo = RadarCustomerDetail::select(['title', 'description', 'cover_id']) ->where('id', $radarId)->where('corpid', $extra['corpid']) ->where('enable', 1)->first(); if(empty($radarInfo)) { Log::logError('未查询到指定雷达信息', [ 'radar_id' => $radarId, 'extra' => $extra ], 'RadarTraceLinkGet'); $errno = 2802; return []; } # 智能雷达目标链接 $extra['radar_id'] = $radarId; $extra['timestamp'] = time(); $extra['token'] = get_token($extra['corpid'], $extra['user_id'], $extra['timestamp'], $radarId); # 用户打开雷达后首先跳转的h5链接地址 $traceUri = env('DOMAIN', 'http://dj.wenxingshuju.com/') . 'playlet/qwh5/dist/index.html#/radar'; $traceUri .= '?' .http_build_query($extra); $auth2Url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $extra['corpid'] .'&redirect_uri='. urlencode($traceUri) .'&response_type=code&scope=snsapi_base&state='.$extra['state'].'#wechat_redirect'; # 雷达封面图 $picUrl = Material::where('id', $radarInfo->cover_id)->value('oss_url'); return [ 'title' => $radarInfo->title, 'picurl' => $picUrl, 'desc' => $radarInfo->description, 'url' => $auth2Url ]; } /** * 根据雷达id获取雷达内容 * */ public static function getRadarContent($corpid, $radarId) { $radarInfo = RadarCustomerDetail::select(['title', 'description', 'cover_id', 'type', 'url_list', 'name']) ->where('id', $radarId)->where('corpid', $corpid) ->where('enable', 1)->first(); if(empty($radarInfo)) { return []; } # 雷达封面图 $picUrl = Material::where('id', $radarInfo->cover_id)->value('oss_url'); return [ 'radar_id' => $radarId, 'title' => $radarInfo->title ?? '', 'name' => $radarInfo->name ?? '', 'description' => $radarInfo->description ?? '', 'pic_url' => $picUrl, 'type' => $radarInfo->type, 'url_list' => json_decode($radarInfo->url_list, true) ]; } /** * 获取H5页面需要展示的雷达内容 * @param $corpid string 企微id * @param $radarId int 雷达id * @param $userId string 客服id * @param $token string 令牌 * @param $timestamp int 时间戳 * */ public static function radarDetailForH5($corpid, $radarId, $userId, $token, $timestamp, &$errno) { # 验签 $authToken = get_token($corpid, $userId, $timestamp, $radarId); if($authToken != $token) { $errno = 1004; return []; } # 获取雷达需要展示的内容类型及内容 $radarDetail = RadarCustomerDetail::select(['type', 'url_list'])->where('id', $radarId) ->where('corpid', $corpid) ->where('enable', 1) ->first(); return $radarDetail; } /** * 雷达用户行为数据上报 * @param $param array 待上报数据 * @param $type integer * @param $token string 签名 * @param int $retry 重试次数 * @return int * */ public static function behaviorReport($param, $type, $token, $timestamp, $retry=0) { try { if(!$retry) { # 验签 $authToken = get_token($param['corpid'], $param['user_id'], $timestamp, $param['radar_id']); if($authToken != $token) { Log::logError('非法数据上报', [ 'params' => $param, 'token' => $token, 'timestamp' => $timestamp ]); return 1004; } } if($param['external_userid']) $param['con_user_cus'] = $param['user_id'] . $param['external_userid']; if(1 == $type) { # 行为数据存储 $result = RadarCustomerBehavior::insert($param); if(!$result) { Log::logError('行为数据上报失败', [ 'param' => $param, 'retry' => $retry ], 'BehaviorReport'); return 2804; } # 处理触发雷达后续操作 if($param['external_userid']) RedisModel::lPush(RadarCustomerDetail::RADAR_CUSTOMER_FOLLOW_UP, json_encode($param)); } else { $info = RadarCustomerBehavior::query() ->where('request_id', $param['request_id']) ->where('radar_id', $param['radar_id']) ->first(); if(empty($info)) { // 第一次的上报数据没有找到 $param['close_time'] = date('Y-m-d H:i:s'); # 行为数据存储 $result = RadarCustomerBehavior::insert($param); if(!$result) { Log::logError('行为数据上报失败', [ 'param' => $param, 'retry' => $retry ], 'BehaviorReport'); return 2804; } # 处理触发雷达后续操作 if($param['external_userid']) RedisModel::lPush(RadarCustomerDetail::RADAR_CUSTOMER_FOLLOW_UP, json_encode($param)); } else { // 正常记录第二次上报 $update['close_time'] = date('Y-m-d H:i:s'); $update['view_time'] = time() - strtotime($info->create_time); # 行为数据存储 $result = RadarCustomerBehavior::query() ->where('id', $info->id) ->update($update); if(!$result) { Log::logError('行为数据上报失败', [ 'param' => $param, 'retry' => $retry ], 'BehaviorReport'); return 2804; } } } } catch (\Exception $e) { Log::logError('行为数据上报发生异常', [ 'param' => $param, 'line' => $e->getLine(), 'msg' => $e->getMessage() ], 'BehaviorReport-Exception'); return 2804; } return 0; } // 渠道列表 public static function channelList() { return RadarCustomerBehavior::CHANNEL; } // h5雷达列表 public static function radarListForH5($params) { try{ $query = RadarCustomerDetail::query() ->where('corpid', '=', $params['corpid']) ->where('enable', '=', 1); if(!empty($params['name'])) { $query->where('title', 'like', '%'.$params['name'].'%'); } if(isset($params['group_id'])) { if(0 == $params['group_id']) { $query->where('group_id', '=', ''); } else { $query->where('group_id', '=', $params['group_id']); } } $count = $query->count(); $data = $query->select(['title', 'description', 'cover_id']) ->selectRaw('id as radar_id') ->orderBy('sort_order', 'desc') ->offset(($params['page'] - 1) * $params['page_size']) ->limit($params['page_size']) ->get(); // 提取雷达封面素材id $coverIdList = array_column($data->toArray(), 'cover_id'); $coverData = Material::query() ->whereIn('id', $coverIdList) ->get(); foreach($data as &$value) { $coverInfo = $coverData->where('id', $value->cover_id)->first(); $value->cover_url = !empty($coverInfo->oss_url) ? $coverInfo->oss_url : null; } return [$data, $count]; } catch (\Exception $exception) { Log::logError('radarListForH5', [ 'params' => $params, 'line' => $exception->getLine(), 'msg' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ], 'interface'); return [[], 0]; } } }