package com.usoftchina.bi.server.controller.dashboard;

import com.usoftchina.bi.core.base.RepCode;
import com.usoftchina.bi.core.base.RepEntity;
import com.usoftchina.bi.core.base.TestPage;
import com.usoftchina.bi.core.utils.EncryUtil;
import com.usoftchina.bi.server.model.bo.ShareReqBO;
import com.usoftchina.bi.server.model.pojo.annotation.CheckToken;
import com.usoftchina.bi.server.model.pojo.annotation.Log;
import com.usoftchina.bi.server.model.vo.dataVo.DashboardExportInfo;
import com.usoftchina.bi.server.model.vo.configVo.ChangeOrderInfo;
import com.usoftchina.bi.server.model.vo.configVo.ChartsToDashInfo;
import com.usoftchina.bi.server.model.vo.configVo.DashboardsInfo;
import com.usoftchina.bi.server.model.vo.dataVo.DashboardCopyInfo;
import com.usoftchina.bi.server.service.dashboard.DashboardsService;
import com.usoftchina.bi.server.service.dashboard.DashboardsToChartsUtilService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.URLEncoder;
import java.sql.SQLException;

@RestController
@Api(description = "看板相关接口")
public class DashboardsController {
    @Autowired
    DashboardsService dashboardsService;
    @Autowired
    DashboardsToChartsUtilService dashboardsToChartsUtilService;

    /*
    保存看板
     */
    @ApiOperation(value = "保存看板", notes = "保存看板", response = RepEntity.class)
    @CheckToken
    @Log(module = "报表", name = "#body.bdName", token = "#token")
    @PostMapping("/inputDashboards")
    public RepEntity saveDashboards(@RequestHeader String token, @RequestBody DashboardsInfo body){
        return dashboardsService.setDashboards(token, body);
    }

    /*
    更新看板
     */
    @ApiOperation(value = "更新看板", notes = "更新看板", response = RepEntity.class)
    @CheckToken
    @Log(module = "报表", name = "#body.bdName", token = "#token")
    @PostMapping("/updateDashboards")
    public RepEntity updateDashboards(@RequestHeader String token,@RequestBody DashboardsInfo body){
        return dashboardsService.updateDashboards(token, body);
    }
    /*
    删除看板
     */
    @ApiOperation(value = "删除看板", notes = "删除看板", response = RepEntity.class)
    @CheckToken
    @PostMapping("/delDashboards")
    public RepEntity delDashboards(@RequestHeader String token,@RequestBody Integer body){
        return dashboardsService.delDashboards(token, body);
    }

    /*
    查看列表
     */
    @ApiOperation(value = "查看列表", notes = "查看列表", response = RepEntity.class)
    @CheckToken
    @GetMapping("/getListDashboards")
    public RepEntity getListDashboards(@RequestHeader String token, TestPage body){
        return dashboardsService.getListDashboards(token, body);
    }

    /*
    查看单个看板
     */
    @ApiOperation(value = "单个看板", notes = "查看单个看板(通过看板ID)", response = RepEntity.class)
    @CheckToken
    @GetMapping("/getDashboards")
    public RepEntity getDashboards(@RequestHeader String token,@RequestParam("id") int id){
        return dashboardsService.getDashboards(token, id);
    }

    /**
     * 看板编号加密
     * @param code
     * @return
     */
    @ApiOperation(value = "单个看板", notes = "查看单个看板(通过看板编号)", response = RepEntity.class)
    @GetMapping("/encryptCode")
    public RepEntity encryptCode(@RequestParam("code") String code){
        return new RepEntity(RepCode.success, EncryUtil.encryptBiCode(code + "&" + System.currentTimeMillis()));
    }

    /**
     * 通过看板编号查看单个看板详情
     * @param code
     * @return
     */
    @ApiOperation(value = "单个看板", notes = "查看单个看板(通过看板编号)", response = RepEntity.class)
    @GetMapping("/getDashboardByCode")
    public RepEntity getDashboardByCode(@RequestParam("code") String code){
        return dashboardsService.getDashboardByCode(code);
    }

    /*
    转交看板
     */
    @ApiOperation(value = "转交看板", notes = "转交看板", response = RepEntity.class)
    @CheckToken
    @PostMapping("/changeDashOrder")
    public RepEntity changeDashOrder(@RequestHeader String token,@RequestBody ChangeOrderInfo body){
        return dashboardsService.changeDashOrder(token, body);
    }

    /*
    查看看板分发列表
     */
    @ApiOperation(value = "查看看板分发列表", notes = "查看看板分发列表", response = RepEntity.class)
    @CheckToken
    @PostMapping("/getDashOrder")
    public RepEntity getDashOrder(@RequestHeader String token,@RequestBody int body){
        return dashboardsService.getDashOrder(token, body);
    }

    /*
    看板生成图表
     */
    @ApiOperation(value = "看板生成图表", notes = "看板生成图表", response = RepEntity.class)
    @PostMapping("/showChartsInDash")
    public RepEntity showChartsInDash(@RequestHeader(required = false) String token, @RequestBody ChartsToDashInfo body) throws SQLException {
        return dashboardsToChartsUtilService.getChartsInDash(body, token);
    }

    /**
     * 生成分享链接
     * @param shareReqBO
     * @return
     */
    @ApiOperation(value = "生成分享链接", notes = "生成分享链接", response = RepEntity.class)
    @PostMapping("/share")
    public RepEntity share(@RequestBody ShareReqBO shareReqBO){
        return dashboardsService.shareDashBoard(shareReqBO);
    }

    /**
     * 通过分享链接获取看板信息
     * @param data
     * @return
     */
    @ApiOperation(value = "通过分享链接获取看板信息", notes = "通过分享链接获取看板信息", response = RepEntity.class)
    @GetMapping("/getSharedDashboard")
    public RepEntity getSharedDashboard(@RequestParam("data") String data) {
        return dashboardsService.getSharedDashboard(data);
    }

    /**
     * 复制看板
     * @param token
     * @param dashboardCopyInfo
     * @return
     */
    @ApiOperation(value = "复制", notes = "复制", response = RepEntity.class)
    @CheckToken
    @PostMapping("/copyDashboard")
    public RepEntity copy(@RequestHeader String token, @RequestBody DashboardCopyInfo dashboardCopyInfo){
        return dashboardsService.copy(token, dashboardCopyInfo);
    }

    @ApiOperation(value = "导出", notes = "导出")
    @CheckToken
    @PostMapping("/dashboard/export")
    public void export(@RequestHeader String token, @RequestBody DashboardExportInfo dashboardExportInfo,  HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = dashboardsService.export(dashboardExportInfo);
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(dashboardExportInfo.getDashboardName() + ".xlsx", "UTF-8"));
        OutputStream out = response.getOutputStream();
        workbook.write(out);
        out.close();
    }
}
