package com.sundot.airport.web.controller.home;

import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.core.domain.model.LoginUser;
import com.sundot.airport.common.utils.SecurityUtils;
import com.sundot.airport.item.domain.home.RoleBasedSeizureReportDTO;
import com.sundot.airport.item.service.SeizureReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 查获上报 控制器
 */
@RestController
@RequestMapping("/system/check/seizureReport")
public class SeizureReportController {

    @Autowired
    private SeizureReportService seizureReportService;

    /**
     * 获取查获上报数据
     *
     * @param startDate  开始时间
     * @param endDate    结束时间
     * @param timeRange  时间范围, 可选值: week: 周, month: 月, quarter: 季度, half_year: 半年, year: 年
     * @param dataSource individual=个人数据，team=班组数据（默认）
     */
    @GetMapping("/data")
    public AjaxResult getSeizureReportData(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @RequestParam(required = false, defaultValue = "year") String timeRange,
            @RequestParam(required = false, defaultValue = "team") String dataSource,
            @RequestHeader(value = "X-Request-Source", defaultValue = "mobile") String source
    ) {
        try {
            // 获取当前登录用户信息
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (loginUser == null) {
                return AjaxResult.error("用户未登录");
            }

            Long userId = loginUser.getUserId();
            Long deptId = loginUser.getUser().getDeptId();

            // 调用服务获取按角色分类的数据
            RoleBasedSeizureReportDTO roleBasedDto = seizureReportService.getRoleBasedSeizureReportData(userId, deptId, startDate, endDate, timeRange, dataSource, source);

            return AjaxResult.success(roleBasedDto);
        } catch (Exception e) {
            return AjaxResult.error("获取查获上报数据失败: " + e.getMessage());
        }
    }

}