/**
 *
 */
package com.huatek.busi.controller;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huatek.busi.service.report.BusiExportReportService;
import com.huatek.busi.util.FreemarkerUtil;
import com.huatek.torch.util.uuid.Uuid;

import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
  * @ClassName: BusiExportReport
  * @FullClassPath: com.huatek.busi.controller.BusiExportReport
  * @Description: 导出报告
  * @author: Arno
  * @date: Sep 23, 2019 10:24:29 AM
  * @version: 1.0
  */
@Api(tags = {"导出报告"})
@RestController
@RequestMapping("/api/BusiExportReport")
public class BusiExportReportController {
    public static final String encoding = "UTF-8";
    @Reference(check = false)
    private BusiExportReportService busiExportReportService;

	@SuppressWarnings("unchecked")
	@ApiOperation(value="导出报告", notes="")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "exportType", value =  "001:油色谱日报告		 	002:油色谱月报告     		 003:避雷器检测(月报告) "
				+ 										"004-1:变压器局放检测月报告    004-2:GIS局放检测月报告 	 004-3: 电缆局放检测月报告 "
				+ 										"005:SF6检测月报告  006:变压器套管检测月报告 007-1:铁芯检测月报告  007-2:夹件检测月报告 "
				+ 										"008-1:变压器拍照设备检测月报告 008-2:GIS拍照设备检测月报告 008-3:避雷器拍照设备检测月报告 008-4:其他拍照设备检测月报告 "
				+                                       "009:容性检测月报告 010-1:变压器红外热成像检测月报告 010-2:GIS红外热成像检测月报告 010-3:避雷器红外热成像检测月报告  010-4:其他红外热成像检测月报告"
                +                                       "011:无线测温检测月报告  012:其他电缆环流检测月报告  013:电缆-电缆环流月报告  014:发电机振动检测月报告"
                +                                       "015:发电机压力检测月报告  016:发电机局放检测月报告", required = true),
		@ApiImplicitParam(name = "exportTime", value =  "报告日期", required = true),
		@ApiImplicitParam(name = "sernsorCodes", value =  "传感器编码", required = true)
	  })
    @GetMapping(value = "/export")
    public void export(@RequestParam("exportType") String exportType,
        @RequestParam("sernsorCodes") String[] sernsorCodes,
        @RequestParam("exportTime") String exportTime,
        @RequestParam("startTime") String startTime,
        @RequestParam("endTime") String endTime,
        HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        Template template = FreemarkerUtil.getInstance().getTemplate(exportType);
        List<String> sensors = Arrays.asList(sernsorCodes);
        // 获取数据
        Map<String, Object> dataMap = busiExportReportService.buildExportData(exportType, sensors, exportTime,startTime,endTime);
        String fileName =
            dataMap.get("fileName") == null ? Uuid.getUUID() + ".doc" : dataMap.get("fileName").toString() + ".doc";
        Map<String, Object> data = (Map<String, Object>)dataMap.get("data") == null ? new HashMap<>()
            : (Map<String, Object>)dataMap.get("data");
        response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream;charset=UTF-8");
        String newfileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + newfileName);
        Writer out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), encoding), 10240);
        template.process(data, out);
        out.close();
    }
}
