ExamAccessLogsService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\Models\ActionLogModel;
  14. use App\Services\BaseService;
  15. use App\Services\ConfigService;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * Class UserService
  22. * @package App\Services\Common
  23. */
  24. class ExamAccessLogsService extends BaseService
  25. {
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * UserService constructor.
  31. */
  32. public function __construct()
  33. {
  34. }
  35. /**
  36. * 静态入口
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = new static();
  42. }
  43. return self::$instance;
  44. }
  45. public function examAccessStats(array $params)
  46. {
  47. $dateType = $params['dateType'] ?? 'day';
  48. $page = max(1, (int) ($params['page'] ?? 1));
  49. $limit = max(1, (int) ($params['limit'] ?? 10));
  50. // 时间范围
  51. $startDate = !empty($params['start_time']) ? $params['start_time'] : '2000-01-01';
  52. $endDate = !empty($params['end_time']) ? $params['end_time'] : date('Y-m-d');
  53. if ($endDate < $startDate) {
  54. $endDate = $startDate;
  55. }
  56. // 分组字段
  57. switch ($dateType) {
  58. case 'month':
  59. $groupRaw = "DATE_FORMAT(date, '%Y-%m')";
  60. break;
  61. case 'year':
  62. $groupRaw = "DATE_FORMAT(date, '%Y')";
  63. break;
  64. case 'week':
  65. $groupRaw = "CONCAT(YEAR(date), '-', LPAD(WEEK(date, 1), 2, '0'))";
  66. break;
  67. case 'day':
  68. default:
  69. $groupRaw = "DATE_FORMAT(date, '%Y-%m-%d')";
  70. break;
  71. }
  72. // 基础查询
  73. $query = DB::table('exam_access_logs')
  74. ->selectRaw("
  75. {$groupRaw} as stat_date,
  76. type,
  77. SUM(scene_count1) as scene_count1,
  78. SUM(scene_count2) as scene_count2,
  79. SUM(scene_count3) as scene_count3,
  80. SUM(scene_count4) as scene_count4,
  81. SUM(scene_count5) as scene_count5,
  82. SUM(scene_count6) as scene_count6,
  83. SUM(scene_count7) as scene_count7,
  84. SUM(scene_count20) as scene_count20
  85. ")
  86. ->whereBetween('date', [$startDate, $endDate])
  87. ->where('status', 1)
  88. ->where('mark', 1)
  89. ->groupBy('stat_date', 'type')
  90. ->orderBy('stat_date', 'desc');
  91. // 总条数
  92. $total = DB::table(DB::raw("({$query->toSql()}) as t"))
  93. ->mergeBindings($query)
  94. ->count();
  95. // 分页
  96. $list = $query->forPage($page, $limit)->get();
  97. return [
  98. 'list' => $list,
  99. 'total' => $total,
  100. 'page' => $page,
  101. 'limit' => $limit,
  102. ];
  103. }
  104. /**
  105. * 删除七天之前标记软删除的数据
  106. */
  107. public function delete()
  108. {
  109. // 设置日志标题
  110. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除考试访问日志信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  111. ActionLogModel::record();
  112. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  113. return parent::delete();
  114. }
  115. }