package com.ruoyi.web.controller.ehs;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ehsEnterprise.domain.EhsEnterprise;
import com.ruoyi.ehsEnterprise.service.IEhsEnterpriseService;
import com.ruoyi.ehsIndexStatistics.service.EhsIndexStatisticsService;
import com.ruoyi.ehsIndexStatistics.vo.*;
import com.ruoyi.ehsMaterial.domain.EhsMaterial;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 首页统计分析接口
 *
 * @author: 马腾达
 * @date: 2023年12月12日 11:10
 */
@Api(tags = "首页统计分析接口")
@RestController
@RequestMapping("/ehs/indexStatistics")
public class EhsIndexStatisticsController extends BaseController {

    @Autowired
    private EhsIndexStatisticsService ehsIndexStatisticsService;

    /**
     * 查询首页上方文字数据
     */
    @GetMapping("/getEhsIndexStatistics")
    public AjaxResult getEhsIndexStatistics()
    {
        EhsIndexStatistics ehsIndexStatistics = ehsIndexStatisticsService.getEhsIndexStatistics();
        return AjaxResult.success(ehsIndexStatistics);
    }
    /**
     * 查询首页上方文字数据--汇总数据
     */
    @GetMapping("/getSuperviseAndHandle")
    public AjaxResult getSuperviseAndHandle()
    {
        EhsIndexStatistics ehsIndexStatistics = ehsIndexStatisticsService.getSuperviseAndHandle();
        return AjaxResult.success(ehsIndexStatistics);
    }
    /**
     * 查询首页上方文字数据----列表数据
     */
    @GetMapping("/getEhsIndexStatisticsList")
    public AjaxResult getEhsIndexStatisticsList()
    {
        List<EhsIndexStatistics> list = ehsIndexStatisticsService.getEhsIndexStatisticsList();
        return AjaxResult.success(list);
    }

    /**
     * 查询首页上方文字数据点击详情
     */
    @GetMapping("/detailslist")
    public TableDataInfo detailslist(HiddenDangerVo vo)
    {
        startPage();
        List<HiddenDangerVo> list = ehsIndexStatisticsService.getHiddenDangerVoList(vo);
        return getDataTable(list);
    }

    /**
     * 隐患情况---按行政区域统计
     */
    @ApiOperation("隐患情况---按行政区域统计")
    @GetMapping("/getHiddenDangerSituationList")
    public AjaxResult getHiddenDangerSituationList()
    {
        List<HiddenDangerStatisticsVo> list = ehsIndexStatisticsService.getHiddenDangerSituationList();
        return AjaxResult.success(list);
    }

    /**
     * 隐患情况---按地址统计
     */
    @ApiOperation("按地址统计")
    @GetMapping("/getHiddenDangerSituationByEnterpriseNameList")
    public AjaxResult getHiddenDangerSituationByEnterpriseNameList()
    {
        List<HiddenDangerStatisticsVo> list = ehsIndexStatisticsService.getHiddenDangerSituationByEnterpriseNameList();
        return AjaxResult.success(list);
    }
    /**
     * 上报隐患排名
     */
    @ApiOperation("上报隐患排名")
    @GetMapping("/getHiddenDangerRankVoList")
    public AjaxResult getHiddenDangerRankVoList()
    {
        List<HiddenDangerRankVo> list = ehsIndexStatisticsService.getHiddenDangerRankVoList();
        return AjaxResult.success(list);
    }
    @ApiOperation("部门上报隐患排名")
    @GetMapping("/getHiddenDangerRankVoByDeptIdList")
    public AjaxResult getHiddenDangerRankVoByDeptIdList()
    {
        List<HiddenDangerRankVo> list = ehsIndexStatisticsService.getHiddenDangerRankVoByDeptIdList();
        return AjaxResult.success(list);
    }
    /**
     * 行业领域统计数量
     */
    @ApiOperation("行业领域统计数量")
    @GetMapping("/getEnterpriseMapList")
    public AjaxResult getEnterpriseMapList()
    {
        List<Map<String,Object>> list = ehsIndexStatisticsService.getEnterpriseMapList();
        return AjaxResult.success(list);
    }
    /**
     * 经济类型统计数量
     */
    @ApiOperation("经济类型统计数量")
    @GetMapping("/geteconomicTypeMapList")
    public AjaxResult geteconomicTypeMapList()
    {
        List<Map<String,Object>> list = ehsIndexStatisticsService.geteconomicTypeMapList();
        return AjaxResult.success(list);
    }
    /**
     * 企业规模统计数量
     */
    @ApiOperation("企业规模统计数量")
    @GetMapping("/getenterpriseScaleMapList")
    public AjaxResult getenterpriseScaleMapList()
    {
        List<Map<String,Object>> list = ehsIndexStatisticsService.getenterpriseScaleMapList();
        return AjaxResult.success(list);
    }
    /**
     * 隐患类型分析统计
     */
    @ApiOperation("隐患类型分析统计")
    @GetMapping("/getHiddenDangerTypeMapList")
    public AjaxResult getHiddenDangerTypeMapList()
    {
        List<Map<String,Object>> list = ehsIndexStatisticsService.getHiddenDangerTypeMapList();
        return AjaxResult.success(list);
    }
    /**
     * 隐患数据统计分析
     */
    @ApiOperation("隐患数据统计分析")
    @GetMapping("/getHiddenDangerDataMapList")
    public AjaxResult getHiddenDangerDataMapList(String year)
    {
        JSONArray row = new JSONArray();
        List<Map<String,Object>> list = ehsIndexStatisticsService.getHiddenDangerDataMapList(year);
        if(StringUtils.isNotEmpty(list)){
            LinkedHashMap<Object, List<Map<String, Object>>> nameList = list.stream().collect(Collectors.groupingBy(e -> e.get("name"), LinkedHashMap::new, Collectors.toList()));
            if(StringUtils.isNotEmpty(nameList)){
                for (Object key : nameList.keySet()) {
                    JSONObject obj = new JSONObject();
                    System.out.println("map.get(" + key + ") = " + nameList.get(key));
                    obj.put("name",key);
                    List<Object> dataList = nameList.get(key).stream().map(e -> e.get("dataNum")).collect(Collectors.toList());
                    obj.put("data",dataList);
                    row.add(obj);
                }
            }
        }
        return AjaxResult.success(row);
    }

}
