package com.ebts.generator.controller;

import com.ebts.common.annotation.Log;
import com.ebts.common.core.controller.EBTSController;
import com.ebts.common.core.entity.Result;
import com.ebts.common.core.text.Convert;
import com.ebts.common.enums.BusinessType;
import com.ebts.common.utils.poi.ExcelUtil;
import com.ebts.generator.entity.TopSearchTable;
import com.ebts.generator.service.TopSearchTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

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

@RestController
@RequestMapping("/tool/table")
public class TopSearchTableController extends EBTSController {

    @Autowired
    private TopSearchTableService topSearchTableService;

    /**
     * 查询top万能查询主列表
     */
    @PreAuthorize("@ebts.hasPermi('tool:table:list')")
    @PostMapping("/list")
    public Serializable list(@Validated @RequestBody TopSearchTable topSearchTable) {
//        if (topSearchTable.getRoleIds()==null){
//            return Result.error("角色列表不能为空!");
//        }
        startPage(topSearchTable.getPageInfo());
        Result<List<TopSearchTable>> result = topSearchTableService.selectTopSearchTableList(topSearchTable);
        if (result.isSuccess()) {
            return getDataTable(result.getData());
        } else {
            return result;
        }
    }

    @PreAuthorize("@ebts.hasPermi('tool:table:cloumns')")
    @GetMapping("/columns/{id}")
    public Result columns(@PathVariable("id") Long id) {
        return Result.ok(topSearchTableService.selectTableColumns(id));
    }


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

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

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

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

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

    /**
     * 删除top万能查询主
     */
    @PreAuthorize("@ebts.hasPermi('tool:table:remove')")
    @Log(title = "top万能查询主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<Integer> remove(@PathVariable Long[] ids) {
        if (ids.length == 0) {
            return Result.error("id不能为空!");
        }
        return topSearchTableService.deleteTopSearchTableByIds(ids);
    }

    /**
     * 导入表结构（保存）
     */
    @PreAuthorize("@ebts.hasPermi('tool:table:import')")
    @Log(title = "代码生成", businessType = BusinessType.IMPORT)
    @PostMapping("/importTopTable")
    public Result<String> importTableSave(String tables) {
        String[] tableNames = Convert.toStrArray(tables);
        // 查询表信息
        topSearchTableService.importGenTable(tableNames);
        return Result.ok();
    }


    /**
     * 导入表结构（获取）
     */
    @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);
    }


}
