package cn.fateverse.query.controller;

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.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.query.service.impl.SqlUniQueryServiceImpl;
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;

/**
 * @author Clay
 * @date 2023-08-09
 */
@Api(value = "SQL万能查询管理", tags = "SQL万能查询管理")
@RestController
@RequestMapping("/sql/uni")
public class SqlUniQueryController {


    private final UniQueryService uniQueryService;


    public SqlUniQueryController(SqlUniQueryServiceImpl uniQueryService) {
        this.uniQueryService = uniQueryService;
    }

    @ApiOperation("获取万能查询列表")
    @GetMapping
    @Encrypt
    @PreAuthorize("@ss.hasPermission('sql: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('sql:uni-query:export')")
    public void export(UniQueryQuery query) {
        List<UniQueryVo> list = uniQueryService.exportList(query);
        ExcelUtil.exportExcel(list, UniQueryVo.class);
    }

    @ApiOperation("获取万能查询详细信息")
    @GetMapping("/{id}")
    @Encrypt
    @PreAuthorize("@ss.hasPermission('sql: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('sql: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('sql: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('sql: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();
    }

}
