package com.purui.prims.headless.server.rest.api;

import com.purui.prims.auth.api.authentication.pojo.User;
import com.purui.prims.auth.api.authentication.utils.UserHolder;
import com.purui.prims.common.pojo.ResultData;
import com.purui.prims.headless.api.pojo.request.BatchDownloadReq;
import com.purui.prims.headless.api.pojo.request.DownloadMetricReq;
import com.purui.prims.headless.api.pojo.request.QueryMetricReq;
import com.purui.prims.headless.api.pojo.response.SemanticQueryResp;
import com.purui.prims.headless.server.service.DownloadService;
import com.purui.prims.headless.server.service.MetricService;
import com.purui.prims.headless.server.service.SemanticLayerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/api/semantic/query")
@Slf4j
@Api(tags = "指标查询")
public class MetricQueryApiController {

    final SemanticLayerService queryService;

    final MetricService metricService;

    final DownloadService downloadService;

    public MetricQueryApiController(SemanticLayerService queryService, MetricService metricService, DownloadService downloadService) {
        this.queryService = queryService;
        this.metricService = metricService;
        this.downloadService = downloadService;
    }

    @ApiOperation(value = "根据指标查询结果")
    @PostMapping("/metric")
    public SemanticQueryResp queryByMetric(@RequestBody QueryMetricReq queryMetricReq,
                                           HttpServletRequest request,
                                           HttpServletResponse response) throws Exception {
        User user = UserHolder.findUser(request, response);
        return queryService.queryByMetric(queryMetricReq, user);
    }

    @ApiOperation(value = "获取BI查询使用的SQL")
    @GetMapping("/sql/metric/{id}")
    public ResultData buildSqlForBI(@PathVariable Long id) {
        return ResultData.success(metricService.buildAsSelectSql(id));
    }

    @PostMapping("/download/metric")
    public void downloadMetric(@RequestBody DownloadMetricReq downloadMetricReq,
                               HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
        User user = UserHolder.findUser(request, response);
        downloadService.downloadByStruct(downloadMetricReq, user, response);
    }

    @PostMapping("/downloadBatch/metric")
    public void downloadBatch(@RequestBody BatchDownloadReq batchDownloadReq,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        User user = UserHolder.findUser(request, response);
        downloadService.batchDownload(batchDownloadReq, user, response);
    }

}
