| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\UserModel;
- use App\Models\ActionLogModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use Illuminate\Support\Facades\DB;
- /**
- * 服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class UserService
- * @package App\Services\Common
- */
- class ExamAccessLogsService extends BaseService
- {
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * UserService constructor.
- */
- public function __construct()
- {
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- public function examAccessStats(array $params)
- {
- $dateType = $params['dateType'] ?? 'day';
- $page = max(1, (int) ($params['page'] ?? 1));
- $limit = max(1, (int) ($params['limit'] ?? 10));
- // 时间范围
- $startDate = !empty($params['start_time']) ? $params['start_time'] : '2000-01-01';
- $endDate = !empty($params['end_time']) ? $params['end_time'] : date('Y-m-d');
- if ($endDate < $startDate) {
- $endDate = $startDate;
- }
- // 分组字段
- switch ($dateType) {
- case 'month':
- $groupRaw = "DATE_FORMAT(date, '%Y-%m')";
- break;
- case 'year':
- $groupRaw = "DATE_FORMAT(date, '%Y')";
- break;
- case 'week':
- $groupRaw = "CONCAT(YEAR(date), '-', LPAD(WEEK(date, 1), 2, '0'))";
- break;
- case 'day':
- default:
- $groupRaw = "DATE_FORMAT(date, '%Y-%m-%d')";
- break;
- }
- // 基础查询
- $query = DB::table('exam_access_logs')
- ->selectRaw("
- {$groupRaw} as stat_date,
- type,
- SUM(scene_count1) as scene_count1,
- SUM(scene_count2) as scene_count2,
- SUM(scene_count3) as scene_count3,
- SUM(scene_count4) as scene_count4,
- SUM(scene_count5) as scene_count5,
- SUM(scene_count6) as scene_count6,
- SUM(scene_count7) as scene_count7,
- SUM(scene_count20) as scene_count20
- ")
- ->whereBetween('date', [$startDate, $endDate])
- ->where('status', 1)
- ->where('mark', 1)
- ->groupBy('stat_date', 'type')
- ->orderBy('stat_date', 'desc');
- // 总条数
- $total = DB::table(DB::raw("({$query->toSql()}) as t"))
- ->mergeBindings($query)
- ->count();
- // 分页
- $list = $query->forPage($page, $limit)->get();
- return [
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit,
- ];
- }
- /**
- * 删除七天之前标记软删除的数据
- */
- public function delete()
- {
- // 设置日志标题
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除考试访问日志信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
- ActionLogModel::record();
- $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
- return parent::delete();
- }
- }
|