package cn.fateverse.query.controller;

import cn.fateverse.query.entity.dto.DynamicEchartsDto;
import cn.fateverse.query.entity.query.DynamicEchartsQuery;
import cn.fateverse.query.entity.vo.DynamicEchartsVo;
import cn.fateverse.query.service.DynamicEchartsService;
import cn.fateverse.common.core.result.Result;
import cn.fateverse.common.core.entity.Option;
import cn.fateverse.common.core.result.page.TableDataInfo;
import cn.fateverse.common.core.utils.ObjectUtils;
import cn.fateverse.common.log.annotation.Log;
import cn.fateverse.common.log.enums.BusinessType;
import cn.fateverse.common.excel.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 动态echarts图表 Controller
 *
 * @author clay
 * @date 2023-11-16
 */
@Api(value = "动态echarts图表管理",tags = "动态echarts图表管理")
@RestController
@RequestMapping("/echarts")
public class DynamicEchartsController {

    private final DynamicEchartsService dynamicEchartsService;

    public DynamicEchartsController(DynamicEchartsService dynamicEchartsService) {
        this.dynamicEchartsService = dynamicEchartsService;
    }

    @ApiOperation("获取动态echarts图表列表")
    @GetMapping
    @PreAuthorize("@ss.hasPermission('query:echarts:list')")
    public Result<TableDataInfo<DynamicEchartsVo>> list(DynamicEchartsQuery query) {
        TableDataInfo<DynamicEchartsVo> dataInfo = dynamicEchartsService.searchList(query);
        return Result.ok(dataInfo);
    }

    @ApiOperation("获取校验规则option")
    @GetMapping("/option")
    public Result<List<Option>> option(){
        List<Option> options = dynamicEchartsService.searchOptionList();
        return Result.ok(options);
    }

    @ApiOperation("导出excel数据")
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermission('query:echarts:export')")
    public void export(DynamicEchartsQuery query){
        List<DynamicEchartsVo> list = dynamicEchartsService.exportList(query);
        ExcelUtil.exportExcel(list,DynamicEchartsVo.class);
    }

    @ApiOperation("获取动态echarts图表详细信息")
    @GetMapping("/{echartsId}")
    @PreAuthorize("@ss.hasPermission('query:echarts:info')")
    public Result<DynamicEchartsVo> info(@PathVariable Long echartsId) {
        ObjectUtils.checkPk(echartsId);
        DynamicEchartsVo dynamicEcharts = dynamicEchartsService.searchById(echartsId);
        return Result.ok(dynamicEcharts);
    }


    @ApiOperation("新增动态echarts图表")
    @PostMapping
    @Log(title = "新增动态echarts图表",businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermission('query:echarts:add')")
    public Result<Void> add(@RequestBody @Validated DynamicEchartsDto dynamicEcharts){
        dynamicEchartsService.save(dynamicEcharts);
        return Result.ok();
    }

    @ApiOperation("修改动态echarts图表")
    @PutMapping
    @Log(title = "修改动态echarts图表",businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermission('query:echarts:edit')")
    public Result<Void> edit(@RequestBody @Validated DynamicEchartsDto dynamicEcharts){
        ObjectUtils.checkPk(dynamicEcharts.getEchartsId());
        dynamicEchartsService.edit(dynamicEcharts);
        return Result.ok();
    }

    @ApiOperation("删除动态echarts图表")
    @DeleteMapping("/{echartsIdList}")
    @Log(title = "删除动态echarts图表",businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermission('query:echarts:del')")
    public Result<Void> batchDel(@PathVariable List<Long> echartsIdList){
        ObjectUtils.checkPkList(echartsIdList);
        dynamicEchartsService.removeBatch(echartsIdList);
        return Result.ok();
    }

}