package com.ruoyi.SafetyDeclaration.controller.sidebarPc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.SafetyDeclaration.domain.BeCurrentTable;
import com.ruoyi.SafetyDeclaration.domain.SafetyDeclarationTable;
import com.ruoyi.SafetyDeclaration.domain.VoCircularDiagram;
import com.ruoyi.SafetyDeclaration.domain.VoHistogramTable;
import com.ruoyi.SafetyDeclaration.service.IVisualizationService;
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.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

/**
 * 水电校园安全数据可视化
 Controller
 *
 * @date 2024-08-19
 */
@RestController
@RequestMapping("/sidebar/Visualization")
public class VisualizationController extends BaseController {

    @Autowired
    private IVisualizationService iVisualizationService;

    /*
    * 柱状图数据
    * */
    @GetMapping("/histogram")
    public AjaxResult histogram()
    {
        List<VoHistogramTable> list=new ArrayList<>();
        List<BeCurrentTable> tables = iVisualizationService.selectHistogram();

        // 使用Map按name进行分组
        Map<String, VoHistogramTable> seriesMap = new HashMap<>();
        for (BeCurrentTable item : tables) {
            VoHistogramTable series = seriesMap.computeIfAbsent(item.getName(), k -> new VoHistogramTable(k));
            series.addData(item.getValue());
        }
        // 将Map转换为List<ChartSeries>
        List<VoHistogramTable> chartSeriesList = new ArrayList<>(seriesMap.values());
        return AjaxResult.success(chartSeriesList);
    }

    /*折线图*/
    @GetMapping("/lineChart")
    public AjaxResult lineChart()
    {
        List<Double> list=new ArrayList<>();
        List<BeCurrentTable> tables = iVisualizationService.selectLineChart();
        for (BeCurrentTable item : tables) {
            list.add((double)item.getValue());
        }
        return AjaxResult.success(list);
    }

    /*圆环图*/
    @GetMapping("/circularDiagram")
    public AjaxResult circularDiagram(SafetyDeclarationTable table)
    {
        VoCircularDiagram tables = iVisualizationService.SelectCircularDiagram(table);
        return AjaxResult.success(tables);
    }

}
