package com.th.demo.controller.develop;

import com.th.demo.model.develop.DevUpdownPaths;
import com.th.demo.model.system.Type;
import com.th.demo.service.develop.DevImexportService;
import com.th.demo.tools.ExportExcelTools;
import com.th.demo.tools.Tools;
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.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/DevImexport")
public class DevImexportController {

    @Autowired
    private DevImexportService devImexportService;
String result = "";
    @RequestMapping(value = "/query.do")
    public String query(int page,
                        int rows) {
        return devImexportService.query(page, rows);
    }

    @RequestMapping(value = "/save.do")
    public String save(
            String id,
            String name,
            String param1,
            String param2,
            String param3,
            String param4,
            String userId) throws IOException {
        return devImexportService.save(
                id,
                name,
                param1,
                param2,
                param3,
                param4,
                userId);
    }


    @RequestMapping(value = "/remove.do")
    public String remove(String id, String userId) throws IOException {
        return devImexportService.remove(id, userId);
    }
    @RequestMapping(value = "/download.do")
    public void download(  HttpServletResponse response) throws Exception {

        try {
            String[] arrKey = {"id","name","param1","param2", "param3", "param4"};
            String[] arrValue = {"id","名称","参数1","参数2", "参数3", "参数4" };
            int [] arrWidth={50,80,50,50,50,80};
            OutputStream out = Tools.getExportOutStream("导出.xlsx", response);
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();//matchCoalReportService.exportExcel(arrKey, material,type, transUnit,planId, coalStation, downStation, beginDate, endDate);
            ExportExcelTools<List<Map<String, Object>>> exportExcel = new ExportExcelTools<List<Map<String, Object>>>();
            exportExcel.exportXSExcelByColumn("Title", arrValue, arrKey, arrWidth,list, out, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @RequestMapping(value = "/importExcel.do", method = RequestMethod.POST)
    public String importExcel(HttpServletRequest request) throws Exception   {
        try {

            result = devImexportService.importExcel(request);

        } catch (Exception ex) {
            ex.printStackTrace();
            result = Type.FAIL+ex.getMessage();
        }finally {
            return result;
        }
    }


    @RequestMapping(value = "/exportExcel.do")
    public void export(  HttpServletResponse response) throws Exception {
        try {
            String[] arrKey = {"id","name","param1","param2", "param3", "param4"};
            String[] arrValue = {"id","名称","参数1","参数2", "参数3", "参数4" };
            int [] arrWidth={50,80,50,50,50,80};
            OutputStream out = Tools.getExportOutStream("导出.xlsx", response);
            List<Map<String, Object>> list = devImexportService.exportExcel(arrKey);
            ExportExcelTools<List<Map<String, Object>>> exportExcel = new ExportExcelTools<List<Map<String, Object>>>();
            exportExcel.exportXSExcelByColumn("Title", arrValue, arrKey, arrWidth,list, out, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
