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

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

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

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.util.StringUtil;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.dup.ContractStatistical;
import cn.com.countrygarden.bi.main.service.dup.ContractStatisticalService;
import cn.com.countrygarden.bi.main.service.dup.impl.ExcelHelper;
import cn.com.countrygarden.bi.main.service.dup.tool.ContractStatisticalTimerTask;

/**
 * 
 * 
 * <pre>
 * 合同录入统计web
 * </pre>
 * 
 * @author developer developer@meicloud.com
 * @version 1.00.00
 * 
 *          <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 * </pre>
 */
@RestController()
@RequestMapping("/api/biapp/contractStatistical")
public class ContractStatisticalController {
	 private final Logger logger = LoggerFactory.getLogger(ContractStatisticalController.class);
	 
	 @Resource
	 private ContractStatisticalService contractStatisticalService;
	 
	 @Resource
	 private ContractStatisticalTimerTask contractStatisticalTimerTask;
	 
	   /**
      * 删除已录入的
     * @throws ParseException 
      */
     @RequestMapping(value = "/DeleteEnteredContractStatistical", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
     public Result DeleteEnteredContractStatistical(ContractStatistical contractStatistical) throws ParseException{
         if(contractStatistical.getRegDates()!=null) {
           contractStatistical.setRegDate(StringToDate(contractStatistical.getRegDates()));
         }
         return Response.response(contractStatisticalService.DeleteEnteredContractStatistical(contractStatistical));
     }
	 
	 /**
	  * 查询所有
	 * @throws ParseException 
	  */
	 @RequestMapping(value = "/AllContractStatisticalSelect", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	 public Result AllContractStatisticalSelect(ContractStatistical contractStatistical) throws ParseException{
		 if(contractStatistical.getRegDates()!=null) {
		   contractStatistical.setRegDate(StringToDate(contractStatistical.getRegDates()));
		 }
		 return Response.response(contractStatisticalService.AllContractStatisticalSelect(contractStatistical));
	 }
	 /**
	  * 合同未录入查询接口
	  * @return
	 * @throws ParseException 
	  */
	 @RequestMapping(value = "/select", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	 public Result selectContractStatistical(ContractStatistical contractStatistical) throws ParseException{
		 if(contractStatistical.getRegDates()!=null) {
			   contractStatistical.setRegDate(StringToDate(contractStatistical.getRegDates()));
			 }
		 return Response.response(contractStatisticalService.ContractStatisticalSelect(contractStatistical));
	 }
	 
	 /**
	  * 合同未审核的接口
	 * @throws ParseException 
	  */
	 @RequestMapping(value = "/contractStatisticalSelectNoExamine", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	 public Result contractStatisticalSelectNoExamine(ContractStatistical contractStatistical) throws ParseException{
		 if(contractStatistical.getRegDates()!=null) {
			   contractStatistical.setRegDate(StringToDate(contractStatistical.getRegDates()));
			 }
		 contractStatistical.setIsAuditings(0);
		 return Response.response(contractStatisticalService.contractStatisticalSelectNoExamine(contractStatistical));
	 }
	 
	 /**
	  * 合同已审核的接口
	 * @throws ParseException 
	  */
	 @RequestMapping(value = "/contractStatisticalSelectYesExamine", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	 public Result contractStatisticalSelectYesExamine(ContractStatistical contractStatistical) throws ParseException{
		 if(contractStatistical.getRegDates()!=null) {
			   contractStatistical.setRegDate(StringToDate(contractStatistical.getRegDates()));
			 }
		 return Response.response(contractStatisticalService.contractStatisticalSelectYesExamine(contractStatistical));
	 }
	 
	 /**
	  * 统计各个数量
	  */
	 @RequestMapping(value = "/selecttotal", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	 public Result selecttotal() {
		return Response.response(contractStatisticalService.selecttotal());
	 }
	 
	 /**
	  * 导出全部接口
	 * @throws ParseException 
	  */
	 @RequestMapping(value = "/export", produces = "multipart/form-data",  method = RequestMethod.GET)
	 public void ContractStatisticalexport(ContractStatistical contractStatistical, HttpServletRequest request, HttpServletResponse response) throws ParseException {
		 if(contractStatistical.getRegDates()!=null) {
			   contractStatistical.setRegDate(StringToDate(contractStatistical.getRegDates()));
			 }
		 if(!StringUtil.isEmpty(contractStatistical.getNoImport())) {
			 contractStatistical.setNoImport("未录入");
		 }
//		 if("undefined".equals(contractStatistical.getBipNum())) {
//			 contractStatistical.setBipNum("");
//		 }
		 	Workbook workbook = new XSSFWorkbook();
	        Sheet sheet = workbook.createSheet();
	        ExcelHelper helper = new ExcelHelper();
	        helper.createContractHeader(workbook,sheet);//创建表头
	        helper.createContractBody(contractStatisticalService.AllContractStatisticalSelect1(contractStatistical),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){

	            }
	        }
	 }
	 
	 /**
	  * 未录入导出
	 * @throws ParseException 
	  */
	 @SuppressWarnings("unchecked")
	@RequestMapping(value = "/exportNoImport", produces = "multipart/form-data",  method = RequestMethod.GET)
	 public void ContractStatisticalNoImportExport(ContractStatistical contractStatistical, HttpServletRequest request, HttpServletResponse response) throws ParseException {
		 if(contractStatistical.getRegDates()!=null) {
			   contractStatistical.setRegDate(StringToDate(contractStatistical.getRegDates()));
			 }
		 if(!StringUtil.isEmpty(contractStatistical.getNoImport())) {
			 contractStatistical.setNoImport("未录入");
		 }
		 	Workbook workbook = new XSSFWorkbook();
	        Sheet sheet = workbook.createSheet();
	        ExcelHelper helper = new ExcelHelper();
	        helper.createContractHeader(workbook,sheet);//创建表头
	        helper.createContractBody((List<ContractStatistical>)contractStatisticalService.ContractStatisticalSelect(contractStatistical).getData(),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){

	            }
	        }
	 }
	 
	 /**
	  * 已审核的数据导出(未)
	 * @throws ParseException 
	  */
	 @SuppressWarnings("unchecked")
	@RequestMapping(value = "/exportExamine", produces = "multipart/form-data",  method = RequestMethod.GET)
	 public void ContractStatisticalExamineExport(ContractStatistical contractStatistical, HttpServletRequest request, HttpServletResponse response) throws ParseException {
		 if(contractStatistical.getRegDates()!=null) {
			   contractStatistical.setRegDate(StringToDate(contractStatistical.getRegDates()));
			 }
		 if(!StringUtil.isEmpty(contractStatistical.getNoImport())) {
			 contractStatistical.setNoImport("未录入");
		 }
		 String name= null;
		 if(1==contractStatistical.getIsAuditings()) {
			 contractStatistical.setIsAuditings(1);
			 name = "已审核";
		 }else{
			 name = "未审核"; 
			 contractStatistical.setIsAuditings(0);
		 }
		 	Workbook workbook = new XSSFWorkbook();
	        Sheet sheet = workbook.createSheet();
	        ExcelHelper helper = new ExcelHelper();
	        helper.createContractHeader(workbook,sheet);//创建表头
	        List<ContractStatistical> list= (List<ContractStatistical>)contractStatisticalService.contractStatisticalSelectNoExamine(contractStatistical).getData();
	        helper.createContractBody(list,workbook,sheet);
	        response.setContentType("multipart/form-data");
	        String fileName = "export.xlsx";
	        try {
	            fileName = URLEncoder.encode(name+".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 = "/findOrderCoder", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	 public Result findOrderCoder() {
		 return Response.response(contractStatisticalService.findOrderCode());
	 }
	 
	 /**
	  * 根据前端传的区域代码返回对应的项目
	  */
	 @RequestMapping(value = "/findCommName", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	 public Result findCommName(String OrganCode) {
		 return Response.response(contractStatisticalService.findCommName(OrganCode));
	 }
	 
	 
	 /**
	  * 根据前端传的区域代码返回对应的项目
	  */
	 @RequestMapping(value = "/findAllCommName", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	 public Result findAllCommName(String str) {
		 return Response.response(contractStatisticalService.findAllCommName(str));
	 }
	  
	 //将字符串转换为时间
	 private Date StringToDate(String  str) throws ParseException {
		 //将时间转为我需要的格式
		   SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		   Date   date = format.parse(str); 
		return date;
	 }
	 
	 /**
	  * 测试定时调度
	  * @throws com.landray.kmss.km.review.webservice.Exception
	  * @throws RemoteException
	  */
	 @RequestMapping(value = "/testContractStatisticalTimerTask", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
	 public void testContractStatisticalTimerTask(String beginDate,String finishDate){
		 contractStatisticalTimerTask.run(beginDate,finishDate);
	 }
}
