package com.liangjian.dataplatform.member.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.liangjian.dataplatform.member.entity.vo.request.BirdGirdRequest;
import com.liangjian.dataplatform.member.entity.vo.response.BirdGirdTableResponse;
import com.liangjian.dataplatform.member.safe.Tuple;
import com.liangjian.dataplatform.member.service.ISqlExportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;

/**
 * @Author: 黄子奇
 * @Date: 2019/11/4
 */
@RestController
@RequestMapping("/sqlExport")
@Api(value = "动态BirdGird中的sql导出excel数据", tags = "动态BirdGird中的sql导出excel数据", description = "动态BirdGird中的sql导出excel数据")
public class SqlExportController {

    @Autowired
    private ISqlExportService iSqlExportService;

    @PostMapping("/export")
    @ApiOperation(value="导出excel")
    public void export(HttpServletResponse response,
                       @ApiParam(value="数据源id", required = true) @RequestParam String dsId,
                       @ApiParam(value="Sql配置Id", required = true) @RequestParam String sqlId,
                       @ApiParam(value="请求结构",required = true) @RequestBody BirdGirdRequest birdGirdRequest) {

        Tuple<String,Workbook> tuple = iSqlExportService.exportSql(dsId, sqlId, birdGirdRequest);
        Workbook workbook = tuple.get_2();
        try(OutputStream os = response.getOutputStream()) {
            String fileName = tuple.get_1() + ".xlsx";
            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName); // 设置文件名
            response.setContentType("application/vnd.ms-excel");
            workbook.write(os);
        } catch (IOException ex) {
            System.out.println("导出excel发生错误");
            ex.printStackTrace();
        }
    }

    @GetMapping("/export")
    @ApiOperation(value="导出excel")
    public void export2(HttpServletResponse response,
                       @ApiParam(value="数据源id", required = true) @RequestParam String dsId,
                       @ApiParam(value="Sql配置Id", required = true) @RequestParam String sqlId,
                       @ApiParam(value="请求结构",required = true) @RequestParam String requestStr) throws Exception {

        requestStr = URLDecoder.decode(requestStr,"utf8");
        ObjectMapper objectMapper = new ObjectMapper();
        BirdGirdRequest birdGirdRequest = objectMapper.readerFor(BirdGirdRequest.class).readValue(requestStr);

        Tuple<String,Workbook> tuple = iSqlExportService.exportSql(dsId, sqlId, birdGirdRequest);
        Workbook workbook = tuple.get_2();
        try(OutputStream os = response.getOutputStream()) {
            String fileName = tuple.get_1() + ".xlsx";
            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName); // 设置文件名
            response.setContentType("application/vnd.ms-excel");
            workbook.write(os);
        } catch (IOException ex) {
            System.out.println("导出excel发生错误");
            ex.printStackTrace();
        }
    }
}
