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

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.List;

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.SiteResources;
import cn.com.countrygarden.bi.main.model.dup.WeekIncome;
import cn.com.countrygarden.bi.main.service.dup.WeekIncomeService;
import cn.com.countrygarden.bi.main.service.dup.impl.ExcelHelper;

/**
 * 周收入统计表
 * @author pansw
 *
 */

@RestController()
@RequestMapping("/api/fenghuang/WeekIncomeController")
public class WeekIncomeController {
	
	private final Logger logger = LoggerFactory.getLogger(WeekIncomeController.class);
	
	@Resource
	private WeekIncomeService weekIncomeService;
	
	
	/**
	 * 查询项目名称
	 * @return
	 */
	@RequestMapping(value = "/findAllProject", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	public Result findAllProject(){
		
		//List<WeekIncome> project = weekIncomeService.findAllProject();
		return Response.response(weekIncomeService.findAllProject());
	}
	
	
	/**
	 * 查询周收入统计
	 */
	@RequestMapping(value = "/findWeekIncome", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
	public Result findWeekIncomeStatistical(Integer[] commmIds,String startDate,String endDate,WeekIncome income){
		
		Result l= Response.response(weekIncomeService.findWeekIncomeStatistical(commmIds, startDate, endDate,income));
		return l;
	}
	
	
	/**
	  * 周收入导出
	 * @throws ParseException 
	 * export
	  */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/weekIncomeExport", produces = "multipart/form-data",  method = RequestMethod.POST)
	 public void WeekIncomeNoImportExport(Integer[] commmIds,String startDate,String endDate,WeekIncome income, HttpServletRequest request, HttpServletResponse response) throws ParseException {
		 
	 	Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        ExcelHelper helper = new ExcelHelper();
        helper.createWeekIncomeHeader(workbook, sheet);//创建表头
        helper.createWeekIncomeBody((List<WeekIncome>)weekIncomeService.findWeekIncomeStatistical(commmIds, startDate, endDate,income).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){

            }
        }
	 }
	
	
	
	

}
