LogService.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hanl
  5. * Date: 2018/10/26
  6. * Time: 10:13
  7. */
  8. namespace App\Services;
  9. use DB;
  10. use App\Common\Api;
  11. use App\Models\Admin;
  12. use App\Models\Corps;
  13. use App\Models\Orders;
  14. use App\Services\ActivityService;
  15. use Illuminate\Support\Facades\Redis;
  16. class LogService
  17. {
  18. public function __construct($configs = [])
  19. {
  20. }
  21. /**
  22. * 获取日志统计数据
  23. * @param $params
  24. * @return array
  25. */
  26. public static function eventCount($params){
  27. $keyEvent = empty($params['event_type']) || $params['event_type'] == 'all'?'':$params['event_type'];
  28. $keyCorp = empty($params['keyword_corp']) ?'' :$params['keyword_corp'];
  29. $keyAdmin = empty($params['keyword_admin'])?'' :$params['keyword_admin'];
  30. $startTime = empty($params['start_time']) ?time()-3600*24*7 :strtotime($params['start_time']);
  31. $endTime = empty($params['end_time']) ?time()-3600*24 :strtotime($params['end_time']);
  32. $adminIds = [];
  33. $data = [];
  34. if(!empty($keyCorp) || !empty($keyAdmin)){
  35. $type = 'corp';
  36. //存在搜索条件,搜索该企业或该管理员的详细信息
  37. $corpInfo = Corps::where('corp_short_name', $keyCorp)->where('status', 1)->first();
  38. if(!empty($keyAdmin)){
  39. if(!empty($keyCorp)) {
  40. $adminInfo = Admin::where('username', $keyAdmin)->where('corp_id', $corpInfo['corp_id'])->first();
  41. $adminIds[] = $adminInfo['id'];
  42. } else {
  43. $adminInfo = Admin::where('username', $keyAdmin)->first();
  44. $adminIds[] = $adminInfo['id'];
  45. }
  46. } else {
  47. $adminIds = Admin::where('corp_id', $corpInfo['corp_id'])->pluck('id');
  48. }
  49. //获取企业或管理员的行为日志
  50. $curl = env('APP_LOG_URL').'/EventLog/statistics?event_type='.$keyEvent."&admin_ids=".json_encode(collect($adminIds)->toArray()).'&start_time='.$startTime.'&end_time='.$endTime;
  51. $corpData = Api::curl($curl);
  52. $corpData = json_decode($corpData,true);
  53. $data = [];
  54. foreach($corpData as $k=>$v){
  55. $adminInfo = Admin::where('id', $v['admin_id'])->first();
  56. $data[$k]['visit_name'] = $adminInfo['name'];
  57. switch ($v['event']){
  58. case 'changeScene1':
  59. $data[$k]['event_type'] = '体验案例1';
  60. break;
  61. case 'changeScene2':
  62. $data[$k]['event_type'] = '体验案例2';
  63. break;
  64. case 'changeScene3':
  65. $data[$k]['event_type'] = '体验案例3';
  66. break;
  67. case 'changeScene4':
  68. $data[$k]['event_type'] = '体验案例4';
  69. break;
  70. case 'openClick1':
  71. $data[$k]['event_type'] = '点击付费购买体验版';
  72. break;
  73. case 'openClick10':
  74. $data[$k]['event_type'] = '点击付费购买专业版';
  75. break;
  76. case 'openClick100':
  77. $data[$k]['event_type'] = '点击付费购买企业版';
  78. break;
  79. case 'chatClick1':
  80. $data[$k]['event_type'] = '头部客服访问';
  81. break;
  82. case 'chatClick2':
  83. $data[$k]['event_type'] = '图标客服访问';
  84. break;
  85. }
  86. $data[$k]['visit_time'] = date('Y-m-d H:i:s', $v['create_time']);
  87. }
  88. } else {
  89. $type = 'all';
  90. //不存在搜索条件,则搜索全部企业的总计信息
  91. $corpIds = Admin::groupBy('corp_id')->pluck('corp_id');
  92. foreach($corpIds as $corpId){
  93. if($corpId == 0){
  94. continue;
  95. }
  96. $corpAdminIds = Admin::where('corp_id', $corpId)->pluck('id');
  97. $curl = env('APP_LOG_URL').'/EventLog/statistics?event_type='.$keyEvent."&admin_ids=".json_encode(collect($corpAdminIds)->toArray()).'&start_time='.$startTime.'&end_time='.$endTime;
  98. $res = Api::curl($curl);
  99. $corpInfo = Corps::where('corp_id', $corpId)->first();
  100. $data[$corpId]['corp_short_name'] = $corpInfo['corp_short_name'];
  101. $data[$corpId]['agh_corp_id'] = $corpInfo['source_id'];
  102. $data[$corpId]['register_time'] = date('Y-m-d H:i:s', $corpInfo['register_time']);
  103. $data[$corpId]['clickCount'] = count(json_decode($res, true));
  104. }
  105. }
  106. return Api::arr(config('code.success'), trans('msg.success'), $data);
  107. }
  108. /**
  109. * 获取订单信息
  110. * @param $params
  111. * @return mixed
  112. */
  113. public static function orderList($params){
  114. $sql = Orders::where('status', 1)->whereBetween('create_time', [$params['order_start_time'], $params['order_end_time']]);
  115. if(!empty($params['corp_name'])){
  116. $corpIds = Corps::where('corp_name', 'like', '%'.$params['corp_name'].'%')->pluck('corp_id');
  117. $sql = $sql->whereIn('corp_id', $corpIds);
  118. }
  119. $data = $sql->orderBy('create_time')->get();
  120. foreach($data as $k=>$v){
  121. $data[$k]['corp_name'] = Corps::where('corp_id', $v['corp_id'])->value('corp_name');
  122. $data[$k]['admin_name'] = Admin::where('id', $v['admin_id'])->value('name');
  123. if($params['vip_start_time'] > $v['create_time'] + 30*3600*24*$v['months'] || $params['vip_end_time'] < $v['create_time'] + 30*3600*24*$v['months']){
  124. unset($data[$k]);
  125. }
  126. }
  127. return $data;
  128. }
  129. }
  130. ?>