package com.tofly.gpsboot.controller.excel;

import com.google.common.collect.Lists;
import com.tofly.common.core.constant.ResponeStatus;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.business.ExcelExportFileTypeEnum;
import com.tofly.gpsboot.common.business.ExcelImportFileTypeEnum;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.strategy.ExcelStrategyContext;
import com.tofly.gpsboot.util.ExcelUtil;
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());
        } catch (BusinessException e){
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        } catch (Exception e){
            rest.setCode(ResponeStatus.FAIL.getCode());
            rest.setMessage(e.getMessage());
        }
        return rest;
    }
}
