package com.liangjian.dataplatform.member.controller;

import com.liangjian.dataplatform.member.entity.po.primary.SqlEntity;
import com.liangjian.dataplatform.member.service.ISqlEntityService;
import com.liangjian.dataplatform.member.util.SimpleSqlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Author: 黄子奇
 * @Date: 2019/10/18 11:22
 */
@RestController
@RequestMapping("/dataApi")
@Api(value = "sql查询控制器", tags = "sql查询控制器", description = "sql查询控制器")
public class SqlApiController {
    @Value("${clickhouseUrl}")
    private String clickhouseUrl;
    @Value("${clickhouseUser}")
    private String clickhouseUser;
    @Value("${clickhousePassword}")
    private String clickhousePassword;

    @Autowired
    private ISqlEntityService iSqlEntityService;

    @PostMapping("/clickhouse")
    @ApiOperation(value="输入sql查询clickhouse的数据")
    public List<List<String>> getFromClickhouse(@ApiParam(value="要查询的sql",required = true) @RequestBody String sql) throws Exception {
        try {
            List<List<String>> result = SimpleSqlUtil.getDataFromDatabase(clickhouseUrl, clickhouseUser, clickhousePassword, sql, true);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Collections.emptyList();
        }
    }

    @GetMapping("/clickhouse")
    @ApiOperation(value="通过sql配置管理来获取查询信息")
    public List<List<String>> getFromClickhouseByManage(@ApiParam(value="要查询的sql",required = true) @RequestParam String id) throws Exception {
        try {
            SqlEntity sqlEntity = iSqlEntityService.queryById(id);
            if(sqlEntity == null) {
                throw new Exception("没有查询到sql配置");
            }
            List<List<String>> result = SimpleSqlUtil.getDataFromDatabase(clickhouseUrl,clickhouseUser,clickhousePassword, sqlEntity.getSql(), false);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Collections.emptyList();
        }
    }

    @PostMapping("/clickhouse/objects")
    @ApiOperation(value="输入sql查询clickhouse的数据，封装成Map形式")
    public List<Map<String,Object>> getFromClickhouseObjects(@ApiParam(value="要查询的sql",required = true) @RequestBody String sql) throws Exception {
        try {
            List<Map<String,Object>> result = SimpleSqlUtil.getDataFromDatabase2(clickhouseUrl, clickhouseUser, clickhousePassword, sql, true);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Collections.emptyList();
        }
    }

    @GetMapping("/clickhouse/objects")
    @ApiOperation(value="通过sql配置管理来获取查询信息，封装成Map形式")
    public List<Map<String,Object>> getFromClickhouseByManageObjects(@ApiParam(value="要查询的sql",required = true) @RequestParam String id) throws Exception {
        try {
            SqlEntity sqlEntity = iSqlEntityService.queryById(id);
            if(sqlEntity == null) {
                throw new Exception("没有查询到sql配置");
            }
            List<Map<String,Object>> result = SimpleSqlUtil.getDataFromDatabase2(clickhouseUrl,clickhouseUser,clickhousePassword, sqlEntity.getSql(), false);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Collections.emptyList();
        }
    }

    @PostMapping("/clickhouse/wrap")
    @ApiOperation(value="输入sql查询clickhouse的数据，封装成BirdGrid的分页可读形式")
    public Map<String,Object> getFromClickhouseWrap(@ApiParam(value="要查询的sql",required = true) @RequestBody String sql,
                                                    @ApiParam(value="页数") @RequestParam(required = false) Integer pageIndex,
                                                    @ApiParam(value="每页数量") @RequestParam(required = false) Integer pageSize) throws Exception {
        try {
            Map<String,Object> result = SimpleSqlUtil.getDataFromDatabaseWrapper(clickhouseUrl, clickhouseUser, clickhousePassword, sql, true, pageIndex, pageSize);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Collections.emptyMap();
        }
    }

    @GetMapping("/clickhouse/wrap")
    @ApiOperation(value="通过sql配置管理来获取查询信息，封装成BirdGrid的分页可读形式")
    public Map<String,Object> getFromClickhouseByManageWrap(@ApiParam(value="要查询的sql",required = true) @RequestParam String id,
                                                            @ApiParam(value="页数") @RequestParam(required = false) Integer pageIndex,
                                                            @ApiParam(value="每页数量") @RequestParam(required = false) Integer pageSize) throws Exception {
        try {
            SqlEntity sqlEntity = iSqlEntityService.queryById(id);
            if(sqlEntity == null) {
                throw new Exception("没有查询到sql配置");
            }
            Map<String,Object> result = SimpleSqlUtil.getDataFromDatabaseWrapper(clickhouseUrl,clickhouseUser,clickhousePassword, sqlEntity.getSql(), false, pageIndex, pageSize);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Collections.emptyMap();
        }
    }
}
