package com.ebts.generator.controller;

import com.ebts.common.annotation.Log;
import com.ebts.common.core.controller.BaseController;
import com.ebts.common.core.entity.Result;
import com.ebts.common.enums.BusinessType;
import com.ebts.common.utils.poi.ExcelUtil;
import com.ebts.generator.entity.UniQuery;
import com.ebts.generator.service.UniQueryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;

/**
 * 万能查询Controller
 *
 * @author binlin
 * @date 2021-01-30
 */
@RestController
@RequestMapping("/tool/query")
public class UniQueryController extends BaseController {

    @Resource
    private UniQueryService uniQueryService;

    /**
     * 查询万能查询列表
     */
    @PreAuthorize("@ebts.hasPermi('tool:query:list')")
    @GetMapping("/list")
    public Serializable list(UniQuery uniQuery) {
        startPage();
        Result<List<UniQuery>> result = uniQueryService.selectUniQueryList(uniQuery);
        if (result.isSuccess()) {
            return getDataTable(result.getData());
        } else {
            return result;
        }
    }

    /**
     * 导出万能查询列表
     */
    @PreAuthorize("@ebts.hasPermi('tool:query:export')")
    @Log(title = "万能查询", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public Result<UniQuery> export(UniQuery uniQuery) {
        Result<List<UniQuery>> result = uniQueryService.selectUniQueryList(uniQuery);
        ExcelUtil<UniQuery> util = new ExcelUtil<UniQuery>(UniQuery.class);
        if (result.isSuccess()) {
            return util.exportExcel(result.getData(), "query");
        } else {
            return Result.error(result.getMsg());
        }
    }

    /**
     * 获取万能查询详细信息
     */
    @PreAuthorize("@ebts.hasPermi('tool:query:query')")
    @GetMapping(value = "/{id}")
    public Result<UniQuery> getInfo(@PathVariable("id") Long id) {
        Result<UniQuery> result = uniQueryService.selectUniQueryById(id);
        return result;
    }

    /**
     * 新增万能查询
     */
    @PreAuthorize("@ebts.hasPermi('tool:query:add')")
    @Log(title = "万能查询", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody @Validated UniQuery uniQuery) {
        return uniQueryService.insertUniQuery(uniQuery);
    }

    /**
     * 简单修改万能能查询
     */
    @PreAuthorize("@ebts.hasPermi('tool:query:edit')")
    @Log(title = "万能查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Integer> edit(@RequestBody @Validated UniQuery uniQuery) {
        return uniQueryService.updateUniQuery(uniQuery);
    }

    /**
     * 删除万能查询
     */
    @PreAuthorize("@ebts.hasPermi('tool:query:remove')")
    @Log(title = "万能查询", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable @NotNull @NotBlank(message = "id不能为空!") Long[] ids) {
        return uniQueryService.deleteUniQueryByIds(ids);
    }
}