package com.ruoyi.ibs.dashboard.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ibs.dashboard.domain.dto.TabRankingDTO;
import com.ruoyi.ibs.dashboard.domain.entity.TabRanking;
import com.ruoyi.ibs.dashboard.service.ITabRankingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 指标排名Controller
 * 
 * @author ruoyi
 * @date 2025-08-26
 */
@RestController
@RequestMapping("/system/ranking")
@Api("指标排名")
public class TabRankingController extends BaseController
{
    @Autowired
    private ITabRankingService tabRankingService;


    @Resource
    private RedisCache redisCache;

    /**
     * 查询指标排名列表
     */
//    @PreAuthorize("@ss.hasPermi('system:ranking:list')")
    @GetMapping("/list")
    @ApiOperation("查询指标排名列表")
    public TableDataInfo list(TabRankingDTO tabRankingDTO)
    {
        startPage();
        if(StringUtils.isNull(tabRankingDTO.getDataDt())){
            throw new ServiceException("请选择查询日期");
        }
        if(StringUtils.isEmpty(tabRankingDTO.getTabId())){
            throw new ServiceException("请选择指标");
        }
        if(StringUtils.isEmpty(tabRankingDTO.getLevel())){
            throw new ServiceException("请选择层级");
        }
        if(StringUtils.isEmpty(tabRankingDTO.getCustType())){
            throw new ServiceException("请选择数据类型");
        }
        TabRanking tabRanking = new TabRanking();
        if(StringUtils.isNotEmpty(tabRankingDTO.getTabId())){
            tabRanking.setTabId(tabRankingDTO.getTabId());
        }
        if(StringUtils.isNotNull(tabRankingDTO.getDataDt())){
            tabRanking.setDataDt(tabRankingDTO.getDataDt());
        }
        if(StringUtils.isNotEmpty(tabRankingDTO.getLevel())){
            tabRanking.setLevel(tabRankingDTO.getLevel());
        }
        if(StringUtils.isNotEmpty(tabRankingDTO.getCustType())){
            tabRanking.setCustType(tabRankingDTO.getCustType());
        }
        if(StringUtils.isNotEmpty(tabRankingDTO.getCustType())){
            tabRanking.setBelongId(tabRankingDTO.getName());
        }
        String dicName = "sys_dict:product_index_lishui" ;
        Map<String,String> params = new HashMap<>();
        ArrayList<SysDictData> sysDictTypeArrayList = new ArrayList<SysDictData>(redisCache.getCacheObject(dicName));
        sysDictTypeArrayList.forEach(sdt -> {
            params.put(sdt.getDictValue(), sdt.getDictLabel());
        });
        if(!params.containsKey(tabRankingDTO.getTabId())){
            throw new ServiceException("指标不存在！");
        }
        List<TabRanking> list = tabRankingService.selectTabRankingList(tabRanking);
        return getDataTable(list);
    }

    /**
     * 导出指标排名列表
     */
//    @PreAuthorize("@ss.hasPermi('system:ranking:export')")
//    @Log(title = "指标排名", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, TabRanking tabRanking)
//    {
//        List<TabRanking> list = tabRankingService.selectTabRankingList(tabRanking);
//        ExcelUtil<TabRanking> util = new ExcelUtil<TabRanking>(TabRanking.class);
//        util.exportExcel(response, list, "指标排名数据");
//    }

    /**
     * 获取指标排名详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:ranking:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取指标排名详细评论")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tabRankingService.selectTabRankingById(id));
    }

    /**
     * 新增指标排名
     */
//    @PreAuthorize("@ss.hasPermi('system:ranking:add')")
    @PostMapping
    @ApiOperation("新增指标评论")
    public AjaxResult add(@RequestParam("comment") String comment,@RequestParam("rankingId") String rankingId)
    {
        return toAjax(tabRankingService.insertComment(comment,rankingId));
    }

    /**
     * 修改指标排名
     */
//    @PreAuthorize("@ss.hasPermi('system:ranking:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody TabRanking tabRanking)
    {
        return toAjax(tabRankingService.updateTabRanking(tabRanking));
    }


    @GetMapping("/indexList")
    @ApiOperation("指标id查詢")
    public AjaxResult indexList()
    {
        String dicName = "sys_dict:product_index_lishui" ;
        if(StringUtils.isEmpty(dicName)){
            return success(null );
        }
        Map<String,String> params = new HashMap<>();
        ArrayList<SysDictData> sysDictTypeArrayList = new ArrayList<SysDictData>(redisCache.getCacheObject(dicName));
        sysDictTypeArrayList.forEach(sdt -> {
            params.put(sdt.getDictValue(), sdt.getDictLabel());
        });
        return success(params);
    }




    /**
     * 删除指标排名
     */
//    @PreAuthorize("@ss.hasPermi('system:ranking:remove')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tabRankingService.deleteTabRankingByIds(ids));
    }
}
