package com.hchyun.generator.controller;

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


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hchyun.common.constant.HttpStatus;
import com.hchyun.common.constant.ReturnConstants;
import com.hchyun.common.core.controller.BaseController;
import com.hchyun.common.core.controller.HcyBaseController;
import com.hchyun.common.core.page.PageDomain;
import com.hchyun.common.core.page.TableDataInfo;
import com.hchyun.common.core.page.TableSupport;
import com.hchyun.common.utils.ServerResult;
import com.hchyun.common.utils.StringUtils;
import com.hchyun.common.utils.poi.ExcelUtil;
import com.hchyun.common.utils.sql.SqlUtil;
import com.hchyun.generator.entity.UniQuery;
import com.hchyun.generator.service.UniQueryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
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;
import com.hchyun.common.annotation.Log;
import com.hchyun.common.core.entity.AjaxResult;
import com.hchyun.common.enums.BusinessType;

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

    @Autowired
    private UniQueryService uniQueryService;

    /**
     * 查询万能查询列表
     */
    @ApiOperation("查询万能查询列表")
    @PreAuthorize("@ss.hasPermi('tool:query:list')")
    @GetMapping("/list")
    public Serializable list(UniQuery uniQuery) {
        try {
            startPage();
            ServerResult<List<UniQuery>> serverResult = uniQueryService.selectUniQueryList(uniQuery);
            if (serverResult.isStart()) {
                return getDataTable(serverResult.getData());
            } else {
                return AjaxResult.info(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 导出万能查询列表
     */
    @ApiOperation("导出万能查询列表")
    @PreAuthorize("@ss.hasPermi('tool:query:export')")
    @Log(title = "万能查询", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(UniQuery uniQuery) {
        try {
            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());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 获取万能查询详细信息
     */
    @ApiOperation("获取万能查询详细信息")
    @ApiImplicitParam(name = "id" , value = "万能查询id" , required = true, dataType = "Long" , paramType = "path")
    @PreAuthorize("@ss.hasPermi('tool:query:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        try {
            ServerResult<UniQuery> serverResult = uniQueryService.selectUniQueryById(id);
            if (serverResult.isStart()) {
                return AjaxResult.success(serverResult.getData());
            } else {
                return AjaxResult.info(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 新增万能查询
     */
    @ApiOperation("新增万能查询")
    @ApiImplicitParam(name = "uniQuery" , value = "新增万能查询信息" , dataType = "UniQuery")
    @PreAuthorize("@ss.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("描述不能为空!");
        }
        try {
            ServerResult<Integer> serverResult = uniQueryService.insertUniQuery(uniQuery);
            if (serverResult.isStart()) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error(serverResult.getMsg());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 修改万能查询
     */
    @ApiOperation("修改万能查询")
    @ApiImplicitParam(name = "uniQuery" , value = "修改万能查询信息" , dataType = "UniQuery")
    @PreAuthorize("@ss.hasPermi('tool:query:edit')")
    @Log(title = "万能查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UniQuery uniQuery) {
        try {

            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());
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }

    /**
     * 删除万能查询
     */
    @ApiOperation("删除万能查询")
    @ApiImplicitParam(name = "ids" , value = "万能查询ids" , required = true, dataType = "Long" , paramType = "path")
    @PreAuthorize("@ss.hasPermi('tool:query:remove')")
    @Log(title = "万能查询", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        try {
            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());
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return AjaxResult.error(ReturnConstants.SYS_ERROR);
        }
    }
}