package com.chushang.oss.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.chushang.common.core.util.FileUtils;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.core.web.Result;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.oss.entity.FileSourceInfo;
import com.chushang.oss.entity.dto.OcrDTO;
import com.chushang.oss.entity.vo.FileSourceVo;
import com.chushang.oss.enums.OcrTypeEnum;
import com.chushang.oss.remote.RemoteFileController;
import com.chushang.oss.service.FileSourceService;
import com.chushang.oss.service.OcrService;
import com.chushang.security.annotation.RequiresPermissions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping(value = "/file")
public class FileController {

    @Resource
    FileSourceService fileSourceService;

    /**
     * 文件上传
     */
    @SysLog(value = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload")
//    @RequiresPermissions("system:file:upload")
    public AjaxResult uploadFile(@RequestParam(value = "files") MultipartFile[] files,
                                 @RequestParam(value = "ocrType", required = false) String ocrType,
                                 @RequestParam(value = "sealFlag", required = false, defaultValue= "false") Boolean sealFlag,
                                 @RequestParam(value = "formats", required = false) String formats,
                                 @RequestParam(value = "fileType", required = false) String fileType)
    {
        return AjaxResult.success(fileSourceService.addFile(files, ocrType, sealFlag, formats, fileType));
    }

    /**
     * 合同上传
     */
    @SysLog(value = "合同", businessType = BusinessType.UPLOAD)
    @PostMapping(value = "/uploadContract")
    @RequiresPermissions("system:file:upload")
    public AjaxResult uploadContract(@RequestParam(value = "file") MultipartFile file) throws Exception
    {
        return AjaxResult.success(fileSourceService.uploadContract(file));
    }

    /**
     * 文件预览
     * @param fid 文件id
     */
    @SysLog(value = "文件", businessType = BusinessType.PREVIEW)
    @GetMapping(value="/{fid}/preview")
    @RequiresPermissions("system:file:preview")
    public void preview(@PathVariable String fid, HttpServletResponse response)
    {
        log.info("[OSS]preview Request --> param:{}",fid);
        fileSourceService.getFileStream(fid, response, true);
    }

    /**
     * 文件下载
     * @param fid 文件id
     */
    @SysLog(value = "文件下载", businessType = BusinessType.DOWNLOAD)
    @GetMapping(value = "/download/{fid}")
//    @RequiresPermissions("system:file:download")
    public void downloadFile(@PathVariable String fid, HttpServletResponse response) {
        log.info("[OSS]download Request --> param:{}",fid);
        fileSourceService.getFileStream(fid, response, false);
    }

    /**
     * 删除文件
     * @param fid 文件id
     */
    @SysLog(value = "文件", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/del/{fid}")
    @RequiresPermissions("system:file:del")
    public AjaxResult delFile(@PathVariable String fid) {
        fileSourceService.delFile(fid);
        return AjaxResult.success();
    }

    /**
     * 删除文件
     * @param fids 文件fid
     */
    @SysLog(value = "批量文件", businessType = BusinessType.DELETE)
    @PostMapping(value = "/del/batch")
    @RequiresPermissions("system:file:del")
    public AjaxResult delFile(@RequestBody Set<String> fids) {
        return AjaxResult.success( fileSourceService.delFileBatch(fids));
    }

}
