package com.moka.gdtauto.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moka.gdtauto.common.Result;
import com.moka.gdtauto.entity.OssFileStatistics;
import com.moka.gdtauto.service.OssFileStatisticsService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * OSS文件统计表 前端控制器
 * </p>
 *
 * @author mokamvp
 * @since 2026-03-25
 */
@Tag(name = "OSS文件统计", description = "OSS文件统计数据查询")
@Slf4j
@RestController
@RequestMapping("/api/oss-file-statistics")
@RequiredArgsConstructor
public class OssFileStatisticsController {

    private final OssFileStatisticsService ossFileStatisticsService;

    /**
     * 根据fileMd5和日期范围分页查询统计数据
     */
    @Operation(summary = "分页查询文件统计", description = "根据fileMd5和日期范围分页查询统计数据")
    @GetMapping("/list")
    public Result<Page<OssFileStatistics>> listStatistics(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Long size,
            @Parameter(description = "文件MD5", required = true) @RequestParam String fileMd5,
            @Parameter(description = "开始日期，格式：yyyy-MM-dd") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期，格式：yyyy-MM-dd") @RequestParam(required = false) String endDate,
            @Parameter(description = "排序字段(如: date, viewCount, validClickCount, ctr, cost)") @RequestParam(required = false) String sortField,
            @Parameter(description = "排序方式(ascending/descending)") @RequestParam(required = false) String sortOrder) {
        try {
            Page<OssFileStatistics> result = ossFileStatisticsService.listStatisticsByMd5AndDate(page, size, fileMd5, startDate, endDate, sortField, sortOrder);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询文件统计失败", e);
            return Result.fail("查询文件统计失败: " + e.getMessage());
        }
    }
}
