package com.tofly.base.controller;

import com.google.common.collect.Lists;

import com.tofly.base.em.Constant;
import com.tofly.base.em.ExcelExportFileTypeEnum;
import com.tofly.base.em.ExcelImportFileTypeEnum;
import com.tofly.base.strategy.ExcelStrategyContext;
import com.tofly.base.util.ExcelUtil;
import com.tofly.common.core.constant.ResponeStatus;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Excel处理信息接口 前端控制器
 * </p>
 *
 * @author Zev
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/excel-dispose-info")
@Api(tags = "Excel处理信息接口")
@Slf4j
public class ExcelDisposeInfoController {

    @Autowired
    private ExcelStrategyContext excelStrategyContext;

    @ApiOperation(value = "导出excel数据")
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response, String templateServiceType, String templateFlag, String dto) {

        // 根据templateServiceType,templateFlag获取对应模板数据
        ExcelExportFileTypeEnum excelExportFileTypeEnum = ExcelExportFileTypeEnum.getExcelExportFileTypeEnum(templateServiceType, templateFlag);
        if(null == excelExportFileTypeEnum){
            return;
        }
        // 从模型类型枚举根据templateServiceType获取策略对应模板数据
        List<Map<String, Object>> dataList = excelStrategyContext.getExportData(templateServiceType, templateFlag,dto);
        ExcelUtil.export(dataList.get(0), excelExportFileTypeEnum, Constant.FILE_PATH_TEST_EXPORT_EXCEL, response);
    }

    @ApiOperation(value = "导入excel数据")
    @PostMapping("/importData")
    public ResultRespone<Object> importData(@RequestParam(value = "file", required = false) MultipartFile file, String templateServiceType) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 根据templateServiceType获取对应模板数据
        ExcelImportFileTypeEnum excelImportFileTypeEnum = ExcelImportFileTypeEnum.getExcelImportFileTypeEnum(templateServiceType);
        if(null == excelImportFileTypeEnum ){
            return ResultRespone.failed("获取对应模板数据为空");
        }
        try {
            List<Object> dataList = Lists.newArrayList();
            ExcelUtil.read(dataList,excelImportFileTypeEnum, file, true);
            // 数据落库
            excelStrategyContext.excelDataStorage(templateServiceType,dataList);
        } catch (ToflyDeniedException e) {
            log.error("插入数据异常:e = {}", e.getMessage());
            rest.setCode(ResponeStatus.FAIL.getCode());
            rest.setMessage(e.getMessage());
        }
        return rest;
    }

//    @ApiOperation(value = "本地测试导入excel数据")
//    @GetMapping("/testImportData")
//    public String testImportData(@RequestParam(value = "file", required = false) MultipartFile file) {
//        List<Object> userInfoList = Lists.newArrayList();
//        try {
//            // 本地File文件转MultipartFile作临时测试前端上传文件导入数据
//            File fileLocal = FileUtil.newFile(Constants.FILE_PATH_TEST_EXPORT_EXCEL);
//            InputStream inputStream = new FileInputStream(fileLocal);
//            MultipartFile multipartFile = new MockMultipartFile(fileLocal.getName(), inputStream);
//            ExcelUtil.read(userInfoList, ExcelImportFileTypeEnum.IMPORT_RIVER_BASE_INFO_XML, multipartFile, true);
//            System.out.println(JSON.toJSONString(userInfoList));
//            System.out.println(userInfoList.size());
//        } catch (Exception e) {
//            return e.getMessage();
//        }
//        return "SUCCESS";
//    }
}
