// +---------------------------------------------------------------------- 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(); } }