package cn.com.countrygarden.bi.main.controller.project;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.countrygarden.bi.main.service.project.Scheduling.projectSchedu;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.com.countrygarden.bi.main.controller.project.imports.ProjectEmergencyImports;
import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.service.project.BiEmergencyService;
import freemarker.core.ParseException;

@RestController("BiEmergencyController")
@RequestMapping("/api/biEmergency")
public class BiEmergencyController {
	@Autowired
	BiEmergencyService biEmergencyService;
	
	@Autowired
	projectSchedu rojectSchedu;

	@RequestMapping(value = "/search", method = { RequestMethod.GET }, produces = { "application/json;charset=utf-8" })
	public Object search(
			@RequestParam(value = "pageIndex", required = false, defaultValue = "") Integer page,
			@RequestParam(value = "pageSize", required = false, defaultValue = "") Integer size,
			@RequestParam(value = "commName", required = true, defaultValue = "") String commName,
			@RequestParam(value = "strTime", required = true, defaultValue = "") String strTime,
			@RequestParam(value = "endTime", required = true, defaultValue = "") String endTime,
			@RequestParam(value = "timeSort", required = true, defaultValue = "") String timeSort) {
		Map<String, String> m = new HashMap<>();
		m.put("commName", commName);
		m.put("strTime", strTime);
		m.put("endTime", endTime);
		m.put("timeSort", timeSort);
		PageData p = biEmergencyService.search(m, page, size);
		return Response.response(p);
	}

	@RequestMapping(value = "/findOne", method = { RequestMethod.POST }, produces = { "application/json;charset=utf-8" })
	public Object findOne(
			@RequestParam(value = "id", required = false, defaultValue = "") Integer id) {
		return Response.response(biEmergencyService.findOne(id));
	}

	@RequestMapping(value = "/findAppList", method = { RequestMethod.POST }, produces = { "application/json;charset=utf-8" })
	public Object findAppList(
			@RequestParam(value = "id", required = false, defaultValue = "") Integer id) {
		return Response.response(biEmergencyService.findAppList(id));
	}

	/**
	 * 突发事件导出功能
	 * 
	 * @author wangyanlei
	 * @Time 2018年8月15日 16:22:07
	 * @version1.0
	 */
	@RequestMapping(value = "/export",method = RequestMethod.GET)
	public void biEmergencyExport(
//			@RequestParam(value = "pageIndex", required = false, defaultValue = "") Integer page,
//			@RequestParam(value = "pageSize", required = false, defaultValue = "") Integer size,
			@RequestParam(value = "commName", required = true, defaultValue = "") String commName,
			@RequestParam(value = "strTime", required = true, defaultValue = "") String strTime,
			@RequestParam(value = "endTime", required = true, defaultValue = "") String endTime,
			HttpServletRequest request, HttpServletResponse response)
			throws ParseException {

		Map<String, String> m = new HashMap<>(16);
		m.put("commName", commName);
		m.put("strTime", strTime);
		m.put("endTime", endTime);
		PageData p = biEmergencyService.search(m, 1, 10000);
		Workbook workbook = new XSSFWorkbook();
		Sheet sheet = workbook.createSheet();
		ProjectEmergencyImports helper = new ProjectEmergencyImports();
		// 创建表头,创建表体
		helper.createHeader(workbook, sheet);
		helper.createBody(p, workbook, sheet);
		response.setContentType("multipart/form-data");
		String fileName = "export.xlsx";
		try {
			fileName = URLEncoder.encode("突发事件导出明细.xlsx", "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		response.setHeader("Content-Disposition", "attachment;fileName="+ fileName);
		OutputStream outputStream = null;
		try {
			outputStream = response.getOutputStream();
			workbook.write(outputStream);
		} catch (Exception ex) {

		} finally {
			try {
				workbook.close();
			} catch (Exception ex) {

			}
		}
	}
	
	   @RequestMapping(value = "/emergency", method = { RequestMethod.GET }, produces = { "application/json;charset=utf-8" })
    public Object search(){
        rojectSchedu.emergency();
		  return Response.response("ok");
    }
}
