package cn.com.victorysoft.business.analyze.controller;

import cn.com.victorysoft.business.analyze.entity.*;
import cn.com.victorysoft.business.analyze.param.CstDataSourceParam;
import cn.com.victorysoft.business.analyze.param.CstSearchExportParam;
import cn.com.victorysoft.business.analyze.param.CstSearchParam;
import cn.com.victorysoft.business.analyze.service.CustomSearchService;
import cn.com.victorysoft.business.sys.entity.ExportResult;
import cn.com.victorysoft.business.util.ExcelUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "自定义查询")
@RestController
@RequestMapping("analyze/custom")
public class CustomSearchController {

    @Resource
    private CustomSearchService customSearchService;

    @GetMapping("/dict")
    @ApiOperation(value = "查询附录数据", notes = "默认只返回前50条，前端做成使用searchText搜索")
    public List<CstDictData> dict(CstDataSourceParam param) {
        return customSearchService.selectDictData(param);
    }

    @GetMapping("/templates")
    @ApiOperation("查询模板列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unitcode", value = "单位代码", paramType = "query", required = true)
    })
    public List<CstTemplate> templates(String unitcode) {
        return customSearchService.selectTemplates(unitcode);
    }

    @GetMapping("/template/{id}")
    @ApiOperation("查询模板对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模板ID", paramType = "path", required = true)
    })
    public CstTemplate getTemplate(@PathVariable("id") String id) {
        return customSearchService.selectTemplate(id);
    }

    @DeleteMapping("/template/{id}")
    @ApiOperation("删除模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模板ID", paramType = "path", required = true)
    })
    public void deleteTemplate(@PathVariable("id") String id) {
        customSearchService.deleteTemplate(id);
    }

    @PostMapping("/template")
    @ApiOperation("新增模板")
    public void insertTemplate(@RequestBody CstTemplate template) {
        customSearchService.insertTemplate(template);
    }

    @PutMapping("/template")
    @ApiOperation("更新模板")
    public void updateTemplate(@RequestBody CstTemplate template) {
        customSearchService.updateTemplate(template);
    }

    @PostMapping("/template/share/{id}")
    @ApiOperation("分享模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模板ID", paramType = "path", required = true),
            @ApiImplicitParam(name = "unitcodes", value = "单位代码数组", paramType = "array", required = true),
    })
    public void shareTemplate(@PathVariable("id") String id, String[] unitcodes) {
        customSearchService.shareTemplate(id, unitcodes);
    }

    @GetMapping("/template/share/{id}")
    @ApiOperation("查询模板分享单位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "模板ID", paramType = "path", required = true)
    })
    public CstTemplateShareUnit getShareUnits(@PathVariable("id") String id) {
        return customSearchService.selectShareUnits(id);
    }

    @GetMapping("/tables")
    @ApiOperation("查询数据表")
    public List<CstTable> tables() {
        return customSearchService.selectTables();
    }

    @GetMapping("/table/relation/{id}")
    @ApiOperation("查询关联表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据表ID", paramType = "path", required = true),
    })
    public List<CstTable> relateTables(@PathVariable("id") String id) {
        return customSearchService.selectRelateTables(id);
    }

    @GetMapping("/table/indicator/{id}")
    @ApiOperation("查询数据表指标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据表ID", paramType = "path", required = true),
    })
    public List<CstTableIndicator> tableIndicators(@PathVariable("id") String id) {
        return customSearchService.selectTableIndicators(id);
    }

    @GetMapping("/indicator/{tableType}")
    @ApiOperation("根据表类型查询指标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableType", value = "表类型", paramType = "path", required = true),
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true),
            @ApiImplicitParam(name = "searchField", value = "查询文本", paramType = "query", required = false),
    })
    public PageInfo<CstIndicator> indicators(@PathVariable("tableType") String tableType, Integer pageNum, Integer pageSize, String searchField) {
        return customSearchService.selectIndicators(tableType, pageNum, pageSize, searchField);
    }

    @PostMapping("/search")
    @ApiOperation(value = "查询数据结果")
    public PageInfo<Map<String, Object>> search(@RequestBody CstSearchParam param) {
        return customSearchService.search(param);
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出数据结果")
    public ExportResult export(@RequestBody CstSearchExportParam param) {
        List<Map<String, Object>> list = customSearchService.searchList(param, false);
        return ExcelUtils.exportMap(param.getColumnConfig(), list, param.getFileName());
    }

}
