package cn.fateverse.query.controller;

import cn.fateverse.common.decrypt.annotation.Encrypt;
import cn.fateverse.common.decrypt.annotation.EncryptField;
import cn.fateverse.common.excel.utils.ExcelUtil;
import cn.fateverse.common.log.annotation.Log;
import cn.fateverse.common.log.enums.BusinessType;
import cn.fateverse.query.entity.vo.UniQueryVo;
import cn.fateverse.query.entity.dto.UniQueryDto;
import cn.fateverse.query.entity.query.UniQueryQuery;
import cn.fateverse.query.service.UniQueryService;
import cn.fateverse.common.core.result.Result;
import cn.fateverse.common.core.result.page.TableDataInfo;
import cn.fateverse.common.core.utils.ObjectUtils;
import cn.fateverse.query.service.impl.TopoUniQueryServiceImpl;
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;
import java.util.stream.Collectors;

/**
 * 万能查询 Controller
 *
 * @author clay
 * @date 2023-03-01
 */
@Api(value = "TOPO万能查询管理", tags = "TOPO万能查询管理")
@RestController
@RequestMapping({"/topo/uni"})
public class TopoUniQueryController {

    private final UniQueryService uniQueryService;

    public TopoUniQueryController(TopoUniQueryServiceImpl uniQueryService) {
        this.uniQueryService = uniQueryService;
    }

    @ApiOperation("拓扑图获取万能查询列表")
    @GetMapping
    @Encrypt
    @PreAuthorize("@ss.hasPermission('topo:uni-query:list')")
    public Result<TableDataInfo<UniQueryVo>> list(UniQueryQuery query) {
        TableDataInfo<UniQueryVo> dataInfo = uniQueryService.searchList(query);
        return Result.ok(dataInfo);
    }

    @ApiOperation("拓扑图导出excel数据")
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermission('topo:uni-query:export')")
    public void export(UniQueryQuery query) {
        List<UniQueryVo> list = uniQueryService.exportList(query);
        ExcelUtil.exportExcel(list, UniQueryVo.class);
    }

    @ApiOperation("拓扑图数据查看接口")
    @GetMapping("/view/{id}")
    @PreAuthorize("@ss.hasPermission('topo:uni-query:view')")
    @Encrypt
    public Result<String> preview(@PathVariable @EncryptField String id) {
        String url = uniQueryService.view(Long.valueOf(id));
        return Result.ok(url);
    }


    @ApiOperation("拓扑图获取万能查询详细信息")
    @GetMapping("/{id}")
    @Encrypt
    @PreAuthorize("@ss.hasPermission('topo:uni-query:info')")
    public Result<UniQueryVo> info(@PathVariable @EncryptField String id) {
        ObjectUtils.checkPk(id);
        UniQueryVo uniQuery = uniQueryService.searchById(Long.parseLong(id));
        return Result.ok(uniQuery);
    }

    @ApiOperation("拓扑图新增万能查询")
    @PostMapping
    @Log(title = "拓扑图新增万能查询", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermission('topo:uni-query:add')")
    public Result<UniQueryVo> add(@RequestBody @Validated UniQueryDto dto) {
        return uniQueryService.save(dto);
    }

    @ApiOperation("拓扑图修改万能查询")
    @PutMapping
    @Encrypt
    @Log(title = "拓扑图修改万能查询", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermission('topo:uni-query:edit')")
    public Result<Void> edit(@RequestBody @Validated UniQueryDto dto) {
        ObjectUtils.checkPk(dto.getId());
        uniQueryService.edit(dto);
        return Result.ok();
    }

    @ApiOperation("拓扑图删除万能查询")
    @DeleteMapping("/{idList}")
    @Encrypt
    @Log(title = "拓扑图删除万能查询", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermission('topo:uni-query:del')")
    public Result<Void> batchDel(@PathVariable @EncryptField List<String> idList) {
        List<Long> collect = idList.stream().map(Long::valueOf).collect(Collectors.toList());
        ObjectUtils.checkPkList(idList);
        uniQueryService.removeBatch(collect);
        return Result.ok();
    }

}
