package com.ebts.generator.controller;

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


import com.ebts.common.core.controller.EBTSController;
import com.ebts.common.core.text.Convert;
import com.ebts.common.utils.ServerResult;
import com.ebts.generator.entity.GenTable;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ebts.common.annotation.Log;
import com.ebts.common.core.entity.AjaxResult;
import com.ebts.common.enums.BusinessType;
import com.ebts.generator.entity.TopSearchTable;
import com.ebts.generator.service.TopSearchTableService;
import com.ebts.common.utils.poi.ExcelUtil;

/**
 * top万能查询主Controller
 * 
 * @author clay
 * @date 2021-07-20
 */

@RestController
@RequestMapping("/tool/table")
public class TopSearchTableController extends EBTSController {
    protected final Logger logger = LoggerFactory.getLogger(TopSearchTableController.class);

    @Autowired
    private TopSearchTableService topSearchTableService;

    /**
     * 查询top万能查询主列表
     */
    @PreAuthorize("@ebts.hasPermi('tool:table:list')")
    @PostMapping("/list")
    public Serializable list(@Validated @RequestBody TopSearchTable topSearchTable) {
        startPage(topSearchTable.getPageInfo());
        ServerResult<List<TopSearchTable>> genServerResult = topSearchTableService.selectTopSearchTableList(topSearchTable);
        if (genServerResult.isStart()) {
            return getDataTable(genServerResult.getData());
        } else {
            return AjaxResult.info(genServerResult.getMsg());
        }
    }
    @PreAuthorize("@ebts.hasPermi('tool:table:cloumns')")
    @GetMapping("/columns/{id}")
    public AjaxResult columns(@PathVariable("id") Long id){
        return AjaxResult.success(topSearchTableService.selectTableColumns(id));
    }


    /**
     * 查询top万能查询主列表
     */
    @PreAuthorize("@ebts.hasPermi('tool:table:info')")
    @PostMapping("/info")
    public Serializable info() {
        ServerResult<List<TopSearchTable>> genServerResult = topSearchTableService.selectTopSearchTableInfo();
        if (genServerResult.isStart()) {
            return getDataTable(genServerResult.getData());
        } else {
            return AjaxResult.info(genServerResult.getMsg());
        }
    }

    /**
     * 导出top万能查询主列表
     */
    @PreAuthorize("@ebts.hasPermi('tool:table:export')")
    @Log(title = "top万能查询主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@Validated @RequestBody TopSearchTable topSearchTable) {
        ServerResult<List<TopSearchTable>> genServerResult = topSearchTableService.selectTopSearchTableList(topSearchTable);
        ExcelUtil<TopSearchTable> util = new ExcelUtil<TopSearchTable>(TopSearchTable. class);
        if (genServerResult.isStart()) {
            return util.exportExcel(genServerResult.getData(), "table");
        } else {
            return AjaxResult.error(genServerResult.getMsg());
        }
    }

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

    /**
     * 新增top万能查询主
     */
    @PreAuthorize("@ebts.hasPermi('tool:table:add')")
    @Log(title = "top万能查询主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TopSearchTable topSearchTable) {
        ServerResult<Integer> genServerResult = topSearchTableService.insertTopSearchTable(topSearchTable);
        if (genServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(genServerResult.getMsg());
        }
    }

    /**
     * 修改top万能查询主
     */
    @PreAuthorize("@ebts.hasPermi('tool:table:edit')")
    @Log(title = "top万能查询主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TopSearchTable topSearchTable) {
        if (topSearchTable.getRoleIds().length==0){
            return AjaxResult.error("角色不能为空!");
        }
        ServerResult<Integer> genServerResult = topSearchTableService.updateTopSearchTable(topSearchTable);
        if (genServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(genServerResult.getMsg());
        }
    }

    /**
     * 删除top万能查询主
     */
    @PreAuthorize("@ebts.hasPermi('tool:table:remove')")
    @Log(title = "top万能查询主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (ids.length<0){
            return AjaxResult.error("id不能为空!");
        }
        ServerResult<Integer> genServerResult = topSearchTableService.deleteTopSearchTableByIds(ids);
        if (genServerResult.isStart()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(genServerResult.getMsg());
        }
    }
    /**
     * 导入表结构（保存）
     */
    @PreAuthorize("@ebts.hasPermi('tool:table:import')")
    @Log(title = "代码生成", businessType = BusinessType.IMPORT)
    @PostMapping("/importTopTable")
    public AjaxResult importTableSave(String tables) {
        String[] tableNames = Convert.toStrArray(tables);
        // 查询表信息
        List<TopSearchTable> tableList = topSearchTableService.selectDbTableListByNames(tableNames);
        topSearchTableService.importGenTable(tableList);
        return AjaxResult.success();
    }


    /**
     * 导入表结构（获取）
     */
    @PreAuthorize("@ebts.hasPermi('tool:table:db:list')")
    @PostMapping("/db/list")
    public Serializable dbList(@RequestBody Map<String,Object> params){
        startPage(params);
        List<Map<String,Object>> list = topSearchTableService.selectDbTableList(params);
        return getDataTable(list);
    }



}
