BookSearchStat.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace App\Modules\Book\Models;
  3. use DB;
  4. use Illuminate\Database\Eloquent\Model;
  5. class BookSearchStat extends Model
  6. {
  7. protected $table = 'book_search_stats';
  8. protected $fillable = ['uid', 'openid', 'type', 'content'];
  9. static function getInfo($params, $is_all = false)
  10. {
  11. \Log::info($params);
  12. $search_obj = self::select(
  13. 'content',
  14. DB::raw('count(distinct uid) user_num'),
  15. DB::raw('count(*) search_num'));
  16. if (isset($params['begin_time']) && $params['begin_time']) {
  17. $search_obj->where('created_at', '>=', $params['begin_time']);
  18. }
  19. if (isset($params['end_time']) && $params['end_time']) {
  20. $search_obj->where('created_at', '<=', $params['end_time'] . ' 23:59:59');
  21. }
  22. if (isset($params['type']) && $params['type']) {
  23. $search_obj->where('type', $params['type']);
  24. }
  25. $search_obj->orderBy(DB::raw('count(*)'), 'desc');
  26. $search_obj->groupBy('content');
  27. \Log::info($search_obj->toSql());
  28. if ($is_all) {
  29. return $search_obj->get();
  30. } else {
  31. return $search_obj->paginate();
  32. }
  33. }
  34. }