package com.ygtx.gxt.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
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.ygtx.common.core.controller.BaseController;
import com.ygtx.common.core.domain.AjaxResult;
import com.ygtx.gxt.service.IGxtOrderStatisticsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

/**
 * 工单统计Controller
 *
 * @author ygtx
 * @date 2025-11-03
 */
@Api(tags = "工单统计管理")
@RestController
@RequestMapping("/gxt/statistics")
public class GxtOrderStatisticsController extends BaseController {

    @Autowired
    private IGxtOrderStatisticsService statisticsService;

    /**
     * 分组统计工单数据
     */
    @ApiOperation("分组统计工单数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "groupFields", value = "分组字段", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "gxtCenter", value = "维保中心", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "pcsStationName", value = "场站名称", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "brand", value = "品牌", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "model", value = "型号", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class)
    })
    @GetMapping("/groupBy")
    public AjaxResult getGroupStatistics(
            @RequestParam(value = "groupFields", required = false) String groupFields,
            @RequestParam(value = "gxtCenter", required = false) String gxtCenter,
            @RequestParam(value = "pcsStationName", required = false) String pcsStationName,
            @RequestParam(value = "brand", required = false) String brand,
            @RequestParam(value = "model", required = false) String model,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime) {
        
        try {
            List<Map<String, Object>> result = statisticsService.getGroupStatistics(
                    groupFields, gxtCenter, pcsStationName, brand, model, startTime, endTime);
            return AjaxResult.success(result);
        } catch (Exception e) {
            logger.error("分组统计失败", e);
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 获取统计维度选项
     */
    @ApiOperation("获取统计维度选项")
    @GetMapping("/dimensions")
    public AjaxResult getDimensions() {
        try {
            Map<String, Object> dimensions = statisticsService.getDimensions();
            return AjaxResult.success(dimensions);
        } catch (Exception e) {
            logger.error("获取统计维度失败", e);
            return AjaxResult.error("获取失败：" + e.getMessage());
        }
    }

    /**
     * 分组统计维修工单数据
     */
    @ApiOperation("分组统计维修工单数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "groupFields", value = "分组字段", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "gxtCenter", value = "维保中心", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "pcsStationName", value = "场站名称", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "brand", value = "品牌", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "model", value = "型号", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class)
    })
    @GetMapping("/repairOrder")
    public AjaxResult getRepairOrderStatistics(
            @RequestParam(value = "groupFields", required = false) String groupFields,
            @RequestParam(value = "gxtCenter", required = false) String gxtCenter,
            @RequestParam(value = "pcsStationName", required = false) String pcsStationName,
            @RequestParam(value = "brand", required = false) String brand,
            @RequestParam(value = "model", required = false) String model,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime) {
        
        try {
            List<Map<String, Object>> result = statisticsService.getRepairOrderStatistics(
                    groupFields, gxtCenter, pcsStationName, brand, model, startTime, endTime);
            return AjaxResult.success(result);
        } catch (Exception e) {
            logger.error("维修工单统计失败", e);
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }

    /**
     * 获取所有维修类型
     */
    @ApiOperation("获取所有维修类型")
    @GetMapping("/maintenanceTypes")
    public AjaxResult getMaintenanceTypes() {
        try {
            List<String> types = statisticsService.getAllMaintenanceTypes();
            return AjaxResult.success(types);
        } catch (Exception e) {
            logger.error("获取维修类型失败", e);
            return AjaxResult.error("获取失败：" + e.getMessage());
        }
    }

    /**
     * 获取维保运行统计
     */
    @ApiOperation("获取维保运行统计")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "groupFields", value = "分组字段", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "gxtCenter", value = "维保中心", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "pcsStationName", value = "场站名称", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "brand", value = "品牌", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class),
        @ApiImplicitParam(name = "model", value = "型号", required = false, dataType = "String", paramType = "query", dataTypeClass = String.class)
    })
    @GetMapping("/runtimeScore")
    public AjaxResult getRuntimeScoreStatistics(
            @RequestParam(value = "groupFields", required = false) String groupFields,
            @RequestParam(value = "gxtCenter", required = false) String gxtCenter,
            @RequestParam(value = "pcsStationName", required = false) String pcsStationName,
            @RequestParam(value = "brand", required = false) String brand,
            @RequestParam(value = "model", required = false) String model) {
        
        try {
            List<Map<String, Object>> result = statisticsService.getRuntimeScoreStatistics(
                    groupFields, gxtCenter, pcsStationName, brand, model);
            return AjaxResult.success(result);
        } catch (Exception e) {
            logger.error("维保运行统计失败", e);
            return AjaxResult.error("统计失败：" + e.getMessage());
        }
    }
}