package com.hotent.baseInfo.controller;
import com.alibaba.fastjson.JSON;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.model.CommonResult;
import com.hotent.baseInfo.vo.BracketContentInfoVo;
import com.hotent.baseInfo.vo.FileUploadResult;
import com.hotent.config.EipConfig;
import com.hotent.util.FileUploadUtil;
import com.hotent.util.wordexcelutils.BracketContentExtractor;
import io.swagger.annotations.ApiModel;
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 java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/file/v1")
@ApiModel(value = "文件上传接口")
@ApiGroup(group = {ApiGroupConsts.GROUP_COST})
public class FileUploadController {




    /**
     * 单文件上传
     */
    @ApiOperation(value = "单文件上传", notes = "单文件上传")
    @PostMapping("/upload")
    public CommonResult<FileUploadResult> uploadFile(
            @RequestParam("file") MultipartFile file) {


        FileUploadResult fileUploadResult = FileUploadUtil.uploadFile(file, EipConfig.getUploadPath());
        CommonResult<FileUploadResult> result = new CommonResult<>();
        result.value(fileUploadResult);
        result.message("上传成功");
        return result;
    }


    /**
     * 单文件上传
     */
    @ApiOperation(value = "模板解析-单文件上传", notes = "模板解析-单文件上传")
    @PostMapping("/tempUpload")
    public CommonResult<BracketContentInfoVo> tempUploadFile(
            @RequestParam("file") MultipartFile file) throws IOException {


        BracketContentInfoVo vo=new BracketContentInfoVo();
        CommonResult<BracketContentInfoVo> result = new CommonResult<>();
        String originalFilename = file.getOriginalFilename();
        if (originalFilename.endsWith(".doc") || originalFilename.endsWith(".docx")) {
            vo.setBracketContentInfos(BracketContentExtractor.extractFromWord(file));
        }else if(originalFilename.endsWith(".xls") || originalFilename.endsWith(".xlsx")){
            vo.setBracketContentInfos(BracketContentExtractor.extractFromExcel(file));
        }else{
            throw new IOException("不支持的文件格式");
        }
        log.info(JSON.toJSONString(vo));
        FileUploadResult fileUploadResult = FileUploadUtil.uploadFile(file, EipConfig.getUploadPath());
        fileUploadResult.setFullPath(EipConfig.getImgUrl() +fileUploadResult.getSavePath());
        vo.setFileUploadResult(fileUploadResult);
        result.setValue(vo);
        result.message("解析成功");
        return result;
    }

    /**
     * 多文件上传
     */
    @ApiOperation(value = "多文件上传", notes = "多文件上传")
    @PostMapping("/uploads")
    public CommonResult<List<FileUploadResult>> uploadFiles(
            @RequestParam("files") MultipartFile[] files) {
        List<FileUploadResult> fileUploadResults = FileUploadUtil.uploadFiles(Arrays.asList(files), EipConfig.getUploadPath());
        CommonResult<List<FileUploadResult>> result = new CommonResult<>();
        result.value(fileUploadResults);
        result.message("上传成功");
        return result;
    }


}
