12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2022/6/15
- * Time: 15:13
- */
- namespace App\Service;
- use App\Error;
- use App\Log;
- use App\Models\Customer;
- use App\Models\DjUser;
- use App\Models\Material;
- use App\Models\RadarCustomerBehavior;
- use App\Models\RadarCustomerDetail;
- use App\Models\RadarCustomerGroup;
- use App\Models\Tag;
- use App\RedisModel;
- use App\Support\EmailQueue;
- use App\User;
- class RadarService
- {
- public static function radarGroupCreate($params)
- {
- try{
- // 验证雷达组名称是否重复
- $info = RadarCustomerGroup::getGroupInfoByName($params['corpid'], $params['sys_group_id'], $params['group_name']);
- if(!empty($info)) {
- Log::logError('radarGroupCreate', [
- 'params' => $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];
- }
- }
- }
|