package cn.com.countrygarden.bi.main.controller.marketing.backend;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.MarketingUploadFile;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser;
import cn.com.countrygarden.bi.main.service.marketing.history.ExcelDataImportService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenweijiang on 2017/11/3.
 */
@Controller("HistoryDataImport")
@RequestMapping("/api/biapp/marketing/history")
public class HistoryDataImport {

    @Autowired
    private ExcelDataImportService excelDataImportService;

    @ResponseBody
    @RequestMapping(value = "/proposalContract", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    public Result upload(@RequestParam(value = "tenOfThousand", defaultValue = "true", required = true) Boolean tenOfThousand,
                         @RequestParam(value = "uploadfile", defaultValue = "", required = true) MultipartFile multipartFile,
                         HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        Map<String, Object> data = new HashMap<>();

        if (multipartFile == null) {
            throw new ServiceException("请选择需要上传的文件");
        }

        CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;

        // 保存文件并生成上传记录
        String fileName = commonsMultipartFile.getOriginalFilename();

        InputStream inputStream = null;
        try {
            inputStream = commonsMultipartFile.getInputStream();
            Workbook workbook = null;
            if (fileName.indexOf(".xlsx") > 0) {
                workbook = new XSSFWorkbook(inputStream);
            } else {
                workbook = new HSSFWorkbook(inputStream);
            }
            excelDataImportService.importFile(workbook,tenOfThousand);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new ServiceException(ex.getMessage());
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return Response.response(data);
    }
}
