package org.sky.scientific.utils;

import com.alibaba.fastjson.JSON;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.sky.core.tool.api.R;
import org.sky.core.tool.utils.Charsets;

import java.io.PrintWriter;
import java.net.URLEncoder;

public class ExportUtil {

	@SneakyThrows
	public static void processWithNoData(HttpServletResponse response){
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter writer = response.getWriter();
		writer.write(JSON.toJSONString(R.fail("无数据可导出")));
		writer.flush();
	}

	public static void processWithData(HttpServletResponse response, String fileName){
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, Charsets.UTF_8));
	}
}
