企微短剧业务系统

RadarService.php 48KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/6/15
  6. * Time: 15:13
  7. */
  8. namespace App\Service;
  9. use App\Error;
  10. use App\Log;
  11. use App\Models\Customer;
  12. use App\Models\DjUser;
  13. use App\Models\Material;
  14. use App\Models\RadarCustomerBehavior;
  15. use App\Models\RadarCustomerDetail;
  16. use App\Models\RadarCustomerGroup;
  17. use App\Models\Tag;
  18. use App\RedisModel;
  19. use App\Support\EmailQueue;
  20. use App\User;
  21. class RadarService
  22. {
  23. public static function radarGroupCreate($params)
  24. {
  25. try{
  26. // 验证雷达组名称是否重复
  27. $info = RadarCustomerGroup::getGroupInfoByName($params['corpid'], $params['sys_group_id'], $params['group_name']);
  28. if(!empty($info)) {
  29. Log::logError('radarGroupCreate', [
  30. 'params' => $params,
  31. 'message' => '雷达组名称已使用',
  32. ],'interface');
  33. return ['雷达组名称已使用', 3400];
  34. }
  35. // 获取雷达组最大排序值
  36. $maxSortOrder = RadarCustomerGroup::getMaxSortOrder($params['corpid'], $params['sys_group_id']);
  37. $res = RadarCustomerGroup::query()
  38. ->insert([
  39. 'sys_group_id' => $params['sys_group_id'],
  40. 'corpid' => $params['corpid'],
  41. 'group_name' => $params['group_name'],
  42. 'creator_id' => \Auth::id(),
  43. 'sort_order' => $maxSortOrder + 1,
  44. ]);
  45. if($res) {
  46. return ['添加成功', 0];
  47. } else {
  48. Log::logError('radarGroupCreate', [
  49. 'params' => $params,
  50. 'message' => '添加失败',
  51. 'res' => $res,
  52. ],'interface');
  53. return ['添加失败', 400];
  54. }
  55. } catch (\Exception $exception) {
  56. Log::logError('radarGroupCreate', [
  57. 'params' => $params,
  58. 'file' => $exception->getFile(),
  59. 'line' => $exception->getLine(),
  60. 'message' => $exception->getMessage(),
  61. 'trace' => $exception->getTraceAsString(),
  62. ], 'interface');
  63. return ['添加失败', 400];
  64. }
  65. }
  66. // 雷达组列表
  67. public static function radarGroupList($params)
  68. {
  69. try{
  70. $query = RadarCustomerGroup::query()
  71. ->where('corpid', $params['corpid'])
  72. ->where('enable', 1);
  73. if(!empty($params['sys_group_id'])) {
  74. $query->where('sys_group_id', $params['sys_group_id']);
  75. }
  76. if(!empty($params['keyword'])) {
  77. $query->where('group_name', 'like', '%'.$params['keyword'].'%');
  78. }
  79. $count = $query->count();
  80. $list = $query->select(['group_name', 'sort_order'])
  81. ->selectRaw('id as group_id')
  82. ->orderBy('sort_order', 'desc')
  83. ->offset(($params['page']-1) * $params['page_size'])
  84. ->limit($params['page_size'])
  85. ->get();
  86. return [$list, $count];
  87. } catch (\Exception $exception) {
  88. Log::logError('radarGroupList', [
  89. 'params' => $params,
  90. 'file' => $exception->getFile(),
  91. 'line' => $exception->getLine(),
  92. 'message' => $exception->getMessage(),
  93. 'trace' => $exception->getTraceAsString(),
  94. ], 'interface');
  95. return [[], 0];
  96. }
  97. }
  98. public static function radarGroupUpdate($params)
  99. {
  100. try{
  101. // 验证雷达组名称是否重复
  102. $info = RadarCustomerGroup::getGroupInfoByName($params['corpid'], $params['sys_group_id'], $params['group_name']);
  103. if(isset($info->id) && $info->id != $params['group_id']) {
  104. Log::logError('radarGroupUpdate', [
  105. 'params' => $params,
  106. 'message' => '雷达组名称已使用',
  107. ],'interface');
  108. return ['雷达组名称已使用', 3400];
  109. }
  110. $radarGroupInfo = RadarCustomerGroup::query()
  111. ->where('id', $params['group_id'])
  112. ->first();
  113. if($params['group_name'] == $radarGroupInfo->group_name) {
  114. return ['修改成功', 0];
  115. }
  116. $res = RadarCustomerGroup::query()
  117. ->where('id', $params['group_id'])
  118. ->update([
  119. 'group_name' => $params['group_name']
  120. ]);
  121. if($res) {
  122. return ['修改成功', 0];
  123. } else {
  124. Log::logError('radarGroupUpdate', [
  125. 'params' => $params,
  126. 'message' => '修改失败',
  127. 'res' => $res,
  128. ],'interface');
  129. return ['修改失败', 400];
  130. }
  131. } catch (\Exception $exception) {
  132. Log::logError('radarGroupUpdate', [
  133. 'params' => $params,
  134. 'file' => $exception->getFile(),
  135. 'line' => $exception->getLine(),
  136. 'message' => $exception->getMessage(),
  137. 'trace' => $exception->getTraceAsString(),
  138. ], 'interface');
  139. return ['修改失败', 400];
  140. }
  141. }
  142. public static function radarGroupDelete($params)
  143. {
  144. try{
  145. \DB::begintransaction();
  146. $groupRes = RadarCustomerGroup::query()
  147. ->where('id', $params['group_id'])
  148. ->update([
  149. 'enable' => 0
  150. ]);
  151. // 防止出现本来雷达组下就没有雷达,更新不到数据导致整体删除失败
  152. $detailCount = RadarCustomerDetail::query()
  153. ->where('group_id', $params['group_id'])
  154. ->where('enable', 1)
  155. ->count();
  156. if($detailCount > 0) {
  157. if(1 == $params['type']) {
  158. $detailRes = RadarCustomerDetail::query()
  159. ->where('group_id', $params['group_id'])
  160. ->where('enable', 1)
  161. ->update(['group_id' => 0]);
  162. } else {
  163. $detailRes = RadarCustomerDetail::query()
  164. ->where('group_id', $params['group_id'])
  165. ->where('enable', 1)
  166. ->update(['enable' => 0]);
  167. }
  168. } else {
  169. $detailRes = true;
  170. }
  171. if($groupRes && $detailRes) {
  172. \DB::commit();
  173. return ['删除成功', 0];
  174. } else {
  175. \DB::rollBack();
  176. Log::logError('radarGroupDelete', [
  177. 'params' => $params,
  178. 'message' => '修改失败',
  179. 'group_res' => $groupRes,
  180. 'detail_res' => $detailRes,
  181. ],'interface');
  182. return ['删除失败', 400];
  183. }
  184. } catch (\Exception $exception) {
  185. Log::logError('radarGroupDelete', [
  186. 'params' => $params,
  187. 'file' => $exception->getFile(),
  188. 'line' => $exception->getLine(),
  189. 'message' => $exception->getMessage(),
  190. 'trace' => $exception->getTraceAsString(),
  191. ], 'interface');
  192. return ['删除失败', 400];
  193. }
  194. }
  195. public static function radarGroupSortUpdate($params)
  196. {
  197. try{
  198. // 查询被操作分组原始排序值
  199. $operatorGroup = RadarCustomerGroup::query()
  200. ->where('id', $params['operate_group_id'])
  201. ->first();
  202. $operatorGroupOriginalSortOrder = $operatorGroup->sort_order;
  203. if(!empty($params['behind_group_id']) && empty($params['front_group_id'])) {
  204. // 上移到第一位,判断是否有调整后的排在调整分组后面的分组id
  205. $behindGroup = RadarCustomerGroup::query()
  206. ->where('id', $params['behind_group_id'])
  207. ->first();
  208. $behindGroupOriginalSortOrder = $behindGroup->sort_order;
  209. list($data, $code) = self::radarGroupMoveUp($params, $behindGroupOriginalSortOrder, $operatorGroupOriginalSortOrder);
  210. return [$data, $code];
  211. } else if(!empty($params['front_group_id']) && empty($params['behind_group_id'])) {
  212. // 下移到最后一位,判断是否有调整后的排在调整分组前面的分组id
  213. $frontGroup = RadarCustomerGroup::query()
  214. ->where('id', $params['front_group_id'])
  215. ->first();
  216. $frontGroupOriginalSortOrder = $frontGroup->sort_order;
  217. list($data, $code) = self::radarGroupMoveDown($params, $frontGroupOriginalSortOrder, $operatorGroupOriginalSortOrder);
  218. return [$data, $code];
  219. } else if(!empty($params['front_group_id']) && !empty($params['behind_group_id'])) {
  220. $frontGroup = RadarCustomerGroup::query()
  221. ->where('id', $params['front_group_id'])
  222. ->first();
  223. $frontGroupOriginalSortOrder = $frontGroup->sort_order;
  224. $behindGroup = RadarCustomerGroup::query()
  225. ->where('id', $params['behind_group_id'])
  226. ->first();
  227. $behindGroupOriginalSortOrder = $behindGroup->sort_order;
  228. if($operatorGroupOriginalSortOrder > $frontGroupOriginalSortOrder) {
  229. // 下移
  230. list($data, $code) = self::radarGroupMoveDown($params, $frontGroupOriginalSortOrder, $operatorGroupOriginalSortOrder);
  231. } else {
  232. // 上移
  233. list($data, $code) = self::radarGroupMoveUp($params, $behindGroupOriginalSortOrder, $operatorGroupOriginalSortOrder);
  234. }
  235. return [$data, $code];
  236. } else {
  237. Log::logError('radarGroupSortUpdate', [
  238. 'params' => $params,
  239. 'message' => '被操作雷达组前后分组id不可同时为空',
  240. ],'interface');
  241. return ['被操作雷达组前后分组id不可同时为空', 3405];
  242. }
  243. } catch (\Exception $exception) {
  244. Log::logError('radarGroupSortUpdate', [
  245. 'params' => $params,
  246. 'file' => $exception->getFile(),
  247. 'line' => $exception->getLine(),
  248. 'message' => $exception->getMessage(),
  249. 'trace' => $exception->getTraceAsString(),
  250. ], 'interface');
  251. return ['移动失败', 400];
  252. }
  253. }
  254. // 上移
  255. public static function radarGroupMoveUp($params, $behindGroupOriginalSortOrder, $operatorGroupOriginalSortOrder)
  256. {
  257. \DB::begintransaction();
  258. $moveRes = RadarCustomerGroup::query()
  259. ->where('corpid', $params['corpid'])
  260. ->where('sys_group_id', $params['sys_group_id'])
  261. ->where('enable', 1)
  262. ->where('sort_order', '<=', $behindGroupOriginalSortOrder)
  263. ->where('sort_order', '>', $operatorGroupOriginalSortOrder)
  264. ->decrement('sort_order');// 自减1
  265. $operatorRes = RadarCustomerGroup::query()
  266. ->where('id', $params['operate_group_id'])
  267. ->update(['sort_order' => $behindGroupOriginalSortOrder]);
  268. if($moveRes && $operatorRes) {
  269. \DB::commit();
  270. return ['移动成功', 0];
  271. } else {
  272. \DB::rollBack();
  273. Log::logError('radarGroupSortUpdate', [
  274. 'params' => $params,
  275. 'message' => '移动失败',
  276. 'move_res' => $moveRes,
  277. 'operator_res' => $operatorRes,
  278. 'operator_sort_order' => $operatorGroupOriginalSortOrder,
  279. 'behind_sort_order' => $behindGroupOriginalSortOrder,
  280. ],'interface');
  281. return ['移动失败', 400];
  282. }
  283. }
  284. // 下移
  285. public static function radarGroupMoveDown($params, $frontGroupOriginalSortOrder, $operatorGroupOriginalSortOrder)
  286. {
  287. \DB::begintransaction();
  288. $moveRes = RadarCustomerGroup::query()
  289. ->where('corpid', $params['corpid'])
  290. ->where('sys_group_id', $params['sys_group_id'])
  291. ->where('enable', 1)
  292. ->where('sort_order', '>=', $frontGroupOriginalSortOrder)
  293. ->where('sort_order', '<', $operatorGroupOriginalSortOrder)
  294. ->increment('sort_order');// 自增1
  295. $operatorRes = RadarCustomerGroup::query()
  296. ->where('id', $params['operate_group_id'])
  297. ->update(['sort_order' => $frontGroupOriginalSortOrder]);
  298. if($moveRes && $operatorRes) {
  299. \DB::commit();
  300. return ['移动成功', 0];
  301. } else {
  302. \DB::rollBack();
  303. Log::logError('radarGroupSortUpdate', [
  304. 'params' => $params,
  305. 'message' => '移动失败',
  306. 'move_res' => $moveRes,
  307. 'operator_res' => $operatorRes,
  308. 'operator_sort_order' => $operatorGroupOriginalSortOrder,
  309. 'front_sort_order' => $frontGroupOriginalSortOrder,
  310. ],'interface');
  311. return ['移动失败', 400];
  312. }
  313. }
  314. public static function radarCreate($params)
  315. {
  316. try{
  317. // 查询目前该企业排序值最大的排序值
  318. $maxSortOrder = RadarCustomerDetail::getMaxSortOrder($params['corpid'], $params['sys_group_id']);
  319. $params['creator_id'] = \Auth::id();
  320. $params['sort_order'] = $maxSortOrder+1;
  321. $params['url_list'] = json_encode($params['url_list']);
  322. if(1 == $params['customer_label']) {
  323. $params['tag_list'] = json_encode($params['tag_list']);
  324. } else {
  325. $params['tag_list'] = json_encode([]);
  326. }
  327. $params['group_id'] = empty($params['group_id']) ? '' : $params['group_id'];
  328. $res = RadarCustomerDetail::query()
  329. ->insert($params);
  330. if($res) {
  331. return ['创建成功', 0];
  332. } else {
  333. Log::logError('radarCreate', [
  334. 'params' => $params,
  335. 'message' => '创建失败',
  336. 'res' => $res,
  337. ],'interface');
  338. return ['创建失败', 400];
  339. }
  340. } catch (\Exception $exception) {
  341. Log::logError('radarCreate', [
  342. 'params' => $params,
  343. 'file' => $exception->getFile(),
  344. 'line' => $exception->getLine(),
  345. 'message' => $exception->getMessage(),
  346. 'trace' => $exception->getTraceAsString(),
  347. ], 'interface');
  348. return ['创建失败', 400];
  349. }
  350. }
  351. public static function radarDetail($params)
  352. {
  353. try{
  354. $detail = RadarCustomerDetail::query()
  355. ->select(['group_id', 'name', 'type', 'url_list', 'title', 'description', 'cover_id', 'behavior_notification',
  356. 'dynamic_notification', 'customer_label', 'tag_list', 'corpid'])
  357. ->selectRaw('id as radar_id')
  358. ->where('id', $params['radar_id'])
  359. ->where('enable', 1)
  360. ->first();
  361. if(empty($detail)) {
  362. return [];
  363. } else {
  364. // 通过封面素材id查询对应的文件链接
  365. $materialInfo = Material::query()
  366. ->where('id', $detail->cover_id)
  367. ->first();
  368. $detail->cover_url = !empty($materialInfo->oss_url) ? $materialInfo->oss_url : null;
  369. $detail->tag_list = is_array(json_decode($detail->tag_list, 1)) ? json_decode($detail->tag_list, 1) : [];
  370. $detail->url_list = json_decode($detail->url_list, 1);
  371. $detail->group_id = empty($detail->group_id) ? null : $detail->group_id;
  372. return $detail->toArray();
  373. }
  374. } catch (\Exception $exception) {
  375. Log::logError('radarDetail', [
  376. 'params' => $params,
  377. 'file' => $exception->getFile(),
  378. 'line' => $exception->getLine(),
  379. 'message' => $exception->getMessage(),
  380. 'trace' => $exception->getTraceAsString(),
  381. ], 'interface');
  382. return [];
  383. }
  384. }
  385. public static function radarUpdate($params)
  386. {
  387. try{
  388. $info = RadarCustomerDetail::query()
  389. ->where('id', $params['radar_id'])
  390. ->where('enable', 1)
  391. ->first();
  392. if(empty($info)) {
  393. return ['雷达信息查询异常', ];
  394. }
  395. $updateData = [];
  396. if($params['group_id'] && strval($params['group_id']) != $info->group_id) {
  397. $updateData['group_id'] = empty($params['group_id']) ? '' : $params['group_id'];
  398. }
  399. if($params['name'] && $params['name'] != $info->name) {
  400. $updateData['name'] = $params['name'];
  401. }
  402. if($params['url_list'] && json_encode($params['url_list']) != $info->url_list) {
  403. $updateData['url_list'] = json_encode($params['url_list']);
  404. }
  405. if($params['title'] && $params['title'] != $info->title) {
  406. $updateData['title'] = $params['title'];
  407. }
  408. if($params['description'] && $params['description'] != $info->description) {
  409. $updateData['description'] = $params['description'];
  410. }
  411. if($params['cover_id'] && $params['cover_id'] != $info->cover_id) {
  412. $updateData['cover_id'] = $params['cover_id'];
  413. }
  414. if(isset($params['behavior_notification']) && $params['behavior_notification'] != $info->behavior_notification) {
  415. $updateData['behavior_notification'] = $params['behavior_notification'];
  416. }
  417. if(isset($params['dynamic_notification']) && $params['dynamic_notification'] != $info->dynamic_notification) {
  418. $updateData['dynamic_notification'] = $params['dynamic_notification'];
  419. }
  420. if(isset($params['customer_label']) && $params['customer_label'] != $info->customer_label) {
  421. $updateData['customer_label'] = $params['customer_label'];
  422. }
  423. if(1 == $params['customer_label']) {
  424. if(isset($params['tag_list']) && json_encode($params['tag_list']) != $info->tag_list) {
  425. $updateData['tag_list'] = json_encode($params['tag_list']);
  426. }
  427. } else {
  428. if(json_encode([]) != $info->tag_list) {
  429. $updateData['tag_list'] = json_encode([]);
  430. }
  431. }
  432. if(empty($updateData)) {
  433. return ['编辑成功', 0];
  434. }
  435. $res = RadarCustomerDetail::query()
  436. ->where('id', $params['radar_id'])
  437. ->update($updateData);
  438. if($res) {
  439. return ['编辑成功', 0];
  440. } else {
  441. Log::logError('radarUpdate', [
  442. 'params' => $params,
  443. 'message' => '编辑失败',
  444. 'res' => $res,
  445. ],'interface');
  446. return ['编辑失败', 400];
  447. }
  448. } catch (\Exception $exception) {
  449. Log::logError('radarUpdate', [
  450. 'params' => $params,
  451. 'file' => $exception->getFile(),
  452. 'line' => $exception->getLine(),
  453. 'message' => $exception->getMessage(),
  454. 'trace' => $exception->getTraceAsString(),
  455. ], 'interface');
  456. return ['编辑失败', 400];
  457. }
  458. }
  459. public static function radarDelete($params)
  460. {
  461. try{
  462. $res = RadarCustomerDetail::query()
  463. ->where('id', $params['radar_id'])
  464. ->update(['enable' => 0]);
  465. if($res) {
  466. return ['删除成功', 0];
  467. } else {
  468. Log::logError('radarDelete', [
  469. 'params' => $params,
  470. 'message' => '删除失败',
  471. 'res' => $res,
  472. ],'interface');
  473. return ['删除失败', 400];
  474. }
  475. } catch (\Exception $exception) {
  476. Log::logError('radarDelete', [
  477. 'params' => $params,
  478. 'file' => $exception->getFile(),
  479. 'line' => $exception->getLine(),
  480. 'message' => $exception->getMessage(),
  481. 'trace' => $exception->getTraceAsString(),
  482. ], 'interface');
  483. return ['删除失败', 400];
  484. }
  485. }
  486. // 雷达列表
  487. public static function radarList($params)
  488. {
  489. try{
  490. $query = RadarCustomerDetail::query()
  491. ->where('corpid', $params['corpid'])
  492. ->where('enable', 1);
  493. if(!empty($params['group_id'])) {
  494. $query->where('group_id', $params['group_id']);
  495. }
  496. if(!empty($params['sys_group_id'])) {
  497. $query->where('sys_group_id', $params['sys_group_id']);
  498. }
  499. if(!empty($params['name'])) {
  500. $query->where('name', 'like', '%'.$params['name'].'%');
  501. }
  502. if(isset($params['group_id']) && 0 == $params['group_id']) {
  503. $query->whereRaw('group_id is null');
  504. }
  505. $count = $query->count();
  506. $data = $query->select(['name', 'type', 'tag_list', 'create_time', 'group_id', 'customer_label', 'description', 'cover_id', 'url_list', 'title'])
  507. ->selectRaw('id as radar_id')
  508. ->orderBy('sort_order', 'desc')
  509. ->offset(($params['page'] - 1) * $params['page_size'])
  510. ->limit($params['page_size'])
  511. ->get();
  512. // 提取雷达id,雷达分组id,雷达客户标签
  513. $radarIdList = $radarGroupIdList = $tagList = [];
  514. foreach($data as $val) {
  515. if(!empty($val->group_id)) {
  516. $radarGroupIdList[] = $val->group_id;
  517. }
  518. $radarIdList[] = $val->radar_id;
  519. if(!empty($val->tag_list)) {
  520. $radarTagList = json_decode($val->tag_list, 1);
  521. $tagList = array_merge($tagList, $radarTagList);
  522. }
  523. # 处理缩略图
  524. $val->pic_url = Material::where('id', $val->cover_id)->value('oss_url');
  525. }
  526. $tagList = array_unique($tagList);
  527. $radarGroupIdList = array_unique($radarGroupIdList);
  528. // 批量查询分组
  529. if(!empty($radarGroupIdList)) {
  530. $radarGroupList = RadarCustomerGroup::query()
  531. ->whereIn('id', $radarGroupIdList)
  532. ->where('enable', 1)
  533. ->get();
  534. } else {
  535. $radarGroupList = null;
  536. }
  537. // 批量查询客户标签
  538. if(!empty($tagList)) {
  539. $tagDataList = Tag::query()
  540. ->where('corpid', $params['corpid'])
  541. ->whereIn('tag_md5', $tagList)
  542. ->where('enable', 1)
  543. ->get();
  544. } else {
  545. $tagDataList = null;
  546. }
  547. // 批量查询雷达统计信息
  548. $radarStatistics = RadarCustomerBehavior::radarStatistics($radarIdList);
  549. foreach($data as &$value) {
  550. if(1 == $value->customer_label && !empty($value->tag_list)) {
  551. $radarTagList = json_decode($value->tag_list, 1);
  552. if(is_array($radarTagList) && !empty($radarTagList)) {
  553. $value->tag_list = $tagDataList->whereIn('tag_md5', $radarTagList)->pluck('tag_name')->toArray();
  554. } else {
  555. $value->tag_list = [];
  556. }
  557. } else {
  558. $value->tag_list = [];
  559. }
  560. if(!empty($value->group_id)) {
  561. $group_name = $radarGroupList->where('id', $value->group_id)->first();
  562. $value->group_name = !empty($group_name->group_name) ? $group_name->group_name : null;
  563. } else {
  564. $value->group_name = null;
  565. }
  566. $statistics = $radarStatistics->where('radar_id', $value->radar_id)->first();
  567. $value->uv_count = !empty($statistics->uv_count) ? $statistics->uv_count : 0;
  568. $value->send_count = !empty($statistics->send_count) ? $statistics->send_count : 0;
  569. }
  570. return [$data, $count];
  571. } catch (\Exception $exception) {
  572. Log::logError('radarList', [
  573. 'params' => $params,
  574. 'file' => $exception->getFile(),
  575. 'line' => $exception->getLine(),
  576. 'message' => $exception->getMessage(),
  577. 'trace' => $exception->getTraceAsString(),
  578. ], 'interface');
  579. return [[], 0];
  580. }
  581. }
  582. public static function radarSortUpdate($params)
  583. {
  584. try{
  585. // 查询被操作雷达原始排序值
  586. $operatorRadar = RadarCustomerDetail::query()
  587. ->where('id', $params['operate_radar_id'])
  588. ->first();
  589. $operatorRadarOriginalSortOrder = $operatorRadar->sort_order;
  590. if(!empty($params['behind_radar_id']) && empty($params['front_radar_id'])) {
  591. // 上移到第一个,判断是否有调整后的排在调整雷达后面的分组id
  592. $behindRadar = RadarCustomerDetail::query()
  593. ->where('id', $params['behind_radar_id'])
  594. ->first();
  595. $behindRadarOriginalSortOrder = $behindRadar->sort_order;
  596. list($data, $code) = self::radarMoveUp($params, $behindRadarOriginalSortOrder, $operatorRadarOriginalSortOrder);
  597. return [$data, $code];
  598. } else if(!empty($params['front_radar_id']) && empty($params['behind_radar_id'])) {
  599. // 下移到最后一个,判断是否有调整后的排在调整分组前面的分组id
  600. $frontRadar = RadarCustomerDetail::query()
  601. ->where('id', $params['front_radar_id'])
  602. ->first();
  603. $frontRadarOriginalSortOrder = $frontRadar->sort_order;
  604. list($data, $code) = self::radarMoveDown($params, $frontRadarOriginalSortOrder, $operatorRadarOriginalSortOrder);
  605. return [$data, $code];
  606. } else if (!empty($params['behind_radar_id']) && !empty($params['front_radar_id'])) {
  607. // 中间移动,首先判断上移还是下移
  608. $frontRadar = RadarCustomerDetail::query()
  609. ->where('id', $params['front_radar_id'])
  610. ->first();
  611. $frontRadarOriginalSortOrder = $frontRadar->sort_order;
  612. if($operatorRadarOriginalSortOrder > $frontRadarOriginalSortOrder) {
  613. // 下移
  614. list($data, $code) = self::radarMoveDown($params, $frontRadarOriginalSortOrder, $operatorRadarOriginalSortOrder);
  615. } else {
  616. // 上移
  617. $behindRadar = RadarCustomerDetail::query()
  618. ->where('id', $params['behind_radar_id'])
  619. ->first();
  620. $behindRadarOriginalSortOrder = $behindRadar->sort_order;
  621. list($data, $code) = self::radarMoveUp($params, $behindRadarOriginalSortOrder, $operatorRadarOriginalSortOrder);
  622. }
  623. return [$data, $code];
  624. } else {
  625. Log::logError('radarSortUpdate', [
  626. 'params' => $params,
  627. 'message' => '被操作雷达前后雷达id不可同时为空',
  628. ],'interface');
  629. return ['被操作雷达前后雷达id不可同时为空', 3405];
  630. }
  631. } catch (\Exception $exception) {
  632. Log::logError('radarSortUpdate', [
  633. 'params' => $params,
  634. 'file' => $exception->getFile(),
  635. 'line' => $exception->getLine(),
  636. 'message' => $exception->getMessage(),
  637. 'trace' => $exception->getTraceAsString(),
  638. ], 'interface');
  639. return ['移动失败', 400];
  640. }
  641. }
  642. // 上移
  643. public static function radarMoveUp($params, $behindRadarOriginalSortOrder, $operatorRadarOriginalSortOrder)
  644. {
  645. \DB::begintransaction();
  646. $moveRes = RadarCustomerDetail::query()
  647. ->where('corpid', $params['corpid'])
  648. ->where('sys_group_id', $params['sys_group_id'])
  649. ->where('enable', 1)
  650. ->where('sort_order', '<=', $behindRadarOriginalSortOrder)
  651. ->where('sort_order', '>', $operatorRadarOriginalSortOrder)
  652. ->decrement('sort_order');// 自减1
  653. $operatorRes = RadarCustomerDetail::query()
  654. ->where('id', $params['operate_radar_id'])
  655. ->update(['sort_order' => $behindRadarOriginalSortOrder]);
  656. if($moveRes && $operatorRes) {
  657. \DB::commit();
  658. return ['移动成功', 0];
  659. } else {
  660. \DB::rollBack();
  661. Log::logError('radarSortUpdate', [
  662. 'params' => $params,
  663. 'message' => '移动失败',
  664. 'move_res' => $moveRes,
  665. 'operator_res' => $operatorRes,
  666. 'operator_sort_order' => $operatorRadarOriginalSortOrder,
  667. 'behind_sort_order' => $behindRadarOriginalSortOrder,
  668. ],'interface');
  669. return ['移动失败', 400];
  670. }
  671. }
  672. // 下移
  673. public static function radarMoveDown($params, $frontRadarOriginalSortOrder, $operatorRadarOriginalSortOrder)
  674. {
  675. \DB::begintransaction();
  676. $moveRes = RadarCustomerDetail::query()
  677. ->where('corpid', $params['corpid'])
  678. ->where('sys_group_id', $params['sys_group_id'])
  679. ->where('enable', 1)
  680. ->where('sort_order', '>=', $frontRadarOriginalSortOrder)
  681. ->where('sort_order', '<', $operatorRadarOriginalSortOrder)
  682. ->increment('sort_order');// 自增1
  683. $operatorRes = RadarCustomerDetail::query()
  684. ->where('id', $params['operate_radar_id'])
  685. ->update(['sort_order' => $frontRadarOriginalSortOrder]);
  686. if($moveRes && $operatorRes) {
  687. \DB::commit();
  688. return ['移动成功', 0];
  689. } else {
  690. \DB::rollBack();
  691. Log::logError('radarSortUpdate', [
  692. 'params' => $params,
  693. 'message' => '修改失败',
  694. 'move_res' => $moveRes,
  695. 'operator_res' => $operatorRes,
  696. 'operator_sort_order' => $operatorRadarOriginalSortOrder,
  697. 'front_sort_order' => $frontRadarOriginalSortOrder,
  698. ],'interface');
  699. return ['移动失败', 400];
  700. }
  701. }
  702. public static function dataStatisticsTotal($params)
  703. {
  704. try{
  705. // 查询总访问人数,总访问次数
  706. $totalData = RadarCustomerBehavior::radarTotal($params['radar_id'], null, null);
  707. // 查询今日访问人数,今日访问次数
  708. $todayData = RadarCustomerBehavior::radarTotal($params['radar_id'], date('Y-m-d'), null);
  709. $data['total_pv_count'] = !empty($totalData->pv_count) ? $totalData->pv_count : 0;
  710. $data['total_uv_count'] = !empty($totalData->uv_count) ? $totalData->uv_count : 0;
  711. $data['today_pv_count'] = !empty($todayData->pv_count) ? $todayData->pv_count : 0;
  712. $data['today_uv_count'] = !empty($todayData->uv_count) ? $todayData->uv_count : 0;
  713. return $data;
  714. } catch (\Exception $exception) {
  715. Log::logError('dataStatisticsTotal', [
  716. 'params' => $params,
  717. 'file' => $exception->getFile(),
  718. 'line' => $exception->getLine(),
  719. 'message' => $exception->getMessage(),
  720. 'trace' => $exception->getTraceAsString(),
  721. ], 'interface');
  722. return [
  723. 'total_pv_count' => 0,
  724. 'total_uv_count' => 0,
  725. 'today_pv_count' => 0,
  726. 'today_uv_count' => 0,
  727. ];
  728. }
  729. }
  730. // 数据分析
  731. public static function dataStatisticsList($params)
  732. {
  733. try {
  734. if (!empty($params['customer_name'])) {
  735. $externalUseridList = Customer::suffix($params['corpid'])
  736. ->where('corpid', $params['corpid'])
  737. ->where('name', 'like', '%' . $params['customer_name'] . '%')
  738. ->pluck('external_userid')
  739. ->toArray();
  740. if (empty($externalUseridList)) {
  741. Log::logError('dataStatisticsList', [
  742. 'params' => $params,
  743. 'message' => '根据客户名称查询结果为空',
  744. ], 'interface');
  745. return [['list' => [], 'customer_count' => 0], 0];
  746. }
  747. $params['external_userid_list'] = $externalUseridList;
  748. }
  749. $statisticsQuery = RadarCustomerBehavior::radarStatisticsList($params);
  750. $statisticsCountQuery = clone($statisticsQuery);
  751. $statisticsCustomerCountQuery = clone($statisticsQuery);
  752. $count = $statisticsCountQuery->select('con_user_cus')->distinct()->count();
  753. $customerCount = $statisticsCustomerCountQuery->selectRaw('count(distinct(`external_userid`)) as count')
  754. ->first()->count;
  755. $data = $statisticsQuery->selectRaw('max(id) as id, user_id, external_userid, con_user_cus')
  756. ->groupBy('con_user_cus')
  757. ->offset(($params['page'] - 1) * $params['page_size'])
  758. ->limit($params['page_size'])
  759. ->get();
  760. $data = $data->toArray();
  761. // 提取id,用来统计最近一次点击的时间以及渠道
  762. // 提取客户external_userid ,用来批量查询客户名称备注等信息
  763. // 提取客服user_id,用来查询客服信息
  764. // 提取con_user_cus,用来批量计算客户访问总次数
  765. $behaviorIdList = $externalUserList = $userList = $conusercusList = [];
  766. foreach ($data as $val) {
  767. $behaviorIdList[] = $val['id'];
  768. $externalUserList[] = $val['external_userid'];
  769. $userList[] = $val['user_id'];
  770. $conusercusList[] = $val['con_user_cus'];
  771. }
  772. $externalUserList = array_unique($externalUserList);
  773. $userList = array_unique($userList);
  774. $behaviorStatistics = RadarCustomerBehavior::query()
  775. ->whereIn('id', $behaviorIdList)
  776. ->get();
  777. $customerInfoList = Customer::suffix($params['corpid'])
  778. ->where('corpid', $params['corpid'])
  779. ->whereIn('external_userid', $externalUserList)
  780. ->get();
  781. $userInfoList = DjUser::query()
  782. ->where('corpid', $params['corpid'])
  783. ->whereIn('user_id', $userList)
  784. ->get();
  785. $conusercusStatisticsQuery = RadarCustomerBehavior::radarStatisticsList(array_merge($params, ['con_user_cus_list' => $conusercusList]));
  786. $conusercusStatistics = $conusercusStatisticsQuery->selectRaw('con_user_cus, count(1) as pv_count')
  787. ->groupBy('con_user_cus')
  788. ->get();
  789. $channelList = array_column(RadarCustomerBehavior::CHANNEL, null, 'key');
  790. foreach ($data as &$value) {
  791. $behaviorInfo = $behaviorStatistics->where('id', $value['id'])->first();
  792. $value['last_click_time'] = !empty($behaviorInfo->create_time) ? $behaviorInfo->create_time : null;
  793. $value['last_click_channel'] = !empty($channelList[$behaviorInfo->channel]['value']) ? $channelList[$behaviorInfo->channel]['value'] : null;
  794. $customerInfo = $customerInfoList->where('external_userid', $value['external_userid'])->first();
  795. $value['customer_name'] = !empty($customerInfo->name) ? $customerInfo->name : null;
  796. $value['type'] = !empty($customerInfo->type) ? $customerInfo->type : null;
  797. $value['customer_id'] = !empty($customerInfo->id) ? $customerInfo->id : null;
  798. $userInfo = $userInfoList->where('user_id', $value['user_id'])->first();
  799. $value['user_name'] = !empty($userInfo->name) ? $userInfo->name : null;
  800. $conusercusStatisticsInfo = $conusercusStatistics->where('con_user_cus', $value['con_user_cus'])->first();
  801. $value['pv_count'] = !empty($conusercusStatisticsInfo->pv_count) ? $conusercusStatisticsInfo->pv_count : 0;
  802. }
  803. return [['list' => $data, 'customer_count' => $customerCount], $count];
  804. } catch (\Exception $exception) {
  805. Log::logError('dataStatisticsList', [
  806. 'params' => $params,
  807. 'file' => $exception->getFile(),
  808. 'line' => $exception->getLine(),
  809. 'message' => $exception->getMessage(),
  810. 'trace' => $exception->getTraceAsString(),
  811. ], 'interface');
  812. return [['list' => [], 'customer_count' => 0], 0];
  813. }
  814. }
  815. /**
  816. * 使用雷达id获取链接类型信息体
  817. * */
  818. public static function getLinkMsgOfRadar($radarId, $corpid, $userId, $channel, $ruleId, $state='STATE')
  819. {
  820. try {
  821. $params['rule_id'] = $ruleId;
  822. $params['corpid'] = $corpid;
  823. $params['user_id'] = $userId;
  824. $params['channel'] = $channel;
  825. $params['state'] = $state;
  826. $linkMsg = RadarService::getTraceLink($radarId, $params, $errno);
  827. if($errno) {
  828. EmailQueue::rPush('雷达信息转图文链接信息失败', Error::getError($errno), ['xiaohua.hou@kuxuan-inc.com', 'song.shen@kuxuan-inc.com'],
  829. '雷达信息转图文链接信息失败');
  830. return [];
  831. }
  832. } catch (\Exception $e) {
  833. EmailQueue::rPush('雷达信息处理成链接类型消息体过程发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com', 'song.shen@kuxuan-inc.com'],
  834. '雷达信息处理成链接类型消息体过程发生异常');
  835. Log::logError('雷达信息处理成链接类型消息体过程发生异常', [
  836. 'line' => $e->getLine(),
  837. 'msg' => $e->getMessage()
  838. ], 'RadarAttachmentDealTrace');
  839. return [];
  840. }
  841. return $linkMsg;
  842. }
  843. /**
  844. * 雷达信息转图文链接信息
  845. * */
  846. public static function getTraceLink($radarId, $extra, &$errno)
  847. {
  848. # 校验参数信息
  849. if(!isset($extra['corpid']) || !isset($extra['user_id'])) {
  850. $errno = 2803;
  851. return [];
  852. }
  853. # 获取雷达对应的链接消息四要素(标题、封面图、描述和链接)
  854. $radarInfo = RadarCustomerDetail::select(['title', 'description', 'cover_id'])
  855. ->where('id', $radarId)->where('corpid', $extra['corpid'])
  856. ->where('enable', 1)->first();
  857. if(empty($radarInfo)) {
  858. Log::logError('未查询到指定雷达信息', [
  859. 'radar_id' => $radarId,
  860. 'extra' => $extra
  861. ], 'RadarTraceLinkGet');
  862. $errno = 2802;
  863. return [];
  864. }
  865. # 智能雷达目标链接
  866. $extra['radar_id'] = $radarId;
  867. $extra['timestamp'] = time();
  868. $extra['token'] = get_token($extra['corpid'], $extra['user_id'], $extra['timestamp'], $radarId);
  869. # 用户打开雷达后首先跳转的h5链接地址
  870. $traceUri = env('DOMAIN', 'http://dj.wenxingshuju.com/') . 'playlet/qwh5/dist/index.html#/radar';
  871. $traceUri .= '?' .http_build_query($extra);
  872. $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';
  873. # 雷达封面图
  874. $picUrl = Material::where('id', $radarInfo->cover_id)->value('oss_url');
  875. return [
  876. 'title' => $radarInfo->title,
  877. 'picurl' => $picUrl,
  878. 'desc' => $radarInfo->description,
  879. 'url' => $auth2Url
  880. ];
  881. }
  882. /**
  883. * 根据雷达id获取雷达内容
  884. * */
  885. public static function getRadarContent($corpid, $radarId)
  886. {
  887. $radarInfo = RadarCustomerDetail::select(['title', 'description', 'cover_id', 'type', 'url_list', 'name'])
  888. ->where('id', $radarId)->where('corpid', $corpid)
  889. ->where('enable', 1)->first();
  890. if(empty($radarInfo)) {
  891. return [];
  892. }
  893. # 雷达封面图
  894. $picUrl = Material::where('id', $radarInfo->cover_id)->value('oss_url');
  895. return [
  896. 'radar_id' => $radarId,
  897. 'title' => $radarInfo->title ?? '',
  898. 'name' => $radarInfo->name ?? '',
  899. 'description' => $radarInfo->description ?? '',
  900. 'pic_url' => $picUrl,
  901. 'type' => $radarInfo->type,
  902. 'url_list' => json_decode($radarInfo->url_list, true)
  903. ];
  904. }
  905. /**
  906. * 获取H5页面需要展示的雷达内容
  907. * @param $corpid string 企微id
  908. * @param $radarId int 雷达id
  909. * @param $userId string 客服id
  910. * @param $token string 令牌
  911. * @param $timestamp int 时间戳
  912. * */
  913. public static function radarDetailForH5($corpid, $radarId, $userId, $token, $timestamp, &$errno)
  914. {
  915. # 验签
  916. $authToken = get_token($corpid, $userId, $timestamp, $radarId);
  917. if($authToken != $token) {
  918. $errno = 1004;
  919. return [];
  920. }
  921. # 获取雷达需要展示的内容类型及内容
  922. $radarDetail = RadarCustomerDetail::select(['type', 'url_list'])->where('id', $radarId)
  923. ->where('corpid', $corpid)
  924. ->where('enable', 1)
  925. ->first();
  926. return $radarDetail;
  927. }
  928. /**
  929. * 雷达用户行为数据上报
  930. * @param $param array 待上报数据
  931. * @param $type integer
  932. * @param $token string 签名
  933. * @param int $retry 重试次数
  934. * @return int
  935. * */
  936. public static function behaviorReport($param, $type, $token, $timestamp, $retry=0)
  937. {
  938. try {
  939. if(!$retry) {
  940. # 验签
  941. $authToken = get_token($param['corpid'], $param['user_id'], $timestamp, $param['radar_id']);
  942. if($authToken != $token) {
  943. Log::logError('非法数据上报', [
  944. 'params' => $param, 'token' => $token, 'timestamp' => $timestamp
  945. ]);
  946. return 1004;
  947. }
  948. }
  949. if($param['external_userid'])
  950. $param['con_user_cus'] = $param['user_id'] . $param['external_userid'];
  951. if(1 == $type) {
  952. # 行为数据存储
  953. $result = RadarCustomerBehavior::insert($param);
  954. if(!$result) {
  955. Log::logError('行为数据上报失败', [
  956. 'param' => $param,
  957. 'retry' => $retry
  958. ], 'BehaviorReport');
  959. return 2804;
  960. }
  961. # 处理触发雷达后续操作
  962. if($param['external_userid'])
  963. RedisModel::lPush(RadarCustomerDetail::RADAR_CUSTOMER_FOLLOW_UP, json_encode($param));
  964. } else {
  965. $info = RadarCustomerBehavior::query()
  966. ->where('request_id', $param['request_id'])
  967. ->where('radar_id', $param['radar_id'])
  968. ->first();
  969. if(empty($info)) {
  970. // 第一次的上报数据没有找到
  971. $param['close_time'] = date('Y-m-d H:i:s');
  972. # 行为数据存储
  973. $result = RadarCustomerBehavior::insert($param);
  974. if(!$result) {
  975. Log::logError('行为数据上报失败', [
  976. 'param' => $param,
  977. 'retry' => $retry
  978. ], 'BehaviorReport');
  979. return 2804;
  980. }
  981. # 处理触发雷达后续操作
  982. if($param['external_userid'])
  983. RedisModel::lPush(RadarCustomerDetail::RADAR_CUSTOMER_FOLLOW_UP, json_encode($param));
  984. } else {
  985. // 正常记录第二次上报
  986. $update['close_time'] = date('Y-m-d H:i:s');
  987. $update['view_time'] = time() - strtotime($info->create_time);
  988. # 行为数据存储
  989. $result = RadarCustomerBehavior::query()
  990. ->where('id', $info->id)
  991. ->update($update);
  992. if(!$result) {
  993. Log::logError('行为数据上报失败', [
  994. 'param' => $param,
  995. 'retry' => $retry
  996. ], 'BehaviorReport');
  997. return 2804;
  998. }
  999. }
  1000. }
  1001. } catch (\Exception $e) {
  1002. Log::logError('行为数据上报发生异常', [
  1003. 'param' => $param,
  1004. 'line' => $e->getLine(),
  1005. 'msg' => $e->getMessage()
  1006. ], 'BehaviorReport-Exception');
  1007. return 2804;
  1008. }
  1009. return 0;
  1010. }
  1011. // 渠道列表
  1012. public static function channelList()
  1013. {
  1014. return RadarCustomerBehavior::CHANNEL;
  1015. }
  1016. // h5雷达列表
  1017. public static function radarListForH5($params)
  1018. {
  1019. try{
  1020. $query = RadarCustomerDetail::query()
  1021. ->where('corpid', '=', $params['corpid'])
  1022. ->where('enable', '=', 1);
  1023. if(!empty($params['name'])) {
  1024. $query->where('title', 'like', '%'.$params['name'].'%');
  1025. }
  1026. if(isset($params['group_id'])) {
  1027. if(0 == $params['group_id']) {
  1028. $query->where('group_id', '=', '');
  1029. } else {
  1030. $query->where('group_id', '=', $params['group_id']);
  1031. }
  1032. }
  1033. $count = $query->count();
  1034. $data = $query->select(['title', 'description', 'cover_id'])
  1035. ->selectRaw('id as radar_id')
  1036. ->orderBy('sort_order', 'desc')
  1037. ->offset(($params['page'] - 1) * $params['page_size'])
  1038. ->limit($params['page_size'])
  1039. ->get();
  1040. // 提取雷达封面素材id
  1041. $coverIdList = array_column($data->toArray(), 'cover_id');
  1042. $coverData = Material::query()
  1043. ->whereIn('id', $coverIdList)
  1044. ->get();
  1045. foreach($data as &$value) {
  1046. $coverInfo = $coverData->where('id', $value->cover_id)->first();
  1047. $value->cover_url = !empty($coverInfo->oss_url) ? $coverInfo->oss_url : null;
  1048. }
  1049. return [$data, $count];
  1050. } catch (\Exception $exception) {
  1051. Log::logError('radarListForH5', [
  1052. 'params' => $params,
  1053. 'line' => $exception->getLine(),
  1054. 'msg' => $exception->getMessage(),
  1055. 'trace' => $exception->getTraceAsString(),
  1056. ], 'interface');
  1057. return [[], 0];
  1058. }
  1059. }
  1060. }