package com.hs.api.controller;

import com.hs.api.common.Result;
import com.hs.api.common.annotations.Token;
import com.hs.api.common.handlers.GlobalExceptionHandler;
import com.hs.api.model.reqmodel.ExportReq;
import com.hs.api.service.ExcelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "Excel导出")
@RestController
public class ExcelController {

    static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @Autowired
    private ExcelService excelService;

    @PostMapping("export")
    @Token
    @ApiOperation("将Table转换为Xls")
    public Object tableToXls(HttpServletRequest request,@RequestBody ExportReq info) {
        String tableStr = StringEscapeUtils.unescapeHtml4(info.getTableStr());
        log.info("table参数:" + tableStr);
        String realPath = request.getSession().getServletContext().getRealPath("/");
        String fileUrl = excelService.tableToXls(realPath,tableStr);
        return Result.success(fileUrl);
    }
}
