package com.bonus.sgzb.system.controller;

import com.bonus.sgzb.common.core.utils.StringHelper;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.system.domain.FileInfo;
import com.bonus.sgzb.system.service.SysFileService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.util.IOUtils;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @author bns
 */
@RestController
@RequestMapping("/sys/file")
public class SysFileController {

    @Resource
    private SysFileService service;

    @Resource
    private ResourceLoader resourceLoader;


    @PostMapping("/upload")
    @ApiOperation(value = "文件上传")
    public AjaxResult upload(HttpServletRequest request) {
        String limitWords = request.getParameter("limitWords");
        FileInfo file = new FileInfo();
        try {
            file = service.uploadFile(request);
            if (limitWords != null && file.getWords() > Integer.parseInt(limitWords)){
                return AjaxResult.error("上传文件字数超出限制字数！");
            }
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (file != null && file.getId() != 0){
            return AjaxResult.success(file);
        }else {
            return AjaxResult.error("文件上传失败！");
        }

    }

    @ApiOperation(value = "模板", httpMethod = "GET")
    @GetMapping("download")
    public void download(HttpServletRequest request, HttpServletResponse response, String filename) {
        InputStream inputStream = null;
        ServletOutputStream servletOutputStream = null;
        try {
            String path = "download/" + filename;
            org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:" + path);
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition", "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);

            inputStream = resource.getInputStream();
            servletOutputStream = response.getOutputStream();
            IOUtils.copy(inputStream, servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (servletOutputStream != null) {
                    servletOutputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                // 召唤jvm的垃圾回收器
                System.gc();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
