ExamAccessLogsService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\UserModel;
  13. use App\Services\BaseService;
  14. use App\Services\ConfigService;
  15. use Illuminate\Support\Facades\DB;
  16. /**
  17. * 服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class UserService
  21. * @package App\Services\Common
  22. */
  23. class ExamAccessLogsService extends BaseService
  24. {
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * UserService constructor.
  30. */
  31. public function __construct()
  32. {
  33. }
  34. /**
  35. * 静态入口
  36. */
  37. public static function make()
  38. {
  39. if (!self::$instance) {
  40. self::$instance = new static();
  41. }
  42. return self::$instance;
  43. }
  44. public function examAccessStats(array $params)
  45. {
  46. $dateType = $params['dateType'] ?? 'day';
  47. $page = max(1, (int) ($params['page'] ?? 1));
  48. $limit = max(1, (int) ($params['limit'] ?? 10));
  49. // 时间范围
  50. $startDate = !empty($params['start_time']) ? $params['start_time'] : '2000-01-01';
  51. $endDate = !empty($params['end_time']) ? $params['end_time'] : date('Y-m-d');
  52. if ($endDate < $startDate) {
  53. $endDate = $startDate;
  54. }
  55. // 分组字段
  56. switch ($dateType) {
  57. case 'month':
  58. $groupRaw = "DATE_FORMAT(date, '%Y-%m')";
  59. break;
  60. case 'year':
  61. $groupRaw = "DATE_FORMAT(date, '%Y')";
  62. break;
  63. case 'week':
  64. $groupRaw = "CONCAT(YEAR(date), '-', LPAD(WEEK(date, 1), 2, '0'))";
  65. break;
  66. case 'day':
  67. default:
  68. $groupRaw = "DATE_FORMAT(date, '%Y-%m-%d')";
  69. break;
  70. }
  71. // 基础查询
  72. $query = DB::table('exam_access_logs')
  73. ->selectRaw("
  74. {$groupRaw} as stat_date,
  75. type,
  76. SUM(scene_count1) as scene_count1,
  77. SUM(scene_count2) as scene_count2,
  78. SUM(scene_count3) as scene_count3,
  79. SUM(scene_count4) as scene_count4,
  80. SUM(scene_count5) as scene_count5,
  81. SUM(scene_count6) as scene_count6,
  82. SUM(scene_count7) as scene_count7,
  83. SUM(scene_count20) as scene_count20
  84. ")
  85. ->whereBetween('date', [$startDate, $endDate])
  86. ->where('status', 1)
  87. ->where('mark', 1)
  88. ->groupBy('stat_date', 'type')
  89. ->orderBy('stat_date', 'desc');
  90. // 总条数
  91. $total = DB::table(DB::raw("({$query->toSql()}) as t"))
  92. ->mergeBindings($query)
  93. ->count();
  94. // 分页
  95. $list = $query->forPage($page, $limit)->get();
  96. return [
  97. 'list' => $list,
  98. 'total' => $total,
  99. 'page' => $page,
  100. 'limit' => $limit,
  101. ];
  102. }
  103. }