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

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.dup.DivertStatistical;
import cn.com.countrygarden.bi.main.service.dup.DivertStatisticalService;
import cn.com.countrygarden.bi.main.service.dup.impl.ExcelHelper;
import freemarker.core.ParseException;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <pre>
 * 。转名统计web
 * </pre>
 *
 * @author developer developer@meicloud.com
 * @version 1.00.00
 *
 * <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容:
 * </pre>
 */
@RestController()
@RequestMapping("/api/biapp/DivertStatisticalController")
public class DivertStatisticalController {

    @Resource
    private DivertStatisticalService divertStatisticalService;

    private final static Logger logger = LoggerFactory.getLogger(DivertStatisticalController.class);

    /**
     * 查询转名统计
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/findDivertStaticalLike", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    public Result findDivertStaticalLike(DivertStatistical divertStatistical) {
        return Response.response(divertStatisticalService.findDivertStaticalLike(divertStatistical));
    }

    /**
     * 转名统计导出
     *
     * @throws ParseException
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/exportNoImport", produces = "multipart/form-data", method = RequestMethod.GET)
    public void DivertStaticalNoImportExport(DivertStatistical divertStatistical, HttpServletRequest request, HttpServletResponse response) throws ParseException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        ExcelHelper helper = new ExcelHelper();

        Long start1 = System.currentTimeMillis();
        List<String> list = divertStatisticalService.findRoomType();
        Long start2 = System.currentTimeMillis();
        logger.info("findRoomType执行时间1{}", start2 - start1);

        helper.createDivertStaticalHeader(list, workbook, sheet);//创建表头

        Long start3 = System.currentTimeMillis();
        logger.info("执行时间2{}", start3 - start2);

        helper.createDivertStaticalBody(divertStatisticalService.findDivertStatical(divertStatistical), list, workbook, sheet);

        Long start4 = System.currentTimeMillis();
        logger.info("执行时间3{}", start4 - start3);
        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) {
            ex.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    @RequestMapping(value = "/findList", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    public Result findList() {
        return Response.response(divertStatisticalService.findRoomType());
    }

}
