package com.happy.Until;

import com.happy.Until.Enum.B;
import com.happy.constant.ResultStatusCode;
import com.happy.dto.IPage;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ResponseUtil {

	public static void write(Object o, HttpServletResponse response)
			throws Exception {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println(o.toString());
		out.flush();
		out.close();
	}

	public static void write(String o, HttpServletResponse response)
			throws Exception {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println(o.toString());
		out.flush();
		out.close();
	}

	public static void writeJsonPageData(HttpServletResponse respone, List listPage, int page, int rows, int total) {
		respone.setContentType("application/json;charset=utf-8");

		JSONObject dataJson = new JSONObject();
		dataJson.put(B.code, ResultStatusCode.OK.getMsg());

		if (listPage == null || listPage.size() == 0) {
			dataJson.put("rows", "");
			dataJson.put("total", 0);
		} else {
			dataJson.put("rows", rows);
			// int total = listAll.size();
			dataJson.put("total", total);// 总记录数
			int totalPage = total % rows == 0 ? (total / rows) : (total / rows) + 1;// 总页数
			dataJson.put("totalPage", totalPage);
			dataJson.put("currentPage", page);// 当前页
			dataJson.put("numPerPage", rows);// 每页数
			dataJson.put("nextPage", totalPage - page == 0 ? page : page + 1);// 下一页
			dataJson.put("previousPage", page - 0 == 1 ? page : page - 1);// 上一页
			dataJson.put("hasPreviousPage", true);// 有上一页
			dataJson.put("hasNextPage", true);// 有下一页
			dataJson.put("firstPage", true);// 首页
			dataJson.put("lastPage", true);// 尾页
			dataJson.put(B.data, listPage);

		}

		PrintWriter out;
		try {
			out = respone.getWriter();
			out.print(dataJson.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			// //System.out.println("Comm_Util_writeJson---->" + e);
		}
	}

	public static void writeJsonIPage(HttpServletResponse respone, IPage iPage) {
		respone.setContentType("application/json;charset=utf-8");

		JSONObject dataJson = new JSONObject();
		dataJson.put(B.code, ResultStatusCode.OK.getStatus());
		dataJson.put(B.message, "查询成功");
		dataJson.put(B.data,iPage); // 分页数据
		PrintWriter out;
		try {
			out = respone.getWriter();
			out.print(dataJson.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			// //System.out.println("Comm_Util_writeJson---->" + e);
		}
	}

	public static void writeJson(HttpServletResponse respone, String result) {
		respone.setContentType("application/json;charset=utf-8");
		PrintWriter out;
		try {
			out = respone.getWriter();
			out.print(result);
			out.flush();
			out.close();
		} catch (Exception e) {
			// //System.out.println("Comm_Util_writeJson---->" + e);
		}
	}

	public static void export(HttpServletResponse response, Workbook wb,
			String fileName) throws Exception {
		response.setHeader("Content-Disposition", "attachment;filename="
				+ new String(fileName.getBytes("utf-8"), "iso8859-1"));
		response.setContentType("application/ynd.ms-excel;charset=UTF-8");
		OutputStream out = response.getOutputStream();
		wb.write(out);
		out.flush();
		out.close();
	}
}
