package com.ebts.generator.controller;

import java.io.Serializable;
import java.util.List;


import com.ebts.common.annotation.Log;
import com.ebts.common.core.controller.BaseController;
import com.ebts.common.core.entity.AjaxResult;
import com.ebts.common.enums.BusinessType;
import com.ebts.common.utils.ServerResult;
import com.ebts.common.utils.poi.ExcelUtil;
import com.ebts.generator.entity.UniQuery;
import com.ebts.generator.service.UniQueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 万能查询Controller
 *
 * @author binlin
 * @date 2021-01-30
 */
@RestController
@RequestMapping("/tool/query")
public class UniQueryController extends BaseController {
    protected final Logger logger = LoggerFactory.getLogger(UniQueryController.class);

    @Autowired
    private UniQueryService uniQueryService;

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

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

    /**
     * 获取万能查询详细信息
     */
    @PreAuthorize("@ebts.hasPermi('tool:query:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ServerResult<UniQuery> ServerResult = uniQueryService.selectUniQueryById(id);
        if (ServerResult.isStart()) {
            return AjaxResult.success(ServerResult.getData());
        } else {
            return AjaxResult.info(ServerResult.getMsg());
        }
    }

    /**
     * 新增万能查询
     */
    @PreAuthorize("@ebts.hasPermi('tool:query:add')")
    @Log(title = "万能查询", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UniQuery uniQuery) {
        if (uniQuery.getUqName() == null || uniQuery.getUqName().equals("")) {
            return AjaxResult.error("名称不能为空!");
        }
        if (uniQuery.getUqDescribe() == null || uniQuery.getUqDescribe().equals("")) {
            return AjaxResult.error("描述不能为空!");
        }
        ServerResult<Integer> ServerResult = uniQueryService.insertUniQuery(uniQuery);
        if (ServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(ServerResult.getMsg());
        }
    }

    /**
     * 简单修改万能能查询
     */
    @PreAuthorize("@ebts.hasPermi('tool:query:edit')")
    @Log(title = "万能查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UniQuery uniQuery) {
        if (uniQuery.getUqName() == null || uniQuery.getUqName().equals("")) {
            return AjaxResult.error("名称不能为空!");
        }
        if (uniQuery.getUqDescribe() == null || uniQuery.getUqDescribe().equals("")) {
            return AjaxResult.error("描述不能为空!");
        }
        ServerResult<Integer> ServerResult = uniQueryService.updateUniQuery(uniQuery);
        if (ServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(ServerResult.getMsg());
        }
    }

    /**
     * 删除万能查询
     */
    @PreAuthorize("@ebts.hasPermi('tool:query:remove')")
    @Log(title = "万能查询", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (ids.length < 0) {
            return AjaxResult.error("id不能为空!");
        }
        ServerResult<Integer> ServerResult = uniQueryService.deleteUniQueryByIds(ids);
        if (ServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(ServerResult.getMsg());
        }
    }
}