package com.yonyou.hrcloud.pm.result.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.yonyou.hrcloud.adder.context.SpringHelper;
import com.yonyou.hrcloud.adder.exception.AdderBusinessException;
import com.yonyou.hrcloud.adder.mvc.result.MessageResult;
import com.yonyou.hrcloud.pm.helper.DownloadHelper;
import com.yonyou.hrcloud.pm.helper.ExcleCell;
import com.yonyou.hrcloud.pm.helper.ExportPdfHelper;
import com.yonyou.hrcloud.pm.result.service.IPerExportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 绩效考核结果导出控制器
 *
 * @author zrk
 */
@RestController
@RequestMapping("/pm/result")
@Api(value = "PerExportController | 绩效考核结果导出控制器")
public class PerExportController {


    @Resource
    private IPerExportService exportService;

    /**
     * 导出绩效考核结果
     *
     * @param sheetids
     * @return
     */
    @RequestMapping(value = "/exportPerformanceResult", method = RequestMethod.GET)
    @ApiOperation(value = "导出绩效考核结果", httpMethod = "GET")
    @ApiImplicitParam(paramType = "query", name = "sheetids", value = "量表id集合", required = true, dataType = "String")
    public MessageResult exportPerformanceResult(HttpServletRequest request, HttpServletResponse response, String sheetids) throws Exception {
        MessageResult result = new MessageResult("导出模板成功!");
        if (sheetids == null) {
            throw new AdderBusinessException("参数不能为空", "pm.i18n_core.comm.002");
        }
        exportService.exportPerformanceResult(request, response, sheetids);
        return result;
    }

    /**
     * 读取单元格配置信息-并下载excle文件
     *
     * @return
     */
    @RequestMapping(value = "/readExcleInfoAndDownload", method = RequestMethod.GET)
    @ApiOperation(value = "读取单元格配置信息", httpMethod = "GET")
    public MessageResult readExcleInfoAndDownload(HttpServletRequest request, HttpServletResponse response, String sheetids) throws Exception {
        MessageResult result = new MessageResult("导入模板成功!");
        MultipartHttpServletRequest mulRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
        MultipartFile[] f = null;
        if (mulRequest!=null){
            f = mulRequest.getFileMap().values().toArray(new MultipartFile[0]);
        }
        MultipartFile file = f == null || f.length == 0 ? null : f[0];
        exportService.exportBasePerformanceResult(file, request, response, sheetids);
        return result;
    }
}
